Homework12_ch8 多态——运算符重载(2)

  1. 人民币的排序

题目描述如下:

设计人民币类,类设计和运算符重载参看homework11. 增加重载比较运算符:小于 <、大于 >、小于等于 <=、大于等于>= 、不等于 !=、等于 == .

功能说明:

在主程序中:

  1. 10个人民币:

+ 1 2 3 + 22 33 44+ 0 0 0- 100 0 2 + 77 6 0+ 207 8 9- 60 56 3+ 88 8 8 -203 45 3+ 95 6 6

这些数据是原始数据,有些需要格式化(进位)。在程序中请使用格式化的人民币。

  1. 请使用数组保存这10个人民币数据

3.  按升序排序并输出排序结果

//Money.h
#pragma once
#include<iostream>
using namespace std;
class Money {
public:
	Money(char c = '+', int y = 0, int j = 0, int f = 0);
	void standard();
	Money operator+(const Money& m);
	Money operator-(const Money& m);
	Money& operator++();
	Money operator ++(int);
	bool operator <(const Money& m);
	bool operator >(const Money& m);
	bool operator <=(const Money& m);
	bool operator >=(const Money& m);
	bool operator !=(const Money& m);
	bool operator ==(const Money& m);
	friend Money& operator--(Money& );
	friend Money operator--(Money& , int);
	friend ostream& operator << (ostream& os, const Money& m);
	friend istream& operator >>(istream& os, Money& m);
	void Show();
	void Set(char c = '+', int y = 0, int j = 0, int f = 0);
private:
	char sign;
	int yuan;
	int jiao;
	int fen;
};
//Money.cpp
#include"Money.h"
#include<iostream>
using namespace std;
Money::Money(char c, int y, int j, int f)
{
	sign = c;
	yuan = y;
	jiao = j;
	fen = f;
}
void Money::standard()
{
	jiao += fen / 10;
	fen %= 10;
	yuan += jiao / 10;
	jiao %= 10;
}

Money Money::operator+(const Money& m)
{
	Money mo;
	if (sign == m.sign) {
		mo.yuan = yuan + m.yuan;
		mo.jiao = jiao + m.jiao;
		mo.fen = fen + m.fen;
		mo.standard();
	}
	else {
		if (sign == '-') {
			Money tmp('+', yuan, jiao, fen);
			Money t = m;
			mo = t - tmp;
		}
		else {
			Money tmp('+', m.yuan, m.jiao, m.fen);
			mo = *this - tmp;
		}
	}
	return mo;
}

Money Money::operator-(const Money& m)
{
	Money mo;
	Money tmp = m;
	if (sign != m.sign) {
		if (m.sign == '-') {
			tmp.sign = '+';
		}
		else {
			tmp.sign = '-';
		}
		return *this + tmp;
	}
	if (yuan > m.yuan || (yuan == m.yuan && jiao > m.jiao) || (yuan == m.yuan && jiao == m.jiao && fen >= m.fen))
	{
		mo = *this; tmp = m;
	}
	else { mo = m; tmp = *this; mo.sign = (sign == '+') ? '-' : '+'; }
	if (mo.fen < tmp.fen) {
		mo.jiao--;
		mo.fen += 10;
	}
	mo.fen -= tmp.fen;
	if (mo.jiao < 0 || mo.jiao < tmp.jiao) {
		mo.yuan--;
		mo.jiao += 10;
	}
	mo.jiao -= tmp.jiao;
	mo.yuan -= tmp.yuan;
	return mo;
}

Money& Money::operator++() {    //前置
	Money m('+', 0, 0, 1);
	*this = *this + m;
	return *this;
}
Money Money::operator++(int) {
	Money m = *this;
	++(*this);
	return m;
}
Money& operator--(Money& x)
{
	Money m('+', 0, 0, 1);
	x = x - m;
	return x;
}

