#include <string> #include <cstdio> #include <iostream> #include <sstream> using namespace std; struct num{ string before; string behind; }; string opstring(string h,string r){ int rlen=r.length(); int hlen=h.length(); int flag=0; int pos=0; for(int i=rlen-1;i>=0;i--){ if(r[i]-'0'!=0){ flag=1; pos=i; break; } } if(!flag){ return h; } else{ // cout<<r.substr(0,pos)<<endl; return h+"."+r.substr(0,pos+1); } } int main(){ string s1,s2; while(cin>>s1>>s2){ stringstream ss1(s1); stringstream ss2(s2); string temp; num a,b; int count1=0; while(getline(ss1,temp,'.')){ if(count1==0){ a.before=temp; count1++; } else if(count1==1){ a.behind=temp; } } int count2=0; while(getline(ss2,temp,'.')){ if(count2==0){ b.before=temp; count2++; } else if(count2==1){ b.behind=temp; } } int ahlen=a.behind.length(); int bhlen=b.behind.length(); if(ahlen>bhlen){ int deta=ahlen-bhlen; for(int i=0;i<deta;i++){ b.behind+="0"; } bhlen=ahlen; } if(ahlen<bhlen){ int deta=bhlen-ahlen; for(int i=0;i<deta;i++){ a.behind+="0"; } ahlen=bhlen; } num res; int add=0; for(int i=ahlen-1;i>=0;i--){ int aa=a.behind[i]-'0'; int bb=b.behind[i]-'0'; int kk=aa+bb+add; if(kk>9){ add=kk/10; } else{ add=0;//位清空 } res.behind=to_string(kk%10)+res.behind; } int aqlen=a.before.length(); int bqlen=b.before.length(); if(aqlen>bqlen){ int deta=aqlen-bqlen; for(int i=0;i<deta;i++){ b.before="0"+b.before; } bqlen=aqlen; } if(aqlen<bqlen){ int deta=bqlen-aqlen; for(int i=0;i<deta;i++){ a.before="0"+a.before; } aqlen=bqlen; } for(int i=aqlen-1;i>=0;i--){ int aa=a.before[i]-'0'; int bb=b.before[i]-'0'; int kk=aa+bb+add; if(kk>9){ add=kk/10; } else{ add=0;//位清空 } res.before=to_string(kk%10)+res.before; if(i==0&&add!=0){ res.before=to_string(add)+res.before; } } // cout<<res.before<<":"<<res.behind; string final=opstring(res.before,res.behind); cout<<final<<endl; } return 0; } |
Double click to view unformatted code.