PageRenderTime 93ms CodeModel.GetById 50ms app.highlight 29ms RepoModel.GetById 12ms app.codeStats 0ms

/c++/dynamic array/source/BIG_INT_NUMBERS.CPP

https://bitbucket.org/boria8/c-works
C++ | 279 lines | 215 code | 64 blank | 0 comment | 32 complexity | 8200ec05d6d3bba8ebe0d09443430bbc MD5 | raw file
  1#include <iostream>
  2#include <stdio.h>
  3#include "string.h"
  4#include "darray.h"
  5#include "math.h"
  6#include "big_integer_numbers.h"
  7#define IF_RETURN(expression,ret) if(expression) {return ret;}
  8BIG_INT_NUMBERS::BIG_INT_NUMBERS(char *_str):darray(strlen(_str)+1)
  9{
 10    int point=0;
 11
 12
 13       chek_if_valid(_str,&point);
 14
 15       int  i_bef_dot=(int)strlen(_str)-point+1;
 16
 17       if(point==0)
 18       {
 19           i_bef_dot=0;
 20       }
 21
 22       for(int i=0;i<((int)strlen(_str)-i_bef_dot);i++)
 23       {
 24           inser_last(*(_str+i)-'0');
 25       }
 26
 27
 28       char* str=new char[((int)strlen(_str))-i_bef_dot+1];
 29
 30       memcpy(str,_str,((int)strlen(_str))-i_bef_dot);
 31
 32       *(str+(int)strlen(_str)-i_bef_dot+1)='\0';
 33
 34       big_num=str;
 35
 36       cout<<"your number is"<<str<<endl;
 37}
 38BIG_INT_NUMBERS:: BIG_INT_NUMBERS(int _num)
 39{
 40       char    _str[100];
 41       int     point=0;
 42
 43      sprintf(_str,"%d",_num);
 44      chek_if_valid(_str,&point);
 45
 46       for(int i=0;i<(int)strlen(_str);i++)
 47       {
 48           inser_last(*(_str+i)-'0');
 49       }
 50       char* str=new char[strlen(_str)+1];
 51       strcpy(str,_str);
 52       big_num=str;
 53
 54}
 55
 56const int*           BIG_INT_NUMBERS::operator[](const int& _index)const
 57{
 58
 59    if(elements<_index)
 60    {
 61        return 0;
 62    }
 63
 64    return *(array+_index-1);
 65}
 66
 67ostream&             operator<<(ostream& os,const BIG_INT_NUMBERS& _num)
 68{
 69    if (! _num.get_size())
 70    {
 71         return os<<endl<<"you don't have number"<<endl;
 72    }
 73
 74    for(int i=0;i<_num.get_size();i++)
 75    {
 76        cout<<*_num[i+1];
 77    }
 78
 79    return os<<endl<<"table of your elements"<<endl;
 80
 81}
 82BIG_INT_NUMBERS      BIG_INT_NUMBERS::operator+(const BIG_INT_NUMBERS& _num)const
 83{
 84
 85    BIG_INT_NUMBERS temp;
 86    int     i=0;
 87    int     carry=0;
 88
 89    temp.inser_last((*operator[](elements-i)+*_num[_num.elements-i])%10);
 90
 91    carry=(*operator[](elements-i)+*_num[_num.elements-i])/10;
 92
 93    i++;
 94
 95    while (1)
 96    {
 97
 98        if(((elements-i)==0)&&((_num.elements-i)==0))
 99        {
100            if (carry)
101            {
102               temp.insert_element_bef(i,carry);
103            }
104            char* str=new char[elements+1];
105            temp.copy_to_str(str);
106            return temp;
107        }
108
109        else if((elements-i!=0)&&((_num.elements-i)!=0))
110        {
111             temp.insert_element_bef(i,((*operator[](elements-i))+(*_num[_num.elements-i])+carry)%10);
112             carry=(*operator[](elements-i)+*_num[_num.elements-i]+carry)/10;
113             i++;
114        }
115
116        else if(((elements-i)!=0))
117        {
118            temp.insert_element_bef(i,(*operator[](elements-i)+carry)%10);
119            carry=(*operator[](elements-i)+carry)/10;
120            i++;
121        }
122
123        else if(((_num.elements-i)!=0))
124        {
125            temp.insert_element_bef(i,(*_num[_num.elements-i]+carry)%10);
126            carry=(*_num[_num.elements-i]+carry)/10;
127            carry=0;
128            i++;
129        }
130    }
131}
132
133BIG_INT_NUMBERS      BIG_INT_NUMBERS::operator-(const BIG_INT_NUMBERS& _num)const
134{
135
136    BIG_INT_NUMBERS temp;
137    int     i=0;
138    int     carry=0;
139
140    if((*operator[](elements-i)-*_num[elements-i]-carry)>0)
141    {
142        temp.inser_last(*operator[](elements-i)-*_num[elements-i]-carry);
143        carry=0;
144        i++;
145    }
146    else if (((*operator[](elements-i)-*_num[elements-i]-carry)<0))
147    {
148        temp.inser_last(*operator[](elements-i)-*_num[elements-i]+10);
149        carry=1;
150        i++;
151    }
152    else
153    {
154        temp.inser_last(0);
155        i++;
156        carry=0;
157    }
158
159
160
161
162
163    while (1)
164    {
165
166        if((elements-i==0)&&(_num.elements-i==0))
167        {
168            if (carry)
169            {
170               temp.insert_element_bef(i,carry);
171            }
172
173            char* str=new char[elements+1];
174            temp.copy_to_str(str);
175            return temp;
176        }
177
178        else if((elements-i!=0)&&(_num.elements-i!=0))
179        {
180
181            if((*operator[](elements-i)-*_num[elements-i]-carry)>0)
182            {
183                temp.insert_element_bef(i,*operator[](elements-i)-*_num[elements-i]-carry);
184                carry=0;
185                i++;
186            }
187
188            else if (((*operator[](elements-i)-*_num[elements-i]-carry)<0))
189            {
190                temp.insert_element_bef(i,*operator[](elements-i)-*_num[elements-i]+10);
191                carry=1;
192                i++;
193            }
194            else
195            {
196                    temp.insert_element_bef(i,0);
197                    i++;
198                    carry=0;
199            }
200
201        }
202
203        else if((elements-i!=0))
204        {
205            temp.insert_element_bef(i,(*operator[](elements-i)+carry));
206            carry=0;
207            i++;
208        }
209        else
210        {
211            temp.insert_element_bef(i,0);
212            i++;
213        }
214    }
215}
216
217BIG_INT_NUMBERS      BIG_INT_NUMBERS::operator*(const BIG_INT_NUMBERS& _num)const
218{
219
220
221    BIG_INT_NUMBERS temp2;
222    int     i=0;
223
224
225    while(_num.elements>=i)
226    {
227        int j=0;
228
229        while(elements>j)
230        {
231            BIG_INT_NUMBERS temp1((int)(pow(10,i)*(*_num[elements-i])*(*operator[](elements-j))));
232            temp2=temp2+temp1;
233            cout<<temp2<<endl;
234            j++;
235        }
236        i++;
237    }
238    return temp2;
239}
240
241const BIG_INT_NUMBERS&      BIG_INT_NUMBERS::operator=(const BIG_INT_NUMBERS& _num)
242{
243    remove_and_delete_all();
244    delete[] big_num;
245
246    big_num=new char[strlen(_num.big_num)];
247    strcpy(big_num,_num.big_num);
248
249    for(int i=1;i<_num.elements+1;i++)
250    {
251        inser_last(*_num[i]);
252    }
253
254    return *this;
255
256}
257
258BIG_INT_NUMBERS      BIG_INT_NUMBERS::operator/(const BIG_INT_NUMBERS& _num)const
259{
260    BIG_INT_NUMBERS temp;
261    int i=1;
262
263
264    while (elements>_num.elements+i)
265    {
266        temp.inser_last(*operator[](i));
267        i++;
268    }
269
270
271    if(*operator[](i) / *_num[i])
272    {
273
274        temp.inser_last(*operator[](i)/ *_num[i]);
275    }
276
277    return temp;
278
279}