请大家帮我看个代码.帮忙改正下.实在不知道该怎么该了.

RyanJao 2010-12-24 04:24:38
主要在div函数这里,题目要求是,在输入第二个数的时候,不能输入0,如果输入为零的话.
如此打印:
Enter first number: 18.4
Enter second number: 0
Enter a number other than 0: 0.2
18.4 / 0.2 = 92

这就是在switch处,输入d,调用div函数的要求.

实在不知道该怎么处理这个输入条件,输入0该怎么写代码,才能即有返回,又能在输入0的时候可以循环输入.

谢谢了.

#include<stdio.h>
void add(void);
void div(void);
float get_float(void);
char get_choice(void);
char get_first(void);
float get_zero(void);
int main(void)
{
char choice;

while((choice = get_choice()) != 'q')
{
switch(choice)
{
case 'a':add();
break;
case 'b'://subtract();
break;
case 'c'://multiply();
break;
case 'd':div();
break;
case 'q':printf("quit");
break;
}
}
printf("Bye.\n");
return 0;
}

char get_choice(void)
{
int ch;

printf("Enter the letter of your choice:\n");
printf("a. add b. subtract\n");
printf("c. multiply d. divide\n");
printf("q. quit\n");
ch = get_first();
while ((ch < 'a' || ch > 'd') && ch != 'q')
{
printf("Please respond with a, b, c, d, or q.\n");
ch = get_first();
}
return ch;
}

char get_first(void)
{
int ch;

ch = getchar();
while (getchar() != '\n')
continue;
return ch;
}


void add(void)
{
float x, y;
printf("Enter first number: ");
x = get_float(); //需要检测下没有这里的语句可否通过
printf("Enter second number: ");
y = get_float();
while (getchar() != '\n')
continue;
printf("%.1f + %.f = %.1f\n", x, y, x + y);
}

void div(void)
{
float x, y;
printf("Enter first number: ");
x = get_float();
printf("Enter second number: ");
y = get_zero();
while(getchar() != '\n')
continue;
printf("%.1f / %.1f = %.1f\n", x, y, x / y);
}

float get_zero(void)
{
float input;
/*while(scanf("%f", &input) != 1)
{
while(putchar(input) == 0)
printf("Enter a number other than 0: ");
break;
}
return input;*/
scanf("%f", &input);
if(input == 0)
printf("Enter a number other than 0: \n");
else
return input;
}

float get_float(void)
{
float input;
char ch;

while (scanf("%f", &input) != 1)
{
while ((ch = getchar()) != '\n')
putchar(ch); // dispose of bad input
printf(" is not a number.\nPlease enter a ");
printf("number, such as 2.5, -1.78E8, or 3: ");
}
return input;
}
...全文
78 5 打赏 收藏 转发到动态 举报
写回复
用AI写文章
5 条回复
切换为时间正序
请发表友善的回复…
发表回复
RyanJao 2010-12-24
  • 打赏
  • 举报
回复
悲催的,太郁闷了.看到2楼朋友给的代码,感觉用递归理解挺简单的.

再看4楼的代码,我勒个去,郁闷死.

这不是while循环里最基础的循环语句么..

不行,今天晚上,还得看一遍循环和跳转这部分.
yuyan_linger 2010-12-24
  • 打赏
  • 举报
回复

float get_zero(void)
{
float input;
scanf("%f", &input);
while(input ==0)
{
printf("Enter a number other than 0: ");
scanf("%f", &input);
}
return input;
}
RyanJao 2010-12-24
  • 打赏
  • 举报
回复
谢两位了,goto语句不是很熟悉,我看教材里说C语言,可以完全不使用goto语句,所以看的时候也不是很专心.

所以,刚才做题的时候,一丁点都没往那边想.

果然,技多不压身,无论什么东西,只要相关,都得看看.何况对我现在这种一介"白身"呢..

谢两位了.

以后再有问题的话,请多指教.
yuppy 2010-12-24
  • 打赏
  • 举报
回复

float get_zero(void)
{
float input;
/*while(scanf("%f", &input) != 1)
{
while(putchar(input) == 0)
printf("Enter a number other than 0: ");
break;
}
return input;*/
scanf("%f", &input);
if(input == 0){
printf("Enter a number other than 0: \n");
return get_zero();
}
else
return input;
}
MrEdifier 2010-12-24
  • 打赏
  • 举报
回复
goto。。如果有人说迭代,其实就是goto的思想。。

69,371

社区成员

发帖
与我相关
我的任务
社区描述
C语言相关问题讨论
社区管理员
  • C语言
  • 花神庙码农
  • 架构师李肯
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