有关加减乘除的疑问

sunyuqian 2009-12-19 11:59:58
大家好,对加减乘除这道题有几个疑问,请高手解答

1、我在加,减,乘,除的函数里写了请输入第1,2个数,然后各自用scanf()接收,感觉罗嗦,如何只写一次就能让这些函数都能用呢?如果我把这些话写在主函数里,有一些问题,在主函数里,我对switch进行判断,如果把这些话写switch上,当选择错误的字母时,还是会先让输入两个数,然后才说选择错误,逻辑不对,写在下面,没赋值就调用函数了,答案肯定不对。如果单写一个函数用来输入两个数,我写的是input(),返回值只能一个,在不用指针的情况下,还有别的办法吗?大家是怎么做的呢?

2、如果在输入中输的不是数字,应该提示说输入错误,并重新进行输入,这个怎么做呢?ctype.h库里有个isdigit()函数判断是否是数字,我调用以后,如果输入的不是数字,重新调用scanf()总是出错,这种情况该怎么处理?
打个比方:

printf("Enter the frist number: ");
scanf("%lf", &num1);
if(isdigit(num1))
{
printf("请输入数字:\n ");
scanf("%lf", &num1);
}
这样的话就只能重输入一次,而且运行后是错的


#include <stdio.h>
#include <ctype.h>

int getfirst(void);
void menu(void);
void input();
double add();
double sbtract();
double multiply();
double divide();

int main(void)
{
double result;
char response;

menu();
while ((response = getfirst()) != 'q')
{
response = tolower(response);
switch (response)
{
case 'a' : result = add(); break;
case 's' : result = sbtract(); break;
case 'm' : result = multiply(); break;
case 'd' : result = divide(); break;
default : printf("Please enter a, s, m, d, or q.\n");
menu();
continue;
}
printf("%.2f\n",result);
menu();
}
printf("Done.\n");

return 0;
}

void menu(void)
{
printf("Enter the operation of your choice:\n");
printf("a. add s. sbtract\n");
printf("m. multiply d. divide\n");
printf("q. quit\n");
}

