Chinaunix首页 | 论坛 | 博客
  • 博客访问: 2223050
  • 博文数量: 556
  • 博客积分: 11457
  • 博客等级: 上将
  • 技术积分: 5973
  • 用 户 组: 普通用户
  • 注册时间: 2011-02-24 22:33
文章分类

全部博文(556)

文章存档

2013年(22)

2012年(74)

2011年(460)

分类: Java

2012-04-09 23:09:53


点击(此处)折叠或打开

  1. package cn.edu.xaut.junit;
  2. import cn.edu.xaut.exception.*;
  3. public class Equation {
  4.     private static float x1;
  5.     private static float x2;
  6.     private static float x;
  7.     private static float real;
  8.     private static float imag;
  9.     private static float[] results;
  10.     
  11.     private static float a;
  12.     private static float b;
  13.     private static float c;
  14.     
  15.     private static float delt;
  16.      public static float[] calc(float w,float p,float q)throws Exception{
  17.          results=new float[2];
  18.          a=w; b=p; c=q;
  19.          if(0.0f==a){ //bx+c=0; x=-c/b;

  20.                  if(0.0f!=b){
  21.                     x=-c/b;
  22.                     results[0]=x;
  23.                     print();
  24.                  return results;
  25.                  }
  26.                  else{
  27.                      throw new MyArithmeticException("除数不能为0");
  28.                  }
  29.             
  30.          }else{
  31.              delt=b*b-4*a*c;
  32.              if(delt>=0){
  33.                 x1=(float) ((-b+Math.sqrt(delt))/(2*a));
  34.                 x2=(float) ((-b-Math.sqrt(delt))/(2*a));
  35.                 results[0]=x1;
  36.                 results[1]=x2;
  37.                 print();
  38.                 return results;
  39.             
  40.              }else{
  41.                  real=-b/(2*a);
  42.                  imag=(float) (Math.sqrt(-delt)/(2*a));
  43.                  results[0]=real;
  44.                  results[1]=imag;
  45.                  print();
  46.                     return results;
  47.              }
  48.          }
  49.      }
  50.     
  51.     public static void print(){
  52.          if(0.0f==a){
  53.              if(0.0f!=b){
  54.                  System.out.println("该方程是一元一次方程,其根为:x="+results[0]);
  55.              }else{
  56.                  System.err.println("不是一个方程!");
  57.              }
  58.          }else{
  59.              if(delt>=0){
  60.                  System.out.println("该方程是一元二次方程,两根分别为:x1="+results[0]+",x2="+results[1]);
  61.              }else{
  62.                  System.out.println("该一元二次方程的根为复数,两根分别为:x1="+results[0]+"+"+results[1]+"i"+",x2="+results[0]+"-"+results[1]+"i");
  63.              }
  64.          }
  65.      }
  66. }

点击(此处)折叠或打开

  1. package cn.edu.xaut.exception;

  2. public class MyArithmeticException extends Exception {

  3.     public MyArithmeticException() {
  4.     }

  5.     public MyArithmeticException(String message) {
  6.         super(message);
  7.     }

  8.     public MyArithmeticException(Throwable cause) {
  9.         super(cause);
  10.     }

  11.     public MyArithmeticException(String message, Throwable cause) {
  12.         super(message, cause);
  13.     }
  14. }

点击(此处)折叠或打开

  1. package test;
  2. import org.junit.Assert;
  3. import org.junit.Test;
  4. import cn.edu.xaut.junit.Equation;

  5. public class EquationTest {
  6.     @Test
  7.     public void testCalc() {
  8.         float a = 1.0f;
  9.         float b = 2.0f;
  10.         float c = 1.0f;
  11.         float[] expecteds = {-1.0f,-1.0f };
  12.         //float[] expecteds = {-0.5f,0.5f};

  13.          //float expecteds =-0.5f;

  14.          float[] results;
  15.         try {
  16.             results = Equation.calc(a, b, c);
  17.             Assert.assertArrayEquals(expecteds, results, 0.0f);
  18.              //Assert.assertEquals(expecteds,results[0],0.0f);

  19.         } catch (Exception e) {
  20.             e.printStackTrace();
  21.         }
  22.     }
  23. }
优秀的代码是调试出来的,这个代码设计是参考Math.sqrt() 
欢迎大家拍砖!
附上源码:
阅读(2900) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~