结对编程
#include<stdio.h> #include<stdlib.h> #include<time.h> #include<math.h> #include <string.h> #include <assert.h> #include<conio.h> //#define N 10 //题目数量 #define BUF_MAX 100 void jiandan(); void fuzha(); void jiechen(); double handle_space(); main() { system("color a1"); int c=0; int t=0; do{ printf(" 欢迎来到四则运算测试系统 \n"); printf("\n**********四则运算*********\n\n\n"); printf(" 1.简单 2.复杂 \n\n"); printf(" 3.阶乘 0.退出 \n\n"); printf("\n***************************\n\n\n"); printf("请输入选项:\n"); scanf("%d",&c); switch(c) { case 1: { printf("\n\n\n"); jiandan(); } break; case 2: { printf("\n\n\n"); fuzha(); } break; case 3: { printf("\n\n\n"); jiechen(); } break; case 0: system("cls"); printf("是否确定退出?(0.是 1.否)\n"); printf("请输入选项:\n"); scanf("%d",&c); switch(t) { case 1: { printf("\n\n\n"); main(); } case 0: break; } break; } }while(c!=0); } void jiandan() { system("color f2"); clock_t start = clock(); char opChar; double a,b,system1; double user,user1; int op; int i,rightType,wrongType; double rightNumber=0; int ret; int N; double duration; srand(time(NULL)); printf("请输入答题个数:"); scanf("%d",&N); while(N > 5) { printf("请重新输入:"); scanf("%d",&N); } for(i=0;i<N;i++) { a=rand()%10+1; b=rand()%10+1; op=rand()%4+1; rightType=rand()%4+1; wrongType=rand()%4+1; switch(op) { case 1: opChar='+'; system1=a+b; break; case 2: opChar='-'; system1=a-b; break; case 3: opChar='*'; system1=a*b; break; case 4: opChar='/'; if(fabs(b)<=1e-7) { printf(" division by zero\n"); } else { system1=(double)a/b; } break; default: printf("Unknown operator!"); } printf("%.0lf %c %.0lf= ? \n",a,opChar,b); ret=scanf("%lf",&user1); if(ret!=1) { printf("输入错误,应输入数字\n"); while(getchar()!='\n'); printf("请重新输入\n"); scanf("%lf",&user1); } if(user1==system1 ) { switch(rightType) { case 1: printf(" very good!\n"); break; } rightNumber++; } else { printf(" wrong type!\n"); printf("不正确, 正确答案为%.0lf\n",system1); } } printf(" 总共答对题数 %.0lf\n",rightNumber); printf(" Rate of correnctness is %.0lf %% \n",rightNumber/N*100); clock_t finish = clock(); duration = (double)(finish - start) / CLOCKS_PER_SEC; printf(" 总共用时:"); printf( " %f seconds\n", duration ); } void fuzha() { clock_t start = clock(); system("color b8"); int i,j,ret,k=0; double s1=0,s2=0; double us=0; int x,y,z,rightnum; double duration2; char b[1]; char f[]={'+','-','*','/'}; srand(time(NULL)); printf("请输入答题个数:"); scanf("%d",&k); while(k > 5) { printf("请重新输入:"); scanf("%d",&k); } for(i=0;i<k;i++) { x=rand()%10+1; y=rand()%10+1; z=rand()%10+1; for(j=0;j<2;j++) { b[j]=f[rand()%4]; } printf("%d%c%d%c%d=\n",x,b[0],y,b[1],z); ret=scanf("%lf",&us); if(ret!=1) { printf("输入错误,应输入数字\n"); while(getchar()!='\n'); printf("请重新输入\n"); scanf("%lf",&us); } if(b[0]=='+') { if(b[1]=='+'){ s1=(double)x+y; s2=(double)s1+z; } else if(b[1]=='-') { s1=(double)x+y; s2=(double)s1-z; } else if(b[1]=='*') { s1=y*z; s2=s1+x; } else if(b[1]=='/') { s1=(double)y/z; s2=x+s1; } } else if(b[0]=='-') { if(b[1]=='+') { s1=x-y; s2=s1+z;} else if(b[1]=='-') { s1=x-y; s2=s1-z;} else if(b[1]=='*') { s1=y*z; s2=x-s1;} else if(b[1]=='/') { s1=(double)y/z; s2=x-s1;} } else if(b[0]=='*') { if(b[1]=='+') { s1=x*y; s2=s1+z; } else if(b[1]=='-') { s1=x*y; s2=s1-z;} else if(b[1]=='*') { s1=y*z; s2=x*s1;} else if(b[1]=='/') { s1=x*y; s2=(double)s1/z; } } else if(b[0]=='/') { if(b[1]=='+') { s1=(double)x/y; s2=s1+z;} else if(b[1]=='-') { s1=(double)x/y; s2=s1-z;} else if(b[1]=='*') { s1=(double)x/y; s2=z*s1;} else if(b[1]=='/') { s1=(double)x/y; s2=(double)s1/z; } } if(us==s2) { printf(" very good!\n"); break; rightnum++; } else { printf(" wrong type!\n"); printf("不正确, 正确答案为%lf\n",s2); } } printf(" 总共答对题数 %.0lf\n",rightnum); printf(" Rate of correnctness is %.0lf %% \n",rightnum/k*100); clock_t finish = clock(); printf(" 总共用时:"); duration2 = (double)(finish - start) / CLOCKS_PER_SEC; printf( " %f seconds\n", duration2 ); } void jiechen() { system("color b8"); clock_t start = clock(); int p=0,ret1,N1; double rightnum1=0; int u=1; int p1; double duration1; srand(time(NULL)); printf("请输入答题个数:"); scanf("%d",&N1); while(N1 > 5) { printf("请重新输入:"); scanf("%d",&N1); } for(int i=0;i<N1;i++) { p1=1; p=rand()%10+1; printf("%d ! =\n",p); ret1=scanf("%d",&u); for(int i=1;i<=p;i++) { p1=p1*i; } if(ret1!=1) { printf("输入错误,应输入数字\n"); while(getchar()!='\n'); printf("请重新输入\n"); scanf("%d",&u); } if(u==p1) { printf("right!\n"); rightnum1++; } else { printf("wrong!\n"); printf("正确答案为:%d\n",p1); } } printf(" 总共答对题数 %.01f\n",rightnum1); printf(" Rate of correnctness is %.01f %% \n",rightnum1/N1*100); clock_t finish = clock(); duration1 = (double)(finish - start) / CLOCKS_PER_SEC; printf(" 总共用时:"); printf( " %f seconds\n", duration1 ); } /*void output() { printf(" 使用情况统计 \n"); printf("\n 总共出题数 \n",N+K+N1); printf("\n总共答对题数: \n" rightnumber+rightnum+rightnum1); printf("\n总共答题用时: \n"duration+duration1+duration2); }*/ /*void fuzha() { char buf[BUF_MAX]; double res; printf("Please input your formula:\n"); fgets(buf,BUF_MAX,stdin); printf("The result is %f\n",res); } double handle(char *str) //处理没括号的 { double res,*data=NULL,*data_old=NULL; char *flag=NULL,*flag_old=NULL,*temp=str,*temp_old=str; int n=0,m=0,i=0; assert(NULL!=str); while(*temp) { if(*temp==' ') strcpy(temp,temp+1); else temp++; } temp=str; while(*temp) { if(temp==temp_old && *temp=='-') { temp++; continue; } else if (*temp=='+' || *temp=='-' ||*temp=='*' ||*temp=='/') n++; temp++; } flag_old=(char *)malloc(sizeof(char)*n); if(NULL==flag_old) { perror("malloc fail!\n"); } data_old=(double *)malloc(sizeof(double)*(n+1)); if(NULL==data_old) { perror("malloc fail!\n"); } temp=str; flag=flag_old; data=data_old; while(*temp!='\0') { if(temp==temp_old && *temp=='-') { temp++; continue; } else if(*temp=='+' || *temp=='-' ||*temp=='*' ||*temp=='/') { *flag++=*temp; *temp='\0'; *data++=atof(temp_old); temp_old=temp+1; } temp++; } *data=atof(temp_old); flag=flag_old; data=data_old; do { m=n; for(i=0;i<m;i++) { if('*'==*(flag+i) || '/'==*(flag+i)) { if('*'==*(flag+i)) { *(data+i)*=*(data+i+1); } else { *(data+i)/=*(data+i+1); } if(i<m-1) { memcpy(flag+i,flag+i+1,sizeof(char)*(m-(i+1))); memcpy(data+i+1,data+i+2,sizeof(double)*(m-i-1)); } n-=1; break; } } if(n==m) { for(i=0;i<m;i++) { if('+'==*(flag+i) || '-'==*(flag+i)) { if('+'==*(flag+i)) { *(data+i)+=*(data+i+1); } else { *(data+i)-=*(data+i+1); } if(i<m-1) { memcpy(flag+i,flag+i+1,sizeof(char)*(m-(i+1))); memcpy(data+i+1,data+i+2,sizeof(double)*(m-i-1)); } n-=1; break; } } } }while(n>0); res=*data_old; free(flag_old); free(data_old); return res; } double handle_space(char *str) //处理带括号的 { char p_space_e[100]; char *temp=str; char *space_b=NULL,*space_e=NULL; double res_temp,res; assert(str); while(*temp) { if('('==*temp) { space_b=temp; temp++; } else if(')'==*temp && space_e==NULL && space_b!=NULL) { space_e=temp; *temp='\0'; res_temp=handle(space_b+1); strcpy(p_space_e,space_e+1); sprintf(space_b,"%f",res_temp); strcat(str,p_space_e); temp=str; } else temp++; } return handle(str); }*/
运行截图:
工作图片:
开发环境: VC++6.0
队员:201306114407-吴哲永 20130611410-董大为
功能:
1.用户可以选择出题的个数(最多不能超过5个题目),答题结束可以显示用户答错的题目个数和答对的题目个数
2.程序可以出单个整数阶乘的题目:如:4!=24
3.程序可以设置答题时间,时间设置为整数,单位为秒,
4.可以改变界面的颜色
5.程序可以出正整数四则运算,除法保留两位小数
分工方面:这次我们结对并没有很明确分工,因为我们还是第一次合作,在很多方面都有不同风格,特别是在算法和传递变量方面,很多时候都是2个人一同讨论完成,大概呢可以说队友完成界面和和程序模块中的随机给出值,我是完善以及加入容错功能。