本帖最后由 icris 于 2014-7-16 22:37 编辑
- private static void cal() throws IOException {
- BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
- String string = "";
- String buf = null;
- double d = 0 ;
-
- for(;;){
- buf = reader.readLine();
- if (buf.length() <= 0) {
- continue;
- }
- if (buf.equals("end")) {
- break;
- }
- if (buf.startsWith("+")) {
- d+=new Double(buf.substring(1));
- string += buf;
- }else if (buf.startsWith("-")) {
- d-=new Double(buf.substring(1));
- string += buf;
- }else if (buf.startsWith("*")) {
- d*=new Double(buf.substring(1));
- string = "("+string+")"+buf;
- }else if (buf.startsWith("/")) {
- d/=new Double(buf.substring(1));
- string = "("+string+")"+buf;
- }else {
- d = new Double(buf);
- string = buf;
- }
- if ((d - Math.round(d)) == 0) {
- System.out.print((int)d);
- }else {
- System.out.print(d);
- }
- System.out.println( "\t(" + string + ")");
- }
- }
复制代码
想了一下好像应该用 StringBuidler 不过就这样吧…
bug 非常多,不想改了…只要严格按照第一步输数字然后输运算符加数字就可以了。 |