跳到正文

个人记录

重载运算符高精加

wangzhiqin约 1 分钟
原文
文章信息与版本

文章信息

发布
2023年9月24日
收录
2025年12月12日
更新
2026年11月14日
篇幅
约 1 分钟

版本

  1. v1当前
C++
#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;
}
原文