# 利用回调函数实现一个计算器，实现加减乘除操作

``#define _CRT_SECURE_NO_WARNINGS #include<stdio.h> int Add(int x, int y) { return x + y; } int Sub(int x, int y) { return x - y; } int Mul(int x, int y) { return x * y; } int Div(int x, int y) { return x / y; } void menu() { printf("*************************\n"); printf("******1.Add 2.Sub*****\n"); printf("******3.Mul 4.Div*****\n"); printf("****** 0.exit ********\n"); printf("*************************\n"); } int calc(int(*pfun)(int, int)) //函数指针作为参数，pfun将会接收到形参（函数）的地址 { int x = 0; int y = 0; printf("请输入两个要参与计算的数>:"); scanf("%d %d", &x, &y); return pfun(x, y); } int main() { menu(); int input = 0; do { printf("请输入序号>:"); scanf("%d", &input); int ret = 0; switch (input) { case 1: ret = calc(Add); printf("ret=%d\n", ret); break; case 2: ret = calc(Sub); printf("ret=%d\n", ret); break; case 3: ret = calc(Mul); printf("ret=%d\n", ret); break; case 4: ret = calc(Div); printf("ret=%d\n", ret); break; case 0: break; default: printf("选择错误\n"); break; } } while (input); system("pause"); return 0; }``

return 0;
}

原文作者：weixin_37922654
原文地址: https://blog.csdn.net/weixin_37922654/article/details/71750855
本文转自网络文章，转载此文章仅为分享知识，如有侵权，请联系博主进行删除。