Skip to content

Instantly share code, notes, and snippets.

@0x61726b
Last active December 9, 2018 20:04
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save 0x61726b/c3e231317c9919f8501ed2533efc5ded to your computer and use it in GitHub Desktop.
Save 0x61726b/c3e231317c9919f8501ed2533efc5ded to your computer and use it in GitHub Desktop.
#include <iostream>
#include "some_header.h"
int main()
{
inf_int x1(19);
inf_int x2("125");
inf_int x3;
inf_int x4(-97);
inf_int x5("-35");
x2.deletFirstDigit();
x3.setValue(42);
x1.add(x2);
x1.changeSign();
x1.print();
x3.concatenate(x2);
x4 = x3;
x3++;
x5 = x3 + x2; // The return value isnt 4129 because we did x4 = x3. Meaning that x4 is not -97 anymore xD
return 0;
}
#include "some_header.h"
#include <iostream>
inf_int::inf_int(int n)
{
int nDigit = GetNumberOfDigits(n);
thesign = GetSign(n);
int strLen = thesign ? nDigit : nDigit + 1;
digit = new char[strLen + 1];
sprintf(digit, "%d", n);
digit[strLen] = '\0'; // NULL terminator
length = nDigit;
}
inf_int::inf_int(const char * s)
{
digit = new char[strlen(s) + 1];
strcpy(digit, s);
digit[strlen(s)] = '\0'; // NULL terminator
length = strlen(s);
thesign = GetSign(s);
}
inf_int::inf_int()
{
digit = new char[2];
strcpy(digit, "0");
digit[1] = '\0';
length = 1;
thesign = false;
}
inf_int::inf_int(const inf_int & x)
{
*this = x; // calls assignment op
}
inf_int::~inf_int()
{
delete[] digit;
}
inf_int & inf_int::operator=(const inf_int & rhs)
{
int n = atoi(rhs.digit);
int nDigit = GetNumberOfDigits(n);
thesign = GetSign(n);
int strLen = thesign ? nDigit : nDigit + 1;
digit = new char[strLen + 1];
sprintf(digit, "%d", n);
digit[strLen] = '\0'; // NULL terminator
length = nDigit;
return *this;
}
inf_int & inf_int::operator++(int)
{
// TODO: insert return statement here
int current = atoi(digit);
current += 1;
inf_int tmp(current);
*this = tmp; // calls assignment op
return *this;
}
inf_int & inf_int::operator+(const inf_int & rhs)
{
// TODO: insert return statement here
int current = atoi(rhs.digit);
current += atoi(digit);
inf_int tmp(current);
*this = tmp; // calls assignment op
return *this;
}
void inf_int::deletFirstDigit()
{
for (int i = 1; i < length; i++)
{
digit[i - 1] = digit[i];
}
char* newDigit = new char[length]; // (oldLength - 1) + 1 // NULL terminator
strcpy(newDigit, digit);
newDigit[length - 1] = '\0';
inf_int tmp(newDigit);
*this = tmp;
}
void inf_int::setValue(int n)
{
int nDigit = GetNumberOfDigits(n);
thesign = GetSign(n);
int strLen = thesign ? nDigit : nDigit + 1;
digit = new char[strLen + 1];
sprintf(digit, "%d", n);
digit[strLen] = '\0'; // NULL terminator
length = nDigit;
}
void inf_int::add(const inf_int & x)
{
*this = *this + x; // use + operator for this
}
void inf_int::changeSign()
{
int n = atoi(digit) * -1;
int nDigit = GetNumberOfDigits(atoi(digit));
thesign = !thesign;
int strLen = thesign ? nDigit : nDigit + 1;
digit = new char[strLen + 1];
sprintf(digit, "%d", n);
digit[strLen] = '\0'; // NULL terminator
length = nDigit;
}
void inf_int::print()
{
std::cout << digit << std::endl;
}
void inf_int::concatenate(const inf_int & x)
{
int newLen = x.length + length;
char* newDigits = new char[newLen + 1];
newDigits[0] = '\0'; // strncat needs NULL terminator
strncat(newDigits, digit, length);
strncat(newDigits, x.digit, x.length);
setValue(atoi(newDigits));
delete[] newDigits;
}
#pragma once
#define _CRT_SECURE_NO_WARNINGS
#include <cstdlib>
class inf_int {
private:
char* digit;
unsigned int length;
bool thesign;
public:
inf_int(int n); // integer constructor
inf_int(const char* s); // string constructor
inf_int(); // default constructor
inf_int(const inf_int& x); // copy constructor
~inf_int(); // destructor
inf_int& operator=(const inf_int& rhs); // assignment overload
inf_int& operator++(int rhs); // ++ overload
inf_int& operator+(const inf_int& rhs); // += overload
void deletFirstDigit();
void setValue(int n);
void add(const inf_int& x);
void changeSign();
void print();
void concatenate(const inf_int& x);
private:
int GetNumberOfDigits(int n)
{
int digits = 0;
while (n != 0) { n /= 10; digits++; }
return digits;
}
bool GetSign(int n)
{
return n > 0 ? true : false;
}
bool GetSign(const char* s)
{
return GetSign(atoi(s));
}
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment