HDU 2424-Gary's Calculator(表达式计算+大数)

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

给出一个只含+和*的表达式,求其值。由于数据太大,使用BigInteger 。 可以采用栈来计算表达式,先算*,后算+;
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) != '+') {
					res = res.add(new BigInteger(x));
				}
			}
			System.out.println(res);
		}
	}
}

HDU 2424-Gary's Calculator(表达式计算+大数)

时间: 2024-10-06 04:28:21

HDU 2424-Gary's Calculator(表达式计算+大数)的相关文章

【HDOJ】2424 Gary&#39;s Calculator

大数乘法加法,直接java A了. 1 import java.util.Scanner; 2 import java.math.BigInteger; 3 4 public class Main { 5 public static void main(String[] args) { 6 Scanner cin = new Scanner(System.in); 7 int n; 8 int i, j, k, tmp; 9 int top; 10 boolean flag; 11 int t

表达式计算

1 #include<iostream> 2 #include<string> 3 #include<cstdlib> 4 #include<cstring> 5 #include<iomanip> 6 #include<stack> 7 using namespace std; 8 9 #define OK 0 10 #define ERROR -1 11 #define OVERFLOW -1 12 #define OPSETSI

C# - 二叉树表达式计算

很早以前就写过双栈的表达式计算. 这次因为想深入学一下二叉树,网上都是些老掉牙的关于二叉树的基本操作. 感觉如果就学那些概念,没意思也不好记忆.于是动手写了一个表达式计算的应用例子. 这样学习印象才深嘛. 我喜欢逆过来贴代码~ 这是运行结果: cal() 是节点类里的计算方法,从根节点调用,递归所有子节点进行计算.Left,Right分别是左右子节点. 1 public double cal() 2 { 3 if (this.isDig==false) 4 { 5 return CAL(Fuha

Vs2013在Linux开发中的应用(26):表达式计算

快乐虾 http://blog.csdn.net/lights_joy/ 欢迎转载,但请保留作者信息 当VS调试时将鼠标移到一个变量上面的时候,VS将显示这个变量的值,实际上这个时候VS进行了表达式的计算,我们所需要做的,就是把这个过程转换为gdb的命令: Operation Description -enable-pretty-printing enable Python-based pretty-printing -var-create create a variable object -v

C++实现 逆波兰表达式计算问题

C++实现 逆波兰表达式计算问题 #include <iostream> #include <string> using namespace std; class Stack { private: int size; int top; float *listArray; public: Stack(int sz=20); ~Stack(); bool push(float it);//入栈 bool pop(float& it);//出栈 bool isEmpty();//

爪哇国新游记之二十二----算术表达式计算求值

代码: import java.util.ArrayList; import java.util.List; // 辅助类 class Item{ String value; boolean isNumber; public Item(String value,boolean isNumber){ this.value=value; this.isNumber=isNumber; } public Item(char c,boolean isNumber){ this.value=String.

hdu 1086(计算几何入门题——计算线段交点个数)

链接:http://acm.hdu.edu.cn/showproblem.php?pid=1086 You can Solve a Geometry Problem too Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total Submission(s): 7167    Accepted Submission(s): 3480 Problem Description Ma

HDU 1250 Hat&#39;s Fibonacci(Java大数相加)+讲解

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1250 Problem Description A Fibonacci sequence is calculated by adding the previous two members the sequence, with the first two members being both 1. F(1) = 1, F(2) = 1, F(3) = 1,F(4) = 1, F(n>4) = F(n -

C#动态表达式计算

应该有不少人开发过程中遇到过这样的需求,我们直接看图说话: 如上图所示,其中Entity为实体类,其中包括五个属性,该五个属性的值分别来自于数据库查询结果: 用户通过可视化界面进行某些条件的配置以及某些算法的配置并自动生成表达式或者生成数学模型: 程序中需要通过生成的表达式以及动态从数据库中获取的数据进行算法映射以及自动计算出结果. 该需求这边可以举出几个应用场景: 1.报表设计器 我们可以通过报表设计器设计数据库的映射关系并配置数据之间的算法关系,然后动态生成报表: 2.某些采集工具 定向采集