Money operator--(Money& x, int) {
	Money m = x;
	--(x);
	return m;
}
ostream& operator<<(ostream& os, const Money& m)
{
	os << m.sign << m.yuan << "元" << m.jiao << "角" << m.fen << "分";
	return os;
}
istream& operator>>(istream& os, Money& m)
{
	char c;
	int y, j, f;
	os >> c >> y >> j >> f;
	m.sign = c;
	m.yuan = y;
	m.jiao = j;
	m.fen = f;
	m.standard();
	return os;
}
void Money::Show()
{
	cout << sign << yuan << "元" << jiao << "角" << fen << "分" << endl;
}
bool Money::operator<(const Money& m)
{
	int f = 0;
	if (sign != m.sign) return sign > m.sign;
	if (sign == '-') f = 1;
	if (yuan != m.yuan) return (yuan < m.yuan) ^ f;
	if (jiao != m.jiao) return (jiao < m.jiao) ^ f;
	return (fen < m.fen) ^ f;
}
bool Money::operator>(const Money& m) {
	int f = 0;
	if (sign != m.sign) return sign < m.sign;
	if (sign == '-') f = 1;
	if (yuan != m.yuan) return (yuan > m.yuan) ^ f;
	if (jiao != m.jiao) return (jiao > m.jiao) ^ f;
	return (fen > m.fen) ^ f;
}
bool Money::operator<=(const Money& m) {
	if (sign == m.sign && yuan == m.yuan && jiao == m.jiao && fen == m.fen) return true;
	int f = 0;
	if (sign != m.sign) return sign > m.sign;
	if (sign == '-') f = 1;
	if (yuan != m.yuan) return (yuan < m.yuan) ^ f;
	if (jiao != m.jiao) return (jiao < m.jiao) ^ f;
	return (fen < m.fen) ^ f;
}
bool Money::operator>=(const Money& m) {
	if (sign == m.sign && yuan == m.yuan && jiao == m.jiao && fen == m.fen) return true;
	int f = 0;
	if (sign != m.sign) return sign < m.sign;
	if (sign == '-') f = 1;
	if (yuan != m.yuan) return (yuan > m.yuan) ^ f;
	if (jiao != m.jiao) return (jiao > m.jiao) ^ f;
	return (fen > m.fen) ^ f;
}
bool Money::operator!=(const Money& m) {
	if (sign != m.sign) return true;
	if (yuan != m.yuan) return true;
	if (jiao != m.jiao) return true;
	if (fen != m.fen) return true;
	return false;
}
bool Money::operator==(const Money& m) {
	if (sign != m.sign) return false;
	if (yuan != m.yuan) return false;
	if (jiao != m.jiao) return false;
	if (fen != m.fen) return false;
	return true;
}
void Money::Set(char c, int y, int j, int f)
{
	sign = c;
	yuan = y;
	jiao = j;
	fen = f;
	this->standard();
}
//main.cpp
#include"Money.h"
#include<iostream>
#include<algorithm>

#include<stdlib.h>
using namespace std;
int n;
int main() {
	cin >> n;
	Money *m = new Money[n];
	char s;
	int y, j, f;
	cout << "input " << n << " RMBs:" << endl;
	for (int i = 0; i < n; i++) {
		cin >> s >> y >> j >> f;
		m[i].Set(s, y, j, f);
		//cout << m[i] << endl;
	}
	sort(m, m + n);
	cout << "sorted:" << endl;
	for (int i = 0; i < n; i++) {
		cout << m[i] << endl;
	}
	return 0;
}

 

  1. 人民币的排序_文件输入输出

题目描述如下:

 基本要求请看1. 必做题。只是把需要排序的若干人民币数据存放在文件中rmbin.txt.  排序后的结果也存放在文件中rmbout.txt .

rmbin.txt:  存放着数字100100个待排序的人民币。

100

+ 1 2 3 + 22 33 44 + 0 0 0 - 100 0 2 + 77 6 0 + 207 8 9 - 60 56 3 + 88 8 8 -203 45 3 + 95 6 6

+ 45 6 7 - 98 1 1 + 53 23 1 + 567 35 0 - 0 0 0 - 123 3 8 +99 0 0 - 54 7 6 - 9 8 7 + 267 12 45

- 24 7 5 + 62 64 5 + 45 81 27 + 61 9 9 + 2 53 92 - 82 21 16 + 1 95 4 - 26 7 8 + 94 3 11 - 22 33 73

- 64 4 1 + 53 6 4 + 23 41 29 - 78 6 5 + 90 42 88 - 6 40 42 - 29 70 50 + 6 1 93 + 48 29 23 - 84 5 6

+ 44 3 2 + 23 3 3 + 18 2 9 + 41 33 15 - 6 73 6 - 21 45 24 - 72 0 29 + 77 3 97 - 55 67 55 + 74 31 52

- 50 50 41 - 24 66 30 - 87 5 3 - 45 9 9 + 58 1 88 + 22 6 6 + 62 55 0 + 59 4 37 - 48 83 95 + 41 2 50

