重载运算符高精加

· · 个人记录

#include <iostream>
using namespace std;
string operator+(string &p1 ,string &p2){//重载+号
    string str;
    int len1=p1.length();
    int len2=p2.length();
    if(len1<len2){
        for(int i=1;i<=len2-len1;i++)p1="0"+p1;
    }else{
        for(int i=1;i<=len1-len2;i++)p2="0"+p2;
    }
    len1=p1.length();
    int cf=0,temp;
    for(int i=len1-1;i>=0;i--){
        temp=p1[i]-'0'+p2[i]-'0'+cf;
        cf=temp/10;
        temp%=10;
        str=char(temp+'0')+str;
    }
    if(cf!=0)str=char(cf+'0')+str;
    return str;//朴素高精加
}
int main(){
    string p1,p2;
    cin>>p1>>p2;
    cout<<p1+p2;
    return 0;
}