• 作业4:结对编程项目四则运算


    我的小组成员是李静远,学号130201210  博客地址http://home.cnblogs.com/u/li457803908/

    优缺点:我的缺点是基础比较差,优点是态度积极,不会的地方会上网查询和询问同学。

    代码:

    package Rational;

    import java.util.StringTokenizer;
    /**
     * @author sunkun
     * 分数四则运算
     */
    public class Calculator {
     int numerator;  // 分子
     int denominator; // 分母
     
     Calculator(){
     }

     Calculator(int a,int b){
      if(a == 0){
       numerator = 0;
       denominator = 1;
      }
      else{
       setNumeratorAndDenominator(a,b);
      }
     }
     
     void setNumeratorAndDenominator(int a, int b){  //
    int c = f(Math.abs(a),Math.abs(b));         // 计算最大公约数
      numerator = a / c;
      denominator = b / c;
      if(numerator<0 && denominator<0){
       numerator = - numerator;
       denominator = - denominator;
      }
     }


    int getNumerator(){
      return numerator;
     }

     int getDenominator(){
      return denominator;
     }
     
     int f(int a,int b){  // 求a和b的最大公约数
      if(a < b){
       int c = a;
       a = b;
       b = c;
      }
      int r = a % b;
      while(r != 0){
       a = b;
                      b = r;;
       r = a % b;
      }
      return b;
     }
     
     Calculator add(Calculator r){  // 加法运算
      int a = r.getNumerator();
      int b = r.getDenominator();
      int newNumerator = numerator * b + denominator * a;
      int newDenominator = denominator * b;
      Calculator result = new Calculator(newNumerator,newDenominator);
      return result;
     }
     
     Calculator sub(Calculator r){  // 减法运算
      int a = r.getNumerator();
      int b = r.getDenominator();
      int newNumerator = numerator * b - denominator * a;
      int newDenominator = denominator * b;
      Calculator result = new Calculator(newNumerator,newDenominator);
      return result;
     }
     
     Calculator muti(Calculator r){ // 乘法运算
                                    int a = r.getNumerator();
      int b = r.getDenominator();
      int newNumerator = numerator * b - denominator * a;
      int newDenominator = denominator * b;
      Calculator result = new Calculator(newNumerator,newDenominator);
      return result;
     }
                      Calculator div(Calculator r){  // 除法运算
      int a = r.getNumerator();
      int b = r.getDenominator();
      int newNumerator = numerator * b;
      int newDenominator = denominator * a;
      Calculator result = new Calculator(newNumerator,newDenominator);
      return result;
     }

                   // 封装了具体运算,主要为对输入进行转换,对输出封装
     public static void compute(String data1,String operation,String data2){
      StringTokenizer fenxi = new StringTokenizer(data1,"/");
         int data1_1 = Integer.parseInt(fenxi.nextToken());
         int data1_2 = Integer.parseInt(fenxi.nextToken());
      fenxi = new StringTokenizer(data2,"/");
         int data2_1 = Integer.parseInt(fenxi.nextToken());
         int data2_2 = Integer.parseInt(fenxi.nextToken());
             
      Calculator r1 = new Calculator(data1_1,data1_2);
      Calculator r2 = new Calculator(data2_1,data2_2);
      
      Calculator result;
      int a,b;
      if(operation.equals("+")){
        result = r1.add(r2);
       a = result.getNumerator();
        b = result.getDenominator();
       System.out.println(data1+" "+operation+" " +data2+" = " + a + "/" + b);
    }

      if(operation.equals("-")){
        result = r1.sub(r2);
       a = result.getNumerator();
        b = result.getDenominator();
       System.out.println(data1+" "+operation+" " +data2+" = " + a + "/" + b);
      }
      
      if(operation.equals("*")){
        result = r1.muti(r2);
       a = result.getNumerator();
        b = result.getDenominator();
       System.out.println(data1+" "+operation+" " +data2+" = " + a + "/" + b);
      }
      
      if(operation.equals("/")){
        result = r1.div(r2);
       a = result.getNumerator();
        b = result.getDenominator();
       System.out.println(data1+" "+operation+" " +data2+" = " + a + "/" + b);
      }
     }
    }

    总结:作业做得不是很好,大部分都是询问其他同学,不过我们也努力完成了。

  • 相关阅读:
    rsync安装配置实时同步
    ORA-00392: log 4 of thread 2 is being cleared, operation not allowed
    CentOS 6.4安装配置LNMP服务器(Nginx+PHP+MySQL)
    ERROR 2002 (HY000): Can't connect to local MySQL server through socket '/var/lib/mysql/mysql.sock'
    常见服务端口
    linux客户端传输文件到Windows本地
    windows/Linux主机重启NetBackup服务和后台进程
    linux命令之grep命令
    linux命令之awk命令
    Shiro安全框架
  • 原文地址:https://www.cnblogs.com/wann/p/5378395.html
Copyright © 2020-2023  润新知