计算机算出来的结果有过错误吗,计算器出错:把所有的‘0’都变成了&

#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)

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值