任务
代码
#include<stdio.h>
#include<math.h>
#define e 2.71828
#define PI 3.1415926
float Add(float x, float y)
{
return x + y;
}
float Sub(float x, float y)
{
return x - y;
}
float Mul(float x, float y)
{
return x * y;
}
float Div(float x, float y)
{
return x / y;
}
float Mod(float x, float y)
{
return (int)x % (int)y;
}
void menu1()
{
printf("********************\n");
printf("**** 1.算术运算 ****\n");
printf("**** 2.对数运算 ****\n");
printf("**** 3.三角运算 ****\n");
printf("**** 4.其他 ****\n");
printf("**** 0.退出 ****\n");
printf("********************\n");
}
void menu2()
{
printf("*******************\n");
printf("**** 1.加法 *******\n");
printf("**** 2.减法 *******\n");
printf("**** 3.乘法 *******\n");
printf("**** 4.除法 *******\n");
printf("**** 5.模运算 *****\n");
printf("**** 0.返回上层****\n");
printf("*******************\n");
}
void menu3()
{
printf("********************\n");
printf("**** 1.以10为底 ****\n");
printf("**** 2.以e为底 *****\n");
printf("**** 0.返回上层 ****\n");
printf("********************\n");
}
void menu4()
{
printf("********************\n");
printf("**** 1.sin *********\n");
printf("**** 2.cox *********\n");
printf("**** 3.tan *********\n");
printf("**** 0.返回上层 ****\n");
printf("********************\n");
}
void menu5()
{
printf("********************\n");
printf("**** 1.倒数 ****\n");
printf("**** 2.平方 ****\n");
printf("**** 3.平方根 ****\n");
printf("**** 4.e的指数 ****\n");
printf("**** 0.返回上层 ****\n");
printf("********************\n");
}
void Calc(float(*p)(float ,float))
{
float x = 0;
float y = 0;
printf("请输入两个操作数:\n");
scanf("%f%f", &x, &y);
float ret = (*p)(x, y);
printf("%.2f\n", ret);
}
int main()
{
int choose = 0;
int choose2 = 0;
float num3 = 0;
float angle = 0;
do
{
menu1();
printf("请选择运算类型:\n");
scanf("%d", &choose);
switch (choose)
{
case 1:
{
do
{
menu2();
printf("请选择算术运算类型:\n");
scanf("%d", &choose2);
switch (choose2)
{
case 1:Calc(Add);
break;
case 2:Calc(Sub);
break;
case 3:Calc(Mul);
break;
case 4:Calc(Div);
break;
case 5:Calc(Mod);
break;
case 0: printf("返回上层\n");
break;
default:printf("选择错误,请重新选择\n\n");
break;
}
} while (choose2);
}
break;
case 2:
{
do
{
menu3();
printf("请选择对数运算类型:\n");
scanf("%d", &choose2);
switch (choose2)
{
case 1:
{
printf("请输入操作数:\n");
scanf("%f",&num3);
printf("%.2f\n", log10(num3));
}
break;
case 2:
{
printf("请输入操作数:\n");
scanf("%f", &num3);
printf("%.2f\n", log(num3));
}
break;
case 0:printf("返回上层\n");
break;
default:printf("输入错误,请重新输入:\n\n");
break;
}
} while (choose2);
}
break;
case 3:
{
do
{
menu4();
printf("请选择三角函数类型:\n");
scanf("%d", &choose2);
if (choose2 == 1)
{
printf("请输出计算的角度:\n");
scanf("%f", &angle);
printf("%.2f\n", sin(PI * angle / 180) );
}
else if (choose2 == 2)
{
printf("请输出计算的角度:\n");
scanf("%f", &angle);
printf("%.2f\n", cos(PI * angle / 180) );
}
else if (choose2 == 3)
{
printf("请输出计算的角度:\n");
scanf("%f", &angle);
printf("%.2f\n", sin(PI * angle / 180) / cos(PI * angle / 180) );
}
else if (choose2 == 0)
{
printf("返回上层\n");
}
else printf("输入错误,请重新输入\n\n");
} while (choose2);
}
break;
case 4:
{
do
{
menu5();
printf("请选择运算类型:\n");
scanf("%d", &choose2);
switch (choose2)
{
case 1:
{
printf("请输入操作数:\n");
scanf("%f", &num3);
printf("%.2f\n", 1 / num3);
}
break;
case 2:
{
printf("请输入操作数:\n");
scanf("%f", &num3);
printf("%.2f\n", num3* num3);
}
break;
case 3:
{
printf("请输入操作数:\n");
scanf("%f", &num3);
printf("%.2f\n", pow(num3,3));
}
break;
case 4:
{
printf("请输入操作数:\n");
scanf("%f", &num3);
printf("%.2f\n", pow(e, num3));
}
break;
case 0:printf("返回上层\n");
break;
default:printf("输入错误,请重新输入:\n\n");
break;
}
} while (choose2);
}
break;
case 0:
printf("退出计算器\n");
break;
default:
printf("选择错误,请重新选择\n\n");
break;
}
} while (choose);
return 0;
}