给定一个合法的表达式字符串,其中只包含非负整数、加法、减法以及乘法符号(不会有括号),例如7+3*4*5+2+4-3-1,请写程序计算该表达式的结果并输出;
给定一个合法的表达式字符串,其中只包含非负整数、加法、减法以及乘法符号(不会有括号),例如7+3*4*5+2+4-3-1,请写程序计算该表达式的结果并输出;
输入有多行,每行是一个表达式,输入以END作为结束
每行表达式的计算结果
7+3*4*5+2+4-3-1 2-3*1 END
69 -1
每个表达式的长度不超过10000,保证所有中间结果和最后结果在[-2e9,2e9]范围内
import java.io.*; public class Main { public static void main(String[] args) throws IOException { BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); String str = ""; while (str.indexOf("END") == -1){ str+= (br.readLine() + ","); } str = str.substring(0,str.indexOf("END")-1); String[] arr = str.split(","); for(int i=0;i<arr.length;i++){ System.out.println(calcFunc(arr[i])); } } static int calcFunc(String ne){ String operators = "+-*"; char lastOperator = '+'; //上个运算符 char tempOperator = ' '; //临时运算符 int lastValue = 0; //上个值 int previousVal = 0; //上上个值 String currVal = ""; char temp = ' '; int sum = 0; for(int i=0;i<ne.length();i++){ temp = ne.charAt(i); if(operators.indexOf(temp) != -1){ lastValue = lastOperator!='*'?Integer.valueOf(currVal):lastValue*Integer.valueOf(currVal); currVal = ""; if(temp == '*'){ if(lastOperator == '*'){ lastOperator = temp; }else{ tempOperator = lastOperator; lastOperator=temp; } }else{ if(lastOperator == '*'){ sum +=previousVal; previousVal = Integer.valueOf(""+tempOperator+lastValue); lastOperator = temp; }else{ sum += previousVal; previousVal = lastOperator == '+'? lastValue : Integer.valueOf(""+lastOperator+lastValue); lastOperator = temp; } } }else{ currVal += temp; if(i == ne.length()-1){ if(lastOperator == '*'){ lastValue = lastValue * Integer.valueOf(currVal); if(tempOperator == '-'){ sum += (previousVal - lastValue); }else { sum +=(previousVal + lastValue); } }else{ sum +=(previousVal + Integer.valueOf(""+lastOperator + currVal)); } } } } return sum; } }
import java.util.*; public class Main{ public static void main(String args[]){ Scanner sc = new Scanner(System.in); while(sc.hasNext()){ String str = sc.next(); if(!str.equals("END")){ int res=0; int d=0; char sign='+'; Stack<Integer> S = new Stack<>(); for(int i=0;i<str.length();++i){ char c = str.charAt(i); if(c>='0'){ d = d*10 -'0' + c; } if((c<'0')||i==str.length()-1){ if(sign=='+'){ S.push(d); } else if(sign=='-'){ S.push(-d); } else if(sign=='*'){ int tmp = S.peek()*d; S.pop(); S.push(tmp); } d=0; sign=c; } } while(!S.isEmpty()){ res+=S.peek(); S.pop(); } System.out.println(res); } } } }
import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.util.ArrayList; /** * @Author: coderjjp * @Date: 2020-05-07 18:36 * @Description: * @version: 1.0 */ public class Main { public static void main(String[] args) throws IOException { BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); String cur; while (!(cur=br.readLine()).equals("END")){ String s1[] = cur.split("[+\\-*]");// ArrayList<Integer> nums = new ArrayList<>();//保存数字 for (int i = 0; i < s1.length; i++) nums.add(Integer.valueOf(s1[i])); StringBuilder ch = new StringBuilder()//保存符号 .append(cur.replaceAll("\\d","")); int i = 0; while (i < ch.length()){ if (ch.charAt(i) == '*'){//核心代码,只有4行 nums.set(i, nums.get(i)*nums.get(i+1)); nums.remove(i+1); ch.deleteCharAt(i); continue; } i++; } /*至此只有+-,从前向后计算即可*/ if (nums.size()==1) System.out.println(nums.get(0)); else { int res = addOrSub(nums.get(0), nums.get(1), ch.charAt(0)); for (i = 1; i < ch.length(); i++) res = addOrSub(res, nums.get(i+1),ch.charAt(i)); System.out.println(res); } } } private static int addOrSub(int num1, int num2, char c){ if (c == '+') return num1 + num2; else return num1 - num2; } }
import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.util.ArrayList; /** * @Author: coderjjp * @Date: 2020-05-07 16:02 * @Description: * @version: 1.0 */ public class Main { public static void main(String[] args) throws IOException { BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); String cur; while (!(cur=br.readLine()).equals("END")){ String s1[] = cur.split("[+\\-*/]"); int nums[] = new int[s1.length]; for (int i = 0; i < s1.length; i++) nums[i] = Integer.valueOf(s1[i]); String s2 = cur.replaceAll("\\d",""); char[] chs = new char[s2.length()]; for (int i = 0; i < chs.length; i++) chs[i] = s2.charAt(i); ArrayList<Integer> n = new ArrayList<>(); ArrayList<Character> c = new ArrayList<>(); for (int i = 0; i < chs.length; i++ ){ if (chs[i] == '+' || chs[i] == '-'){ n.add(nums[i]); c.add(chs[i]); }else { int temp = mulOrDiv(nums[i], nums[i+1],chs[i]); for (i = i + 1; i < chs.length && (chs[i] == '*' || chs[i] == '/');i++){ temp = mulOrDiv(temp, nums[i+1],chs[i]); } n.add(temp); if (i < chs.length) c.add(chs[i]); } } if (n.size() == c.size()) n.add(nums[nums.length-1]); if (n.size()==1){ System.out.println(n.get(0)); continue; } int res = addOrSub(n.get(0), n.get(1), c.get(0)); for (int i = 1; i < c.size(); i++ ) res = addOrSub(res, n.get(i+1), c.get(i)); System.out.println(res); } } private static int mulOrDiv(int num1, int num2, char c){ if (c == '*') return num1 * num2; else return num1 / num2; } private static int addOrSub(int num1, int num2, char c){ if (c == '+') return num1 + num2; else return num1 - num2; } }