I'm writing a custom arithmetics for a big numbers class (unlimited lenghth of a single number)
Dividing using multiple decrementing numer A by B fails when A is much grater than B. I'm trying to implement written division, but i find it too complicated in my situation.
I can't store numbers in string (it is the main limitation of the project), so i store them in groups of 4 digits in a list of int's. I tried to treat it like the whole 4 digit structure is a single digit in popular written division, but i got lost during implementing, by overloading / operator.
I'd like to get a hint if I'm doing the last, main part of dividing correct? How can I improve the method if dividing in this class?
struct Node{
int value;
Node* next,*prev;
};
class number {
public:
Node* head;
number(); //konstruktor domyślny
~number(); //destruktor
void addNode(string str); //dodanie nowego wezla na poczatek liczby
void addNode(int str); //dodanie nowego wezla na poczatek liczby (z wartosci int)
number& operator+(number& licz); //operator dodawania
number& operator-(number& licz); //operator odejmowania
bool operator>(number& licz); //operator porównania (czy a > b)
bool operator<(number& licz); //operator porównania (a mniejsze od b)
bool operator==(number& licz); //operator porównania (czy a równe b)
number& operator=(const number& licz); //operator przypisania
number& operator-(); //operator zamiany liczby na przeciwną
friend istream& operator>>(istream& input,number& li); //operator pobierania liczby
friend ostream& operator<<(ostream& s,number& li); //operator wypisania
void validate(); //funkcja usuwajaca zera z poczatku liczby
number& operator/(number& licz); //dzielenie calkowitoliczbowe
number& operator*(number& licz); //mnożenie
void pushNode(int str);
};
number& number::operator/(number& licz)
{
/////////cases of dividing//////
if (this->head->value<0 && licz.head->value<0) {
return (-(*this))/(-licz);
}
if (this->head->value<0 && licz.head->value>0) {
return -((-(*this))/licz);
}
if (this->head->value>0 && licz.head->value<0) {
return -(*this/(-licz));
}
number tmp_this=*this;
number tmp_licz=licz;
number zero;
zero.addNode(0);
//dividing by zero//
if (licz==zero) {
cout<<"dividing by zero"<<endl;
number* t=new number;
t->addNode(0);
return *t;
}
//dividing zero by sth ///
if (*this==zero) {
number* t=new number;
t->addNode(0);
return *t;
}
number i,jeden;
i.addNode(0);
jeden.addNode(1);
if (licz == jeden) {
return *this;
}
/// here real mess start///
string pl="";
number* tmp=new number;
Node* p=this->head,*q=licz.head;
tmp->pushNode(q->value);
while (p && *tmp < licz) {
p=p->next;
tmp->pushNode(p->value);
}
number* wynik=new number;
wynik=tmp;
int j;
while (*wynik > zero || *wynik==zero) {
*wynik=*wynik-tmp_licz;
j++;
}
char* str;
sprintf(str, "%d", j);
///end od mess///
};