From c7bef784804993756654de88a1312897c6238f14 Mon Sep 17 00:00:00 2001 From: pe3p9fgz7 <2137809217@qq.com> Date: Wed, 29 Nov 2023 18:04:56 +0800 Subject: [PATCH] ADD file via upload --- main.c | 72 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 72 insertions(+) create mode 100644 main.c 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; +}