#include <iostream>
#include <string>
using namespace std;
class BigInt
{
private:
string bigint;
public:
BigInt(string a="") { bigint= a; }
friend ostream & operator<< (ostream &os, const BigInt &a) {
os << a.bigint;
return os; }
friend istream& operator>>(istream &is, BigInt &x)//輸入
{
is >> x.bigint;
return is;
}
BigInt operator +(BigInt &a)
{
string templong;
string tempshort;
bool jinwei=false;
string sum;
if (a.bigint.size() > bigint.size())
{
templong = a.bigint;
tempshort = bigint;
}
else {
templong = bigint;
tempshort = a.bigint;
}for (int i =1 ; i <=tempshort.size(); i++)
{
int l, s;
l = templong[templong.size() - i] - '0';
s = tempshort[tempshort.size() - i] - '0';
if (jinwei) {
jinwei = false;
if (s+l+1> 9) {
jinwei = true;
sum = to_string((s+l+1)% 10) + sum;
}
else { sum = to_string((s+l+1)) + sum; }
}
else {
if ((s+l)> 9) {
jinwei = true;
sum = to_string((s+l) % 10) + sum;
}
else { sum =to_string((s+l)) + sum; }
}
}
for (int i = tempshort.size()+1; i <= templong.size(); i++)
{
int l;
l = templong[templong.size() - i] - '0';
if (jinwei)
{ jinwei = false;
if ((l + 1) > 9)
{
jinwei = true;
sum = to_string((l+1) % 10) + sum;
}
else
{
sum = to_string(l + 1) + sum;;
}
}
else
{
sum = to_string(l) + sum; }
}
return BigInt(sum);
}
};
int main() {
BigInt a, b, c;
cin >> a >> b;
c = a + b;
cout << a << "+" << b << "=" << c << endl;
return 0;
}
我在pta的時候這個題目顯示我部分正確,但是我想不到錯誤的地方。。。求大佬糾一下錯。。。
uj5u.com熱心網友回復:
至少說明一下這個問題的背景啊,否則不會有人有時間幫你除錯代碼的uj5u.com熱心網友回復:
就是實作輸入兩個可以大于整數型大小的數字(即作為字串輸入),然后按位置加減,同時運用邏輯性整數進行進位運算....初來乍到,問題不完善不好意思啊....
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/45983.html
標籤:C++ 語言
下一篇:有大佬會寫這個代碼嘛,可太難了
