zoukankan      html  css  js  c++  java
  • 实验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]!='';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]!='';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]=='') 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]=='') 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
    ",OPND[topND]);
    145     else printf("%.0f
    ",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
    ",OPND[topND]);
    175     else printf("%.0f
    ",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
    ",OPND[topND]);
    205     else printf("%.0f
    ",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
  • 相关阅读:
    48. Rotate Image
    47. Permutations II
    46. Permutations
    45. Jump Game II
    44. Wildcard Matching
    43. Multiply Strings
    42. Trapping Rain Water
    41. First Missing Positive
    40. Combination Sum II
    39. Combination Sum
  • 原文地址:https://www.cnblogs.com/lglh/p/10798051.html
Copyright © 2011-2022 走看看