C++帮助,#includevoid getOption(int *option);void getNumbers(int *option,float *number1,float *number2);float calc(int *option,float *number1,float *number2);void printResult(float number1,float number2,float result,int option);main(){float outcome;
来源:学生作业帮助网 编辑:作业帮 时间:2024/11/17 12:40:53
C++帮助,#includevoid getOption(int *option);void getNumbers(int *option,float *number1,float *number2);float calc(int *option,float *number1,float *number2);void printResult(float number1,float number2,float result,int option);main(){float outcome;
C++帮助,
#include
void getOption(int *option);
void getNumbers(int *option,float *number1,float *number2);
float calc(int *option,float *number1,float *number2);
void printResult(float number1,float number2,float result,int option);
main()
{
float outcome;
float number1,number2;
int option;
do
{ getOption (&option);
if (option = 5)
{
getNumbers(&option,&number1,&number2);
outcome=calc(&option,&number1,&number2);
printResult(number1,number2,outcome,option);
}
} while ( option!=5);
}
void getOption(int *option)
{
printf(":\n");
scanf("%f",number1);
scanf("%f",number2);
if (*option==4 && *number2==0)
printf("除数不能为零!\n");
} while(*option==4 && *number2==0);
}
float calc(int *option,float *number1,float *number2)
{
float result;
switch(*option)
{ case 1:result=*number1+ *number2; break;
case 2:result=*number1- *number2; break;
case 3:result=*number1* *number2; break;
case 4:result=*number1/ *number2; break;
}
return result;
}
void printResult(float number1,float number2,float result,int option)
{
switch (option)
{
case 1:printf("%.2f + %.2f = %.2f\n",number1,number2,result); break;
case 2:printf("%.2f - %.2f = %.2f\n",number1,number2,result); break;
case 3:printf("%.2f * %.2f = %.2f\n",number1,number2,result); break;
case 4:printf("%.2f / %.2f = %.2f\n",number1,number2,result); break;
}
}
C++帮助,#includevoid getOption(int *option);void getNumbers(int *option,float *number1,float *number2);float calc(int *option,float *number1,float *number2);void printResult(float number1,float number2,float result,int option);main(){float outcome;
#include
//:\n");
scanf("%f",number1);
scanf("%f",number2);
if (*option==4 && *number2==0) //意思为option为4(除号),并除数为0,不能计算
printf("除数不能为零!\n");
} while(*option==4 && *number2==0); //直到输入的number2不为0 退出循环
}
float calc(int *option,float *number1,float *number2) //调用此函数是为了计算
{
float result;
switch(*option) //根据option指向的数据值(即一个1-4的int值),确定下一步
{ case 1:result=*number1+ *number2; break; //case1-4 就是分别加减乘除的计算
case 2:result=*number1- *number2; break;
case 3:result=*number1* *number2; break;
case 4:result=*number1/ *number2; break;
}
return result; //返回结果
}
void printResult(float number1,float number2,float result,int option) //输出结果 很简单不用介绍了吧
{
switch (option) //根据运算符 选择输出形式
{
case 1:printf("%.2f + %.2f = %.2f\n",number1,number2,result); break;
case 2:printf("%.2f - %.2f = %.2f\n",number1,number2,result); break;
case 3:printf("%.2f * %.2f = %.2f\n",number1,number2,result); break;
case 4:printf("%.2f / %.2f = %.2f\n",number1,number2,result); break;
}
}
还有不懂的消息我~