zoukankan      html  css  js  c++  java
• # 结对编程1.0

```#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 ret1;
int t=0;
do{
printf("     欢迎来到四则运算测试系统
");
printf("
**********四则运算*********

");
printf("      1.简单         2.复杂

");
printf("      3.阶乘         0.退出

");
printf("
***************************

");
printf("请输入选项:
");
scanf("%d",&c);
if(ret1!=1)
{
printf("输入错误，应输入数字
");
while(getchar()!='
');
printf("请重新输入
");
scanf("%d",&c);
}
switch(c)
{
case 1:
{
printf("

");
jiandan();
}
break;
case 2:
{
printf("

");
fuzha();
}
break;
case 3:
{
printf("

");
jiechen();
}
break;
case 0:
system("cls");
printf("是否确定退出？（0.是  1.否）
");
printf("请输入选项:
");
scanf("%d",&c);
switch(t)
{
case 1:
{
printf("

");
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
");
}
else
{
system1=(double)a/b;
}
break;
default:
printf("Unknown operator!");
}

printf("%.0lf %c %.0lf= ?
",a,opChar,b);
ret=scanf("%lf",&user1);
if(ret!=1)
{
printf("输入错误，应输入数字
");
while(getchar()!='
');
printf("请重新输入
");
scanf("%lf",&user1);
}

if(user1==system1 )
{
switch(rightType)
{
case 1:
printf(" very good!
");
break;
}
rightNumber++;

}
else
{
printf(" wrong type!
");
printf("不正确, 正确答案为%.0lf
",system1);
}

}
printf(" 总共答对题数 %.0lf
",rightNumber);
printf(" Rate of correnctness is %.0lf %%
",rightNumber/N*100);
clock_t finish = clock();
duration = (double)(finish - start) / CLOCKS_PER_SEC;
printf(" 总共用时：");
printf( " %f seconds
", 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=
",x,b[0],y,b[1],z);
ret=scanf("%lf",&us);
if(ret!=1)
{
printf("输入错误，应输入数字
");
while(getchar()!='
');
printf("请重新输入
");
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!
");
break;

rightnum++;

}
else
{
printf(" wrong type!
");
printf("不正确, 正确答案为%lf
",s2);
}
}
printf(" 总共答对题数 %.0lf
",rightnum);
printf(" Rate of correnctness is %.0lf %%
",rightnum/k*100);
clock_t finish = clock();
printf(" 总共用时：");
duration2 = (double)(finish - start) / CLOCKS_PER_SEC;
printf( " %f seconds
", 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 ! =
",p);
ret1=scanf("%d",&u);
for(int i=1;i<=p;i++)
{
p1=p1*i;
}
if(ret1!=1)
{
printf("输入错误，应输入数字
");
while(getchar()!='
');
printf("请重新输入
");
scanf("%d",&u);
}
if(u==p1)
{
printf("right!
");
rightnum1++;
}
else
{
printf("wrong!
");
printf("正确答案为：%d
",p1);
}
}
printf(" 总共答对题数 %.01f
",rightnum1);
printf(" Rate of correnctness is %.01f %%
",rightnum1/N1*100);
clock_t finish = clock();
duration1 = (double)(finish - start) / CLOCKS_PER_SEC;
printf(" 总共用时：");
printf( " %f seconds
", duration1 );
}

/*void output()
{
printf("   使用情况统计
");
printf("
总共出题数
",N+K+N1);
printf("
总共答对题数：
" rightnumber+rightnum+rightnum1);
printf("
总共答题用时：
"duration+duration1+duration2);
}*/

/*void fuzha()
{
char buf[BUF_MAX];
double res;
");
fgets(buf,BUF_MAX,stdin);

printf("The result is %f
",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!
");
}
data_old=(double *)malloc(sizeof(double)*(n+1));
if(NULL==data_old)
{
perror("malloc fail!
");
}
temp=str;
flag=flag_old;
data=data_old;
while(*temp!='')
{
if(temp==temp_old && *temp=='-')
{
temp++;
continue;
}
else if(*temp=='+' || *temp=='-' ||*temp=='*' ||*temp=='/')
{
*flag++=*temp;
*temp='';
*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='';
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);
}*/```
View Code

运行截图：

改善：

完善当用户做题的时候，用户输入非法字符，会出现死循环。一开始助教说会死循环时，我以为是在程序模块里出现问题，

因为我程序模块的容错功能是我写的，就没有注意到初始界面那里原来用户选择那里也有（同伴写的），后面再找才发现。

• 相关阅读:
可以foreach的 必须继承IEnumable 接口才行
.net 委托的用法
匿名类的使用
检测到有潜在危险的 Request.Form 值——ValidateRequest的使用
IsPostBack用法
Ajax 与 jquery
好用的模板引擎NVelocity
题解【AcWing275】[NOIP2008]传纸条
题解【AcWing274】移动服务
题解【AcWing271】杨老师的照相排列
• 原文地址：https://www.cnblogs.com/case1/p/4424157.html