+ 21 48 9 + 68 8 81 + 34 5 9 - 18 38 0 - 48 83 7 - 21 10 17 + 13 14 9 + 16 35 51 - 3 2 8 + 44 9 89

- 2 9 8 - 93 4 2 - 58 18 80 + 96 8 8 + 89 8 9 + 57 72 22 - 57 58 91 + 15 8 5 - 11 2 34 + 72 55 28

+ 69 2 4 - 16 8 9 + 22 51 21 - 99 57 76 - 10 3 69 + 61 88 1 + 89 55 23 + 2 85 82 - 3 3 6 - 54 21 8

+ 76 29 68 - 92 2 5 + 60 18 53 - 3 3 9 - 96 87 2 + 49 37 66 + 86 5 88 - 82 5 3 - 14 1 16 + 71 86 63

//Money.h
#pragma once
#include<iostream>
using namespace std;
class Money {
public:
	Money(char c = '+', int y = 0, int j = 0, int f = 0);
	void standard();
	Money operator+(const Money& m);
	Money operator-(const Money& m);
	Money& operator++();
	Money operator ++(int);
	bool operator <(const Money& m);
	bool operator >(const Money& m);
	bool operator <=(const Money& m);
	bool operator >=(const Money& m);
	bool operator !=(const Money& m);
	bool operator ==(const Money& m);
	friend Money& operator--(Money& );
	friend Money operator--(Money& , int);
	friend ostream& operator << (ostream& os, const Money& m);
	friend istream& operator >>(istream& os, Money& m);
	void Show();
	void Set(char c = '+', int y = 0, int j = 0, int f = 0);
private:
	char sign;
	int yuan;
	int jiao;
	int fen;
};
//Money.cpp
#include"Money.h"
#include<iostream>
using namespace std;
Money::Money(char c, int y, int j, int f)
{
	sign = c;
	yuan = y;
	jiao = j;
	fen = f;
}
void Money::standard()
{
	jiao += fen / 10;
	fen %= 10;
	yuan += jiao / 10;
	jiao %= 10;
}

Money Money::operator+(const Money& m)
{
	Money mo;
	if (sign == m.sign) {
		mo.yuan = yuan + m.yuan;
		mo.jiao = jiao + m.jiao;
		mo.fen = fen + m.fen;
		mo.standard();
	}
	else {
		if (sign == '-') {
			Money tmp('+', yuan, jiao, fen);
			Money t = m;
			mo = t - tmp;
		}
		else {
			Money tmp('+', m.yuan, m.jiao, m.fen);
			mo = *this - tmp;
		}
	}
	return mo;
}

Money Money::operator-(const Money& m)
{
	Money mo;
	Money tmp = m;
	if (sign != m.sign) {
		if (m.sign == '-') {
			tmp.sign = '+';
		}
		else {
			tmp.sign = '-';
		}
		return *this + tmp;
	}
	if (yuan > m.yuan || (yuan == m.yuan && jiao > m.jiao) || (yuan == m.yuan && jiao == m.jiao && fen >= m.fen))
	{
		mo = *this; tmp = m;
	}
	else { mo = m; tmp = *this; mo.sign = (sign == '+') ? '-' : '+'; }
	if (mo.fen < tmp.fen) {
		mo.jiao--;
		mo.fen += 10;
	}
	mo.fen -= tmp.fen;
	if (mo.jiao < 0 || mo.jiao < tmp.jiao) {
		mo.yuan--;
		mo.jiao += 10;
	}
	mo.jiao -= tmp.jiao;
	mo.yuan -= tmp.yuan;
	return mo;
}

Money& Money::operator++() {    //前置
	Money m('+', 0, 0, 1);
	*this = *this + m;
	return *this;
}
Money Money::operator++(int) {
	Money m = *this;
	++(*this);
	return m;
}
Money& operator--(Money& x)
{
	Money m('+', 0, 0, 1);
	x = x - m;
	return x;
}

