手柄君的小阁

个人私货聚集地

淮南师范学院OJ 79.DS 计算 & H5计算器

79.DS 计算

时长限制: 1000 MS
内存限制: 128 MB

描述

小明在你的帮助下,破密了Ferrari 设的密码门,正要往前走,突然又出现了一个密码门,门上有一个算式,其中只有“(”,“)”,“0-9”,“+”,“-”,“*”,“/”,“^”,求出的值就是密码。小明数学学得不好,还需你帮他的忙。(“/”用整数除法)

输入

输入的第一行为一个整数n。接下来n行,每行一个算式。

输出

对n个算式,每行输出一个整数,表示计算结果。输出后换行。

示例输入

1
1+(3+2)*(7^2+6*9)/(2)

示例输出

258

解题思路

首先拆分括号为算式,然后根据运算优先级别,找到首先要运算的运算符,并在两侧查找数字并转换为Int进行计算

Ac源码(Java)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
import java.util.Scanner;
 
public class Main {
 
	public static void main(String[] args) {
		Scanner in = new Scanner(System.in);
		int times = in.nextInt();
		in.nextLine();
		String[] formulas = new String[times];
		for (int i = 0; i < times; i++) {
			formulas[i] = in.nextLine();
		}
		in.close();
		for (String e : formulas) {
			System.out.println(calc(e));
		}
	}
 
	private static String calc(String e) {
		//System.out.println(e);
		// e = e.replaceAll("\\+-", "-");
		// e = e.replaceAll("-", "+-");
		// System.out.println(e);
		// if(e.charAt(0) == '+'){
		// e = e.replaceFirst("\\+-", "-");
		// }
		// System.out.println(e);
		if (e.length() <= 0) {
			return "";
		}
		int start = -1;
		int end = -1;
		int counter = 0;
		int flag = 0;
		for (int i = 0; i < e.length(); i++) { if (e.charAt(i) == '(') { if (start == -1) { start = i; flag = 1; } counter++; } if (e.charAt(i) == ')') { counter--; if (counter == 0) { end = i; break; } } } // System.out.println("" + start + "," + end); String l, r; l = ""; r = ""; if (flag == 1) { if (start > 0) {
				l = e.substring(0, start);
			}
			if (e.length() - end > 0) {
				r = e.substring(end + 1, e.length());
			}
			return calc(l + calc(e.substring(start + 1, end)) + r);
		}
		int index;
		if (e.indexOf('^') > -1) {
			flag = 2;
			index = e.indexOf('^');
		} else if (e.indexOf('*') > -1 || e.indexOf('/') > -1) {
			flag = 3;
			int a = e.indexOf('*');
			int b = e.indexOf('/');
			if ((a > -1 && b > -1 && a < b) || (a > -1 && b == -1)) {
				index = a;
			} else {
				index = b;
			}
		} else if (e.indexOf('+') > -1 || e.length() > 0 && e.substring(1).indexOf('-') > -1) {
			flag = 4;
			int a = e.indexOf('+');
			int b = e.substring(1).indexOf('-') + 1;
			if ((a > 0 && b > 0 && a < b) || (a > 0 && b == 0)) {
				index = a;
			} else {
				index = b;
			}
		} else {
			index = -1;
		}
		//System.out.println(index);
		if (flag > 1) {
			// if (flag == 4) {
			boolean superFlag = false;
			start = 0;
			for (int i = index - 1; i > -1; i--) {
				if (!Character.isDigit(e.charAt(i))) {
					if (!superFlag && e.charAt(i) == '-') {
						superFlag = true;
						continue;
					}
					start = i + 1;
					break;
				} else if (superFlag) {
					start = i + 2;
					break;
				}
			}
			superFlag = false;
			int i = index + 1;
			end = e.length() - 1;
			if (e.charAt(index + 1) == '-') {
				superFlag = true;
				i = index + 2;
			}
			for (; i < e.length(); i++) { if (!Character.isDigit(e.charAt(i))) { end = i - 1; break; } } String strL = e.substring(start, index); String strR = e.substring(index + 1, end + 1); //System.out.println(strL); //System.out.println(strR); int numL = 0; int numR = 0; if (strL.length() > 0) {
				numL = Integer.parseInt(strL);
			}
			if (strR.length() > 0) {
				numR = Integer.parseInt(strR);
			}
			// System.out.println(numL);
			// System.out.println(numR);
			int result = 0;
			switch (flag) {
			case 2: {
				result = (int) Math.pow(numL, numR);
				break;
			}
			case 3: {
				if (e.charAt(index) == '*') {
					result = numL * numR;
				} else {
					result = numL / numR;
				}
				break;
			}
			case 4: {
				if (e.charAt(index) == '+') {
					result = numL + numR;
				} else {
					result = numL - numR;
				}
				break;
			}
			}
			if (start > 0) {
				l = e.substring(0, start);
			}
			if (e.length() - 1 - end > 0) {
				r = e.substring(end + 1, e.length());
			}
			return calc(l + result + r);
		}
		return e;
	}
 
}

