Java 简化分数的分数计算器

Java 简化分数的分数计算器,java,arrays,calculator,Java,Arrays,Calculator,我的代码有几个问题,虽然我已经尝试过了,但我还没有发现它有什么问题。这是: public class FracCalc { public static void main(String[] args) { // String to test //String input = "5_3/4 - 6_7/8 + 3/4"; System.out.println(produceAnswer("-3_3/4 / -2_2/3")); }

我的代码有几个问题,虽然我已经尝试过了,但我还没有发现它有什么问题。这是:

public class FracCalc {
    public static void main(String[] args) {
        // String to test
        //String input = "5_3/4 - 6_7/8 + 3/4";
        System.out.println(produceAnswer("-3_3/4 / -2_2/3"));
    }

    public static String produceAnswer(String input) {

        // Splits everything at every space
        String[] array = input.split(" ", 0);

        int allWholes = 0;
        int allNumerators = 0;
        int allDenominators = 1;

        // Multiplier is 1 or -1, 1 is add, -1 is subtract
        int multiplier = 1;
        // Operation mode; 0=+,-; 1=*; 2=/
        int mode = 0;

        // the for loop is used to identify and solve the the test
        for (int i = 0; i < array.length; i++) {
            String part = array[i];
            // 
            int whole = 0;
            int numerator = 0;
            int denominator = 0;

            // If it has a _, then it's a whole + fraction
            if (part.contains("_")) {

                // Convert Int from start of part (0) to the character before _, "55_30/40" -> 55

                whole = Integer.parseInt(part.substring(0, part.indexOf("_")));

                // Convert Int from the character AFTER _ to the character before /, "55_30/40" -> 30

                numerator = Integer.parseInt(part.substring(part.indexOf("_") + 1, part.indexOf("/")));

                // Convert Int from the character AFTER / to the end, "55_30/40" -> 40

                denominator = Integer.parseInt(part.substring(part.indexOf("/") + 1, part.length()));
            }
            // If no _, but still has a / AND it is not just the symbol "/", then it's just a fraction
            else if (part.contains("/") && part.length() != 1) {
                numerator = Integer.parseInt(part.substring(0, part.indexOf("/")));
                denominator = Integer.parseInt(part.substring(part.indexOf("/") + 1, part.length()));
            } //else if(part.contains("*") && part.contains("/")){}
            else if (part.contains("/") ) {
                mode = 2;
            }
            // TODO: Make multiplication

            // Negative sign(if number is negative)
            else if (part.contains("-")) {
                multiplier = -1;
            }
            // Positive sign(if number is positive)

            else if (part.contains("+")) {
                multiplier = 1;
            }
            // If neither _ nor /, then it's a whole
            else {
                whole = Integer.parseInt(part);
            }

            // Add all the wholes together
            allWholes += whole * multiplier;

            // If denom is the same
            if (denominator == allDenominators) {
                allNumerators += numerator * multiplier;
            }
            // If they're not the same
            else if (numerator != 0 && denominator != 0) {
                if (mode == 0 ) {
                    // Cross multiply
                    allNumerators *= denominator;

                    // Add
                    allNumerators += numerator * allDenominators * multiplier;


                    // Second part of cross multiply
                    allDenominators *= denominator;
                }
                // Multiplication
                else if (mode == 1) {

                    allDenominators *= denominator;

                    allNumerators *= numerator;

                }
                // Division
                else if (mode == 2) {
                    // Reverse multiply because (1/2)/(1/2) -> (1*1)/(2*2)
                    allNumerators = allNumerators * denominator;
                    allDenominators = allDenominators * numerator;
                }
            }
        }

        // Simplifies fraction by checking to see id the top is bigger than bottom
        // 9/4 -> 2_1/4


        while(allNumerators > allDenominators){
             allWholes = allNumerators / allDenominators;
             allNumerators %= allDenominators;
        }


        if (allWholes == 0) {
            return (allNumerators + "/" + allDenominators);
        }
        else if (allNumerators == 0 || allDenominators == 0) {
            return allWholes + "";
        }
        else {
            return allWholes + "_" + (allNumerators + "/" + allDenominators);
        }
    }
}
公共类分形{
公共静态void main(字符串[]args){
//要测试的字符串
//字符串输入=“5_3/4-6_7/8+3/4”;
System.out.println(produceAnswer(“-3_3/4/-2_2/3”);
}
公共静态字符串produceAnswer(字符串输入){
//在每个空间拆分所有内容
字符串[]数组=input.split(“,0);
整数整=0;
int-all分子=0;
int所有分母=1;
//乘数是1或-1,1是加法,-1是减法
整数乘数=1;
//操作模式;0=+,-;1=*;2=/
int模式=0;
//for循环用于识别和解决测试问题
for(int i=0;i55之前的字符
整数=Integer.parseInt(part.substring(0,part.indexOf(“”));
//将Int从u之后的字符转换为/,“55_30/40”->30之前的字符
分子=整数.parseInt(部分子字符串(部分indexOf(“”)+1,部分indexOf(“/”));
//将Int从/后面的字符转换为“55_30/40”->40
分母=Integer.parseInt(part.substring(part.indexOf(“/”)+1,part.length());
}
//如果没有,但仍然有一个/并且它不仅仅是符号“/”,那么它只是一个分数
else if(part.contains(“/”)&&part.length()!=1){
分子=Integer.parseInt(part.substring(0,part.indexOf(“/”));
分母=Integer.parseInt(part.substring(part.indexOf(“/”)+1,part.length());
}//如果(part.contains(“*”)&&part.contains(“/”){}
else if(部分包含(“/”){
模式=2;
}
//TODO:进行乘法运算
//负号(如果数字为负数)
else if(部分包含(“-”){
乘数=-1;
}
//正号(如果数字为正数)
else if(部件包含(“+”)){
乘数=1;
}
//如果既不是u,也不是/,那么它就是一个整体
否则{
整=整数。parseInt(部分);
}
//把所有的全部加在一起
全部整数+=整数*乘数;
//如果denom是相同的
if(分母==所有分母){
所有分子+=分子*乘数;
}
//如果他们不一样
else if(分子!=0和分母!=0){
if(模式==0){
//交叉乘法
所有分子*=分母;
//加
所有分子+=分子*所有分母*乘数;
//交叉乘法的第二部分
所有分母*=分母;
}
//倍增
否则如果(模式==1){
所有分母*=分母;
所有分子*=分子;
}
//分部
否则如果(模式==2){
//反向乘法是因为(1/2)/(1/2)->(1*1)/(2*2)
所有分子=所有分子*分母;
所有分母=所有分母*分子;
}
}
}
//通过检查顶部id是否大于底部id来简化分数
// 9/4 -> 2_1/4
while(所有分子>所有分母){
AllWhists=所有分子/所有分母;
所有分子%=所有分母;
}
如果(所有批发==0){
返回值(所有分子+“/”+所有分母);
}
else if(所有分子==0 | |所有分母==0){
返回全部整数+“”;
}
否则{
返回所有整数+“”+(所有分子+“/”+所有分母);
}
}
}

我的目标是制作一个简单答案的分数计算器(作为分数)。我很确定我的乘法、除法和简化while循环是个问题。谢谢大家!

代码中有很多好的想法,但正如您所知,也存在一些问题

  • 你要想知道真相会有很多麻烦 计算的全部部分。和小学数学一样,使用 整个零件通过以下操作创建不适当的分数:

          numerator += whole*denominator;
    
        if (allDenominators < 0)
        {
          allNumerators = -allNumerators;
          allDenominators = -allDenominators;
        }
    
    然后用
    分子和
    分母做数学运算,完成所有计算后提取整个部分,这是您已经在做的

  • 你解析负数的方式是错误的。此外,还应删除 使用
    乘数
    ,只需使用负的
    分子

  • 如果删除
    乘数
    ,则需要单独跟踪
    -
    +
    操作,但这很容易做到

  • 在每次计算结束时,您应确保
    所有分母
    通过执行以下操作为正:

          numerator += whole*denominator;
    
        if (allDenominators < 0)
        {
          allNumerators = -allNumerators;
          allDenominators = -allDenominators;
        }
    
    如果
    allNumerators==allDenominators
    ,即答案应该是 是1吗?另外,一旦您完成
    allNumerators%=alldemonators
    while循环中的条件不再为真。就这么做吧 相反:

  • >P>最后考虑一个更大的公约数(GCD)例程 简化分数,例如从
    48/128
    3/8

    这里有一个
    public class FracCalc
    {
      public static void main(String[] args)
      {
        // String to test
        System.out.println(produceAnswer("5_3/4 - 6_7/8 + 3/4"));
        System.out.println(produceAnswer("-3_3/4 / -2_2/3"));
      }
    
      public static String produceAnswer(String input)
      {
        // Splits everything at every space
        String[] array = input.split(" ", 0);
    
        int allNumerators = 0;
        int allDenominators = 1;
    
        // Operation mode; 0=+; 1=-; 2=*; 3=/
        int mode = 0;
    
        // the for loop is used to identify and solve the the test
        for (int i = 0; i < array.length; i++)
        {
          String part = array[i];
    
          int numerator = 0;
          int denominator = 0;
    
          // If it has a _, then it's a whole + fraction
          if (part.contains("_"))
          {
            // Convert Int from start of part (0) to the character before _,
            // "55_30/40" -> 55
    
            int whole = Integer.parseInt(part.substring(0, part.indexOf("_")));
    
            // Convert Int from the character AFTER _ to the character before /,
            // "55_30/40" -> 30
    
            numerator = Integer.parseInt(part.substring(part.indexOf("_") + 1, part.indexOf("/")));
    
            // Convert Int from the character AFTER / to the end, "55_30/40" -> 40
    
            denominator = Integer.parseInt(part.substring(part.indexOf("/") + 1, part.length()));
    
            if (whole > 0)
              numerator += whole * denominator;
            else
              numerator = -numerator + whole * denominator;
          }
          // If no _, but still has a / AND it is not just the symbol "/", then it's
          // just a fraction
          else if (part.contains("/") && part.length() != 1)
          {
            numerator = Integer.parseInt(part.substring(0, part.indexOf("/")));
            denominator = Integer.parseInt(part.substring(part.indexOf("/") + 1, part.length()));
          } // else if(part.contains("*") && part.contains("/")){}
          else if (part.equals("/"))
          {
            mode = 3;
          }
          else if (part.equals("+"))
          {
            mode = 0;
          }
          else if (part.equals("-"))
          {
            mode = 1;
          }
          else if (part.equals("*"))
          {
            mode = 2;
          }
          // If neither _ nor /, then it's a whole
          else
          {
            numerator = Integer.parseInt(part);
            denominator = 1;
          }
    
          if (numerator != 0 && denominator != 0)
          {
            if (mode < 2)
            {
              if (denominator == allDenominators)
              {
                allNumerators += numerator;
              }
              else
              {
                // Cross multiply
                allNumerators *= denominator;
    
                if (mode == 0)
                {
                  // Add
                  allNumerators += numerator * allDenominators;
                }
                else
                {
                  // Sub
                  allNumerators -= numerator * allDenominators;
                }
    
                // Second part of cross multiply
                allDenominators *= denominator;
              }
            }
            // Multiplication
            else if (mode == 2)
            {
              allDenominators *= denominator;
              allNumerators *= numerator;
            }
            // Division
            else if (mode == 3)
            {
              // Reverse multiply because (1/2)/(1/2) -> (1*1)/(2*2)
              allNumerators = allNumerators * denominator;
              allDenominators = allDenominators * numerator;
            }
    
            // make sure denominators are +ve
            if (allDenominators < 0)
            {
              allNumerators = -allNumerators;
              allDenominators = -allDenominators;
            }
          }
        }
    
        // Simplifies fraction by checking to see id the top is bigger than bottom
        // 9/4 -> 2_1/4
    
        int allWholes = 0;
        if (allNumerators >= allDenominators)
        {
          allWholes = allNumerators / allDenominators;
          allNumerators %= allDenominators;
        }
    
        // consider implementing GCD to reduce num/den to simplest form
    
        if (allWholes == 0)
        {
          return (allNumerators + "/" + allDenominators);
        }
        else if (allNumerators == 0 || allDenominators == 0)
        {
          return allWholes + "";
        }
        else
        {
          return allWholes + "_" + (allNumerators + "/" + allDenominators);
        }
      }
    }
    
    -48/128
    1_13/32