int getfirst(void)
{
int ch;

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

double add()
{
double num1,num2;
input();
return num1 + num2;
}

double sbtract()
{
double num1,num2;
printf("Enter the frist number: ");
scanf("%lf", &num1);
printf("Enter the second number: ");
scanf("%lf", &num2);
return num1 - num2;
}

double multiply()
{
double num1,num2;
printf("Enter the frist number: ");
scanf("%lf", &num1);
printf("Enter the second number: ");
scanf("%lf", &num2);
return num1 * num2;
}

double divide()
{
double num1,num2;
printf("Enter the frist number: ");
scanf("%lf", &num1);
printf("Enter the second number: ");
scanf("%lf", &num2);
if(num2 == 0)
{
printf("除数不能为0,请重新输入:\n");
scanf("%lf", &num2);
}
return num1 / num2;
}

void input()
{
double num1,num2;
printf("Enter the frist number: ");
scanf("%lf",&num1);
printf("Enter the second number: ");
scanf("%lf", &num2);
}
...全文
126 12 打赏 收藏 转发到动态 举报
写回复
用AI写文章
12 条回复
切换为时间正序
请发表友善的回复…
发表回复
mmilmf 2009-12-20
  • 打赏
  • 举报
回复
Enter the frist number: 1
Enter the second number: 2
Enter the operation of your choice:
a. add s. sbtract
m. multiply d. divide
q. quit
a //输入
The Result is 3.00 //结果
Enter the operation of your choice:
a. add s. sbtract
m. multiply d. divide
q. quit
s //输入
The Result is -1.00 //结果
Enter the operation of your choice:
a. add s. sbtract
m. multiply d. divide
q. quit
q //输入
Done.
Press any key to continue
mmilmf 2009-12-20
  • 打赏
  • 举报
回复

#include <stdio.h>
#include <ctype.h>

int getfirst(void);
void menu(void);
void input();
double add();
double sbtract();
double multiply();
double divide();

double num1,num2; //在这里定义num1,num2

int main(void)
{
double result;
char response;

input(); //mark
menu();

while ((response = getfirst()) != 'q')
{
response = tolower(response);
switch (response)
{
case 'a' : result = add(); break;
case 's' : result = sbtract(); break;
case 'm' : result = multiply(); break;
case 'd' : result = divide(); break;
default : printf("Please enter a, s, m, d, or q.\n");
menu();
continue;
}
printf("The Result is %.2f\n",result); //mark
menu();
}
printf("Done.\n");

return 0;
}

void menu(void)
{
printf("Enter the operation of your choice:\n");
printf("a. add s. sbtract\n");
printf("m. multiply d. divide\n");
printf("q. quit\n");
}

int getfirst(void)
{
int ch;

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

double add()
{
//double num1,num2;
//input();
return num1 + num2;
}

double sbtract()
{
/* double num1,num2;
printf("Enter the frist number: ");
scanf("%lf", &num1);
printf("Enter the second number: ");
scanf("%lf", &num2);*/
return num1 - num2;
}

double multiply()
{

/* printf("Enter the frist number: ");
scanf("%lf", &num1);
printf("Enter the second number: ");
scanf("%lf", &num2);*/
return num1 * num2;
}

double divide()
{
/* double num1,num2;
printf("Enter the frist number: ");
scanf("%lf", &num1);
printf("Enter the second number: ");
scanf("%lf", &num2);*/
if(num2 == 0)
{
printf("除数不能为0,请重新输入:\n");
scanf("%lf", &num2);
}
return num1 / num2;
}

void input()
{
printf("Enter the frist number: ");
scanf("%lf",&num1);
printf("Enter the second number: ");
scanf("%lf", &num2);
}



所有改动的地方已标记
sunyuqian 2009-12-20
  • 打赏
  • 举报
回复
谢谢了
sunyuqian 2009-12-20
  • 打赏
  • 举报
回复
[Quote=引用 9 楼 hlyces 的回复:]
C/C++ codevoid input()
{
printf("Enter the frist number:");while(scanf("%lf",&num1)!=1)
{
printf("输入出错!请重新输入:\n");
scanf("%lf",&num1);//请把此句XX掉 }
printf("Enter the second number?-
[/Quote]

还是不行
mmilmf 2009-12-20
  • 打赏
  • 举报
回复

void input()
{
int i ,j ;
do
{
fflush(stdin);
printf("Enter the frist number: ");
i = scanf("%lf",&num1);
if(i <= 0 ) continue ;
again: printf("Enter the second number: ");
fflush(stdin);
j = scanf("%lf", &num2);
if(j <= 0 ) goto again;
}
while(i <= 0 || j <= 0);
}


这样试试,虽然成功了,但是使用goto语句,
你可以把这两个数的输入,分开写成两个循环,就像你上面写的那样。
hlyces 2009-12-20
  • 打赏
  • 举报
回复

void input()
{
printf("Enter the frist number: ");
while(scanf("%lf",&num1)!=1)
{
printf("输入出错!请重新输入:\n");
scanf("%lf",&num1);//请把此句XX掉
}
printf("Enter the second number: ");
while(scanf("%lf",&num2)!=1)
{
printf("输入出错!请重新输入:\n");
scanf("%lf",&num2);//请把此句XX掉
}
}

sunyuqian 2009-12-20
  • 打赏
  • 举报
回复
[Quote=引用 7 楼 la_feng 的回复:]
引用 3 楼 sunyuqian 的回复:
谢谢,我现在还有一个问题,就是在input()里判断输入的是不是数字,如果不是,就提示重输,我写的是

C/C++ codevoid input()
{
    printf("Enter the frist number:");while(scanf("%lf",&num1)!=1)
    {
    printf("请输入数字:\n");
    scanf("%lf",&num1);
    }
    printf("Enter the second number:");while(scanf("%lf",&num2)!=1)
    {
    printf("请输入数字:\n");
    scanf("%lf",&num2);
    }
}

如果我输的不是数字,他就一直循环了,该怎么处理呢
这难道不是你想要的效果吗?C/C++ codevoid input()
{
printf("Enter the frist number:");while(scanf("%lf",&num1)!=1)
{
printf("输入出错!请重新输入:\n");
scanf("%lf",&num1);
}
printf("Enter the second number:");while(scanf("%lf",&num2)!=1)
{
printf("输入出错!请重新输入:\n");
scanf("%lf",&num2);
}
}
[/Quote]

如果输入的不是数字,就一直循环了,程序出错了
la_feng 2009-12-20
  • 打赏
  • 举报
回复
[Quote=引用 3 楼 sunyuqian 的回复:]
谢谢,我现在还有一个问题,就是在input()里判断输入的是不是数字,如果不是,就提示重输,我写的是

C/C++ codevoid input()
{
printf("Enter the frist number:");while(scanf("%lf",&num1)!=1)
{
printf("请输入数字:\n");
scanf("%lf",&num1);
}
printf("Enter the second number:");while(scanf("%lf",&num2)!=1)
{
printf("请输入数字:\n");
scanf("%lf",&num2);
}
}

如果我输的不是数字,他就一直循环了,该怎么处理呢
[/Quote]这难道不是你想要的效果吗?
void input()
{
printf("Enter the frist number: ");
while(scanf("%lf",&num1)!=1)
{
printf("输入出错!请重新输入:\n");
scanf("%lf",&num1);
}
printf("Enter the second number: ");
while(scanf("%lf",&num2)!=1)
{
printf("输入出错!请重新输入:\n");
scanf("%lf",&num2);
}
}
sunyuqian 2009-12-20
  • 打赏
  • 举报
回复
谢谢了
sunyuqian 2009-12-20
  • 打赏
  • 举报
回复
在input()里判断输入的是不是数字,如果不是,就提示重输,我写的是

C/C++ codevoid input()
{
printf("Enter the frist number:");
while(scanf("%lf",&num1)!=1)
{
printf("请输入数字:\n");
scanf("%lf",&num1);
}
printf("Enter the second number:");
while(scanf("%lf",&num2)!=1)
{
printf("请输入数字:\n");
scanf("%lf",&num2);
}
}

如果我输的不是数字,他就一直循环了,该怎么处理呢

这个有没有人会
shashenyidaoOCEAN 2009-12-20
  • 打赏
  • 举报
回复
学习 这几天我是越来越困惑了
sunyuqian 2009-12-20
  • 打赏
  • 举报
回复
谢谢,我现在还有一个问题,就是在input()里判断输入的是不是数字,如果不是,就提示重输,我写的是

void input()
{
printf("Enter the frist number: ");
while(scanf("%lf",&num1)!=1)
{
printf("请输入数字:\n");
scanf("%lf",&num1);
}
printf("Enter the second number: ");
while(scanf("%lf",&num2)!=1)
{
printf("请输入数字:\n");
scanf("%lf",&num2);
}
}


如果我输的不是数字,他就一直循环了,该怎么处理呢

69,371

社区成员

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

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