模拟计算器

时间:2023-02-01 12:00:51

方法一

#include<stdio.h>
int add(int a, int b)
{
return a + b;
}
int sub(int a, int b)
{
return a - b;
}
int mul(int a, int b)
{
return a * b;
}
int div(int a, int b)
{
return a / b;
}
int main()
{
int x, y;
int input = 0;
int ret = 0;
do
{
printf("*************************\n");
printf(" 1:add 2:sub \n");
printf(" 1:mul 2:div \n");
printf("*************************\n");
printf("请选择: ");
scanf("%d", &input);
switch (input)
{
case 1:
printf("输入操作数: ");
scanf("%d%d", &x, &y);
ret = add(x, y);
printf("ret=%d\n", ret);
break;
case 2:
printf("输入操作数: ");
scanf("%d%d", &x, &y);
ret = sub(x, y);
printf("ret=%d\n", ret);
break;
case 3:
printf("输入操作数: ");
scanf("%d%d", &x, &y);
ret = mul(x, y);
printf("ret=%d\n", ret);
break;
case 4:
printf("输入操作数: ");
scanf("%d%d", &x, &y);
ret = div(x, y);
printf("ret=%d\n", ret);
break;
case 0:
printf("退出程序\n");
break;
}
} while (input);
return 0;
}

方法二(函数指针数组)

#include <stdio.h>
int add(int a, int b)
{
return a + b;
}
int sub(int a, int b)
{
return a - b;
}
int mul(int a, int b)
{
return a * b;
}
int div(int a, int b)
{
return a / b;
}
int main()
{
int x, y;
int input = 1;
int ret = 0;
int(*p[5])(int x, int y) = { 0,add,sub,mul,div };//转换表
while (input)
{
printf("***************************\n");
printf(" 1:add 2:sub\n");
printf(" 3:mul 3:div\n");
printf("***************************\n");
printf("请输入:");
scanf("%d ", &input);
if ((input <= 4 && input >= 1))
{
printf("输入操作数:");
scanf("%d%d", &x, &y);
ret = ((*p[input])(x, y));
}
else
printf("输入有误\n");
printf("ret=%d\n", ret);
}
return 0;
}