實機體驗,再天天抽M8老王現身說法關鍵字魅力!攝影界美人正妹指定相機款籌400萬醫藥費 俞嫻...
2012-07-18 13:40:24 人氣(123) | 回應(0) | 推薦(0) | 收藏(0) 上一篇 | 下一篇

[UVA][Java] 288 - Arithmetic Operations With Large Integers

0
收藏
0
推薦


 Arithmetic Operations With Large Integers 

This problem is about calculations with large numbers. Large means numbers with at most one thousand digits. The operations are limited to addition, substraction, multiplication and raising to a higher power.

There are no limitations to the operants of addition, substraction and multiplication. The base in raising to higher powers is positive and smaller than ten. The exponent is positive.

The inputfile consists of a valid expression with any number of operations. There are no parentheses, but the normal arithmetic priority rules are still valid.

An example of a valid expression is: 12345678 * 129876 + 2**1993. An invalid expression is: 12345678 * 129876 + 12**1993 because the base is greater than nine.

Input

The input contains several test cases, each one on a different line. Each test case contains numbers and operands in the following way: n op n { op n } . n is a positive decimal number with at most one thousand digits, stored as an ASCII-text. op is one of the following: +, -, *, ** (** means ``raising to higher powers''). There can be at most one hundred operations per test case. There are no spaces or other illegal characters in the input.

Output

The output contains the exact result of the evaluated expressions given in the input. Print each test case in a different line. Each test case won't have more than three thousand characters.

Sample Input

12345678*129876+2**1993

Sample Output

896977105683011347056900938420064050017435704756793125373158388145129891712\\
789307700515223684770523373785909874208955291755561688174261977676508872005\\
197801086953040197752187505381087095625350558038492109870986287356370809737\\
409093338414265941143390397695285610643740694879918793932122262001282984143\\
224073001319601441082075018589725061828585163552941409601583724270514300953\\
188533095947591884905338415676554651534516617357655143781579373852994152663\\
198702360093129335607684294312805938140290754926427776409574872859496315224\\
893901812925850900592061583009183090068756428459147015355107518672556877720




import java.math.BigInteger;
import java.util.Scanner;

public class Main {
public static void main(String[] args) {
Scanner cin = new Scanner(System.in);
String line;
while(cin.hasNextLine()) {
line = cin.nextLine();
BigInteger[] S = new BigInteger[201];
int[] neg = new int[200];
int Idx = 0, len = line.length();
int oper = 0;
for(int i = 0; i < len; i++) {
String sub = "";
while(i < len && Character.isDigit(line.charAt(i))) {
sub += line.charAt(i);
i++;
}
if(oper == 0) { // add
S[Idx] = new BigInteger(sub);
neg[Idx] = 0;
Idx++;
} else if(oper == 1) {// subtract
S[Idx] = new BigInteger(sub);
neg[Idx] = 1;
Idx++;
} else if(oper == 2) {// multiply
S[Idx-1] = S[Idx-1].multiply(new BigInteger(sub));
} else {
if(S[Idx-1].longValue() < 2)
continue;
S[Idx-1] = S[Idx-1].pow(Integer.parseInt(sub));
}
if(i < len && line.charAt(i) == '+') {
oper = 0;
} else if(i < len && line.charAt(i) == '-') {
oper = 1;
} else if(i < len && line.charAt(i) == '*') {
if(i+1 < len && line.charAt(i+1) == '*') {
i++;
oper = 3;
} else {
oper = 2;
}
}
}
BigInteger ans = new BigInteger("0");
for(int i = 0; i < Idx; i++) {
if(neg[i] == 1)
ans = ans.subtract(S[i]);
else
ans = ans.add(S[i]);
}
System.out.println(ans);
}
}
}


288Arithmetic OperationsWith Large Integers
台長:Morris
人氣(123) | 回應(0)| 推薦 (0)| 收藏 (0)| 轉寄
全站分類: 不分類 | 個人分類: UVA |
此分類下一篇:[UVA] 255 - Correct Move
此分類上一篇:[UVA] 296 - Safebreaker

我要回應
是 (若未登入"個人新聞台帳號"則看不到回覆唷!)
* 請輸入識別碼:
請輸入以下數字 (ex:123)

(有*為必填)
詳全文