1 #include"stdio.h" 2 #include"string.h" 3 #define address "jisuanqi.txt" 4 5 char stack[200]; 6 int out[200]; 7 int k=0,t=0; 8 9 void up(int a,int b,char flag) 10 { 11 int q; 12 if(flag=='*'){ 13 q=a*b; 14 out[k]=q; 15 k++; 16 } 17 else{ 18 q=a/b; 19 out[k]=q; 20 k++; 21 } 22 } 23 24 void result() 25 { 26 int len1=strlen(stack)-1,len2=strlen(out)-1; 27 while(len2>=0) 28 { 29 if(stack[len1]=='+')out[len2]=out[len2+1]+out[len2]; 30 else out[len2]=out[len2]-out[len2+1]; 31 len2-=1; 32 stack[len1]=NULL; 33 len1--; 34 } 35 printf("%d",out[0]); 36 } 37 38 void translate(char str[]) 39 { 40 stack[0]='1'; 41 char ch,flag; 42 int i=0,l=0,j=0; 43 int a,b; 44 45 ch=str[i]; 46 47 while(ch!='\0') 48 { 49 switch(ch){ 50 case '-': 51 case '+': 52 for(l;l<strlen(stack);l++) 53 if(stack[l]=='*'||stack[l]=='/')break; 54 flag=stack[l]; 55 stack[l]=ch; 56 k=k-2; 57 a=out[k];b=out[k+1]; 58 out[k]=NULL; 59 60 up(a,b,flag); 61 break; 62 case '*': 63 case '/': 64 stack[t]=ch;t++; 65 break; 66 default: 67 j=0; 68 while(ch>='0'&&ch<='9'){ 69 j=j*10+(ch-'0'); 70 i++; 71 ch=str[i]; 72 } 73 i--; 74 out[k]=j;k++; 75 break; 76 } 77 i++; 78 ch=str[i]; 79 } 80 } 81 82 int main() 83 { 84 FILE *fp; 85 char str[200]; 86 fp=fopen(address,"w"); 87 printf("please input a string:\n"); 88 gets(str); 89 fputs(str,fp); 90 translate(str); 91 result(); 92 fputc('=',fp); 93 fputc(out[0],fp); 94 fclose(fp); 95 }
3 * 2 / (2 * 2)