#include <stdlib.h>
#include <string.h>
#include <math.h>
#define _CRT_SECURE_NO_WARNINGS
void ArithmeticMenu() { //排序子菜单 数学求四则运算
printf("*****************************\n");
printf("*\t\t1.加法\t\t*\n");
printf("*\t\t2.减法\t\t*\n");
printf("*\t\t3.乘法\t\t*\n");
printf("*\t\t4.除法\t\t*\n");
printf("******************************\n");
}
void Arithmetic() {
double x, double y;
int choice;
printf("请输入您的选择:\n");
scanf_s("%d", &choice);
switch (choice) {
case 1:printf("请输入要算的数字:\n");
scanf_s("%lf+%lf", &x, &y);
printf("x+y=%lf\n", x + y);
break;
case 2:printf("请输入要算的数字:\n");
scanf_s("%lf-%lf", &x, &y);
printf("x-y=%lf\n", x - y);
break;
case 3:printf("请输入要算的数字:\n");
scanf_s("%lf*%lf", &x, &y);
printf("x*y=%lf\n", x * y);
break;
case 4:printf("请输入要算的数字:\n");
scanf_s("%lf %lf", &x, &y);
printf("x/y=%lf\n", x / y);
break;
default:printf("请重新输入\n");
break;
}
}
int main()
{
ArithmeticMenu();
Arithmetic();
return 0;
}
#include <stdio.h>你都没加啊
double x,y;
修改如下:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <math.h>
#define _CRT_SECURE_NO_WARNINGS
void ArithmeticMenu() { //排序子菜单 数学求四则运算
printf("*****************************\n");
printf("*\t\t1.加法\t\t*\n");
printf("*\t\t2.减法\t\t*\n");
printf("*\t\t3.乘法\t\t*\n");
printf("*\t\t4.除法\t\t*\n");
printf("******************************\n");
}
void Arithmetic() {
double x, y;
int choice;
printf("请输入您的选择:\n");
scanf_s("%d", &choice);
switch (choice) {
case 1:printf("请输入要算的数字:\n");
scanf_s("%lf+%lf", &x, &y);
printf("x+y=%lf\n", x + y);
break;
case 2:printf("请输入要算的数字:\n");
scanf_s("%lf-%lf", &x, &y);
printf("x-y=%lf\n", x - y);
break;
case 3:printf("请输入要算的数字:\n");
scanf_s("%lf*%lf", &x, &y);
printf("x*y=%lf\n", x * y);
break;
case 4:printf("请输入要算的数字:\n");
scanf_s("%lf/%lf", &x, &y);
printf("x/y=%lf\n", x / y);
break;
default:printf("请重新输入\n");
break;
}
system("pause");
}
int main()
{
ArithmeticMenu();
Arithmetic();
system("pause");
return 0;
}
定义错了,不能一条语句写两次类型
代码存在以下问题:
1.函数中定义变量时,只需要写变量名,不需要重复写变量类型,例如:
double x, double y;
应该改为:
double x, y;
2.在scanf_s()函数中,输入格式中的加号“+”和乘号“*”需要转义,否则会导致程序崩溃。应该改为:
scanf_s("%lf+%lf", &x, &y); // 加号“+”前面需要加上“\”
scanf_s("%lf-%lf", &x, &y);
scanf_s("%lf*%lf", &x, &y); // 乘号“*”需要前后加上“\”
3.在除法运算中,如果除数为0,会导致程序崩溃。应该加上判断,如果除数为0,输出错误信息。
修改后的代码如下:
#include <stdlib.h>
#include <string.h>
#include <math.h>
#define _CRT_SECURE_NO_WARNINGS
void ArithmeticMenu() { //排序子菜单 数学求四则运算
printf("*****************************\n");
printf("*\t\t1.加法\t\t*\n");
printf("*\t\t2.减法\t\t*\n");
printf("*\t\t3.乘法\t\t*\n");
printf("*\t\t4.除法\t\t*\n");
printf("******************************\n");
}
void Arithmetic() {
double x, y;
int choice;
printf("请输入您的选择:\n");
scanf_s("%d", &choice);
switch (choice) {
case 1:
printf("请输入要算的数字:\n");
scanf_s("%lf+%lf", &x, &y);
printf("x+y=%lf\n", x + y);
break;
case 2:
printf("请输入要算的数字:\n");
scanf_s("%lf-%lf", &x, &y);
printf("x-y=%lf\n", x - y);
break;
case 3:
printf("请输入要算的数字:\n");
scanf_s("%lf*%lf", &x, &y);
printf("x*y=%lf\n", x * y);
break;
case 4:
printf("请输入要算的数字:\n");
scanf_s("%lf/%lf", &x, &y);
if (y == 0) {
printf("除数不能为0!\n");
}
else {
printf("x/y=%lf\n", x / y);
}
break;
default:
printf("请重新输入\n");
break;
}
}
int main()
{
ArithmeticMenu();
Arithmetic();
return 0;
}