用字符串的形式输入两个数字,编写函数将两个字符串转换为数值,并输出这两个数和差积商的值,该怎么做?
例1,输入:请输入两个数字:3.52.82输出:3.5+2.82=6.323.5-2.82=0.683.5*2.82=9.873.5/2.82=1.24113...
例1,
输入:
请输入两个数字:
3.5
2.82
输出:
3.5+2.82=6.32
3.5-2.82=0.68
3.5*2.82=9.87
3.5/2.82=1.24113 展开
输入:
请输入两个数字:
3.5
2.82
输出:
3.5+2.82=6.32
3.5-2.82=0.68
3.5*2.82=9.87
3.5/2.82=1.24113 展开
1个回答
2016-06-19
展开全部
你这是什么语言做啊。。。。
c++:
#include <iostream>
#include<sstream>
using namespace std;
double getDouble(string str);
void main(){
string str1="",str2="";
double num1 = 0, num2 = 0;
cout<<"输入:\n请输入两个数字:\n";
cin>>str1;
cin>>str2;
num1 = getDouble(str1);
num2 = getDouble(str2);
if(num2 == 0){
cout<<"输出:\n"
<<num1<<"+"<<num2<<"="<<num1+num2<<endl
<<num1<<"-"<<num2<<"="<<num1-num2<<endl
<<num1<<"*"<<num2<<"="<<num1*num2<<endl
<<"0不能为除数";
}else{
cout<<"输出:\n"
<<num1<<"+"<<num2<<"="<<num1+num2<<endl
<<num1<<"-"<<num2<<"="<<num1-num2<<endl
<<num1<<"*"<<num2<<"="<<num1*num2<<endl
<<num1<<"/"<<num2<<"="<<num1/num2<<endl;
}
system("pause");
}
double getDouble(string str){
double d = 0;
stringstream ss;
ss<<str;
if(ss>>d){
return d;
}else{
cout<<"输入错误的参数,结果未知"<<endl;
return 0;
}
}
c++:
#include <iostream>
#include<sstream>
using namespace std;
double getDouble(string str);
void main(){
string str1="",str2="";
double num1 = 0, num2 = 0;
cout<<"输入:\n请输入两个数字:\n";
cin>>str1;
cin>>str2;
num1 = getDouble(str1);
num2 = getDouble(str2);
if(num2 == 0){
cout<<"输出:\n"
<<num1<<"+"<<num2<<"="<<num1+num2<<endl
<<num1<<"-"<<num2<<"="<<num1-num2<<endl
<<num1<<"*"<<num2<<"="<<num1*num2<<endl
<<"0不能为除数";
}else{
cout<<"输出:\n"
<<num1<<"+"<<num2<<"="<<num1+num2<<endl
<<num1<<"-"<<num2<<"="<<num1-num2<<endl
<<num1<<"*"<<num2<<"="<<num1*num2<<endl
<<num1<<"/"<<num2<<"="<<num1/num2<<endl;
}
system("pause");
}
double getDouble(string str){
double d = 0;
stringstream ss;
ss<<str;
if(ss>>d){
return d;
}else{
cout<<"输入错误的参数,结果未知"<<endl;
return 0;
}
}
更多追问追答
追问
这个和C语言有什么区别吗
可以用c做吗,大神 求救啊
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询