diff --git a/main.c b/main.c new file mode 100644 index 0000000..5adf360 --- /dev/null +++ b/main.c @@ -0,0 +1,72 @@ +#include +// 加法函数 +double add(double a, double b) +{ + return a+b; +} +// 减法函数 +double subtract(double a, double b) +{ + return a-b; +} +// 乘法函数 +double multiply(double a, double b) +{ + return a*b; +} +// 除法函数 +double divide(double a, double b) +{ + if(b == 0) + { + printf("Error: 被除数不能为零!\n"); + return 0; + } + else + { + return a/b; + } +} +// 求模函数 +int mod(int a,int b) +{ + if(b==0) + { + printf("Error: 被除数不能为零!\n"); + return 0; + } + else + { + return a%b; + } +} +int main() +{ + double a, b, result; + char operator; + printf("请输入所要计算的式 (eg:1 + 2): "); + scanf("%lf%c%lf",&a,&operator,&b); + switch (operator) + { + case '+': + result=add(a,b); + break; + case '-': + result=subtract(a,b); + break; + case '*': + result=multiply(a,b); + break; + case '/': + result=divide(a,b); + break; + case '%': + result=mod((int)a,(int)b); + break; + default: + printf("Error:请重新输入!\n"); + return 1; + } + printf("结果为:%.2lf",result); + return 0; +}