Money operator--(Money& x, int) {
	Money m = x;
	--(x);
	return m;
}
ostream& operator<<(ostream& os, const Money& m)
{
	os << m.sign << m.yuan << "元" << m.jiao << "角" << m.fen << "分";
	return os;
}
istream& operator>>(istream& os, Money& m)
{
	char c;
	int y, j, f;
	os >> c >> y >> j >> f;
	m.sign = c;
	m.yuan = y;
	m.jiao = j;
	m.fen = f;
	m.standard();
	return os;
}
void Money::Show()
{
	cout << sign << yuan << "元" << jiao << "角" << fen << "分" << endl;
}
bool Money::operator<(const Money& m)
{
	int f = 0;
	if (sign != m.sign) return sign > m.sign;
	if (sign == '-') f = 1;
	if (yuan != m.yuan) return (yuan < m.yuan) ^ f;
	if (jiao != m.jiao) return (jiao < m.jiao) ^ f;
	return (fen < m.fen) ^ f;
}
bool Money::operator>(const Money& m) {
	int f = 0;
	if (sign != m.sign) return sign < m.sign;
	if (sign == '-') f = 1;
	if (yuan != m.yuan) return (yuan > m.yuan) ^ f;
	if (jiao != m.jiao) return (jiao > m.jiao) ^ f;
	return (fen > m.fen) ^ f;
}
bool Money::operator<=(const Money& m) {
	if (sign == m.sign && yuan == m.yuan && jiao == m.jiao && fen == m.fen) return true;
	int f = 0;
	if (sign != m.sign) return sign > m.sign;
	if (sign == '-') f = 1;
	if (yuan != m.yuan) return (yuan < m.yuan) ^ f;
	if (jiao != m.jiao) return (jiao < m.jiao) ^ f;
	return (fen < m.fen) ^ f;
}
bool Money::operator>=(const Money& m) {
	if (sign == m.sign && yuan == m.yuan && jiao == m.jiao && fen == m.fen) return true;
	int f = 0;
	if (sign != m.sign) return sign < m.sign;
	if (sign == '-') f = 1;
	if (yuan != m.yuan) return (yuan > m.yuan) ^ f;
	if (jiao != m.jiao) return (jiao > m.jiao) ^ f;
	return (fen > m.fen) ^ f;
}
bool Money::operator!=(const Money& m) {
	if (sign != m.sign) return true;
	if (yuan != m.yuan) return true;
	if (jiao != m.jiao) return true;
	if (fen != m.fen) return true;
	return false;
}
bool Money::operator==(const Money& m) {
	if (sign != m.sign) return false;
	if (yuan != m.yuan) return false;
	if (jiao != m.jiao) return false;
	if (fen != m.fen) return false;
	return true;
}
void Money::Set(char c, int y, int j, int f)
{
	sign = c;
	yuan = y;
	jiao = j;
	fen = f;
	this->standard();
}
//Main.cpp
#include"Money.h"
#include<iostream>
#include<algorithm>
#include<fstream>
#include<stdlib.h>
using namespace std;
int n;
int main() {
	ifstream file1("rmbin.txt");
	ofstream file2("rmbout.txt");
	file1 >> n;
	Money *m = new Money[n];
	char s;
	int y, j, f;
	//cout << "input " << n << " RMBs:" << endl;
	for (int i = 0; i < n; i++) {
		file1 >> s >> y >> j >> f;
		m[i].Set(s, y, j, f);
		//cout << m[i] << endl;
	}
	sort(m, m + n);
	//cout << "sorted:" << endl;
	for (int i = 0; i < n; i++) {
		file2 << m[i] << endl;
	}
	file1.close();
	file2.close();
	return 0;
}

 

3.  大整数类HUGE_INT

设计大整数类HUGE_INT, 它的功能是:

       可以构造200位以内的无符号整数

       可以输入、输出这种整数

       可以完成这种整数间的四则运算:+ - * /

       设计并实现这个类,并且编写主程序测试它的所有功能。

//BigInteger.h
#pragma once
#include<string>
#include<vector>
#include<iostream>
using namespace std;

class BigInteger {
	friend ostream& operator <<(ostream& out, const BigInteger& num);
	friend istream& operator >>(istream& in, BigInteger& num);
public:
	BigInteger(long long num = 0);
	BigInteger(const string& s);

	BigInteger& operator = (long long num);
	BigInteger& operator = (const string& s);
	bool operator == (const BigInteger& rs);
	bool operator != (const BigInteger& rs);
	bool operator > (const BigInteger& rs);
	bool operator >= (const BigInteger& rs);
	bool operator < (const BigInteger& rs);
	bool operator <= (const BigInteger& rs);

	BigInteger operator + (const BigInteger& rs) const;
	BigInteger operator +=(const BigInteger& rs);

	BigInteger operator - (const BigInteger& rs) const;
	BigInteger operator -= (const BigInteger& rs);

