Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <string>
- using namespace std;
- class BigInt {
- public:
- BigInt(); //Initializes the BigInt to zero
- BigInt(int x);//Initializes the BigInt to have the same value as x
- explicit BigInt(string x);//Initializes the BigInt to have the value of the given string
- friend ostream& operator<<(ostream& out, const BigInt& right);
- friend BigInt operator+(const BigInt& left, const BigInt& right);
- friend BigInt operator-(const BigInt & left, const BigInt & right);
- friend string addition_helper(string one, string two);
- friend string subtraction_helper(string one, string two);
- friend bool operator<(const BigInt& left, const BigInt& right);
- private:
- string data;
- bool isNegative;
- };
- #include <iostream>
- #include <string>
- using namespace std;
- #include "BigInt.h"
- int main() {
- cout << "This works here" << endl;
- BigInt a(-1000);
- cout << "This works here" << endl;
- BigInt b("+999");
- cout << "This works here" << endl;
- cout << b << "+" << a << endl << "= " << b + a << endl;
- a = BigInt("1000");
- b = BigInt("999");
- cout << a << "-" << b << endl << "= " << a - b << endl;
- a = BigInt("99999999999999999999999999999999999999999999999999999999999999999999999999999999");
- b = BigInt("1");
- cout << " " << a << "+" << b << endl << "= " << a + b << endl;
- cout << "Expecting 1, got: " << (BigInt("-30") < BigInt("-1")) << endl;
- cout << "Expecting 0, got: " << (BigInt("30") < BigInt("-1")) << endl;
- cout << "Expecting 1, got: " << (BigInt("30") < BigInt("300")) << endl;
- cout << "Expecting 0, got: " << (BigInt("-30") < BigInt("-300")) << endl;
- return 0;
- }
- BigInt::BigInt() {
- isNegative = false;
- data = "0";//format for a string
- }
- BigInt::BigInt(int x) {
- if (x < 0) {
- isNegative = true;
- }
- else {
- isNegative = false;
- }
- data = to_string(x);
- }
- BigInt::BigInt(string x) {
- string y;//for necessary input for what we need from x
- int i = 0;
- while (iswspace(x[i])) {
- i++;
- }
- if (x[i] == '-') {
- isNegative = true;
- y.insert(y.begin(), x[i]);
- }
- else if (x[i] == '+') {
- isNegative = false;
- }
- else if (isdigit(x[i])) {
- isNegative = false;
- y.insert(y.begin(), x[i]);
- }
- else {
- cout << "tried to construct an invalid BigInt from string:" << endl;
- cout << x << endl;
- cout << "exiting";
- exit(1);
- }
- i++;
- while (!iswspace(x[i])){
- if (isdigit(x[i])) {
- y.insert(y.end(), x[i]);
- }
- else {
- cout << "tried to construct an invalid BigInt from string:" << endl;
- cout << x << endl;
- cout << "exiting";
- exit(1);
- }
- i++;
- }
- data = y;
- }
- ostream& operator<<(ostream& out, const BigInt& right) {
- return out << right.data;
- }
- bool operator<(const BigInt& left, const BigInt& right) {
- int i = 0;
- //if (left.data.length() > right.data.length()) { //comparing sizes
- // swap(left, right);
- //}
- // comparing signs
- if (left.isNegative == true && right.isNegative == false) {//if the left is negative and the right is positive
- return true;
- }
- else if (left.isNegative == false && right.isNegative == true) { //if the left is negative and the right is positive
- return false;
- }
- else if (left.isNegative == true && right.isNegative == true) {// if both are negative
- //comparing sizes of each
- if (left.data.size() > right.data.size()) {
- return true;
- }
- else if (left.data.size() < right.data.size()) {
- return false;
- }
- while (left.data[i] != '\0' && right.data[i] != '\0') { //while the left and right data strings do not hit the null terminator
- if (left.data[i] != right.data[i]) { //if they are differences spotted
- if (left.data[i] > right.data[i]) {
- return true;
- }
- else if (left.data[i] < right.data[i]) {
- return false;
- }
- if (left.data[i] == right.data[i]) {
- return false;
- }
- }
- }
- }
- else if (!left.isNegative && !right.isNegative) {
- //check for different lengths
- if (left.data.size() < right.data.size()) {
- return true;
- }
- if (left.data.size() > right.data.size()) {
- return false;
- }
- while (left.data[i] != '\0' && right.data[i] != '\0') { //while the left and right data strings do not hit the null terminator
- if (left.data[i] != right.data[i]) { //if they are differences spotted
- if (left.data[i] < right.data[i]) {
- return true;
- }
- if (left.data[i] > right.data[i]) {
- return false;
- }
- else if (left.data[i] == right.data[i]) {
- return false;
- }
- }
- }
- }
- }
- string addition_helper(string one, string two) {
- int carry = 0; //carry each calculation
- /*if (one.length() > two.length()) {
- swap(one, two);
- }*/
- string data = "";//to put the final sum in after adding the first two together
- int total1 = one.size() - 1;
- int total2 = two.size() - 1;
- while (total1 > 0 && total2 > 0) {
- int sum = ((one[total1] - '0') + (two[total2] - '0')) + carry;// Calculate the sum of digits by adding the previous carry.
- //take the number and subtract it by the character zero. DO it to the other number
- //add each other
- data.push_back((sum % 10) + '0');//push the result onto the string
- carry = sum / 10; // change carry for the next numbers
- total1--;//moving left towards the next numbers
- total2--;//moving left towards the next numbers
- }
- while (total1 >= 0) {
- int sum = (one[total1] - '0') + carry; // Add digits to the carry and store the carry
- data.push_back((sum / 10) + '0');
- carry = sum / 10;//update carry
- total1--;
- }
- while (total2 >= 0) { // Add digits to the carry and store the carry
- int sum = (two[total2] - '0') + carry;
- data.push_back((sum / 10) + '0');
- carry = sum / 10;//update carry
- total2--;
- }
- if (carry != 0) { //If after finishing adding two numbers, if there is a leftover carry, take it into the final sum.
- data.push_back(carry % 10 + '0'); // store onto stack
- return data;
- }
- }
- string subtraction_helper(string one, string two) {
- int borrow = 0; //carry each calculation
- if (one.length() > two.length()) {
- swap(one, two);
- }
- string data = "";//to put the FINAL sum in after adding the first two together
- int total1 = one.size() - 1;
- int total2 = two.size() - 1;
- while (total1 > 0 && total2 > 0) {
- int difference = ((one[total1] - '0') - (two[total2] - '0'));//subtract '0' from each integer in the string
- //and add carry which is 0 unless otherwise
- while (total1 >= 0) {
- int difference = ((one[total1] - '0') - borrow);
- if (difference < 0){
- difference = difference + 10;
- borrow = 1;
- }
- else {
- borrow = 0;
- data.push_back(difference + '0');
- }
- }
- while (total2 >= 0) { // Add digits to the carry and store the carry
- int difference = ((two[total2] - '0') - borrow);
- if (difference < 0){
- difference = difference + 10;
- borrow = 1;
- }
- else {
- borrow = 0;
- data.push_back(difference + '0');
- }
- }
- if (difference < 0) { // If subtraction is less then zero
- difference = difference + 10;// add 10 for borrowing
- borrow = 1;//carry = 1 because borrowed
- }
- else {
- borrow = 0;//no borrowing needed
- data.push_back(difference + '0');
- }
- }
- return data;
- }
- BigInt operator+(const BigInt& left, const BigInt& right) {
- string result = addition_helper(left.data, right.data);
- return BigInt(result);
- }
- BigInt operator-(const BigInt& left, const BigInt& right) {
- string result = subtraction_helper(left.data, right.data);
- return BigInt(result);
- }
- //
- //bool operator<(const BigInt& left, const BigInt& right){
- // int i = 0;
- // //if (left.data.length() > right.data.length()) { //comparing sizes
- // // swap(left, right);
- // //}
- // // comparing signs
- // if (left.isNegative == true && right.isNegative == false) {//if the left is negative and the right is positive
- // return true;
- // }
- // else if (left.isNegative == false && right.isNegative == true) { //if the left is negative and the right is positive
- // return false;
- // }
- // else if (left.isNegative == true && right.isNegative == true) {// if both are negative
- // //comparing sizes of each
- // if (left.data.size() > right.data.size()) {
- // return true;
- // }
- // else if (left.data.size() < right.data.size()) {
- // return false;
- // }
- // while (left.data[i] != '\0' && right.data[i] != '\0') { //while the left and right data strings do not hit the null terminator
- // if (left.data[i] != right.data[i]) { //if they are differences spotted
- // if (left.data[i] > right.data[i]) {
- // return true;
- // }
- // else if (left.data[i] < right.data[i]) {
- // return false;
- // }
- // if (left.data[i] == right.data[i]) {
- // return false;
- // }
- // }
- //
- // }
- // }
- // else if (!left.isNegative && !right.isNegative) {
- // //check for different lengths
- // if (left.data.size() < right.data.size()) {
- // return true;
- // }
- // if (left.data.size() > right.data.size()) {
- // return false;
- // }
- // while (left.data[i] != '\0' && right.data[i] != '\0') { //while the left and right data strings do not hit the null terminator
- // if (left.data[i] != right.data[i]) { //if they are differences spotted
- // if (left.data[i] < right.data[i]) {
- // return true;
- // }
- // if (left.data[i] > right.data[i]) {
- // return false;
- // }
- // else if (left.data[i] == right.data[i]) {
- // return false;
- // }
- // }
- //
- // }
- // }
- //}
- //BigInt operator+(const BigInt& left, const BigInt& right) {
- // string result = addition_helper(left.data, right.data);
- // return BigInt(result);
- //}
- //
- //BigInt operator-(const BigInt& left, const BigInt& right) {
- // string result = subtraction_helper(left.data, right.data);
- // return BigInt(result);
- //}
- //to do addition BigInt
- //reverse the two strings
- //comapre the size of each string
- //start from the back
- //take the two numbers
- //subtract the number from character zero
- //do it with the second string
- //add the two numbers and mod(%) by 10 if you need to carry over a 1
- //repeat this adn return the answer in BigInt
- //to do subtraction operator
- //comapre the size of each string
- //reverse the two strings
- //start from the back
- //take the two numbers
- //subtract the number from character zero
- //do it with the second string
- //if str1 < str2 , add 10 to str 2 element and subtract one from second element in the reverse string for str1
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement