1#include <iostream>2#include <string>3#include <algorithm>4 using namespacestd;5 intMain () {6 stringstr1;7 stringstr2;8 while(cin>>str1>>str2) {9 intSize1 = Str1.size ()-1;Ten intSize2 = Str2.size ()-1; One intFlag =0; A for(intI=0; i<=size1;i++){ - if(! (str1[i]>='0'&& Str1[i] <='9')){ -cout<<"Error"<<Endl; theFlag =1; - Break; - } - } + if(flag) Break; - for(intI=0; i<=size2;i++){ + if(! (str2[i]>='0'&& Str2[i] <='9')){ Acout<<"Error"<<Endl; atFlag =1; - Break; - } - } - if(flag) Break; - stringsum; in intcarry =0; - intj =0; to while(size1>=0&& size2>=0){ + intNsum = (Str1[size1]-'0') + (Str2[size2]-'0') +carry; -carry = nsum/Ten; theNsum = nsum%Ten; *Sum + =to_string (nsum); $size1--;Panax Notoginsengsize2--; - } the while(Size1 >=0){ + intNsum = (Str1[size1]-'0') +carry; Acarry = nsum/Ten; theNsum = nsum%Ten; +Sum + =to_string (nsum); -size1--; $ } $ while(Size2 >=0){ - intNsum = (Str2[size2]-'0') +carry; -carry = nsum/Ten; theNsum = nsum%Ten; -Sum + =to_string (nsum);Wuyisize2--; the } - if(carry) { WuSum + ='1'; - } About Reverse (Sum.begin (), Sum.end ()); $cout<<sum<<Endl; - } -}
Please design an algorithm that can complete the addition of two integers stored in a string, and return an error to an illegal input