实验4

表达式求值是进行数据处理的最基本操作。请编写程序完成一个简单算术表达式的求值。要求如下: 

(1) 运算符包括:+、-、*、-、^(乘方)、括号

(2)运算量为数值常量,根据自己的能力可以对运算量做不同的约束,例如1位整数、多位整数、实数等(会有不同的测试用例);

 

输入:一行,即表达式,以“=”结束。例如:

           5*(8-3)+6/5=

输出:一行,即表达式的值。结果值为整数时输出为整数,如果有小数时保留5位小数。

           26.20000

AC代码:

  1 #include <bits/stdc++.h>
  2 #include <stdio.h>
  3 #include <stdlib.h>
  4 #include <string.h>
  5 using namespace std;
  6 const int maxn=1e3+50;
  7 const double eps=1e-8;
  8 char str[maxn],OP[10]={'+','-','*','/','^','(',')','#'};
  9 bool flag;
 10 bool judge_d()
 11 {
 12     for(int i=1;str[i]!='\0';i++)
 13     {
 14         if(str[i]<='9'&&str[i]>='0'&&str[i+1]<='9'&&str[i+1]>='0') return true;
 15     }
 16     return false;
 17 }
 18 
 19 bool judge_lf()
 20 {
 21     for(int i=1;str[i]!='\0';i++)
 22     {
 23         if(str[i]<='9'&&str[i]>='0'&&str[i+1]=='.'&&str[i+2]<='9'&&str[i+2]>='0')
 24         {
 25             return true;
 26         }
 27     }
 28     return false;
 29 }
 30 
 31 double scan_lf(string cnt)
 32 {
 33         int in;double Dec=0.1,num;
 34         bool IsN=false,IsD=false;
 35         in=0;
 36         if(cnt[in]=='\0') return 0;
 37         while(cnt[in]!='-'&&cnt[in]!='.'&&(cnt[in]<'0'||cnt[in]>'9'))
 38                 in++;
 39         if(cnt[in]=='-'){IsN=true;num=0;}
 40         else if(cnt[in]=='.'){IsD=true;num=0;}
 41         else num=cnt[in]-'0';
 42         if(!IsD){
 43                 while(in++,cnt[in]>='0'&&cnt[in]<='9'){
 44                         num*=10;num+=cnt[in]-'0';}
 45         }
 46         if(cnt[in]!='.'){
 47                 if(IsN) num=-num;
 48                 return num;
 49         }else{
 50                 while(in++,cnt[in]>='0'&&cnt[in]<='9'){
 51                         num+=Dec*(cnt[in]-'0');Dec*=0.1;
 52                 }
 53         }
 54         if(IsN) num=-num;
 55         return num;
 56 }
 57 
 58 double scan_d(string cnt)
 59 {
 60         int in=0,num;bool IsN=false;
 61         if(cnt[in]=='\0') return 0;
 62         while(cnt[in]!='-'&&(cnt[in]<'0'||cnt[in]>'9')) in++;
 63         if(cnt[in]=='-'){ IsN=true;num=0;}
 64         else num=cnt[in]-'0';
 65         while(in++,cnt[in]>='0'&&cnt[in]<='9'){
 66                 num*=10,num+=cnt[in]-'0';
 67         }
 68         if(IsN) num=-num;
 69         return num;
 70 }
 71 
 72 bool In(char ch)
 73 {
 74     for(int i=0;i<=7;i++) if(OP[i]==ch) return true;
 75     return false;
 76 }
 77 double Operate_single(double x,char op,double y)
 78 {
 79     if(op=='+') return x+y;
 80     else if(op=='-') return x-y;
 81     else if(op=='*') return x*y;
 82     else if(op=='/')
 83     {
 84         if(abs(x-y*int(x/y))>eps) flag=true;
 85         return x/y;
 86     }
 87     else if(op=='^')
 88     {
 89         int tmp=x;
 90         while(--y) tmp*=x;
 91         return tmp;
 92     }
 93 }
 94 
 95 char Precede(char ch1,char ch2)
 96 {
 97     if(ch1=='+' || ch1=='-')
 98     {
 99         if(ch2=='*' || ch2=='/' || ch2=='(' || ch2=='^') return '<';
100         else return '>';
101     }
102     else if(ch1=='*' || ch1=='/')
103     {
104         if(ch2=='(' || ch2=='^') return '<';
105         else return '>';
106     }
107     else if(ch1=='(')
108     {
109         if(ch2==')') return '=';
110         else return '<';
111     }
112     else if(ch1==')') return '>';
113     else if(ch1=='^')
114     {
115         if(ch2=='(') return '<';
116         else return '>';
117     }
118     else
119     {
120         if(ch2=='#') return '=';
121         else return '<';
122     }
123 }
124 
125 void work_single()
126 {
127     char OPTR[100],theta;
128     double OPND[100],a,b;
129     int topTR=0,topND=0,c=1;
130     OPTR[++topTR]='#';
131     while(str[c]!='#' || OPTR[topTR]!='#')
132     {
133         if(!In(str[c])) {OPND[++topND]=str[c]-'0'+0.0;c++;}
134         else
135         {
136             switch (Precede(OPTR[topTR],str[c])){
137                 case '<': OPTR[++topTR]=str[c];c++; break;
138                 case '=': topTR--; c++; break;
139                 case '>': theta=OPTR[topTR--]; b=OPND[topND--]; a=OPND[topND--];
140                 OPND[++topND]=Operate_single(a,theta,b);break;
141             }
142         }
143     }
144     if(flag) printf("%.5f\n",OPND[topND]);
145     else printf("%.0f\n",OPND[topND]);
146 }
147 void work_d()
148 {
149     char OPTR[100],theta;
150     double OPND[100],a,b;
151     int topTR=0,topND=0,c=1;
152     OPTR[++topTR]='#';
153     while(str[c]!='#' || OPTR[topTR]!='#')
154     {
155         if(!In(str[c])) {
156             string cnt="";
157             while(!In(str[c]))
158             {
159                 cnt+=str[c];
160                 c++;
161             }
162             OPND[++topND]=(double)scan_d(cnt);
163         }
164         else
165         {
166             switch (Precede(OPTR[topTR],str[c])){
167                 case '<': OPTR[++topTR]=str[c];c++; break;
168                 case '=': topTR--; c++; break;
169                 case '>': theta=OPTR[topTR--]; b=OPND[topND--]; a=OPND[topND--];
170                 OPND[++topND]=Operate_single(a,theta,b);break;
171             }
172         }
173     }
174     if(flag) printf("%.5f\n",OPND[topND]);
175     else printf("%.0f\n",OPND[topND]);
176 }
177 void work_lf()
178 {
179     char OPTR[100],theta;
180     double OPND[100],a,b;
181     int topTR=0,topND=0,c=1;
182     OPTR[++topTR]='#';
183     while(str[c]!='#' || OPTR[topTR]!='#')
184     {
185         if(!In(str[c])) {
186             string cnt="";
187             while(!In(str[c]))
188             {
189                 cnt+=str[c];
190                 c++;
191             }
192             OPND[++topND]=(double)scan_lf(cnt);
193         }
194         else
195         {
196             switch (Precede(OPTR[topTR],str[c])){
197                 case '<': OPTR[++topTR]=str[c];c++; break;
198                 case '=': topTR--; c++; break;
199                 case '>': theta=OPTR[topTR--]; b=OPND[topND--]; a=OPND[topND--];
200                 OPND[++topND]=Operate_single(a,theta,b);break;
201             }
202         }
203     }
204     if(flag) printf("%.5f\n",OPND[topND]);
205     else printf("%.0f\n",OPND[topND]);
206 }
207 int main()
208 {
209     scanf("%s",str+1);
210     int len=strlen(str+1);
211     str[len]='#';
212     if(judge_lf()) work_lf();
213     else if(judge_d()) work_d();
214     else work_single();
215     return 0;
216 }
217 //5*(8-3)+60.2/5=
View Code

 

转载于:https://www.cnblogs.com/lglh/p/10798051.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值