重生之我是一名程序员 33

哈喽大家晚上好啊!今天呢给大家分享的知识是------教大家实现简易的代码计算机(拥有简单的加减乘除运算)。首先呢,想要实现一个计算机,就必须有一份简单的选择菜单,想必大家应该还记得我之前给大家讲过函数吧,那么这个菜单我们就用一个函数mune()函数实现,如下:

cpp 复制代码
void mune()
{
 printf("*************************\n");
 printf("*     1:add 2:sub       *\n");
 printf("*      3:mul 4:div      *\n");
 printf("*     0:exit            *\n");
 printf("*************************\n");
}

而加减乘除运算也用4个简单的函数实现,如下:

cpp 复制代码
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;
}

最后,我们用do-while循环以及switch结构来实现这个计算机,如下:

cpp 复制代码
#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;
 do
 {
 mune();
 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;
 default:
 printf("选择错误\n");
 break;
 }
 } while (input);
 return 0;
}

但是,此代码虽然可以实现我们的计算机程序,可一旦我们需要的运算多了(加减乘除之外的运算),switch结构所构成的代码就会非常的长,那我们是否可以改进一下代码呢?当然是可以的,见面给大家讲过指针数组,以及函数指针,那我们今天就用二者的结合------函数指针数组来优化这段代码,在此之前,先给大家介绍一下函数指针数组,函数指针数组顾名思义是指一个数组,其中的每一个元素都是一个指向函数的指针。函数指针数组的元素可以是不同的函数指针,也可以是相同的函数指针。通常,函数指针数组用于实现函数的动态调用或者实现函数的多态性。例如,可以根据函数指针数组中的元素来动态调用不同的函数,或者根据函数指针数组中的元素实现不同的算法,从而达到代码复用和灵活性的目的。最后,优化后的代码如下:

cpp 复制代码
#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 }; //函数指针数组
 do
 {
 mune()
 printf( "请选择:" );
 scanf("%d", &input);
 if ((input <= 4 && input >= 1))//通过数组中的下标来构成判断
 {
 printf( "输⼊操作数:" );
 scanf( "%d %d", &x, &y);
 ret = (*p[input])(x, y);
 printf( "ret = %d\n", ret);
 }
 else if(input == 0)
 {
 printf("退出计算器\n");
 }
 else
 {
 printf( "输⼊有误\n" );
 }
 }while (input);
 return 0;
}

好啦,现在大家就应该知道怎么去构成一个简易的计算机了,那么今天的知识分享就到此结束啦,大家明天见!

相关推荐
NiceCloud喜云4 小时前
Opus 4.8 的 Effort Control 怎么选:Low 到 Max 五档策略
android·java·大数据·前端·c++·python·spring
小羊在睡觉5 小时前
力扣84. 柱状图中最大的矩形
后端·算法·leetcode·golang·go
cjhbachelor5 小时前
c++继承
c++
3DVisionary5 小时前
蓝光三维扫描:医疗制造的精度焦虑怎么解
人工智能·算法·制造·蓝光三维扫描·医疗制造·三维检测·义齿检测
AI玫瑰助手5 小时前
Python函数:默认参数的定义与注意事项
开发语言·python·信息可视化
好评笔记5 小时前
机器学习面试八股——常用损失函数
人工智能·深度学习·算法·机器学习·校招
weixin_468466855 小时前
全局与局部注意力机制新手实战指南
人工智能·python·深度学习·算法·自然语言处理·transformer·注意力机制
油炸自行车5 小时前
Claude Code 错误:API Error: 400 Failed to deserialize the JSON body into the
开发语言·javascript·json·trae·claude code·api error 400
肩上风骋5 小时前
C++14特性
开发语言·c++·c++14特性
_日拱一卒5 小时前
LeetCode:994腐烂的橘子
java·数据结构·算法·leetcode·深度优先