	BigInteger operator * (const BigInteger& rs) const;
	BigInteger operator *= (const BigInteger& rs);

	BigInteger operator / (BigInteger& rs) const;
	BigInteger operator /=(BigInteger& rs);

private:
	//尝试把每个大数看成一个BASE进制的数来处理
	static BigInteger Divide(const BigInteger a,const BigInteger b);
	static BigInteger sub(const BigInteger& a, const BigInteger& b);
	static void removeHighestZero(BigInteger& x);
	static void formatBits(BigInteger& x);

	static const int BASE = 10000;
	static const int WIDTH = 4;
	vector<int> m_bits;
};
//BigInteger.cpp
#include"BigInteger.h"
#include<stdio.h>
BigInteger::BigInteger(long long num)
{
	*this = num;
}
BigInteger& BigInteger::operator = (long long num)
{
	//cout << "调用了" << endl;
	m_bits.clear();
	do
	{
		m_bits.push_back(num % BASE);
		num /= BASE;
	} while (num);
	return *this;
}
BigInteger::BigInteger(const string& s)
{
	*this = s;
}
BigInteger& BigInteger::operator = (const string& s)
{
	m_bits.clear();
	int bit, len = (s.length() - 1) / WIDTH + 1;
	for (int i = 0; i < len; i++)
	{
		int end = s.length() - i * WIDTH;
		int start = max(0, end - WIDTH);
		sscanf_s(s.substr(start, end - start).c_str(), "%d", &bit);
		//cout << bit << endl;
		m_bits.push_back(bit);
	}
	return *this;
}
bool BigInteger::operator>(const BigInteger& rs) {
	if (m_bits.size() != rs.m_bits.size()) return m_bits.size() > rs.m_bits.size();
	for (int i = m_bits.size() - 1; i >= 0; i--) {
		if (m_bits[i] != rs.m_bits[i]) return m_bits[i] > rs.m_bits[i];
	}
	return false;
}
bool BigInteger::operator >= (const BigInteger& rs) {
	return (*this > rs || *this == rs);
}
bool BigInteger::operator<(const BigInteger& rs) {
	return !(*this >= rs);
}
bool BigInteger::operator<=(const BigInteger& rs) {
	return !(*this > rs);
}

BigInteger BigInteger::operator+(const BigInteger& rs) const
{
	BigInteger res;
	res.m_bits.clear();
	for (int i = 0, x = 0;; i++) {
		if (i >= m_bits.size() && i >= rs.m_bits.size() && x == 0) break;
		int num = x;       //缓存相当于表示当前两位相加
		if (i < m_bits.size())
			num += m_bits[i];
		if (i < rs.m_bits.size())
			num += rs.m_bits[i];
		res.m_bits.push_back(num % BASE);
		x = num / BASE;    //进位
	}
	return res;
}
BigInteger BigInteger::operator+=(const BigInteger& rs)
{
	*this = *this + rs;
	return *this;
}
BigInteger BigInteger::operator-(const BigInteger& rs) const
{
	BigInteger tmp = *this;
	if (tmp > rs) return sub(*this, rs);
	else return sub(rs, *this);
}
BigInteger BigInteger::operator-=(const BigInteger& rs)
{
	*this = *this - rs;
	return *this;
}
BigInteger BigInteger::operator*(const BigInteger& rs) const
{
	BigInteger res;
	res.m_bits.clear();
	//size_t 类型表示C中任何对象所能达到的最大长度
	res.m_bits.resize(m_bits.size() + rs.m_bits.size(), 0);
	for (size_t i = 0; i < m_bits.size(); i++) {
		for (size_t j = 0; j < rs.m_bits.size(); j++) {
			int num = m_bits[i] * rs.m_bits[j];
			res.m_bits[i + j] += num;
		}
	}
	formatBits(res);
	removeHighestZero(res);
	return res;
}
BigInteger BigInteger::operator*=(const BigInteger& rs) {
	*this = *this + rs;
	return *this;
}

