c++编程问题:将数字、小数点、正负号组成的字符串转换成相应的实数
1个回答
2013-06-10
展开全部
#include<iostream>
using namespace std;
void read_and_clean(double &n);
int main()
{
double input_number;
read_and_clean(input_number);
cout<<input_number;
}
void read_and_clean(double &n)
{
int flag=1;
cout<<"input number";
const int Arrary_Size=10;
char digit_string[Arrary_Size];
char next;
cin.get(next);
int index=0;
for(;next!='\n';)
{
if(next=='+'||next=='-')
{
if(next=='-')
flag=-1;
cin.get(next);
}
if(next!='+'&&next!='-'&&next!='.'&&!isdigit(next))
{index=-1;break;}
digit_string[index]=next;
index++;
cin.get(next);
}
if(index==-1)
{
cout<<"input error";
}
else
{
digit_string[index]='\0';
n=flag*atof(digit_string);
}
}
using namespace std;
void read_and_clean(double &n);
int main()
{
double input_number;
read_and_clean(input_number);
cout<<input_number;
}
void read_and_clean(double &n)
{
int flag=1;
cout<<"input number";
const int Arrary_Size=10;
char digit_string[Arrary_Size];
char next;
cin.get(next);
int index=0;
for(;next!='\n';)
{
if(next=='+'||next=='-')
{
if(next=='-')
flag=-1;
cin.get(next);
}
if(next!='+'&&next!='-'&&next!='.'&&!isdigit(next))
{index=-1;break;}
digit_string[index]=next;
index++;
cin.get(next);
}
if(index==-1)
{
cout<<"input error";
}
else
{
digit_string[index]='\0';
n=flag*atof(digit_string);
}
}
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询