Advertisement
Guest User

Untitled

a guest
Feb 21st, 2020
81
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 7.82 KB | None | 0 0
  1. #include "List.h"
  2. #include <string.h>
  3. #include <iostream>
  4. #include <cmath>
  5. #include <string>
  6.  
  7. using namespace std;
  8. enum{N=200};
  9. Cislo::Cislo(): size(0), cifry(new Data[N]), znamenko(0)
  10. {init();}
  11.  
  12. Cislo::Cislo(const Cislo &other)
  13. :size(0), cifry(new Data[N]), znamenko(0)
  14. {
  15. init();
  16. copyFrom(other);
  17. }
  18.  
  19. Cislo::Cislo(Cislo &&other)
  20. {
  21. this->cifry=other.cifry;
  22. this->size=other.size;
  23. this->znamenko=other.znamenko;
  24. other.znamenko=0;
  25. other.cifry=nullptr;
  26. other.size=0;
  27.  
  28. }
  29.  
  30.  
  31. void Cislo::init()
  32. {
  33. for(int i =0; i<N; i++) cifry[i]=0;
  34. znamenko ='+';
  35. size = 1;
  36. }
  37.  
  38. Cislo::~Cislo(){
  39. delete [] cifry;
  40. }
  41.  
  42.  
  43. void Cislo::freeMemory()
  44. {
  45. init();
  46. size=0;
  47. znamenko=0;
  48. delete [] cifry;
  49. cifry=0;
  50. }
  51.  
  52. void Cislo::naplnit(const char *d)
  53. {
  54. if(d[0]=='-')
  55. {
  56. size=strlen(d)-1;
  57. znamenko=d[0];
  58. for(int i =0; i<size; i++)
  59. cifry[i]=(int)d[size-i]-48;
  60. }
  61.  
  62. else
  63. { size=strlen(d);
  64.  
  65. znamenko='+';
  66. for(int i =0; i<size; i++)
  67. cifry[i]=(int)d[size-i-1]-48;
  68. }
  69.  
  70. }
  71.  
  72. void Cislo::print()
  73. {
  74. cout << znamenko;
  75. for(int i =0; i <size; i++)
  76. {
  77. cout << cifry[size-1-i];
  78.  
  79. }
  80. cout << endl;
  81. }
  82.  
  83. void Cislo::copyFrom(const Cislo &source)
  84. {
  85. this->size = source.size;
  86. this->znamenko=source.znamenko;
  87. for(int i=0; i <source.size; i++)
  88. this->cifry[i]=source.cifry[i];
  89. }
  90.  
  91.  
  92. Cislo& Cislo::operator=(const Cislo &rhs){
  93. if(this == &rhs){
  94. return *this;
  95. }
  96. copyFrom(rhs);
  97. return *this;
  98. }
  99.  
  100. Cislo& Cislo::operator=(Cislo &&rhs){
  101.  
  102. if(this == &rhs){
  103. return *this;
  104. }
  105. this->freeMemory();
  106. this->cifry = rhs.cifry;
  107. this->size = rhs.size;
  108. rhs.cifry = nullptr;
  109. rhs.size = 0;
  110. return *this;
  111. }
  112.  
  113.  
  114.  
  115. bool operator==(const Cislo &a,const Cislo &b)
  116. {
  117. if(a.size == b.size && a.znamenko==b.znamenko)
  118. for(int i=0;i<a.size; i++)
  119. {if(a.cifry[i]!=b.cifry[i])
  120. return false;}
  121.  
  122. return true;
  123.  
  124. }
  125.  
  126. bool operator<(const Cislo &a,const Cislo &b)
  127. {
  128. if(a.znamenko!=b.znamenko)
  129. {if(a.znamenko=='-')
  130. return true;
  131. else
  132. return false;}
  133. if(a.size == b.size)
  134. {for(int i=0;i<a.size; i++)
  135. {if(a.cifry[a.size-1-i]>b.cifry[b.size-1-i])
  136. {if(a.znamenko == '+')
  137. return false;
  138. else
  139. return true;
  140.  
  141. }
  142. else if (a.cifry[a.size-1-i]<b.cifry[b.size-1-i])
  143. {if(a.znamenko == '+')
  144. return true;
  145. else
  146. return false;
  147.  
  148. }
  149. }
  150. return false;
  151. }
  152. else if(a.size > b. size)
  153. return false;
  154. else
  155. return true;
  156. }
  157.  
  158.  
  159. bool operator>(const Cislo &a, const Cislo &b)
  160. {
  161. return!(a<b || a==b);
  162. }
  163.  
  164. void Cislo::spocitejVelikost()
  165. {
  166. int s=199;
  167. while(s>=0)
  168. { if(cifry[s]!=0){
  169. size=s+1;
  170. break;}
  171. if(s==0)
  172. {size=1;
  173. break;}
  174. s--;
  175. }
  176.  
  177.  
  178. }
  179.  
  180. Cislo Cislo::absolut(const Cislo &a)
  181. {
  182. Cislo c= a;
  183. c.znamenko='+';
  184. return c;
  185. }
  186. Cislo operator-(Cislo &a)
  187. {
  188. if(a.znamenko=='+')
  189. a.znamenko='-';
  190.  
  191. else
  192. a.znamenko='+';
  193. return a;
  194. }
  195.  
  196.  
  197. void Cislo::scitaci_pomocnik1(Cislo &a, Cislo &b)
  198. {
  199. int s= 0;
  200. for(int i = 0; i < max(a.size, b.size)+1; i++){
  201. s=a.cifry[i]+b.cifry[i];
  202. if(s+this->cifry[i]>9)
  203. {this->cifry[i]=this->cifry[i] + s-10;
  204. //if i+1=200; error
  205. this->cifry[i+1]++;
  206. cout << "bof";
  207. }
  208. else {
  209. this->cifry[i]=this->cifry[i] +s;
  210. }
  211. }
  212. this->znamenko=a.znamenko;
  213. }
  214.  
  215. void Cislo::scitaci_pomocnik2(Cislo &a, Cislo &b)
  216. {
  217.  
  218. int s= 0;
  219.  
  220.  
  221. Cislo c;
  222. for(int i = 0; i < max(a.size, b.size)+2; i++){
  223. s=a.cifry[i]-b.cifry[i]-c.cifry[i];
  224. if(s<0)
  225. {this->cifry[i]= 10 +s;
  226. //if i+1=200; error
  227. c.cifry[i+1]++;
  228.  
  229. }
  230. else {
  231.  
  232. this->cifry[i]=s;
  233. }
  234.  
  235. }
  236.  
  237. this->spocitejVelikost();
  238.  
  239. }
  240.  
  241. Cislo operator+(Cislo &a,Cislo &b)
  242. {
  243. Cislo e=b;
  244. Cislo c;
  245. if(a.znamenko==b.znamenko){
  246. c.scitaci_pomocnik1(a, b);
  247. c.znamenko=a.znamenko;
  248. }
  249. else if(a.znamenko=='-')
  250. {if(a<-e) {
  251. c.scitaci_pomocnik2(a,b);
  252. c.znamenko='-';
  253. }
  254. else
  255. { c.scitaci_pomocnik2(b,a);
  256. c.znamenko='+';
  257.  
  258. }
  259. }
  260. else
  261. { if(a>-e)
  262. {c.scitaci_pomocnik2(a,b);
  263. c.znamenko='+';
  264. }
  265. else{c.scitaci_pomocnik2(b,a);
  266. c.znamenko='-';
  267.  
  268. }
  269. }
  270. c.spocitejVelikost();
  271. return c;
  272. }
  273.  
  274.  
  275. Cislo operator-(Cislo &a,Cislo &b)
  276. {
  277. Cislo c=-b;
  278. return a+c;
  279. }
  280.  
  281.  
  282. Cislo operator*(Cislo &a, Cislo &b)
  283. {
  284. Cislo c;
  285. c.init();
  286. int s=0;
  287.  
  288. for(int k =0; k< a.size+b.size;k++)
  289. {Cislo d;
  290. d.init();
  291.  
  292.  
  293. for(int i =0; i< a.size+b.size;i++)
  294. {
  295. /* s= a.cifry[k]*b.cifry[i];
  296. d.cifry[i+1+k]=s/10;
  297.  
  298. d.cifry[i+k]=d.cifry[i+k] + s-d.cifry[i+1+k]*10;
  299. */ s= a.cifry[k]*b.cifry[i];
  300. d.cifry[i+1+k]=(d.cifry[i+k]+s)/10;
  301. d.cifry[i+k]=d.cifry[i+k]+s-d.cifry[i+k+1]*10;
  302. }
  303.  
  304.  
  305.  
  306. d.spocitejVelikost();
  307.  
  308. c.znamenko = '+';
  309.  
  310. c = c+d;
  311.  
  312. c.spocitejVelikost();
  313.  
  314. }
  315. if(a.znamenko==b.znamenko)
  316. c.znamenko='+';
  317. else
  318. c.znamenko='-';
  319.  
  320. return c;
  321. }
  322.  
  323.  
  324. Cislo operator*(Cislo &a, int b)
  325. {
  326. Cislo c;
  327. char buf[16];
  328. sprintf(buf,"%d",b);
  329. const char* p = buf;
  330. c.naplnit(p);
  331.  
  332. return c*a;
  333. }
  334.  
  335.  
  336.  
  337.  
  338. Cislo operator+(Cislo &a, int b)
  339. {
  340. Cislo c;
  341. char buf[16];
  342. sprintf(buf,"%d",b);
  343. const char* p = buf;
  344. c.naplnit(p);
  345. c=c+a;
  346. return c;
  347. }
  348.  
  349. Cislo operator/(Cislo &a, Cislo &b)
  350. {
  351.  
  352. Cislo delenec=a;
  353. Cislo delitel=b;
  354. delenec.znamenko='+';
  355. delitel.znamenko='+';
  356. Cislo pomocnik;
  357.  
  358. Cislo vysledek;
  359.  
  360.  
  361. int idx =0;
  362. pomocnik.cifry[0] = delenec.cifry[delenec.size -1 -(idx)];
  363. pomocnik.spocitejVelikost();
  364.  
  365. // cout << endl;
  366. while (pomocnik < delitel)
  367. {
  368. int pomoc = delenec.cifry[delenec.size -1 -(++idx)];
  369. // pomocnik.print();
  370. pomocnik = pomocnik * 10;
  371. pomocnik.znamenko='+';
  372. pomocnik=pomocnik + pomoc;
  373. pomocnik.znamenko='+';
  374.  
  375. // cout << pomoc << endl;
  376.  
  377. }
  378.  
  379. while (delenec.size >idx)
  380. {
  381.  
  382. ++idx;
  383. cout<< idx;
  384. int pomoc=1;
  385.  
  386.  
  387. while(pomoc<11)
  388. {
  389.  
  390. if((delitel*pomoc)>pomocnik)
  391. { vysledek = vysledek*10;
  392. vysledek.znamenko='+';
  393. vysledek = vysledek + (pomoc-1);
  394. vysledek.znamenko='+';
  395. //
  396.  
  397. int pomoc2 = delenec.cifry[delenec.size -1 -(idx)];
  398. Cislo delitel_pomoc=delitel*(pomoc-1);
  399. delitel_pomoc.znamenko='+';
  400. pomocnik= pomocnik-delitel_pomoc;
  401. pomocnik.znamenko='+';
  402. pomocnik= pomocnik*10;
  403. pomocnik.znamenko='+';
  404. pomocnik= pomocnik + pomoc2;
  405. pomocnik.znamenko='+';
  406.  
  407.  
  408.  
  409. }
  410. pomoc++;
  411. }
  412. }
  413. vysledek.spocitejVelikost();
  414. // když je delitel vetsi než delenec
  415. if (vysledek.size == 0)
  416. //CHYBA
  417. cout << "vysoký delitel";
  418. if(a.znamenko==b.znamenko)
  419. vysledek.znamenko='+';
  420. else
  421. vysledek.znamenko='-';
  422.  
  423. return vysledek;
  424. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement