首页 > 代码库 > [JAVA][HDU 1237][九度 1019][简单计算器]
[JAVA][HDU 1237][九度 1019][简单计算器]
本来以为是一道很简单的stack题目,居然花了四五十分钟来解决,JAVA本身就有stack的应用优势,但还是花了自己很多时间。。
提供一些要点吧:
1.首先是来自九度的测试案例
1 + 2
5
4 + 2 * 5 - 7 / 11
3
0 + 5
1 - 2 * 3 * 4 + 5 * 6
1 * 2 * 3 + 5 + 6 - 7 * 8 + 9 / 10
0 + 0 * 0
1 + 5 * 0
0 + 5
0
2.输入0时结束,但是运算到0的时候不结束,这个应该很容易排除
3.输出之前再检查一遍stack是否有残留,如果有就再次运算,直到operator的stack为空,num的stack只剩一个的情况即为结果,保留两位输出就行
4.利用float提交时出现WA,换用double就解决了。(似乎很多人都是错在这里)
5.在HDU的OJ提交的时候,利用System.out.printf()输出‘\n‘来换行的情况下出现PE,但是换成System.out.println()来换行直接AC了,都是一些小细节的问题
最后是sourcecode:
[java] view plaincopyprint?
- import java.io.BufferedInputStream;
- import java.util.Scanner;
- import java.util.Stack;
- public class Main {
- public static char toChar(String str) {
- return str.charAt(0);
- }
- public static boolean isNumeric(String str) {
- for (int i = str.length(); --i >= 0;) {
- if (!Character.isDigit(str.charAt(i))) {
- return false;
- }
- }
- return true;
- }
- public static int prior(char op1, char op2)
- // compare the priority of 2 ops
- // return the higher priority op‘s num
- // if the priority is same return 0
- {
- if (op1 == ‘*‘ || op1 == ‘/‘) {
- if (op2 == ‘*‘ || op2 == ‘/‘) {
- return 0;
- } else {
- return 1;
- }
- } else {
- if (op2 == ‘*‘ || op2 == ‘/‘) {
- return 2;
- } else {
- return 0;
- }
- }
- }
- public static double cal(double num1, double num2, char op) {
- switch (op) {
- case ‘+‘:
- return (num1 + num2);
- case ‘-‘:
- return num1 - num2;
- case ‘*‘:
- return num1 * num2;
- case ‘/‘:
- return num1 / num2;
- }
- return 0;
- }
- public static void main(String[] args) {
- Scanner sc = new Scanner(new BufferedInputStream(System.in));
- String expression = sc.nextLine();
- while (!expression.equals("0")) {
- Stack<Character> op = new Stack<Character>();
- Stack<Double> num = new Stack<Double>();
- String element[] = expression.split(" ");
- for (int i = 0; i < element.length; i++) {
- if (isNumeric(element[i])) {// if the element searching is a num
- if (num.size() <= 1) {// if less than one num in the stack,push the num in
- num.push(Double.valueOf(element[i]));
- }
- } else {// if it is a operator
- if (op.isEmpty()) {// if the operator stack is empty push it in
- op.push(toChar(element[i]));
- } else {
- char op1 = op.peek();
- char op2 = toChar(element[i]);
- if (prior(op1, op2) <= 1) {// if the last operator is more prior
- double nextNum = num.pop();
- double lastNum = num.pop();
- num.push(cal(lastNum, nextNum, op.pop()));
- op.push(op2);
- } else if (prior(op1, op2) == 2
- // && i + 2 < element.length
- // && prior(toChar(element[i + 2]), op2) != 1
- ) {
- // if the new operator is more prior
- double nextNum = Double.valueOf(element[++i]);// get the next num
- double lastNum = num.pop();
- num.push(cal(lastNum, nextNum, op2));
- } else if (prior(op1, op2) == 2) {
- double nextNum = Double.valueOf(element[++i]);// get the next num
- double lastNum = num.pop();
- }
- }
- }
- }
- if (!op.empty()) {
- double nextNum = num.pop();
- double lastNum = num.pop();
- System.out.printf("%.2f", cal(lastNum, nextNum, op.pop()));
- System.out.println();
- } else if (num.size() == 1) {
- System.out.printf("%.2f\n", num.pop());
- System.out.println();
- }
- expression = sc.nextLine();
- }
- }
- }
[JAVA][HDU 1237][九度 1019][简单计算器]
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。