Skip to content

Instantly share code, notes, and snippets.

@lexdene
Forked from masquevil/bigInt.cpp
Last active August 29, 2015 14:08
Show Gist options
  • Save lexdene/d49cbae9f0f31ff00e2f to your computer and use it in GitHub Desktop.
Save lexdene/d49cbae9f0f31ff00e2f to your computer and use it in GitHub Desktop.
#include<iostream>
#include<string>
#include<time.h>
#include "bigInt.h"
using namespace std;
BigInt::BigInt(){ //构造函数
head = tail = temp = NULL;
symbol = '+';
length = 0;
}
BigInt::BigInt(const BigInt &bigNum){ //复制构造函数
NumUnit *p;
head = tail = temp = NULL;
symbol = bigNum.symbol;
length = 0;
p = bigNum.tail;
while (p){
insertHead(p->num);
p = p->prev;
}
}
BigInt::~BigInt(){ //析构函数
if (head == NULL) return;
NumUnit *next;
temp = head;
while (temp){
next = temp->next;
delete temp;
temp = next;
}
head = tail = temp = NULL;
}
void BigInt::randBigNum(){ //随机产生数
int MAX = rand() % 49;
for (int i = 1; i < MAX; i++){
insertHead(rand() % 10);
}
insertHead(rand() % 9 + 1);
}
void BigInt::insertHead(int num){ //插入到头部
temp = new NumUnit;
temp->num = num;
temp->prev = NULL;
if (!head){
head = tail = temp;
temp->next = NULL;
}
else{
temp->next = head;
head->prev = temp;
head = temp;
}
length++;
}
void BigInt::deleteHead(){ //头部为0时删除头部
temp = head;
head = head->next;
delete temp;
length--;
if (length == 0) symbol = '+'; //长度为0时,符号为+
}
BigInt BigInt::operator + (const BigInt &bigNum) const{ // + 的重载
BigInt result;
NumUnit *num1 = tail, *num2 = bigNum.tail;
int sum, carry = 0;
if (symbol != bigNum.symbol){
result = *this - bigNum;
return result;
}
result.symbol = symbol;
while (num1 && num2){
sum = num1->num + num2->num + carry;
carry = sum / 10;
sum %= 10;
result.insertHead(sum);
num1 = num1->prev;
num2 = num2->prev;
}
if (num2)num1 = num2;
while (num1){
sum = num1->num + carry;
carry = sum / 10;
sum %= 10;
result.insertHead(sum);
num1 = num1->prev;
}
if (carry)
result.insertHead(carry);
return result;
}
BigInt BigInt::operator - (const BigInt &bigNum) const{ // - 的重载
BigInt result;
NumUnit *num1 = tail, *num2 = bigNum.tail;
int sub, carry = 0;
if (symbol != bigNum.symbol){
result = *this + -bigNum;
return result;
}
if (*this < bigNum){
result = -(bigNum - *this);
return result;
}
result.symbol = symbol;
while (num1 && num2){
sub = num1->num - num2->num - carry;
sub += 10;
carry = 1 - (sub / 10);
sub %= 10;
result.insertHead(sub);
num1 = num1->prev;
num2 = num2->prev;
}
if (num2)num1 = num2;
while (num1){
sub = num1->num - carry;
sub += 10;
carry = 1 - (sub / 10);
sub %= 10;
result.insertHead(sub);
num1 = num1->prev;
}
if (carry)
result.insertHead(carry);
while (result.head && !result.head->num){
result.deleteHead();
}
return result;
}
BigInt BigInt::operator - () const{ // - (负号)的重载
BigInt result = *this;
result.symbol = symbol == '+' ? '-' : '+';
return result;
}
BigInt BigInt::operator = (const BigInt &bigNum){ // = 的重载
if (this == &bigNum)return *this;
NumUnit *p;
temp = head = tail = NULL;
length = 0;
p = bigNum.tail;
while (p){
insertHead(p->num);
p = p->prev;
}
return *this;
}
bool BigInt::operator < (const BigInt &bigNum) const{ // < 的重载
if (symbol == '-'){
if (bigNum.symbol == '+')return true;
if (length > bigNum.length || (length == bigNum.length && head->num > bigNum.head->num))return true;
}
else if (bigNum.symbol == '-')return false;
else if (length < bigNum.length || (length == bigNum.length && head->num < bigNum.head->num))return true;
return false;
}
int BigInt::getLength(){
return length;
}
void BigInt::display(){
if (head){
if (symbol == '-')cout << symbol;
cout << head->num;
temp = head->next;
}else{
cout << 0 << endl;
return;
}
while (temp){
cout << temp->num;
temp = temp->next;
}
cout << endl;
}
int main(){
BigInt a, b, sum, sub1, sub2;
srand((unsigned)time(NULL));
a.randBigNum();
b.randBigNum();
cout << "a = ";
a.display();
cout << "b = ";
b.display();
cout << "-a = ";
(-a).display();
cout << "-b = ";
(-b).display();
BigInt c;
c = -a;
cout << "c = ";
c.display();
cout << "a < b = " << (a < b) << endl;
cout << "b < a = " << (b < a) << endl;
sum = a + b;
cout << "a + b = ";
sum.display();
sub1 = a - b;
cout << "a - b = ";
sub1.display();
sub2 = b - a;
cout << "b - a = ";
sub2.display();
return 0;
}
struct NumUnit{
int num;
NumUnit *next, *prev;
};
class BigInt{
private:
NumUnit *head, *tail, *temp;
int length;
char symbol;
void insertHead(int num);
void deleteHead();
public:
BigInt();
BigInt(const BigInt &bigNum);
BigInt operator + (const BigInt &bigNum) const;
BigInt operator - (const BigInt &bigNum) const;
BigInt operator - () const;
BigInt operator = (const BigInt &bigNum);
bool operator < (const BigInt &bigNum) const;
int getLength();
void randBigNum();
void display();
~BigInt();
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment