# Gary's Calculator

Time Limit: 5000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 950    Accepted Submission(s): 209

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.

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.

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

import java.io.*;
import java.util.*;
import java.math.*;

public class Main {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
int n, cas = 1;
while (in.hasNext()) {
n = in.nextInt();
String[] s = new String[n];
for (int i = 0; i < n; i++)
s[i] = in.next();
int flag = 1;
if (n % 2 == 0)
flag = 0;
for (int i = 0; i < n; i++) {
if ((i % 2 == 1 && s[i].charAt(0) != '+' && s[i].charAt(0) != '*')
|| (i % 2 == 0 && (s[i].charAt(0) == '+' || s[i]
.charAt(0) == '*')))
flag = 0;
}
System.out.print("Case " + cas + ": ");
cas++;
if (flag == 0 || n == 0) {
System.out.println("Invalid Expression!");
continue;
}
Stack stack = new Stack();
for (int i = 0; i < n; i++) {
int tag = 0;
if (s[i].charAt(0) != '+' && s[i].charAt(0) != '*') {
if (stack.size() != 0) {
String t = stack.peek().toString();
if (t.charAt(0) == '*') {
stack.pop();
if (stack.size() != 0) {
String tt = stack.pop().toString();
BigInteger a = new BigInteger(s[i].toString());
BigInteger b = new BigInteger(tt.toString());
stack.push(a.multiply(b).toString());
tag = 1;
}
}
}
}
if (tag == 0)
stack.push(s[i]);
}
BigInteger res = BigInteger.ZERO;
while (stack.size() != 0) {
String x = stack.pop().toString();
if (x.charAt(0) != '+') {
}
}
System.out.println(res);
}
}
}


• 本文已收录于以下专栏：

## HDU 2424-Gary's Calculator

• u012127882
• 2015年06月21日 15:47
• 503

## 【HDU】5238 Calculator 【中国剩余定理+线段树】

• u013368721
• 2015年05月29日 12:55
• 1557

## hdu - 5238 Calculator（线段树+中国剩余定理）线段树好题

• u010660276
• 2015年06月06日 13:33
• 819

## Beaver's Calculator coderforces 70A1 蓝桥杯

Beaver's Calculator coderforces 70A1 这道题贪心 思路简单 但是方法对于我来说比较巧了 题解看源码 #include #include using n...
• liyanfeng1996
• 2017年02月02日 21:02
• 552

## 算法训练 Beaver's Calculator/codeforces 207A1 排序+贪心

• HowardEmily
• 2017年02月07日 17:51
• 1075

## Beaver's Calculator（蓝桥杯 算法训练）sort排序

• qq_36950575
• 2017年12月31日 17:53
• 47

## HDU 3624: Digital Calculator

• SDU_Phonism
• 2013年10月23日 20:44
• 547

## Beaver's Calculator 【归并排序+贪心】

• u014641529
• 2016年05月14日 11:13
• 1139

## Hdu 大数运算

Font Size: ← → Problem Description One of the first users of BIT's new supercomputer was Chip Diller...
• zp___waj
• 2015年03月31日 22:09
• 718

## 蓝桥杯- 算法训练-Beaver's Calculator

• survivorone
• 2016年12月27日 13:45
• 1336

举报原因： 您举报文章：HDU 2424-Gary's Calculator（表达式计算+大数） 色情 政治 抄袭 广告 招聘 骂人 其他 (最多只允许输入30个字)