69,371
社区成员
发帖
与我相关
我的任务
分享
#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);
}
#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);
}
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);
}
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掉
}
}
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);
}
}
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);
}
}