程序设计题(是C++)

来源:百度知道 编辑:UC知道 时间:2024/06/20 13:49:41
编写一个程序,实现如下功能:
(1) 从键盘输入a op b。其中a,b为数值:op为字符,限制为+、-、*,/。
(2) 调用函数count(op,a,b),计算表达式a op b的值,由主函数输出结果。

#include "stdafx.h"
#include<iostream>
#include <string>
using namespace std;
float count(char op,float a,float b);
int main()
{
char op;
float a,b,result;
cout<<"请输入表达式如2/3!!!"<<endl;
cin>>a>>op>>b;
cout<<a<<op<<b<<"=";
result=count(op,a,b);
cout<<result<<endl;
return 0;
}
float count(char op,float a,float b)
{
if (op=='+' )
return (a+b);

if (op=='-' )
return a-b;

if (op=='*' )
return a*b;

if (op=='/')
{
if(b==0)
{
cout<<endl<<"0不能做除数!!!"<<endl;
return 1;
}
else
return a/b;
}
}
楼上做的很好,就是函数调用有点小问题。。。

#include<iostream>
#include <string>
using namespace std;
floa