Розбір
Read the first term into the variable . Next, split the remaining line into pairs: an operator symbol and a term.
Sequentially read the pairs (character, number) until the end of the file, performing the corresponding operations.
Algorithm realization
Read the first term into the variable .
scanf("%d",&res);
Read the operation sign (addition or subtraction), followed by an integer . Add to or subtract it from the total result .
while (scanf("%c%d", &ch, &x) == 2) if (ch == '+') res += x; else res -= x;
Print the result of the computations.
printf("%d\n",res);
Java realization
import java.util.*; public class Main { public static void main(String[] args) { Scanner con = new Scanner(System.in); String s = con.nextLine(); StringTokenizer st1 = new StringTokenizer(s, "+-"); StringTokenizer st2 = new StringTokenizer(s, "0123456789"); int res = Integer.parseInt(st1.nextToken()); while (st1.hasMoreTokens()) { int x = Integer.parseInt(st1.nextToken()); if (st2.nextToken().equals("+")) res += x; else res -= x; } System.out.println(res); con.close(); } }
Python realization
Compute the value of the input expression and print the result.
print(eval(input()))