Advertisement
huutho_96

BIGINT Bản Đầy Đủ Toán Tử

May 25th, 2015
861
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 8.21 KB | None | 0 0
  1. //https://www.facebook.com/pages/C%C3%B9ng-h%E1%BB%8Dc-l%E1%BA%ADp-tr%C3%ACnh/632038696941833
  2. #include <iostream>
  3. #include <conio.h>
  4. using namespace std;
  5. struct Node
  6. {
  7. int x;
  8. Node *pNext;
  9. };
  10. struct List
  11. {
  12. Node *pHead, *pTail;
  13. };
  14. void CreateList(List &L)
  15. {
  16. L.pHead = L.pTail = NULL;
  17. }
  18. Node* CreateNode(int x)
  19. {
  20. Node *p = new Node;
  21. if (p == NULL) exit(1);
  22. p->x = x;
  23. p->pNext = NULL;
  24. return p;
  25. }
  26. void AddHead(List &L, Node *p)
  27. {
  28. if (L.pHead == NULL)
  29. {
  30. L.pHead = L.pTail = p;
  31. }
  32. else
  33. {
  34. p->pNext = L.pHead;
  35. L.pHead = p;
  36. }
  37. }
  38. void DelHead(List &L)
  39. {
  40. Node *p;
  41. if (L.pHead == NULL) return;
  42. p = L.pHead;
  43. L.pHead = p->pNext;
  44. delete p;
  45. if (L.pHead == NULL) L.pTail = NULL;
  46. }
  47. void DelTail(List &L)
  48. {
  49. Node *p = L.pHead;
  50. Node *pBef = NULL;
  51. if (p == NULL) return;
  52. if (L.pHead == L.pTail)
  53. {
  54. L.pHead = L.pTail = NULL;
  55. delete p;
  56. return;
  57. }
  58. while (p != L.pTail)
  59. {
  60. pBef = p;
  61. p = p->pNext;
  62. }
  63. pBef->pNext = NULL;
  64. L.pTail = pBef;
  65. }
  66. void Input(List &L)
  67. {
  68. int x;
  69. Node *p = NULL;
  70. cout << endl;
  71. cout << "nhap so nguyen: ";
  72. do
  73. {
  74. x = getch();
  75. if (x >= 48 && x <= 57)
  76. {
  77. x = x - 48;
  78. cout << x;
  79. p = CreateNode(x);
  80. AddHead(L, p);
  81. }
  82. else
  83. {
  84. if (x == 8)
  85. {
  86. if (L.pHead != NULL)
  87. {
  88. p = L.pHead;
  89. L.pHead = L.pHead->pNext;
  90. delete p;
  91. putch(8);
  92. putch(' ');
  93. putch(8);
  94. }
  95. }
  96. }
  97. } while (x != 13);
  98. }
  99. void Output(List &L)
  100. {
  101. Node *p = L.pHead;
  102. while (p != NULL)
  103. {
  104. cout << p->x;
  105. p = p->pNext;
  106. }
  107. }
  108. void AddTail(List &L, Node *p)
  109. {
  110. if (L.pHead == NULL)
  111. {
  112. L.pHead = L.pTail = p;
  113. }
  114. else
  115. {
  116. L.pTail->pNext = p;
  117. L.pTail = p;
  118. }
  119. }
  120. int ListLen(List L)
  121. {
  122. int dem = 0;
  123. Node *p = L.pHead;
  124. while (p != NULL)
  125. {
  126. dem++;
  127. p = p->pNext;
  128. }
  129. return dem;
  130. }
  131. int SoSanhListCungDoDai(Node *p1, Node *p2)
  132. {
  133. int t;
  134. if (p1->pNext != NULL && p2->pNext != NULL)
  135. {
  136. t = SoSanhListCungDoDai(p1->pNext, p2->pNext);
  137. if (t != 0) return t;
  138. }
  139. if ((p1->pNext == NULL && p2->pNext == NULL) || t == 0)
  140. {
  141. if (p1->x > p2->x) return 1;
  142. if (p1->x < p2->x) return -1;
  143. return 0;
  144. }
  145. }
  146. int SoSanhList(List L1, List L2)
  147. {
  148. int len1 = ListLen(L1), len2 = ListLen(L2);
  149. if (len1 > len2) return 1;
  150. if (len1 == len2) return SoSanhListCungDoDai(L1.pHead, L2.pHead);
  151. if (len1 < len2) return -1;
  152. }
  153. void CongList(List L1, List L2, List &L3)
  154. {
  155. Node *p1 = L1.pHead, *p2 = L2.pHead;
  156. Node *p3 = NULL;
  157. int r = 0;
  158. int x;
  159. while (p1 != NULL && p2 != NULL)
  160. {
  161. x = p1->x + p2->x + r;
  162. if (x >= 10)
  163. {
  164. r = 1;
  165. x = x - 10;
  166. }
  167. else
  168. r = 0;
  169. p3 = CreateNode(x);
  170. AddHead(L3, p3);
  171. p1 = p1->pNext;
  172. p2 = p2->pNext;
  173. }
  174. while (p1 != NULL)
  175. {
  176. x = p1->x + r;
  177. if (x >= 10)
  178. {
  179. r = 1;
  180. x = x - 10;
  181. }
  182. else
  183. r = 0;
  184. p3 = CreateNode(x);
  185. AddHead(L3, p3);
  186. p1 = p1->pNext;
  187. }
  188. while (p2 != NULL)
  189. {
  190. x = p2->x + r;
  191. if (x >= 10)
  192. {
  193. r = 1;
  194. x = x - 10;
  195. }
  196. else
  197. r = 0;
  198. p3 = CreateNode(x);
  199. AddHead(L3, p3);
  200. p2 = p2->pNext;
  201. }
  202. if (r == 1)
  203. {
  204. p3 = CreateNode(1);
  205. AddHead(L3, p3);
  206. }
  207. }
  208. void TruList(List L1, List L2, List &L3)
  209. {
  210. List L;
  211. int dau = 1;
  212. if (SoSanhList(L1, L2) == -1)
  213. {
  214. L.pHead = L1.pHead;
  215. L1.pHead = L2.pHead;
  216. L2.pHead = L.pHead;
  217. dau = -1;
  218.  
  219. }
  220. Node *p1 = L1.pHead, *p2 = L2.pHead, *p3 = NULL;
  221. int r = 0, x;
  222. while (p1 != NULL && p2 != NULL)
  223. {
  224. x = p1->x - p2->x - r;
  225. if (x < 0)
  226. {
  227. r = 1;
  228. x = x + 10;
  229. }
  230. else
  231. r = 0;
  232. p3 = CreateNode(x);
  233. AddHead(L3, p3);
  234.  
  235. p1 = p1->pNext;
  236. p2 = p2->pNext;
  237. }
  238. while (p1 != NULL)
  239. {
  240. x = p1->x - r;
  241. if (x < 0)
  242. {
  243. r = 1;
  244. x = x + 10;
  245. }
  246. else
  247. r = 0;
  248. p3 = CreateNode(x);
  249. AddHead(L3, p3);
  250. p1 = p1->pNext;
  251. }
  252. p3 = L3.pHead;
  253. while (L3.pHead != NULL)
  254. {
  255. if (L3.pHead->x == 0)
  256. {
  257. p2 = L3.pHead;
  258. L3.pHead = L3.pHead->pNext;
  259. delete p2;
  260. if (ListLen(L3) == 1)
  261. {
  262. if (L3.pHead->x == 0) dau = 0;
  263. break;
  264. }
  265. }
  266. else
  267. break;
  268. }
  269. if (L3.pHead) L3.pHead->x *= dau;
  270. else
  271. CreateList(L3);
  272. }
  273. void Chen(List &L)
  274. {
  275. AddHead(L, CreateNode(0));
  276. }
  277. void HuyList(List &L1)
  278. {
  279. Node *p1 = L1.pHead;
  280. while (L1.pHead != NULL)
  281. {
  282. L1.pHead = L1.pHead->pNext;
  283. delete p1;
  284. p1 = L1.pHead;
  285. }
  286. L1.pTail = NULL;
  287. }
  288. void CopyList(List L, List &L1)
  289. {
  290.  
  291. Node *p = L.pHead;
  292. while (p != NULL)
  293. {
  294. AddHead(L1, CreateNode(p->x));
  295. p = p->pNext;
  296. }
  297. }
  298. void NhanListVoiMotSo(List L, List &L1, int x)
  299. {
  300. Node *p = L.pHead;
  301. int r = 0;
  302. int n;
  303. while (p != NULL)
  304. {
  305. n = p->x * x + r;
  306. if (n >= 10)
  307. {
  308. r = n / 10;
  309. n %= 10;
  310. }
  311. else
  312. r = 0;
  313. AddTail(L1, CreateNode(n));
  314. p = p->pNext;
  315. }
  316.  
  317. if (r != 0)
  318. {
  319. AddTail(L1, CreateNode(r));
  320. }
  321. }
  322. void DaoList(List &L)
  323. {
  324. Node *p1 = L.pHead;
  325. List L1;
  326. CreateList(L1);
  327. while (p1 != NULL)
  328. {
  329. AddHead(L1, CreateNode(p1->x));
  330. p1 = p1->pNext;
  331. }
  332. HuyList(L);
  333. L.pHead = L1.pHead;
  334. L.pTail = L1.pTail;
  335. CreateList(L1);
  336. }
  337. void NhanList(List L1, List L2, List &L3)
  338. {
  339. Node *p1 = L1.pHead, *p2 = L2.pHead, *p3 = NULL;
  340. int x = 0;
  341. int r = 0;
  342. int dem = 0;
  343. List L, L4;
  344. CreateList(L);
  345. CreateList(L4);
  346. while (p2 != NULL)
  347. {
  348. NhanListVoiMotSo(L1, L, p2->x);
  349. for (int i = 0; i < dem; i++) Chen(L);
  350.  
  351. CongList(L, L3, L4);
  352. HuyList(L);
  353. HuyList(L3);
  354. CopyList(L4, L3);
  355. HuyList(L4);
  356. p2 = p2->pNext;
  357. dem++;
  358. }
  359. DaoList(L3);
  360. }
  361. int CopyListNode(List &L, Node *&p, int n)
  362. {
  363. int i = 0;
  364. while (p && i < n)
  365. {
  366. AddHead(L, CreateNode(p->x));
  367. p = p->pNext;
  368. i++;
  369. }
  370. if (i < n) return 0;
  371. return 1;
  372. }
  373. int SoSanhListXuoi(List L1, List L2)
  374. {
  375. int len1 = ListLen(L1);
  376. int len2 = ListLen(L2);
  377. if (len1 > len2) return 1;
  378. if (len1 < len2) return -1;
  379. Node *p1 = L1.pHead;
  380. Node *p2 = L2.pHead;
  381. while (p1 && p2)
  382. {
  383. if (p1->x > p2->x) return 1;
  384. if (p1->x < p2->x) return -1;
  385. p1 = p1->pNext;
  386. p2 = p2->pNext;
  387. }
  388. if (p1 == NULL && p2) return -1;
  389. if (p1 && p2 == NULL) return 1;
  390. return 0;
  391. }
  392.  
  393. void ChiaListLay1So(List L1, List L2, int &KetQua)
  394. {
  395. //L1, L2 la list nguoc
  396. int t;
  397. List Tam;
  398. CreateList(Tam);
  399. List L;
  400. CreateList(L);
  401. CopyList(L2, L);
  402. List L3;
  403. CreateList(L3);
  404. CopyList(L1, L3);
  405. //Tam, L va L3 la list xuoi
  406. while (L3.pHead != NULL && L3.pHead->x == 0) DelHead(L3);
  407. DaoList(L);
  408. for (int i = 1; i <= 10; i++)
  409. {
  410. //dua vao list nguoc, tra ve list nguoc
  411. NhanListVoiMotSo(L, Tam, i);
  412. DaoList(Tam);
  413. t = SoSanhListXuoi(L3, Tam);
  414. if (t <= 0)
  415. {
  416. if (t == 0)
  417. {
  418. KetQua = i;
  419. }
  420. else
  421. KetQua = i - 1;
  422. return;
  423. }
  424. else
  425. HuyList(Tam);
  426. }
  427. }
  428. void ChiaList(List L1, List &L2, List &L3)
  429. {
  430. //L1, L2 list nguoc
  431. int len = ListLen(L2);
  432. List Tam;
  433. CreateList(Tam);
  434. List L4;
  435. CreateList(L4);
  436. List L5;
  437. CreateList(L5);
  438. DaoList(L1);//L1 list xuoi
  439. Node *p1 = L1.pHead;
  440. int t = CopyListNode(Tam, p1, len);//list xuoi
  441. if (t == 0)
  442. {
  443. AddHead(L3, CreateNode(0));
  444. return;
  445. }
  446. //l2 list nguoc
  447. //tam là list nguoc
  448. while (1)
  449. {
  450.  
  451. ChiaListLay1So(Tam, L2, t);
  452. NhanListVoiMotSo(L2, L4, t);//list cung chieu
  453. if (t)
  454. {
  455. while (L4.pHead != L4.pTail && L4.pTail->x == 0)
  456. DelTail(L4);
  457. TruList(Tam, L4, L5);
  458. //L5 xuoi
  459. if (L5.pHead && L5.pHead->x == 0)
  460. {
  461. HuyList(Tam);
  462. HuyList(L5);
  463. }
  464. else
  465. if (L5.pHead && L5.pHead->x > 0)
  466. {
  467. Tam.pHead = L5.pHead;
  468. Tam.pTail = L5.pTail;
  469. CreateList(L5);
  470. }
  471. DaoList(Tam);
  472. }
  473. HuyList(L4);
  474. AddTail(L3, CreateNode(t));
  475. if (p1 == NULL) break;
  476. AddHead(Tam, CreateNode(p1->x));
  477. p1 = p1->pNext;
  478. }
  479. DaoList(L2);
  480. if (L3.pHead->x == 0) DelHead(L3);
  481. }
  482.  
  483.  
  484.  
  485. void main()
  486. {
  487. system("start https://www.facebook.com/pages/C%C3%B9ng-h%E1%BB%8Dc-l%E1%BA%ADp-tr%C3%ACnh/632038696941833");
  488. List L1, L2, L3, L4;
  489. int dau;
  490. CreateList(L1);
  491. CreateList(L2);
  492. CreateList(L3);
  493. Input(L1);
  494. Input(L2);
  495. CongList(L1, L2, L3);
  496. cout << endl;
  497. cout << "Cong: ";
  498. Output(L3);
  499. HuyList(L3);
  500. TruList(L1, L2, L3);
  501. cout << endl;
  502. cout << "Tru: ";
  503. Output(L3);
  504. HuyList(L3);
  505. NhanList(L1, L2, L3);
  506. cout << endl;
  507. cout << "Nhan: ";
  508. Output(L3);
  509. HuyList(L3);
  510. ChiaList(L1, L2, L3);
  511. cout << endl;
  512. cout << "Chia: ";
  513. Output(L3);
  514. getchar();
  515. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement