Description
Reads a non-negative integer that contains only a +,-, *,/, evaluates the expression, and computes the value of the expression.
Input
The test input contains several test cases, one row per test case, and no more than 200 characters per line, separated by a space between integers and operators. There is no illegal expression. When only 0 o'clock input is completed in a row, the corresponding result is not output.
Output
Output 1 rows For each test case, that is, the value of the expression, exactly 2 digits after the decimal point.
Sample Input
1 + 2 4 + 2 * 5-7/11 0
Sample Output
3.00 13.36 Analysis: The subject input a string to find the value of its expression can be a single read first elimination * and/from left to right to the number of the array if there is * or/the value of only the * or/is calculated and then the array is added to the last
#include <stdio.h> #include <string.h>int main () {int t,l;d ouble sum;double num[200]; while (scanf ("%d", &t)) {l=0;sum = T*1.0;char c;if (t==0&& (c = GetChar ()) = = ' \ n ') return 0;while ((c = GetChar ()) ! = ' \ n ') {if (c== ' * ') {scanf ("%d", &t); sum*=t;} Calculate the value of if (c== '/') {scanf ("%d", &t), sum/=t*1.0,}//To calculate the value if (c== ' + ') {num[l++] =sum; scanf ("%d", &t); sum = t*1.0; Put sum into array}if (c = = '-') {num[l++] = sum; scanf ("%d", &t); sum =-t*1.0;//put sum in Array}} num[l++] = sum;double Sun = 0;for (int i = 0; I < l;i++) sums the value of the expression by the sum of Sun + + num[i];p rintf ("%.2lf\n", Sun);}
2016HUAS Summer Camp Training question F-Simple calculator