- 计算器的改良
 - @ 2014-12-18 20:23:38
 
是不是java评测机不行?还是我太水了.......
各位大神,求解!!!!!!
我的真的不行了,果然java不适合a题
//整体思路,把整个方程式化简成左边是未知数,而右边是常数的形式
import java.io.*;
    import java.util.*;
    import java.text.DecimalFormat;
    import java.util.Scanner;
public class Main {
public static void main(String[] args) {
            Scanner input = new Scanner(System.in);
            double count = 0, constant = 0; // constant记录未知数的个数,count记录总的数值
            String equation, equation1; // 方程式的字符串
            int Equivalent = 0; // 记录等号位置
            int LastFlag = 0; // 记录最后一个未知数的个数
            char UnknowNumber = 'a'; // 记录改未知数是那个未知数
            equation1 = input.nextLine();
            equation = '+' + equation1;
            equation = equation.replaceAll("=", "=+");
            // System.out.println(equation);
/*
             * System.out.println(equation.length());
             */
char[] chars = equation.toCharArray();
            Equivalent = equation.indexOf('=');
/*
             * 记录等号左边的运算 (因为换边的话会改变符号)
             */
            for (int i = 0; i < Equivalent; i++) {
if ((chars[i] >= 'a' && chars[i] <= 'z')
                        || (chars[i] >= 'A' && chars[i] <= 'Z')) {
                    UnknowNumber = chars[i]; // 记住该未知数
                    int FlagForFirst = 0; // FlagForFirst记录当前未知数的位置
                    String x; // x记录当前未知数的个数
                    boolean XSign = true; // 新的未知数标志记录符号
for (int j = i; j > 0; j--) {
                        if (chars[j] == '+') {
                            FlagForFirst = j;
                            XSign = true;
                            break;
                        }
                        if (chars[j] == '-') {
                            FlagForFirst = j;
                            XSign = false;
                            break;
                        }
                    }
                    x = equation.substring(FlagForFirst + 1, i);
                    int y = Integer.parseInt(x); // y记录当前未知数的个数
                    if (XSign) {
                        constant += y;
                    } else {
                        constant -= y;
                    }
// System.out.println(constant);
}
            }
/*
             * 从这里开始计算剩余项
             */
            boolean Finish = true; // 设置一个标志为记录中间是否有未知数,false表示有字母,true表示没有字母
            String CountForNumber; // 记录数字运算,count记录总的数值
            boolean YSign = true; // 记录数字的符号
            for (int i = 0; i < Equivalent; i++) {
                int j;
                if (chars[i] == '+' || chars[i] == '-') {
                    if (chars[i] == '+') {
                        YSign = true;
                    }
                    if (chars[i] == '-') {
                        YSign = false;
                    }
                    j = i + 1;
                    while (chars[j] != '+' && chars[j] != '-' && chars[j] != '=') {
                        j++;
                    }
// System.out.println(i+" "+j); // 测试在两个运算符号之间
                    Finish = true;
                    for (int k = j; k > i; k--) {
                        if ((chars[k] >= 'a' && chars[k] <= 'z')
                                || (chars[k] >= 'A' && chars[k] <= 'Z')) {
                            Finish = false;
                            break;
                        }
                        if (k == i + 1 && Finish) {
                            CountForNumber = equation.substring(i + 1, j);
// System.out.println(CountForNumber);
int temp = Integer.parseInt(CountForNumber);
                            if (YSign) {
                                count -= temp;
                            } else {
                                count += temp;
                            }
                            // System.out.println(count);
                        }
                    }
}
}
/*
             * 
             * 开始计算右边的值 写到这里其实已经完成了70%了,因为算法是一样的,只不过左边和右边需要变号
             * 修改一下原来的代码,左边的算式计算完毕,接下来就是计算右边的 算式了。
             */
for (int i = Equivalent; i < equation.length(); i++) {
if ((chars[i] >= 'a' && chars[i] <= 'z')
                        || (chars[i] >= 'A' && chars[i] <= 'Z')) {
                    int FlagForFirst = 0; // FlagForFirst记录当前未知数的位置
                    String x; // x记录当前未知数的个数
                    boolean XSign = true; // 新的未知数标志记录符号
for (int j = i; j > 0; j--) {
                        if (chars[j] == '+') {
                            FlagForFirst = j;
                            XSign = true;
                            break;
                        }
                        if (chars[j] == '-') {
                            FlagForFirst = j;
                            XSign = false;
                            break;
                        }
                    }
                    x = equation.substring(FlagForFirst + 1, i);
                    int y = Integer.parseInt(x); // y记录当前未知数的个数
                    if (XSign) {
                        constant -= y;
                    } else {
                        constant += y;
                    }
// System.out.println(constant);
                }
            }
boolean Finish2 = true; // 设置一个标志为记录中间是否有未知数,false表示有字母,true表示没有字母
            String CountForNumber2; // 记录数字运算,count记录总的数值
            boolean YSign2 = true; // 记录数字的符号
            for (int i = Equivalent; i < equation.length() - 2; i++) {
                int j;
                if (chars[i] == '+' || chars[i] == '-') {
                    if (chars[i] == '+') {
                        YSign2 = true;
                    }
                    if (chars[i] == '-') {
                        YSign2 = false;
                    }
                    j = i + 1;
                    while (chars[j] != '+' && chars[j] != '-'
                            && j < equation.length() - 1) {
                        j++;
                    }
// System.out.println(i+" "+j); // 测试在两个运算符号之间
                    Finish2 = true;
                    for (int k = j; k > i; k--) {
                        if ((chars[k] >= 'a' && chars[k] <= 'z')
                                || (chars[k] >= 'A' && chars[k] <= 'Z')) {
                            Finish2 = false;
                            break;
                        }
                        if (k == i + 1 && Finish2) {
                            CountForNumber2 = equation.substring(i + 1, j);
// System.out.println(CountForNumber);
int temp = Integer.parseInt(CountForNumber2);
                            if (YSign2) {
                                count += temp;
                            } else {
                                count -= temp;
                            }
                        }
                    }
}
}
/*
             * System.out.println(constant); System.out.println(count);
             */
            DecimalFormat df = new DecimalFormat("0.000");
            double answer = count / constant;
            System.out.print(UnknowNumber + "=");
            System.out.println(df.format(answer));
        }
}
5 条评论
- 
  qiuhaiyi LV 8 @ 2016-11-15 17:05:14
84行
 - 
  @ 2016-11-11 18:59:16
额额额
 - 
  @ 2014-12-27 16:27:45
纯模拟,仔细查查代码
 - 
  @ 2014-12-26 12:09:24
你程序有错,自己多查查吧。
 - 
  @ 2014-12-25 13:25:10
orz
 
- 1