帮忙写个小程序 c++的
来源:百度知道 编辑:UC知道 时间:2024/06/05 05:17:08
(A,B,符号)
得结果
a b为任意数 符号: + - */
输入 A B两个数 和符号
要求RUN出来是:
例子(4.2,3.5,“+")
will return 7.7.
谢谢
我做了个 但是为什么不能运算?各位高手帮我看看有哪些地方出错了吧
#include <iostream>
using namespace std;
int main()
{
float a,b,willreturn;
char d;
cout<<"double simpleCalculator(double a,double b,string operation):";
cin>>a,b,d;
switch(d)
{
case'+':
willreturn=a+b;
break;
case'-':
willreturn=a-b;
break;
case'*':
willreturn=a*b;
break;
case'/':
willreturn=a/b;
break;
default:
willreturn=0;
break;
}
system("pause");
return 0;
}
我才刚刚开始学 所以
得结果
a b为任意数 符号: + - */
输入 A B两个数 和符号
要求RUN出来是:
例子(4.2,3.5,“+")
will return 7.7.
谢谢
我做了个 但是为什么不能运算?各位高手帮我看看有哪些地方出错了吧
#include <iostream>
using namespace std;
int main()
{
float a,b,willreturn;
char d;
cout<<"double simpleCalculator(double a,double b,string operation):";
cin>>a,b,d;
switch(d)
{
case'+':
willreturn=a+b;
break;
case'-':
willreturn=a-b;
break;
case'*':
willreturn=a*b;
break;
case'/':
willreturn=a/b;
break;
default:
willreturn=0;
break;
}
system("pause");
return 0;
}
我才刚刚开始学 所以
对于这种格式化输入的还是用scanf方便些,用流就麻烦了
#include "stdafx.h"
#include <iostream>
double computer()
{
double dV1,dV2,dResult = 0;
char cOperator;
try
{
scanf("(%lf,%lf,\"%c\")",&dV1,&dV2,&cOperator); //(4.2,3.5,“+") ;
}
catch (...)
{
printf("error!");
}
switch(cOperator)
{
case '+':
dResult = dV1 + dV2;
break;
case '-':
dResult = dV1 - dV2;
break;
case '*':
dResult = dV1 * dV2;
break;
case '/':
dResult = dV1 / dV2;
break;
default:
printf("invoid operator!");
break;
}
return dResult;
}
void main()
{
printf("%lf",computer());
}
#include <stdio.h>
float Result(float a,float b,char c)
{
if(c=='