计算器的一般实现:
#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; } int main() { int input = 1; int a, b, ret; do { printf("*****************************\n"); printf("**1.add 2.sub ***********\n"); printf("**3.mul 4.div ***********\n"); printf("**0.exit ***********\n"); printf("*****************************\n"); scanf("%d", &input); switch (input) { case 1: printf("请输入操作数:"); scanf("%d %d", &a, &b); ret = add(a, b); printf("ret=%d\n", ret); break; case 2: printf("请输入操作数:"); scanf("%d %d", &a, &b); ret = sub(a, b); printf("ret = %d\n", ret); break; case 3: printf("请输入操作数:"); scanf("%d %d", &a, &b); ret = mul(a, b); printf("ret = %d\n", ret); break; case 4: printf("请输入操作数:"); scanf("%d %d", &a, &b); ret = div(a, b); printf("ret = %d\n", ret); break; case 0: printf("退出游戏"); break; default: printf("选择错误"); break; } } while (input); return 0; }