另外,很凑巧,近期由于其他原因上课,被要求做一个网页简易计算器,初步打算使用eval,但由于eval对次方支持不力,故将上方代码修改为Js使用

在线使用:>注意:键盘按键不可用<

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
  /**
   * 计算一个表达式
   * @param {string} 输入的合法表达式
   * @return {string} 返回的结果字符串
   */
  function calc(e) {
    if (e.length <= 0) {
      return "";
    }
    let start = -1;
    let end = -1;
    let counter = 0;
    let flag = 0;
    for (let i = 0; i < e.length; i++) { if (e.charAt(i) == "(") { if (start == -1) { start = i; flag = 1; } counter++; } if (e.charAt(i) == ")") { counter--; if (counter == 0) { end = i; break; } } } let l, r; l = ""; r = ""; if (flag == 1) { if (start > 0) {
        l = e.substring(0, start);
      }
      if (e.length - end > 0) {
        r = e.substring(end + 1, e.length);
      }
      return calc(l + calc(e.substring(start + 1, end)) + r);
    }
    let index;
    if (e.indexOf("^") > -1) {
      flag = 2;
      index = e.indexOf("^");
    } else if (
      e.indexOf("×") > -1 ||
      e.indexOf("÷") > -1 ||
      e.indexOf("%") > -1
    ) {
      flag = 3;
      let a = e.indexOf("×");
      let b = e.indexOf("÷");
      let c = e.indexOf("%");
      index = min(a, b, c);
    } else if (
      e.indexOf("+") > -1 ||
      (e.length > 0 && e.substring(1).indexOf("-") > -1)
    ) {
      flag = 4;
      let a = e.indexOf("+");
      let b = e.substring(1).indexOf("-") + 1;
      if ((a > 0 && b > 0 && a < b) || (a > 0 && b == 0)) {
        index = a;
      } else {
        index = b;
      }
    } else {
      index = -1;
    }
    if (flag > 1) {
      let superFlag = false;
      start = 0;
      for (let i = index - 1; i > -1; i--) {
        if (!Character.isDigit(e.charAt(i))) {
          if (!superFlag && e.charAt(i) == "-") {
            superFlag = true;
            continue;
          }
          start = i + 1;
          break;
        } else if (superFlag) {
          start = i + 2;
          break;
        }
      }
      superFlag = false;
      let i = index + 1;
      end = e.length - 1;
      if (e.charAt(index + 1) == "-") {
        superFlag = true;
        i = index + 2;
      }
      for (; i < e.length; i++) { if (!Character.isDigit(e.charAt(i))) { end = i - 1; break; } } let strL = e.substring(start, index); let strR = e.substring(index + 1, end + 1); let numL = 0; let numR = 0; if (strL.length > 0) {
        numL = parseFloat(strL);
      }
      if (strR.length > 0) {
        numR = parseFloat(strR);
      }
      let result = 0;
      switch (flag) {
        case 2: {
          result = Math.pow(numL, numR);
          break;
        }
        case 3: {
          if (e.charAt(index) == "×") {
            result = numL * numR;
          } else if (e.charAt(index) == "÷") {
            result = numL / numR;
          } else {
            result = numL % numR;
          }
          break;
        }
        case 4: {
          if (e.charAt(index) == "+") {
            result = numL + numR;
          } else {
            result = numL - numR;
          }
          break;
        }
      }
      if (start > 0) {
        l = e.substring(0, start);
      }
      if (e.length - 1 - end > 0) {
        r = e.substring(end + 1, e.length);
      }
      return calc(l + result + r);
    }
    for (let i = 0; i < e.length; i++) {
      if (!(Character.isDigit(e.charAt(i)) || e.charAt(i) == "-")) {
        throw "Wrong!";
      }
    }
    return e;
  }

完整网页代码下载:dscalc

觉得有点意思,资助本站一下吧

查看详细收支和捐款

喵喵喵 喵喵喵

来一发吐槽