14.44 桌面计算器

本文探讨了在C++中使用函数指针和std::function进行数学运算的方法。通过创建映射表,将字符串操作符与对应的函数关联,实现了动态调用不同运算方式的功能。文章详细介绍了如何定义和使用函数指针及std::function,并展示了具体的代码实现。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

使用函数指针

#include <iostream>
#include <string>
#include <map>
#include <functional>

using namespace std;

enum cal_type {addop, minusop, multiop, divop};
map<const string, double(*)(int,int)> cal;
map<const string, cal_type> switcher;

double add_method(int a, int b) {
    return a+b;
}

double Minus_method(int a, int b) {
    return a-b;
}

double multi_method(int a, int b) {
    return a*b;
}

double div_method(int a, int b) {
    return a/b;
}

int main() {
    cal.insert(pair<const string, double(*)(int, int)>("add", add_method));
    cal.insert(pair<const string, double(*)(int, int)>("minus", Minus_method));
    cal.insert(pair<const string, double(*)(int, int)>("multi", multi_method));
    cal.insert(pair<const string, double(*)(int, int)>("div", div_method));

    switcher.insert(pair<const string, cal_type>("add", cal_type::addop));
    switcher.insert(pair<const string, cal_type>("minus", cal_type::minusop));
    switcher.insert(pair<const string, cal_type>("multi", cal_type::multiop));
    switcher.insert(pair<const string, cal_type>("div", cal_type::divop));

    string ret;
    int first, second;
    cout << "Enter the method and the values" << endl;
    while (cin >> ret >> first >> second) {
        switch (switcher[ret])
        {
            case cal_type::addop:
                cout << cal[ret](first, second) << endl;
                break;
            case cal_type::minusop:
                cout << cal[ret](first, second) << endl;
                break;
            case cal_type::multiop:
                cout << cal[ret](first, second) << endl;
                break;
            case cal_type::divop:
                cout << cal[ret](first, second) << endl;
                break;
            default:
                cout << "No match operator" << endl;
                break;
        }
    }

    return 0;
}

注意map cal中的second是函数指针,有*,而不是直接double(int, int),这是一个函数类型,而不是函数指针类型。

使用function:

#include <iostream>
#include <string>
#include <map>
#include <functional>

using namespace std;

enum cal_type {addop, minusop, multiop, divop};
using func = function<double(int,int)>;
map<const string, func> cal;
map<const string, cal_type> switcher;

double add_method(int a, int b) {
    return a+b;
}

double Minus_method(int a, int b) {
    return a-b;
}

double multi_method(int a, int b) {
    return a*b;
}

double div_method(int a, int b) {
    return a/b;
}

int main() {
    cal.insert(pair<const string, func>("add", add_method));
    cal.insert(pair<const string, func>("minus", Minus_method));
    cal.insert(pair<const string, func>("multi", multi_method));
    cal.insert(pair<const string, func>("div", div_method));

    switcher.insert(pair<const string, cal_type>("add", cal_type::addop));
    switcher.insert(pair<const string, cal_type>("minus", cal_type::minusop));
    switcher.insert(pair<const string, cal_type>("multi", cal_type::multiop));
    switcher.insert(pair<const string, cal_type>("div", cal_type::divop));

    string ret;
    int first, second;
    cout << "Enter the method and the values" << endl;
    while (cin >> ret >> first >> second) {
        switch (switcher[ret])
        {
            case cal_type::addop:
                cout << cal[ret](first, second) << endl;
                break;
            case cal_type::minusop:
                cout << cal[ret](first, second) << endl;
                break;
            case cal_type::multiop:
                cout << cal[ret](first, second) << endl;
                break;
            case cal_type::divop:
                cout << cal[ret](first, second) << endl;
                break;
            default:
                cout << "No match operator" << endl;
                break;
        }
    }

    return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值