日志18

利用函数指针实现a和b的不同计算-指针与函数

#include <iostream>
using namespace std;
int calculate(int choice, int a, int b);
int main(){
    int choice;
    cin>>choice;
    int a,b;
    cin>>a>>b;
    int result=calculate(choice,a,b);
    cout<<result<<endl;
    return 0;
}
int calculate(int choice, int a, int b) {
    switch (choice){
    case 1:
        return (a>b)?a:b;
    case 2:
        return (a<b)?a:b;
    case 3:
        return a+b;
    default:
        cout<<"Invalid choice"<<endl;
        return 0;
    }
}

全部评论

相关推荐

评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客网在线编程
牛客网题解
牛客企业服务