用c语言编写计算器

/** *@Brief: 使用c语言编写计算器 *@Author:icepan *@Date:2019/10/29 **/

#include <stdio.h>

float GetResult(float a, char op, float b) { switch(op) { case '+': return a+b; case '-': return a-b; case '': return ab; case '/': return a/b; default: printf("wrong operator!\n"); return 0; } return 0; }

int main() { float a = 0; float b = 0; char op = 0; float result = 0;

printf("Welcome to use a simple calculator!\n");
printf("Please enter two numbers and an operator(like this: 1+2):");
scanf("%f%c%f", &a, &op, &b); //接收输入的数字和符号

result = GetResult(a, op, b); //计算结果

printf("the result is: %f\n", result); //输出结果

return 0;

以上就是关于“用c语言编写计算器”问题的最佳回复,相信您已经得到自己需要的答案了,马上开始行动吧!

用c语言编写计算器

发表评论

您的电子邮箱地址不会被公开。 必填项已用*标注