ZZ的计算器

Problem Description

ZZ自从上大学以来,脑容量就是以SB计算的,这个吃货竟然连算术运算也不会了,可是当今的计算机可是非常强大的,作为ACMer, 几个简单的算术又算得了什么呢?可是该怎么做呢?ZZ只想算简单的加减乘除,由于大脑钝化,ZZ连小数都没有概念了!

Input

有多组测试数据,每组测试数据为一个字符串,字符串为合法的算术运算,运算符号只包含+、-、*、/,数据为整数.

Output

请输出正确的结果,除数为0的时候,输出impossible。(结果保证在长整形之内)

Sample Input

1+1*2
2/2+1

Sample Output

3
2
 1 #include<stdio.h>
 2 #include<string.h>
 3 #define LL long long
 4 
 5 int main()
 6 {
 7     LL d[10000] , temp , cnt;
 8     LL i , j;
 9     char ch[200];
10     while(gets(ch))
11     {
12         cnt=temp=0;
13         for(j=0 ; j<strlen(ch) ; j++)
14         {
15             if(ch[j]>='0'&&ch[j]<='9') temp = temp*10+ch[j]-'0';
16             else break;
17         }
18         d[cnt++]=temp;
19         for(i=j ; i<strlen(ch) ; i++)
20         {
21             temp=0;
22             for(j=i+1 ; ch[j]>='0'&&ch[j]<='9' ; j++) temp = temp*10+ch[j]-'0';
23             if(ch[i]=='*') d[cnt-1] *= temp;
24             else if(ch[i]=='/') d[cnt-1] /= temp;
25             else if(ch[i]=='+') d[cnt++] = temp;
26             else d[cnt++] = (-1)*temp;
27             i=j-1;
28         }
29         for(i=1 ; i<cnt ; i++) d[0]+=d[i];
30         printf("%I64d\n",d[0]);
31     }
32     return 0;
33 }
数组模拟
 1 #include <stdio.h>
 2 #include <string.h>
 3 #include <stack>
 4 using namespace std;
 5 
 6 stack<long long> stk;
 7 stack<char> op;
 8 
 9 int main()
10 {
11     char str[100];
12     bool bl ;
13     int len, i, j, k;
14     long long a, b;
15     while (scanf("%s",str)!=EOF)
16     {
17         bl = true;
18         while (!stk.empty()) stk.pop();
19         while (!op.empty()) op.pop();
20         i = 0;
21         if (str[0] == '-') i++;
22         len = strlen(str);
23         for(; i<len; i++)
24         {
25             a = str[i++]-'0';
26             while(i < len && str[i] >='0' && str[i] <='9')
27             {
28                 a *= 10;
29                 a += str[i++] -'0';
30             }
31             if (!op.empty())
32             {
33                 if (op.top() == '*')
34                 {
35                     b = stk.top();
36                     stk.pop();
37                     a *=b;
38                     op.pop();
39                 }
40                 else if (op.top() == '/')
41                 {
42                     b = stk.top();
43                     stk.pop();
44                     if (a == 0)
45                     {
46                         puts("impossible");
47                         bl = false;
48                         break;
49                     }
50                     a = b / a;
51                     op.pop();
52                 }
53             }
54             stk.push(a);
55             if (i < len) op.push(str[i]);
56         }
57         if (!bl) continue;
58         long long sum=0;
59         while (!op.empty())
60         {
61             a = stk.top();
62             stk.pop();
63             if (op.top() == '+')
64             {
65                 sum += a;
66                 op.pop();
67             }
68             else
69             {
70                 sum -= a;
71                 op.pop();
72             }
73         }
74 
75         if (str[0] == '-') sum -= stk.top();
76         else sum += stk.top();
77         printf("%I64d\n", sum);
78 
79     }
80     return 0;
81 }
两个栈模拟

 

posted @ 2013-07-23 23:02  1002liu  阅读(279)  评论(0编辑  收藏  举报