bool BigInteger::operator==(const BigInteger& rs)
{
	//cout << "调用了" << endl;
	return m_bits == rs.m_bits;
}
bool BigInteger::operator!=(const BigInteger& rs)
{
	return m_bits != rs.m_bits;
}
BigInteger BigInteger::operator/(BigInteger& rs) const
{
	if (rs == 0) throw runtime_error("除数咋能是零\n");
	BigInteger temp = *this;
	return Divide(temp, rs);
}
BigInteger BigInteger::operator/=(BigInteger& rs) {
	*this = *this / rs;
	return *this;
}
BigInteger BigInteger::Divide(BigInteger a, BigInteger b) {
	if (a < b) return 0;
	BigInteger res;
	int maxSize = a.m_bits.size() - b.m_bits.size();
	BigInteger mul, pro;
	while (a >= b) {
		mul.m_bits.clear();
		mul.m_bits.resize(maxSize + 1, 0);
		mul.m_bits[maxSize] = 1;
		pro = b * mul;      //【快速】得到一个mul,使得mul*b小于a,mul*10*b大于a
		while (a >= pro) {
			a -= pro;
			res += mul;
		}
		maxSize--;
	}
	return res;
}
BigInteger BigInteger::sub(const BigInteger& a, const BigInteger& b)
{
	BigInteger res;
	res.m_bits.clear();
	for (int i = 0, c = 0;; i++) {
		if (i >= a.m_bits.size() && i >= b.m_bits.size() && c == 0) break;
		int n = c;
		if (i < a.m_bits.size()) n += a.m_bits[i];
		if (i < b.m_bits.size()) n -= b.m_bits[i];
		n += BASE;
		res.m_bits.push_back(n % BASE);
		c = n / BASE - 1;
	}
	removeHighestZero(res);
	return res;
}
void BigInteger::removeHighestZero(BigInteger& x) {
	while (x.m_bits.size() > 1 && x.m_bits.back() == 0)
		x.m_bits.pop_back();
}
void BigInteger::formatBits(BigInteger& x)
{
	int c = 0;
	for (size_t i = 0; i < x.m_bits.size(); i++) {
		int temp = x.m_bits[i] + c;
		x.m_bits[i] = temp % BASE;
		c = temp / BASE;
	}
	while (c != 0) {
		x.m_bits.push_back(c % BASE);
		c /= BASE;
	}
}
istream& operator >> (istream& in, BigInteger& num)
{
	string str;
	if (!(in >> str))
		return in;
	num = str;
	return in;
}

ostream& operator << (ostream& out, const BigInteger& num) {
	for (int i = num.m_bits.size() - 1; i >= 0; i--) {
		out << num.m_bits[i];
	}
	return out;
}
//main.cpp
#include"BigInteger.h"
using namespace std;

int main() {
	BigInteger bi,ai;
	cout << "test addition, input two big integers:" << endl;
	cin >> ai >> bi;
	cout << ai << "+" << bi << "=" << ai + bi << endl;
	ai += bi;
	cout << "integer1 += integer2, integer1 = " << ai << endl;
	cout << "test subtraction, input two big integers:" << endl;
	cin >> ai >> bi;
	cout << ai << "-" << bi << "=" << ai - bi << endl;
	ai -= bi;
	cout << "integer1 -= integer2, integer1 = " << ai << endl;
	cout << "test multipulication, input two big integers:" << endl;
	cin >> ai >> bi;
	cout << ai << "*" << bi << "=" << ai * bi << endl;
	ai *= bi;
	cout << "integer1 *= integer2, integer1 = " << ai << endl;
	cout << "test division, input two big integers:" << endl;
	cin >> ai >> bi;
	cout << ai << "/" << bi << "=" << ai / bi << endl;
	ai /= bi;
	cout << "integer1 /= integer2, integer1 = " << ai << endl;
	return 0;
}

图 1测试加法

图 2测试减法

图 3乘法

图 4测试除法

算法的核心是把每个大数看作一个BASE进制的数,每一位存于vector中

受int型限制,BASE只能取到10000(乘法溢出),换成long long型可取到1e8

两个等于号重载大大方便了之后的计算

关于比较重载,这样写只用写完其中一个就相当于可以直接进行其他三个运算

加法很显然,一位一位加,加完取模和进位就好

减法和加法思路一样,判下大小就行,大于正常算,小于交换

每次算完格式标准化,不然输出不正确

输入输出流,值得注意的是!(in>>str)支持EOF

乘法也比较好写,但值得注意的是第i位和第j位相乘得到的是第i+j位,当然最后要注意格式标准化。复杂度是O(n^2)

除法很难,做到低复杂度更难,在比较了大小以后我们来到Divide函数,直接用size相减得到位数差,把除数左移相应位数就能得到一个最大的小于被除数的除数,然后每次位数减一就行就行

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值