计算A+B

一个比较简单的程序题。题目虽简单,但写起来需要注意的东西还是很多的。

题目:读入两个小于100的正整数A和B,计算A+B.需要注意的是:A和B的每一位数字由对应的英文单词给出.

输入: 测试输入包含若干测试用例,每个测试用例占一行,格式为"A + B =",相邻两字符串有一个空格间隔.当A和B同时为0时输入结束,相应的结果不要输出.
输出:
对每个测试用例输出1行,即A+B的值.
样例输入:
one + two = 
three four + five six = 
zero seven + eight nine = 
zero + zero =
样例输出:
3 90 96

下面是我写的一段程序:

#include <iostream>
#include < string>
#include <cstdlib>
#include <sstream>
using  namespace std;
const  string digits[ 10]={ " zero ", " one ", " two ", " three ", " four ", " five ", " six ", " seven ", " eight ", " nine "};
string& trim( string& str){
     if(str.empty())
         return str;
    str.erase( 0,str.find_first_not_of( '   '));
    str.erase(str.find_last_not_of( '   ')+ 1);
    
     return str;
}
int index( const  string& s){
     for( int i= 0;i< 10;i++)
         if(s==digits[i])
             return i;
     return - 1;
}
int str2int( const  string& s){
     int n= 0;
    size_t pos= 0;
    size_t pos2=s.find( '   ');
     string sub;
     int di;
     while(pos2!= string::npos){
        sub=s.substr(pos,pos2-pos);
//         cout<<sub<<endl;
        di=index(sub);
        n=n* 10+di;
        pos=pos2+ 1;
        pos2=s.find( '   ',pos);
    }
    sub=s.substr(pos);
//     cout<<sub<<endl;
    di=index(sub);
    n=n* 10+di;
     return n;
}
int add_str( string str){
    size_t pos,pos2;
    pos=str.find( ' + ');
     string s1=str.substr( 0,pos);
    pos2=str.find( ' = ');
     string s2=str.substr(pos+ 1,pos2-(pos+ 1));
    trim(s1);trim(s2);
     int a1=str2int(s1);
     int a2=str2int(s2);

     return a1+a2;
}
string int2str( int a){
    ostringstream  out;
     out<<a;
     string s= out.str();
     string s2;
     string::iterator ite;
     for(ite=s.begin();ite!=s.end();++ite){
        s2+=digits[*ite- ' 0 '];
        s2+= '   ';
    }
     return s2;
}
int test1(){
     for( int i= 0;i< 10;i++){
         int a1=rand()% 1000;
         int a2=rand()% 1000;
         int sum=a1+a2;
         string s1=int2str(a1);
         string s2=int2str(a2);
         string str=s1+ "  +  "+s2+ "  =  ";
         int sum2=add_str(str);
        cout<<a1<< '   '<<a2<< "  < "<<str<< " "<<sum<< "   "<<sum2;
         if(sum==sum2)
            cout<< "  OK "<<endl;
         else
            cout<< "  NO "<<endl;

    }

     return  0;
}
int main1(){
     string str;
     int sum;
     while(getline(cin,str)){
        sum=add_str(str);
        cout<<sum<<endl;
    }
     return  0;
}
int main(){
    test1();
}

下面是测试结果:

 

 

 

转载于:https://www.cnblogs.com/xkfz007/archive/2012/07/16/2594151.html

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

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值