poj 1001 分析

1) n = 0; return 1;

2) n = 1; bool standardizeNumNoDot(string &s){标准化是一定要得} _将‘.’前后的〇全部去除,正常return就ok!

3) s : 整数(前后有〇+有小数点) 小数(前后有〇+只有小数点 eg:0.11 )  处理:1)将 前面的〇去除  2)将 后面的〇去除  3)如果 s.erase() 到了 ‘.’ s = 0 ;

4) string  sPow( string s, int n ) //关键代码

{

string divideStr = sPow(s, n/2);
divideStr = mulStr(divideStr, divideStr);
if (n % 2) divideStr = mulStr(divideStr, s);

}

 

5) string mulStr( string a, string b )//关键代码

{

 

int ap = handleDecimalPoint(a);//后面有介绍
int bp = handleDecimalPoint(b);

string ans(a.size()+b.size(), '0');
for (int i = a.size() - 1; i >= 0 ; i--)
{
int carry = 0;
int an = a[i] - '0';
for (int j = b.size() - 1; j >= 0 ; j--)
{
int bn = b[j] - '0';
int sum = an * bn + carry + ans[i+j+1] - '0';
carry = sum / 10;
ans[i+j+1] = sum % 10 + '0';
}
if (carry) ans[i] += carry;
}
if (ap > 0 || bp > 0) ans.insert(ans.end() - ap - bp, '.');
standardizeNumNoDot(ans);
return ans;

}

6) handleDecimalPoint( string &s )//关键代码

{

for (unsigned i = 0; i < s.size() ; i++)
{
if (fraction > 0) fraction++;
if (s[i] != '.') s[j++] = s[i];
else fraction++;
}
s.erase(s.end()-1);
return fraction - 1;

}

作用:可以得出小数点后面的位数!

 

最后总结: 这个程序对程序的健壮性即适应的情况要求很严格。

      主函数+Exponentiation+StandardizeNumNoDot+sPow+mulStr(handleDecimalPoint)。

注:用到的stl:

  1. #include <string> 
posted @ 2015-03-22 16:51  peerslee2  阅读(101)  评论(0编辑  收藏  举报