高精度问题——Java大数类~系列9——整数基本运算之加法、乘法 hdu 2424
高精度问题(9)——整数基本运算之加法、乘法
题目要求输出算式计算结果,如果运算不合法,就输出"Invalid Expression!",其中的运算只包含加法和乘法。还是用Java大数类实现,同时模拟一下栈操作以确保运算优先级。
hdu 2424 原题如下:
Gary's Calculator
Time Limit: 5000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 829 Accepted Submission(s): 182
Problem Description
Gary has finally decided to find a calculator to avoid making simple calculational mistakes in his math exam. Unable to find a suitable calculator in the market with enough precision, Gary has designed a high-precision calculator himself. Can you help him to write the necessary program that will make his design possible?
For simplicity, you only need to consider two kinds of calculations in your program: addition and multiplication. It is guaranteed that all input numbers to the calculator are non-negative and without leading zeroes.
For simplicity, you only need to consider two kinds of calculations in your program: addition and multiplication. It is guaranteed that all input numbers to the calculator are non-negative and without leading zeroes.
Input
There are multiple test cases in the input file. Each test case starts with one positive integer N (N < 20), followed by a line containing N strings, describing the expression which Gary's calculator should evaluate. Each of the N strings might be a string representing a non-negative integer, a "*", or a "+". No integer in the input will exceed 109.
Input ends with End-of-File.
Input ends with End-of-File.
Output
For each test case, please output one single integer (with no leading zeros), the answer to Gary's expression. If Gary's expression is invalid, output "Invalid Expression!" instead. Please use the format indicated in the sample output.
Sample Input
3
100 + 600
3
20 * 4
2
+ 500
5
20 + 300 * 20
Sample Output
Case 1: 700
Case 2: 80
Case 3: Invalid Expression!
Case 4: 6020
代码如下:
1 //9. 大数简单四则运算之加法、乘法 ~java~ hdu 2424 2 import java.math.BigInteger; 3 import java.util.Scanner; 4 5 public class Main 6 { 7 public static void main(String[] args) 8 { 9 Scanner cin = new Scanner(System.in); 10 int n; 11 String[] f=new String[30]; //字符数组 12 BigInteger[] num=new BigInteger[30]; //大数数组 13 String str; 14 long k=0; 15 f[0]="1"; 16 while(cin.hasNext()) 17 { 18 int flag=1, ntop=0, stop=0; 19 k++; 20 n=cin.nextInt(); 21 if(n%2==0) //n是偶数:意味着缺少数字,或多了运算符 22 flag=0; 23 for(int i=0;i<n;i++) //模拟栈操作,判到乘法先运算,再计算剩下的 24 { 25 str=cin.next(); 26 if(flag==0) //flag==0 表示运算不合法,直接跳出,转入下一组数据 27 continue; 28 if(i%2==0 && (!str.equals("+")) && (!str.equals("*"))) 29 { 30 BigInteger w=new BigInteger(str); 31 num[++ntop]=w; 32 if(f[stop].equals("*")) 33 { 34 num[ntop-1]=num[ntop].multiply(num[ntop-1]); 35 ntop--; 36 stop--; 37 } 38 } 39 else if(i%2!=0 && (str.equals("+") || str.equals("*"))) 40 { 41 f[++stop]=str; 42 if(f[stop].equals("+") && ntop>1) 43 { 44 num[ntop-1]=num[ntop].add(num[ntop-1]); 45 ntop--; 46 stop--; 47 } 48 } 49 else 50 flag=0; 51 } 52 while(flag!=0 && stop>0) //不存在数字多了的情况,所以只要看stop,运算直到运算符全部用完为止 53 { 54 if(f[stop].equals("+")) 55 num[ntop-1]=num[ntop].add(num[ntop-1]); 56 else if(f[stop].equals("*")) 57 num[ntop-1]=num[ntop].multiply(num[ntop-1]); 58 stop--; 59 } 60 if(flag==0) 61 System.out.println("Case "+k+": Invalid Expression!"); 62 else 63 System.out.println("Case "+k+": "+num[1]); 64 } 65 } 66 }
2013-08-11
晴
浙公网安备 33010602011771号