Simple CalculatorTime
limit:2000/1000 MS (java/others) Memory limit:65536/32768 K (java/others)
Total submission (s): 15075 Accepted Submission (s): 5132
Problem description reads a non-negative integer that contains only +,-, *,/, evaluates the expression, and computes the value of the formula.
The input test inputs contain several test cases, one row for each 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 outputs 1 rows for each test case, that is, the value of the expression, exactly 2 digits after the decimal point.
Sample Input
1 + 24 + 2 * 5-7/110
Sample Output
3.0013.36
SOURCE Zhejiang University Computer Postgraduate exam on the machine-2006
recommendjgshining | We have carefully selected several similar problems for you:1230 1235 1228 1248 1256
Statistic | Submit | Discuss | Note
I can't answer the question.
It's actually a simulation calculator. Because there is no parenthesis here is simple. Only the priority of the current operator and the next operator needs to be judged
On the line, using two stacks to store numbers and symbols, I used the array.
I'm just a bad data and I can't find it. Waste more than n time.
There are a few points to note
1: If the first number entered is 0 for example 0 + 2 + 3 should output 5.00 instead of end
2: If the input is 0 (space) (newline) should output 0.00 (I was wrong here.) died of anger)
3. The number may be two-bit and more than two-bit
The last change in the input mode is right. Attach the WA and AC codes
Ac
#include <stdio.h> #include <string.h>int main () {char fuhao[100],str[205]; Double num[100],sum; while (gets (str) &&strcmp (str, "0")!=0) {int len=strlen (str); int t=0,q=0; memset (num,0,sizeof (num)); memset (fuhao,0,sizeof (Fuhao)); for (int i=0;i<len;i++) {if (str[i]>= ' 0 ' &&str[i]<= ' 9 ') {double temp=0; while (str[i]>= ' 0 ' &&str[i]<= ' 9 ') temp=temp*10+str[i]-' 0 ', i++; Num[q++]=temp; } if (str[i]== ' + ' | | str[i]== '-' | | str[i]== ' * ' | | str[i]== '/') fuhao[t++]=str[i]; } memset (Str,0,sizeof (str)); SUM=NUM[0]; for (int i=1;i<q;i++) num[i-1]=num[i]; for (int i=0;i<t;i++) {if (fuhao[i]== ' * ') sum=sum*num[i]; else if (fuhao[i]== '/') sum=sum/num[i]; else if (fuhao[i]== ' + ') {if (fuhao[i+1]== ' * ' | | fuhao[i+1]== '/' &&i+1<t) {double temp=num[i]; while ((fuhao[i+1]== ' * ' | | fuhao[i+1]== '/') &&i+1<t) {if (fuhao[i+1]== ' * ') TEMP=TEMP*NUM[I+1]; else temp=temp/num[i+1]; i++; } sum=sum+temp; } else sum=sum+num[i]; } else if (fuhao[i]== '-') {if (fuhao[i+1]== ' * ' | | fuhao[i+1]== '/' &&i+1<t) {double temp=num[i]; while ((fuhao[i+1]== ' * ' | | fuhao[i+1]== '/') &&i+1<t) {if (fuhao[i+1]== ' * ') TEMP=TEMP*NUM[I+1]; else temp=temp/num[i+1]; i++; } sum=sum-temp; } else sum=sum-num[i]; }} printf ("%.2lf\n", sum); } return 0;}
WA's, that's where the input is different.
#include <stdio.h> #include <string.h>int main () {char fuhao[200]; Double num[200],sum; while (scanf ("%lf", &sum)!=eof) {char mark=getchar (); if (mark== ' \ n ' &&sum==0) {printf ("%.2lf\n", sum); Continue } memset (Fuhao,0,sizeof (Fuhao)); memset (num,0,sizeof (num)); int t=0; while (1) {scanf ("%c", &fuhao[t]); scanf ("%lf", &num[t]); Mark=getchar (); if (mark== ' \ n ') break; t++; } for (int i=0;i<=t;i++) {if (fuhao[i]== ' * ') sum=sum*num[i]; else if (fuhao[i]== '/') sum=sum/num[i]; else if (fuhao[i]== ' + ') {if (fuhao[i+1]== ' * ' | | fuhao[i+1]== '/') {double temp=num[i]; while ((fuhao[i+1]== ' * ' | | fuhao[i+1]== '/') &&i+1<=t) { if (fuhao[i+1]== ' * ') temp=temp*num[i+1]; else temp=temp/num[i+1]; i++; } sum=sum+temp; } else sum=sum+num[i]; } else if (fuhao[i]== '-') {if (fuhao[i+1]== ' * ' | | fuhao[i+1]== '/') {double temp=num[i]; while ((fuhao[i+1]== ' * ' | | fuhao[i+1]== '/') &&i+1<=t) {if (fuhao[i+1]== ' * ') TEMP=TEMP*NUM[I+1]; else temp=temp/num[i+1]; i++; } sum=sum-temp; } else sum=sum-num[i]; }} printf ("%.2lf\n", sum); } return 0;}
Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.
hdu1237 Simple Calculator (analog + stack)