【C语言】每日一题,快速提升(4)!

🔥 博客主页🔥 :【 坊钰_CSDN博客

欢迎各位点赞👍 评论**✍收藏⭐**

题目:实现计算机程序

解答:

该程序运用函数指针数组,具体请看代码

代码:

cs 复制代码
#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
    {
        printf("*************************\n");
        printf("  1:add           2:sub  \n");
        printf("  3:mul           4:div  \n");
        printf("         0:exit          \n");
        printf("*************************\n");
        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;
}
相关推荐
程小k5 分钟前
C++设计模式
c语言·c++
夜晚中的人海36 分钟前
【C++】二分查找算法习题
开发语言·c++·算法
职业考试资料墙43 分钟前
通信安全员备考真题及答案
学习·考试·题库·考证
狮子座的男孩1 小时前
js基础:06、函数(创建函数、参数、返回值、return、立即执行函数、对象(函数))和枚举对象的属性
开发语言·前端·javascript·经验分享·函数·枚举对象·立即执行函数
sulikey2 小时前
【C++ STL 深入解析】insert 与 emplace 的区别与联系(以 multimap 为例)
开发语言·c++·stl·stl容器·insert·emplace
朝朝暮暮Quake2 小时前
提示学习思想
学习
墨白曦煜2 小时前
Java集合框架整体分类(完整的集合框架关系)
java·开发语言
YuanlongWang2 小时前
C# 基础——async/await 的实现原理与最佳实践
开发语言·c#
kkkkk0211062 小时前
JavaScript性能优化实战:深度剖析瓶颈与高效解决方案
开发语言·javascript·性能优化