沐里纷纷


私信TA

用户名:Epoch

访问量:63676

签 名:

我不会算法

等  级
排  名 37
经  验 12906
参赛次数 1
文章发表 172
年  龄 0
在职情况 学生
学  校
专  业

  自我简介:

不会算法

解题思路:

深夜背模板真开心~

有了对象还是要面向对象啊~

我对象最好!

注意事项:

参考代码:

#include <iostream>
#include <algorithm>
#include <string>
#include <stdio.h>
#include <memory.h>

#define N 10000

using namespace std;

class BigInt
{
public:
	BigInt();
	BigInt(string numStr);
	void showInt();
	~BigInt();
	friend BigInt operator+(BigInt& a, BigInt& b);

private:
	int d[N];
	int len;
};

BigInt::BigInt() 
{ 
	len = 0;
	memset(d, 0x00, sizeof(d));
}

BigInt::BigInt(string numStr)
{
	int length = numStr.length();
	this->len = length;
	memset(d, 0x00, sizeof(d));
	for (int i = 0; i < length; i++)
		d[i] = numStr[length - i - 1] - '0';
}

BigInt::~BigInt() { }

void BigInt::showInt()
{
	for (int i = this->len - 1; i >= 0; i--)
		cout << d[i];
	cout << endl;
}

BigInt operator+(BigInt& a, BigInt& b)
{
	BigInt c;
	c.len = a.len > b.len ? a.len : b.len;
	int i;
	for (i = 0; i < c.len; i++)
	{
		c.d[i] += a.d[i] + b.d[i];
		c.d[i + 1] = c.d[i] / 10;
		c.d[i] %= 10;
	}
	c.len = (c.d[i] ? i + 1 : i);
	return c;
}

int main(void)
{
	string strA, strB;
	cin >> strA >> strB;
	BigInt a(strA);
	BigInt b(strB);
	BigInt c = a + b;
	c.showInt();
	return 0;
}


 

0.0分

0 人评分

看不懂代码?想转换其他语言的代码? 或者想问其他问题? 试试问问AI编程助手,随时响应你的问题:

编程语言转换

万能编程问答  

代码解释器

代码纠错

SQL生成与解释

  评论区