C语言作业简单计算器
#include <stdio.h>
#include <stdlib.h>
#include<math.h>
double add(double a,double b)
{
return a+b;
}
double sub(double a,double b)
{
return a-b;
}
double mul(double a,double b)
{
return a*b;
}
double division(double a,double b)
{
return a/b;
}
double pow(double a,double b);
int main()
{
while(1){
printf(“请开始计算\n”);
printf(“请输入计算类型:1.加 2.减 3.乘 4.除 5.幂 0.退出\n”);
char types;
int type;
double a,b,result;
scanf("%d",&type);
if(type>=6){
printf(“cuo”);break;
}
if(type==0){
break;
}
printf("请输入第一个数:");
scanf("%lf",&a);
printf("请输入第二个数:");
scanf("%lf",&b);
switch(type)
{
case 1:result=add(a,b);break;
case 2:result=sub(a,b);break;
case 3:result=mul(a,b);break;
case 4:
if(b==0){printf("除数不能为零\n");
}result=division(a,b);break;
case 5:result=pow(a,b);break;
}
printf("结果为:%lf\n",result);
}return 0;
}
用了循环,函数调用,选择结构,变量计算
还有小bug,如果发现,多多指教