早教吧作业答案频道 -->其他-->
这个程序运行减法的时候有错误.如果我从键盘输入一个负数就会出错.急#include#include#include#includeintscan(){chars[100];inti,t,z=0;do{z=0;gets(s);for(i=0;s[i]!='\0';i++)if(s[i]'9')break;if(i>=strlen(s))for(t=0;s[t]!=
题目详情
这个程序运行减法的时候有错误.如果我从键盘输入一个负数就会出错.急
#include
#include
#include
#include
int scan()
{
char s[100];
int i,t,z=0;
do
{
z=0;
gets(s);
for(i=0;s[i]!='\0';i++)
if(s[i]'9') break;
if(i >= strlen(s))
for(t=0;s[t]!='\0';t++)
z=z*10+((int)s[t]-48);
else
printf("您输入答案形式有误,请输入正确的形式:\n");
}
while(i < strlen(s));
return(z);
}
int x=1,y=1,choice=0,sum=0,right=0,z=0,answer;
float precent;
char symbol;//(符号)
void random()//随机给出等式
{
srand(time(NULL));
x=rand()%100+1;
y=rand()%100+1;
}
void choose()
{
int operation(int,int,int);
if(sum==0) precent=0.0;
else precent=(float)right/sum;
printf("\n如需进行加减乘除运算测试,请分别输:1(加法运算)、2(减法运算)、3(乘法运算)、4(除法运算)\n");
printf("如需退出请输:5(退出运算)\n");
choice=scan();
switch(choice)
{
case 1:printf("欢迎您进行加法运算测试,如需退出请输:99999\n");symbol='+';break;
case 2:printf("欢迎您进行减法运算测试,如需退出请输:99999\n");symbol='-';break;
case 3:printf("欢迎您进行乘法运算测试,如需退出请输:99999\n");symbol='*';break;
case 4:printf("欢迎您进行除法运算测试,如需退出请输:99999\n");symbol='/';break;
case 5:printf("欢迎您下次再来测试\n");
printf("您本次测试共完成%d道题,其中%d道回答正确,正确率为:%f.本次获得积分%d分...\n",sum,right,precent,right);
break;
default:printf("您输入答案形式有误,请输入正确的形式:\n");choose();
}
}
int ans(int x,int y,int choice)
{
switch(choice)
{
case 1:z=x+y;break;
case 2:z=x-y;break;
case 3:z=x*y;break;
case 4:z=x/y;break;
}
return(z);
}
void operation()
{
random();
printf("%d%c%d=",x,symbol,y);
z=ans(x,y,choice);
answer=scan();
if(z==answer)
{
printf("恭喜您,回答正确.\n");
right++;sum++;
operation();
}
else if(answer==99999) choose();
else
{
printf("不正确,正确答案为:%5d\n",z);
sum++;
operation();
}
}
void main()
{
printf("欢迎您来到四则运算测试系统(请每次输入后以Enter键结束)\n");
choose();
while(choice!=5)
\x05 operation();
}
#include
#include
#include
#include
int scan()
{
char s[100];
int i,t,z=0;
do
{
z=0;
gets(s);
for(i=0;s[i]!='\0';i++)
if(s[i]'9') break;
if(i >= strlen(s))
for(t=0;s[t]!='\0';t++)
z=z*10+((int)s[t]-48);
else
printf("您输入答案形式有误,请输入正确的形式:\n");
}
while(i < strlen(s));
return(z);
}
int x=1,y=1,choice=0,sum=0,right=0,z=0,answer;
float precent;
char symbol;//(符号)
void random()//随机给出等式
{
srand(time(NULL));
x=rand()%100+1;
y=rand()%100+1;
}
void choose()
{
int operation(int,int,int);
if(sum==0) precent=0.0;
else precent=(float)right/sum;
printf("\n如需进行加减乘除运算测试,请分别输:1(加法运算)、2(减法运算)、3(乘法运算)、4(除法运算)\n");
printf("如需退出请输:5(退出运算)\n");
choice=scan();
switch(choice)
{
case 1:printf("欢迎您进行加法运算测试,如需退出请输:99999\n");symbol='+';break;
case 2:printf("欢迎您进行减法运算测试,如需退出请输:99999\n");symbol='-';break;
case 3:printf("欢迎您进行乘法运算测试,如需退出请输:99999\n");symbol='*';break;
case 4:printf("欢迎您进行除法运算测试,如需退出请输:99999\n");symbol='/';break;
case 5:printf("欢迎您下次再来测试\n");
printf("您本次测试共完成%d道题,其中%d道回答正确,正确率为:%f.本次获得积分%d分...\n",sum,right,precent,right);
break;
default:printf("您输入答案形式有误,请输入正确的形式:\n");choose();
}
}
int ans(int x,int y,int choice)
{
switch(choice)
{
case 1:z=x+y;break;
case 2:z=x-y;break;
case 3:z=x*y;break;
case 4:z=x/y;break;
}
return(z);
}
void operation()
{
random();
printf("%d%c%d=",x,symbol,y);
z=ans(x,y,choice);
answer=scan();
if(z==answer)
{
printf("恭喜您,回答正确.\n");
right++;sum++;
operation();
}
else if(answer==99999) choose();
else
{
printf("不正确,正确答案为:%5d\n",z);
sum++;
operation();
}
}
void main()
{
printf("欢迎您来到四则运算测试系统(请每次输入后以Enter键结束)\n");
choose();
while(choice!=5)
\x05 operation();
}
▼优质解答
答案和解析
scan函数中第一个for循环:
for(i=0;s[i]!='\0';i++)
if(s[i]'9') break;
此处有问题,因为你如果输入的第一个字符是个负号的话,那么i=0时就回Kill掉for循环,所以后边的if(i >= strlen(s))这个条件判断为假倒至其走到else分支
改动的方法就是将那个第一个for循环里边的if条件改为如下:
if(!(s[i] == '-' ||s[i]>'0' && s[i]
for(i=0;s[i]!='\0';i++)
if(s[i]'9') break;
此处有问题,因为你如果输入的第一个字符是个负号的话,那么i=0时就回Kill掉for循环,所以后边的if(i >= strlen(s))这个条件判断为假倒至其走到else分支
改动的方法就是将那个第一个for循环里边的if条件改为如下:
if(!(s[i] == '-' ||s[i]>'0' && s[i]
看了这个程序运行减法的时候有错误....的网友还看了以下:
about chinese,help meIn "nǐ zài nǎ lǐ gōng zuò?" 2020-05-14 …
已知字母组合成英语单词1、e e t t i n h r 2、e e r a t w h 3、o 2020-05-14 …
一个长度为n的线性表,如果采用顺序存储结构,那么当删除第i个元素(l≤,i, ≤n)时, 需要向前移 2020-05-23 …
国家的另外一种意思例如:h.o.l.l.a.n.d荷兰hopeourlovelastsandnev 2020-06-14 …
下列加点的字,注音全部正确的一组是()。A.寒濑(lài)苔藓(tāi)万籁(lài)皑皑白雪(á 2020-06-28 …
请问L与N的发音到底哪里不一样啊?别人多说我L与N不分,可我不知道如何改正, 2020-07-21 …
英语翻译L-I-N-G-O:LINGOAsanexample,theproductionstaff 2020-07-22 …
假定用两个一维数组L[n+1]和R[n+1]作为有n个结点的二叉树的存储结构,L[i]和R[i]分 2020-08-03 …
下列各项中注音有误的一项是()A.léi身体羸弱秦王嬴政yíng输赢yíngB.lài万籁俱寂石濑l 2020-12-24 …
下列常用词语中字音有错误的一组是A.勒索lè勒紧lēi擂鼓léi闷热mēnB.扪心自问mén愤懑mè 2020-12-26 …