Advertisement
Guest User

Untitled

a guest
Mar 16th, 2019
282
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 9.71 KB | None | 0 0
  1. #include <iostream>
  2. #include <fstream>
  3. using namespace std;
  4. ifstream f ("date.in");
  5. class Lista;
  6. class Numar;
  7. class Fractie;
  8. class Nod
  9. {
  10. int info, size;
  11. Nod *next;
  12. public:
  13. Nod ();
  14. ~Nod();
  15. Nod (Nod*);
  16. void operator= (const Nod*);
  17. int getInfo ();
  18. int getSize();
  19. Nod *getNext ();
  20. friend class Lista;
  21. friend class Numar;
  22. friend class Fractie;
  23. };
  24.  
  25. class Lista
  26. {
  27. Nod *prim, *ultim;
  28. public:
  29. Lista();
  30. ~Lista();
  31. Lista(Lista*);
  32. void addSf (int);
  33. void addInc (int);
  34. Nod* getPrim ();
  35. void operator= (const Lista*);
  36. friend class Numar;
  37. friend class Fractie;
  38. };
  39. class Numar
  40. {
  41. Lista *L;
  42. public:
  43. Numar();
  44. Numar(Lista*);
  45. friend istream &operator>> (istream&, Numar*);
  46. friend ostream &operator<< (ostream&, Numar*);
  47. Numar *operator+ (const Numar*);
  48. void operator= (const Numar*);
  49. Numar *operator- (Numar*);
  50. Numar *operator* (Numar*);
  51. Numar *operator/ (Numar*);
  52. Numar *addInt (int);
  53. Numar *multiply (int);
  54. int getLength ();
  55. int cmp(Numar*, Numar*);
  56. Numar *copyNumar(Numar*);
  57. Numar* operator% (Numar*);
  58. Numar* Euclid (Numar*, Numar*);
  59. friend class Fractie;
  60. };
  61. class Fractie
  62. {
  63. char semn;
  64. Numar *numarator, *numitor;
  65. public:
  66. Fractie();
  67. ~Fractie();
  68. friend istream &operator>> (istream&, Fractie*);
  69. friend ostream &operator<< (ostream&, Fractie*);
  70. Fractie* operator+ (Fractie*);
  71. Fractie* operator- (Fractie*);
  72. Fractie* operator* (Fractie*);
  73. Fractie* operator/ (Fractie*);
  74. Fractie* ireductibila (Fractie*);
  75. };
  76. int main ()
  77. {
  78. Lista *L1 = new Lista;
  79. Numar *n1 = new Numar(L1);
  80. f>>n1;
  81. cout<<n1;
  82.  
  83. /*int k = 87;
  84. Lista *a = new Lista;
  85. Numar *aux = new Numar(a);
  86. aux = n1->multiply(k); // aux = n1 * k
  87. cout<<"numarul inmultit cu "<<k<<" are valoarea "<<aux;*/
  88.  
  89. Lista *L2 = new Lista;
  90. Numar *n2 = new Numar(L2);
  91. f>>n2;
  92. cout<<n2;
  93.  
  94. Lista *L3 = new Lista;
  95. Numar *s = new Numar(L3);
  96. s = *n1 + n2;
  97. cout<<"suma este "<<s;
  98.  
  99. Lista *L4 = new Lista;
  100. Numar *p = new Numar(L4);
  101. p = *n1 * n2;
  102. cout<<"produsul este "<<p;
  103.  
  104. Lista *L5 = new Lista;
  105. Numar *dif = new Numar(L5);
  106. //!!! N1>N2
  107. dif = *n1 - n2;
  108. cout<<"diferenta este "<<dif;
  109.  
  110. Lista *L6 = new Lista;
  111. Numar *cat = new Numar(L6);
  112. cat = *n1 / n2;
  113. cout<<"catul este "<<cat;
  114.  
  115. Lista *L7 = new Lista;
  116. Numar *rest = new Numar(L7);
  117. rest = *n1 % n2;
  118. cout<<"restul este "<<rest;
  119. }
  120. void Nod :: operator= (const Nod* n)
  121. {
  122. this->info = n->info;
  123. this->next = n->next;
  124. }
  125. Nod :: Nod() //constructor
  126. {
  127. info = 0;
  128. size = 0;
  129. next = NULL;
  130. }
  131. Nod :: ~Nod() //destructor
  132. {
  133. delete this;
  134. }
  135. Nod :: Nod (Nod *p)
  136. {
  137. this->info = p->info;
  138. this->size = p->size;
  139. this->next = p->next;
  140. }
  141. Lista :: Lista()
  142. {
  143. prim = ultim = NULL;
  144. }
  145. void Lista :: operator= (const Lista* L)
  146. {
  147. this->prim = L->prim;
  148. this->ultim = L->ultim;
  149. }
  150. Lista :: ~Lista ()
  151. {
  152. Nod *aux = prim;
  153. while(aux)
  154. {
  155. Nod *urm = aux->next;
  156. delete aux;
  157. aux = urm;
  158. }
  159. prim = NULL;
  160. }
  161. Lista :: Lista (Lista *p)
  162. {
  163. this->prim = p->prim;
  164. this->ultim = p->ultim;
  165. }
  166. Fractie :: Fractie ()
  167. {
  168. semn = '+';
  169. this->numarator = 0;
  170. this->numitor = this->numarator + 1; //=1 ?
  171. }
  172. Numar :: Numar (Lista *List)
  173. {
  174. L = List;
  175. }
  176. void Lista :: addInc(int x)
  177. {
  178. Nod *p = new Nod;
  179. p -> info = x;
  180. p->size ++;
  181. p -> next = prim;
  182. prim = p;
  183. }
  184. void Lista :: addSf (int x)
  185. {
  186. Nod *p = new Nod;
  187. p->info = x;
  188. p->size ++;
  189. p->next = NULL;
  190. if (this->prim == NULL)
  191. {
  192. prim = p;
  193. ultim = prim;
  194. }
  195. else
  196. {
  197. ultim->next = p;
  198. ultim = p;
  199. }
  200. }
  201. istream &operator>> (istream &in, Fractie *fr)
  202. {
  203. char semn;
  204. in>>semn;
  205. in>>fr->numarator;
  206. in>>fr->numitor;
  207. return in;
  208. }
  209. istream &operator>> (istream &in, Numar *nr)
  210. {
  211. cout<<"introdu cifrele (pana la -1)"<<endl;
  212. int x;
  213. in>>x;
  214. while (x != -1) //nr se termina cand se introduce -1
  215. {
  216. nr->L->addInc(x);
  217. in>>x;
  218. }
  219. return in;
  220. }
  221. Nod* Lista :: getPrim ()
  222. {
  223. return this->prim;
  224. }
  225. int Nod :: getInfo ()
  226. {
  227. return this->info;
  228. }
  229. int Nod :: getSize ()
  230. {
  231. return this->size;
  232. }
  233. int Numar :: getLength ()
  234. {
  235. int length = 0;
  236. Nod *p = this->L->prim;
  237. while (p)
  238. {
  239. length++;
  240. p = p->next;
  241. }
  242. return length;
  243. }
  244. Numar *Numar :: operator- (Numar *nr2)
  245. {
  246. Lista *L = new Lista;
  247. Numar *rezultat = new Numar(L);
  248.  
  249. Nod *p1 = this->L->prim;
  250. Nod *p2 = nr2->L->prim;
  251. int c = 0, s;
  252. while (p1 || p2)
  253. {
  254. if (p1 && p2)
  255. {
  256. if(p1->info +c >= p2->info)
  257. {
  258. s = p1->info + c - p2->info;
  259. c = 0;
  260. }
  261. else
  262. {
  263. s = p1->info + c +10 - p2->info;
  264. c = -1;
  265. }
  266. p1 = p1->next;
  267. p2 = p2->next;
  268. }
  269. else if (p1 && p2 == NULL)
  270. {
  271. if(p1->info >= 1)
  272. {
  273. s = p1->info + c;
  274. c = 0;
  275. }
  276. else
  277. {
  278. if (c)
  279. {
  280. s = p1->info + 10 +c;
  281. c = -1;
  282. }
  283. else
  284. s = p1->info;
  285. }
  286. p1 = p1->next;
  287. }
  288. rezultat->L->addInc(s);
  289. }
  290. return rezultat;
  291. }
  292. Nod* Nod :: getNext ()
  293. {
  294. return this->next;
  295. }
  296. ostream &operator<< (ostream &out, Numar *nr)
  297. {
  298. Nod *p = nr->L->getPrim();
  299. while(p)
  300. {
  301. int data = p->getInfo();
  302. out<<data;
  303. p = p->getNext();
  304. }
  305. out<<endl;
  306. return out;
  307. }
  308. void Numar :: operator= (const Numar *nr)
  309. {
  310. this->L = nr->L;
  311. }
  312. Numar *Numar :: operator+ (Numar const *nr2)
  313. {
  314. Lista *L = new Lista;
  315. Numar *rezultat = new Numar(L);
  316. int t = 0, x;
  317. Nod *p1 = this->L->prim;
  318. Nod *p2 = nr2->L->prim;
  319. while (p1 && p2)
  320. {
  321. x = (p1->info + p2->info + t)%10;
  322. rezultat->L->addInc(x);
  323. t = (p1->info + p2->info + t)/10;
  324. p1 = p1->next;
  325. p2 = p2->next;
  326. }
  327. while (p1)
  328. {
  329. x = (p1->info + t)%10;
  330. rezultat->L->addInc(x);
  331. t = (p1->info + t)/10;
  332. p1 = p1->next;
  333. }
  334. while (p2)
  335. {
  336. x = (p2->info + t)%10;
  337. rezultat->L->addInc(x);
  338. t = (p2->info + t)/10;
  339. p2 = p2->next;
  340. }
  341. if(t)
  342. rezultat->L->addInc(t);
  343. return rezultat;
  344. }
  345. Numar *Numar :: addInt (int k)
  346. {
  347. Lista *L = new Lista;
  348. Numar *rezultat = new Numar(L);
  349. int x, t = 0;
  350. Nod *p = this->L->prim;
  351. while(p)
  352. {
  353. x = (t + p->info + k)%10;
  354. rezultat->L->addInc(x);
  355. t = (t + p->info + k)/10;
  356. p = p->next;
  357. }
  358. if (t)
  359. rezultat->L->addInc(t);
  360. return rezultat;
  361. }
  362. Numar *Numar :: multiply (int k)
  363. {
  364. Lista *L = new Lista;
  365. Numar *rezultat = new Numar(L);
  366. int x, t = 0;
  367. Nod *p1 = this->L->prim;
  368. while (p1)
  369. {
  370. x = (t + p1->info * k)%10;
  371. rezultat->L->addInc(x);
  372. t = (t + p1->info * k)/10;
  373. p1 = p1->next;
  374. }
  375. if(t)
  376. rezultat->L->addInc(t);
  377. return rezultat;
  378. }
  379. Numar *Numar :: operator* (Numar *nr2)
  380. {
  381. Lista *L = new Lista;
  382. Numar *rezultat = new Numar(L);
  383. Lista *aux = new Lista;
  384. Numar *rez2 = new Numar(aux);
  385. int t = 1;
  386. Nod *p2 = this->L->prim;
  387. while (p2)
  388. {
  389. rez2 = nr2->multiply(p2->info * t); // rez2 = nr2 * p2->info * t
  390. cout<<"rez2 "<<rez2;
  391. rezultat = *rezultat + rez2;
  392. cout<<"rezultat "<<rezultat;
  393. t *= 10;
  394. p2 = p2->next;
  395. }
  396. return rezultat;
  397. }
  398. Numar* Numar :: copyNumar (Numar *n)
  399. {
  400. Lista *L = new Lista;
  401. Numar *rezultat = new Numar(L);
  402. Nod *p = n->L->prim;
  403. while (p)
  404. {
  405. rezultat->L->addSf(p->info);
  406. p = p->next;
  407. }
  408. return rezultat;
  409. }
  410. int Numar :: cmp(Numar *a, Numar *b)
  411. {
  412. if(a->getLength() != b->getLength())
  413. if(a->getLength() > b->getLength())
  414. return 1;
  415. else
  416. return 0;
  417. else
  418. {
  419. Nod *p1 = a->L->prim;
  420. Nod *p2 = b->L->prim;
  421. while(p1 && p2)
  422. {
  423. if(p1->info > p2->info)
  424. return 1;
  425. else if (p1->info < p2->info)
  426. return 0;
  427. else
  428. {
  429. p1 = p1->next;
  430. p2 = p2->next;
  431. }
  432. }
  433. return 2;
  434. }
  435. }
  436. Numar* Numar :: operator/ (Numar *nr2)
  437. {
  438. if(nr2->L->ultim == nr2->L->prim && nr2->L->ultim->info == 0)
  439. {
  440. cout<<"imposibil de impartit la 0 ";
  441. return 0;
  442. }
  443. long long aux = 0;
  444. while (this - nr2)
  445. {
  446. aux++;
  447. *this = *this - nr2;
  448. }
  449. Lista *L = new Lista;
  450. Numar *rezultat = new Numar(L);
  451. while (aux)
  452. {
  453. rezultat->L->addInc(aux % 10);
  454. aux /= 10;
  455. }
  456. return rezultat;
  457. }
  458. Numar* Numar :: operator% (Numar *nr2)
  459. {
  460. if(nr2->L->ultim == nr2->L->prim && nr2->L->ultim->info == 0)
  461. {
  462. cout<<"imposibil de impartit la 0 ";
  463. return 0;
  464. }
  465.  
  466. while (this - nr2)
  467. *this = *this - nr2;
  468. return this;
  469. }
  470. Numar* Numar :: Euclid (Numar *n1, Numar *n2)
  471. {
  472. Lista *L = new Lista;
  473. Numar *c = new Numar(L);
  474. while (n2)
  475. {
  476. c = *n1 % n2;
  477. n1 = n2;
  478. n2 = c;
  479. }
  480. return n1;
  481. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement