1. 程式人生 > >計算器(表達式計算-後綴表達式實現)

計算器(表達式計算-後綴表達式實現)

translate %d sem stdio.h 算術運算 void empty spa amp

【問題描述】

從標準輸入中讀入一個整數算術運算表達式,如24 / ( 1 + 2 + 36 / 6 / 2 - 2) * ( 12 / 2 / 2 )= ,計算表達式結果,並輸出。

要求:

1、表達式運算符只有+、-、*、/,表達式末尾的=字符表示表達式輸入結束,表達式中可能會出現空格;
2、表達式中會出現圓括號,括號可能嵌套,不會出現錯誤的表達式;

3、出現除號/時,以整數相除進行運算,結果仍為整數,例如:5/3結果應為1。

4、要求采用逆波蘭表達式來實現表達式計算。

【輸入形式】

從鍵盤輸入一個以=結尾的整數算術運算表達式。操作符和操作數之間可以有空格分隔。

【輸出形式】

在屏幕上輸出計算結果(為整數,即在計算過程中除法為整除)。

【樣例輸入】

24 / ( 1 + 2 + 36 / 6 / 2 - 2) * ( 12 / 2 / 2 ) =

【樣例輸出】

18
【樣例說明】

按照運算符及括號優先級依次計算表達式的值。

【題解】

  1 #include<stdio.h>
  2 #include<malloc.h>
  3 #include<stdlib.h>
  4 
  5 #define MAXSIZE 100
  6 
  7 typedef struct
  8 {
  9     char stcak[MAXSIZE];
 10     int top;
 11 }Sequence;
12 typedef struct 13 { 14 int data[MAXSIZE]; 15 int top; 16 }Operate; 17 18 int isEmpty(Sequence *s); 19 int push(Sequence *s,char c); 20 int pop(Sequence *s,char *c); 21 int getTop(Sequence *s,char *c); 22 void translate(char s1[],char s2[]); 23 int calculate(char a[]); 24 25
int main(void) 26 { 27 char str1[MAXSIZE],str2[MAXSIZE]; 28 int result; 29 fgets(str1,100,stdin); 30 translate(str1,str2); 31 result=calculate(str2); 32 printf("%d\n",result); 33 34 return 0; 35 } 36 37 int isEmpty(Sequence *s) 38 { 39 if(-1==s->top) 40 return 1; 41 else 42 return 0; 43 } 44 int push(Sequence *s,char c) 45 { 46 if(MAXSIZE==s->top) 47 return 0; 48 49 s->top++; 50 s->stcak[s->top]=c; 51 return 1; 52 } 53 int pop(Sequence *s,char *c) 54 { 55 if(isEmpty((s))) 56 return 0; 57 58 *c=s->stcak[s->top]; 59 s->top--; 60 return 1; 61 } 62 int getTop(Sequence *s,char *c) 63 { 64 if(isEmpty((s))) 65 return 0; 66 67 *c=s->stcak[s->top]; 68 return 1; 69 } 70 void translate(char s1[],char s2[]) 71 { 72 Sequence s; 73 char ch,e=\0; 74 int i=0,j=0; 75 s.top=-1; 76 ch=s1[i++]; 77 while(ch!==) 78 { 79 switch(ch) 80 { 81 case (: 82 push(&s,ch); 83 break; 84 case ): 85 while(getTop(&s,&e) && e!=() 86 { 87 pop(&s,&e); 88 s2[j++]=e; 89 } 90 pop(&s,&e); 91 break; 92 case +: 93 case -: 94 while(!isEmpty(&s) && getTop(&s,&e) && e!=() 95 { 96 pop(&s,&e); 97 s2[j++]=e; 98 } 99 push(&s,ch); 100 break; 101 case *: 102 case /: 103 while(!isEmpty(&s) && getTop(&s,&e) && (/==e || *==e) ) 104 { 105 pop(&s,&e); 106 s2[j++]=e; 107 } 108 push(&s,ch); 109 break; 110 case : 111 break; 112 default: 113 while(ch>=0 && ch<=9) 114 { 115 s2[j++]=ch; 116 ch=s1[i++]; 117 } 118 i--; 119 s2[j++]= ; 120 } 121 ch=s1[i++]; 122 } 123 while(!isEmpty(&s)) 124 { 125 pop(&s,&e); 126 if(e!= ) 127 s2[j++]=e; 128 } 129 s2[j]=\0; 130 } 131 int calculate(char a[]) 132 { 133 Operate s; 134 int i=0,data,result=0; 135 int x1,x2; 136 s.top=-1; 137 while(a[i]!=\0) 138 { 139 if(a[i]>=0 && a[i]<=9) 140 { 141 data=0; 142 while(a[i]!= ) 143 { 144 data=data*10+a[i]-0; 145 i++; 146 } 147 s.top++; 148 s.data[s.top]=data; 149 } 150 else 151 { 152 switch(a[i]) 153 { 154 case +: 155 x1=s.data[s.top--]; 156 x2=s.data[s.top--]; 157 result=x1+x2; 158 s.data[++s.top]=result; 159 break; 160 case -: 161 x1=s.data[s.top--]; 162 x2=s.data[s.top--]; 163 result=x2-x1; 164 s.data[++s.top]=result; 165 break; 166 case *: 167 x1=s.data[s.top--]; 168 x2=s.data[s.top--]; 169 result=x1*x2; 170 s.data[++s.top]=result; 171 break; 172 case /: 173 x1=s.data[s.top--]; 174 x2=s.data[s.top--]; 175 result=x2/x1; 176 s.data[++s.top]=result; 177 break; 178 } 179 i++; 180 } 181 } 182 if(s.top!=-1) 183 result=s.data[s.top--]; 184 185 return result; 186 }

計算器(表達式計算-後綴表達式實現)