1 PackageTest;2 ImportJava.util.Scanner;3 /**4 * Design a calculator, realize multi-line input, and output the result separately5 * Input:6 * 4/2+5+9*7-97 * 3*2+8/2-7+28 * 3+8*4-5+3+19 * 9/4/2+7-6-1Ten * Output: One * A * 5 - * - * @authorlitaiqing the * - */ - Public classTest { - Public Static voidMain (string[] args) { +Scanner Scanner =NewScanner (system.in); - while(Scanner.hasnext ()) { +String in =scanner.nextline (); A System.out.println (Calc (in)); at } - } - Public Static intCalc (String in) { - intresult = 1; - if(In.contains ("+")){ -string[] Nums = In.split ("\\+"); inresult = Calc (nums[0]); - for(inti = 1; i < nums.length; i++){ toResult + =Calc (nums[i]); + } -}Else if(In.contains ("-")){ thestring[] Nums = In.split ("\\-"); *result = Calc (nums[0]); $ for(inti = 1; i < nums.length; i++){Panax NotoginsengResult-=Calc (nums[i]); - } the}Else if(In.contains ("*")){ +string[] Nums = In.split ("\\*"); Aresult = Calc (nums[0]); the for(inti = 1; i < nums.length; i++){ +Result *=Calc (nums[i]); - } $}Else if(In.contains ("/")){ $string[] Nums = In.split ("/"); -result = Calc (nums[0]); - for(inti = 1; i < nums.length; i++){ theResult/=Calc (nums[i]); - }Wuyi}Else{ theresult =Integer.parseint (in); - } Wu returnresult; - } About}
Output
4/2+5+9*7-9
61
3*2+8/2-7+2
5
3+8*4-5+3+1
34
9/4/2+7-6-1
1
The design of a calculator algorithm. Don't know if the code has a bug