#include
#include
#include
#include
using namespace std;
string convertToTrue(string str){
string result;
string::reverse_iterator endIndex;//记录字符串后面不为*的位置;
string::iterator beginIndex;
int count1=0,count2=0;
bool potflag;
string::size_type potIndex=str.find('.');//查询小数点的位置
if(potIndex!=str.npos) potflag=true;
else potflag=false;
for(string::reverse_iterator begin=str.rbegin();begin
//逆序查找字符串,为去除后面无用的0,记录字符串末尾有效位置
if(*begin!='*') break;
count1++;
}
for(string::iterator begin=str.begin();begin
//正序查找字符串,为去除前面无用的0,记录字符串开始有效位置
if(*begin!='*') break;
count2++;
}
int length=strlen(str.c_str());
if(potflag==true){
if(potIndex==1)
result=str.substr(0,length-count1);
else{
length=length-count1-count2;
if(potIndex==count2){
count2--;
length=length+1;
}
result=str.substr(count2,length);
}
}else{
result=str.substr(count2);
}
for(string::iterator begin=result.begin();begin
//将*转换到0
if((*begin)=='*')
*begin='0';
}
return result;
}
double convertToInt(string str){
//将浮点字符串转换到对应的整形
int i,j;
double sum=0;//保存最后得到的整形
string::size_type potIndex;//小数点对应的位置
stack stk;
potIndex=str.find('.');
if(potIndex!=str.npos){//字符串中有小数点
for(i=potIndex-1;i>=0;--i){//计算整形部分的值
sum=pow(10.0,(potIndex-i-1)*1.0)*(str.at(i)-48)+sum;
}
double temp=0.1;
for(i=potIndex+1;i
sum=temp*((double)(str.at(i)-48))+sum;
temp=0.1*temp;
}
}else{//字符串中没有小数点
i=0;
for(string::reverse_iterator begin=str.rbegin();begin
sum=pow(10.0,i)*(*begin-48)+sum;
}
}
return sum;
}
int main(){
string str="1**";
string strNum=convertToTrue(str);
cout<
}
发表于 2015-06-16 17:23:10
回复(0)