四则运算(第二版)
结对编程伙伴:栾骄阳
功能1. 四则运算
支持出题4个数的四则运算题目
#include<stdio.h> #include<stack> #include<stdlib.h> #include<string> #include<math.h> using namespace std; struct Number { double a; bool b;//0数字 1字符 }; stack<Number>s3; void houzhuibds(char str[100])//后缀表达式 { stack<char>s1; stack<Number>s2; int i=0,j=0; for(i=0;str[i]!='\0';i++) { if(str[i]>='0'&&str[i]<='9') { Number num; num.a=0; num.b=0; while(str[i]<='9'&&str[i]>='0') num.a=(str[i++]-'0')+num.a*10; s2.push(num); i--; } else { if(str[i]==')') { while(s1.top()!='(') { Number num; num.b=1; num.a=s1.top(); s2.push(num); s1.pop(); } s1.pop(); } else if(s1.empty()||s1.top()=='('||str[i]=='(') { s1.push(str[i]); } else { if((str[i]=='*'||str[i]=='/')&&(s1.top()=='+'||s1.top()=='-')) s1.push(str[i]); else { Number num; num.b=1; num.a=s1.top(); s2.push(num); s1.pop(); i--; } } } } while(!s1.empty()) { Number num; num.b=1; num.a=s1.top(); s2.push(num); s1.pop(); } while(!s2.empty()) { s3.push(s2.top()); s2.pop(); } /* while(!s3.empty()) { Number num=s3.top(); s3.pop(); if(num.b==0) printf("%d",num.a); else printf("%c",num.a); } printf("\n"); */ } double qiuzhi() { stack<double>s4; while(!s3.empty()) { Number c1=s3.top(); s3.pop(); if(c1.b==0) s4.push(c1.a); else { double c2=s4.top(); s4.pop(); double c3=s4.top(); s4.pop(); double c4; switch((int)c1.a) { case '+':c4=c3+c2;break; case '-':c4=c3-c2;break; case '*':c4=c3*c2;break; case '/':c4=c3/c2;break; } s4.push(c4); } } return s4.top(); } void afterfh(char str[100],int t,int n);//+-*/之后 void aftersz(char str[100],int t,int n); int main() { char str[100]; int s=0; for(int i=0;i<20;i++) { int t=0; int n=0; afterfh(str,t,n); //gets(str);//4个数3个符号 houzhuibds(str); double ans=qiuzhi(); for(int i=0;str[i]!='\0';i++) printf("%c",str[i]); printf("=\n?"); double s; scanf("%lf",&s); if(abs(s-ans)<0.01) { s++; printf("答对了,你真是天才\n"); } else printf("再想想吧,答案似乎是%.2lf喔!\n",ans); } printf("你一共答对%d道题,共20道题。\n",s); return 0; } void aftersz(char str[100],int t,int n) { int num=rand()%4; switch(num) { case 0:str[t]='+';break; case 1:str[t]='-';break; case 2:str[t]='*';break; case 3:str[t]='/';break; } afterfh(str,++t,n); } void afterfh(char str[100],int t,int n)//+-*/之后 { int num=rand()%100; if(num>=10) { int a=num%10; str[t++]=num/10+'0'; str[t]=a+'0'; } else str[t]='0'+num; n++; if(n==4) { str[++t]='\0'; return ; } t++; aftersz(str,t,n); }
功能2:支持括号
代码:
#include<stdio.h> #include<stack> #include<stdlib.h> #include<string> #include<string.h> #include<math.h> using namespace std; int y=0; struct Number { double a; bool b;//0数字 1字符 }; stack<Number>s3; void houzhuibds(char str[100])//后缀表达式 { stack<char>s1; stack<Number>s2; int i=0,j=0; for(i=0;str[i]!='\0';i++) { if(str[i]>='0'&&str[i]<='9') { Number num; num.a=0; num.b=0; while(str[i]<='9'&&str[i]>='0') num.a=(str[i++]-'0')+num.a*10; s2.push(num); i--; } else { if(str[i]==')') { while(s1.top()!='(') { Number num; num.b=1; num.a=s1.top(); s2.push(num); s1.pop(); } s1.pop(); } else if(s1.empty()||s1.top()=='('||str[i]=='(') { s1.push(str[i]); } else { if((str[i]=='*'||str[i]=='/')&&(s1.top()=='+'||s1.top()=='-')) s1.push(str[i]); else { Number num; num.b=1; num.a=s1.top(); s2.push(num); s1.pop(); i--; } } } } while(!s1.empty()) { Number num; num.b=1; num.a=s1.top(); s2.push(num); s1.pop(); } while(!s2.empty()) { s3.push(s2.top()); s2.pop(); } } double qiuzhi() { stack<double>s4; while(!s3.empty()) { Number c1=s3.top(); s3.pop(); if(c1.b==0) s4.push(c1.a); else { double c2=s4.top(); s4.pop(); double c3=s4.top(); s4.pop(); double c4; switch((int)c1.a) { case '+':c4=c3+c2;break; case '-':c4=c3-c2;break; case '*':c4=c3*c2;break; case '/':c4=c3/c2;break; } s4.push(c4); } } return s4.top(); } void afterfh(char str[100],int t,int n);//+-*/之后 void aftersz(char str[100],int t,int n); int main() { char str[100]; int s=0; for(int i=0;i<20;i++) { int t=0; int n=0; afterfh(str,t,n); int len=strlen(str); while(y) { if(str[len-2]=='(') { str[len-2]=str[len-1]; len--; } else if(str[len-3]=='(') { str[len-3]=str[len-2]; str[len-2]=str[len-1]; len--; } else str[len++]=')'; y--; } str[len]='\0'; houzhuibds(str); double ans=qiuzhi(); for(int i=0;str[i]!='\0';i++) printf("%c",str[i]); printf("=\n?"); double s; scanf("%lf",&s); if(abs(s-ans)<0.01) { s++; printf("答对了,你真是天才\n"); } else printf("再想想吧,答案似乎是%.2lf喔!\n",ans); } printf("你一共答对%d道题,共20道题。\n",s); return 0; } void aftersz(char str[100],int t,int n)//数字后面是+-/*(0-4) 右括号(5-9) { int i=rand()%10; if(i<=4) { int j=rand()%4; switch(j) { case 0:str[t]='+';break; case 1:str[t]='-';break; case 2:str[t]='*';break; case 3:str[t]='/';break; } afterfh(str,++t,n); } else//右括号 { if(y>0&&(str[t-2]!='('||str[t-3]!='('&&(str[t-2]<='0'&&str[t-2]>='9'))) { str[t]=')'; y--; aftersz(str,++t,n); } else aftersz(str,t,n); } } void afterfh(char str[100],int t,int n)//+-*/之后 { int p=rand()%10; if(p>=3)//数字 { int num=rand()%100; if(num>=10) { int a=num%10; str[t++]=num/10+'0'; str[t]=a+'0'; } else str[t]='0'+num; n++; if(n==4) { str[++t]='\0'; return ; } aftersz(str,++t,n); } else//左括号 { str[t]='('; y++; afterfh(str,++t,n); } } 功能3:限定题目数量,"精美"打印输出
输入:
打印结果:
代码:
#include<stdio.h> #include<stack> #include<stdlib.h> #include<string> #include<string.h> #include<math.h> using namespace std; int y=0; struct Number { double a; bool b;//0数字 1字符 }; stack<Number>s3; void houzhuibds(char str[100])//后缀表达式 { stack<char>s1; stack<Number>s2; int i=0,j=0; for(i=0;str[i]!='\0';i++) { if(str[i]>='0'&&str[i]<='9') { Number num; num.a=0; num.b=0; while(str[i]<='9'&&str[i]>='0') num.a=(str[i++]-'0')+num.a*10; s2.push(num); i--; } else { if(str[i]==')') { while(s1.top()!='(') { Number num; num.b=1; num.a=s1.top(); s2.push(num); s1.pop(); } s1.pop(); } else if(s1.empty()||s1.top()=='('||str[i]=='(') { s1.push(str[i]); } else { if((str[i]=='*'||str[i]=='/')&&(s1.top()=='+'||s1.top()=='-')) s1.push(str[i]); else { Number num; num.b=1; num.a=s1.top(); s2.push(num); s1.pop(); i--; } } } } while(!s1.empty()) { Number num; num.b=1; num.a=s1.top(); s2.push(num); s1.pop(); } while(!s2.empty()) { s3.push(s2.top()); s2.pop(); } } double qiuzhi() { stack<double>s4; while(!s3.empty()) { Number c1=s3.top(); s3.pop(); if(c1.b==0) s4.push(c1.a); else { double c2=s4.top(); s4.pop(); double c3=s4.top(); s4.pop(); double c4; switch((int)c1.a) { case '+':c4=c3+c2;break; case '-':c4=c3-c2;break; case '*':c4=c3*c2;break; case '/':c4=c3/c2;break; } s4.push(c4); } } return s4.top(); } void afterfh(char str[100],int t,int n);//+-*/之后 void aftersz(char str[100],int t,int n); int main() { FILE *fp; fp=fopen("D:\\2.txt","w"); char str[100]; char s[100]; int z=0; while(1) { int k=0,flag=0; scanf("%s",&s); while(s[k]!='\0') { if(s[k]>='0'&&s[k]<='9') z=z*10+s[k]-'0'; else { flag=1; break; } k++; } if(flag==1) printf("题目数量必须是 正整数。\n"); else break; } getchar(); for(int i=0;i<z;i++) { int t=0; int n=0; afterfh(str,t,n); int len=strlen(str); while(y) { if(str[len-2]=='(') { str[len-2]=str[len-1]; len--; } else if(str[len-3]=='(') { str[len-3]=str[len-2]; str[len-2]=str[len-1]; len--; } else str[len++]=')'; y--; } str[len]='\0'; houzhuibds(str); double ans=qiuzhi(); fprintf(fp,"%25s",str); fprintf(fp,"= %.2lf\n",ans); } return 0; } void aftersz(char str[100],int t,int n)//数字后面是+-/*(0-4) 右括号(5-9) { int i=rand()%10; if(i<=4) { int j=rand()%4; switch(j) { case 0:str[t]='+';break; case 1:str[t]='-';break; case 2:str[t]='*';break; case 3:str[t]='/';break; } afterfh(str,++t,n); } else//右括号 { if(y>0&&(str[t-2]!='('||str[t-3]!='('&&(str[t-2]<='0'&&str[t-2]>='9'))) { str[t]=')'; y--; aftersz(str,++t,n); } else aftersz(str,t,n); } } void afterfh(char str[100],int t,int n)//+-*/之后 { int p=rand()%10; if(p>=3)//数字 { int num=rand()%100; if(num>=10) { int a=num%10; str[t++]=num/10+'0'; str[t]=a+'0'; } else str[t]='0'+num; n++; if(n==4) { str[++t]='\0'; return ; } aftersz(str,++t,n); } else//左括号 { str[t]='('; y++; afterfh(str,++t,n); } }
功能4:未完成
conding.net git@git.coding.net:w547240561/myboke.git
https://git.coding.net/w547240561/myboke.git
结对编程伙伴 栾骄阳及我