PAT B1024/A1073 科学计数法(纯字符串string操作)

本人计算机萌新,初学c++将该题作为stl中的string各种操作的练习
写的较为繁琐,但起到了练习和理解string的操作的作用

#include
#include
using namespace std;
int main()
{
int mark=100000,z=0,k,dotp,ep,cha;
char p1,p2;
string str,str2=".",str3=“0”,str4=“E”;
cin>>str;
p1=str[0];
string::iterator it=str.begin();
for(int i=0;i<=str.size()-1;i++)
{
if(str[i]‘E’)
{ p2=str[i+1];
mark=i+1;//e后的符号由p2存储,标记符号的位置,后面的为指数
}
if(i>mark)
{
k=str[i]-‘0’;
z=z*10+k;
}
}
if(p1
’+’)
str.erase(0,1);//delete+
else
{
cout<<"-";
str.erase(0,1);//delete-
}
if(p2==’-’)
{
dotp=str.find(str2);//找到.的位置
str.erase(dotp,1);
cout<<“0.”;
while(z–>1)
str.insert(0,str3);//insert zero after dot
ep=str.find(str4);//find the position of E
str.erase(ep,str.size()-ep);//erase the string after E
cout<<str;
}
else
{
ep=str.find(str4);
str.erase(ep,str.size()-ep);//erase the string after E
dotp=str.find(str2);//find dotp
cha=str.size()-dotp-1;//cha is the number of number after dot;
str.erase(dotp,1);
z-=cha;//the nmuber of ‘0’ need to insert
while(z–>0)
str.insert(str.size(),str3);
cout<<str;
}
return 0;
}

猜你喜欢

转载自blog.csdn.net/thelastsinger/article/details/88750171