提示:文章写完后,目录可以自动生成,如何生成可参考右边的帮助文档
文章目录
- 介绍
- 关键代码
- 运行代码(3种)
介绍
- 标准计数器:执行加减乘除等等
- 科学计算器:执行分数、统计学、指数函数、对数、三角函数运算等等
- 程序员计算器:用于二进制,八进制,十进制,十六进制等相互转换功能
-
二、关键代码
1、加、减、乘、除的函数调用。
void Add(int x, int y)
{
printf("sum=%d\n", x + y);
}void Sub(int x, int y)
{
printf("sum=%d\n", x - y);
}void Mlu(int x, int y)
{
printf("sum=%d\n", x * y);
}void Div(int x, int y)
{
printf("sum=%d\n", x / y);
}
2、
1、运用do...while进行计算器是否运行。再用switch...case.default选择加还是减或者乘、除。
do
{
menu();
printf("请选择:>");
scanf("%d", &input);
switch (input)
{
case 0:
printf("退出计算器:\n");
break;
case 1:
printf("加法:");
scanf("%d %d", &x, &y);
Add(x, y);
break;
case 2:
printf("减法:");
scanf("%d %d", &x, &y);
Sub(x, y);
break;
case 3:
printf("乘法:");
scanf("%d %d", &x, &y);
Mlu(x, y);
break;
case 4:
printf("除法:");
scanf("%d %d", &x, &y);
Div(x, y);
break;
default:
printf("请重新输入:\n");
break;
}
} while (input);
2、Add、Sub、Mlu和Div中有相同的代码,本着优秀代码利用函数指针数组可进行优化、简单、明了,再运用if...else if...else进行判断是否退出、或者进行计算。
printf("请选择:>");
scanf("%d", &input);
int(*pfArr[5])(int, int) = { Add,Sub,Mlu,Div };if (input > 0 && input < 5)
{
printf("请输入二个数:");
scanf("%d %d", &x, &y);
pfArr[input](x, y);
}
else if (input == 0)
{
printf("退出计算器:\n");
}
else
printf("请重新输入:");
3、运用回调函数进行优化。根据input进行选择计算器中加、减、乘、除,然后传给函数指针pf
,再输入二个要计算的数,再回归Add、Sub、Mlu、Div函数里进行运算。
void Calc(int(* pf)(int, int))
{
int x = 0, y = 0;
printf("请输入二个数:");
scanf("%d %d", &x, &y);
pf(x, y);
}
switch (input)
{
case 0:
printf("退出计算器:\n");
break;
case 1:
Calc(Add);
break;
case 2:
Calc(Sub);
break;
case 3:
Calc(Mlu);
break;
case 4:
Calc(Div);
break;
default:
printf("请重新输入:\n");
break;
}
运行代码
#define _CRT_SECURE_NO_WARNINGS 1
#include<stdio.h>void Add(int x, int y)
{printf("sum=%d\n", x + y);
}void Sub(int x, int y)
{printf("sum=%d\n", x - y);
}void Mlu(int x, int y)
{printf("sum=%d\n", x * y);
}void Div(int x, int y)
{printf("sum=%d\n", x / y);
}void menu()
{printf("*********************\n");printf("**** 1.Add 2.Sub ****\n");printf("**** 3.Mlu 4.Div ****\n");printf("**** play.jsq ****\n");printf("*********************\n");
}//方法一:
int main()
{int x = 0, y = 0;int input = 0;do{menu(); printf("请选择:>");scanf("%d", &input);switch (input){case 0:printf("退出计算器:\n");break;case 1:printf("加法:");scanf("%d %d", &x, &y);Add(x, y);break;case 2:printf("减法:");scanf("%d %d", &x, &y);Sub(x, y);break;case 3:printf("乘法:");scanf("%d %d", &x, &y);Mlu(x, y);break;case 4:printf("除法:");scanf("%d %d", &x, &y);Div(x, y);break;default:printf("请重新输入:\n");break;}} while (input);return 0;
}方法二
int main()
{int x = 0,y = 0;int input = 0;do{menu();printf("请选择:>");scanf("%d", &input);int(*pfArr[5])(int, int) = { Add,Sub,Mlu,Div };if (input > 0 && input < 5){printf("请输入二个数:");scanf("%d %d", &x, &y);pfArr[input](x, y);}else if (input == 0){printf("退出计算器:\n");}elseprintf("请重新输入:");} while (input);return 0;
}方法三
void Calc(int(* pf)(int, int))
{int x = 0, y = 0;printf("请输入二个数:");scanf("%d %d", &x, &y);pf(x, y);
}int main()
{int input = 0;do{menu(); printf("请选择:>");scanf("%d", &input);switch (input){case 0:printf("退出计算器:\n");break;case 1:Calc(Add);break;case 2:Calc(Sub);break;case 3:Calc(Mlu);break;case 4:Calc(Div);break;default:printf("请重新输入:\n");break;}} while (input);return 0;
}