Problem description
Enter a string expression that prints the result of the expression containing only addition and subtraction, and the operation is within the integer range.
Input format
Line, string expression. The length of the expression is not more than 100. Expression has at least one item and ends with an equal sign =
Output format
Row, the output results.
Sample input
2+2345+913-3=
Sample output
3257
Topic Analysis: Simple computational problems
algorithm analysis: to pay attention to the first number of negative, in addition to the input in Java, to pay attention to the use of methods
Algorithm Design:
Import java.util.*;
Class Main {public
static void Main (string[] args) {
Scanner sc = new Scanner (system.in);
String str = Sc.next ();
if (Str.charat (0) = = '-') {
str = ' 0 ' + str;
}
int k=0;
arraylist<character> symbol = new arraylist<character> ();
arraylist<integer> number = new arraylist<integer> ();
for (int i=0;i<str.length (); i++) {
if (Str.charat (i) = = ' + ' | | Str.charat (i) = = '-') {
number.add (Integer.parseint (str.substring (k, i)));
Symbol.add (Str.charat (i));
K=i+1
}
}
Number.add (Integer.parseint (str.substring (K,str.length ()-1));
int Sum=number.get (0);
for (int i=0;i<symbol.size (); i++) {
if (symbol.get (i) = = ' + ') {
sum = number.get (i+1);
}
else if (symbol.get (i) = = ') {
sum-= Number.get (i+1);
}
}
SYSTEM.OUT.PRINTLN (sum);
}