Advertisement
huutho_96

BIGINT Cộng trừ nhân

May 18th, 2015
511
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.26 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 Input(List &L)
  39. {
  40. int x;
  41. Node *p = NULL;
  42. cout << endl;
  43. cout << "nhap so nguyen: ";
  44. do
  45. {
  46. x = getch();
  47. if (x >= 48 && x <= 57)
  48. {
  49. x = x - 48;
  50. cout << x;
  51. p = CreateNode(x);
  52. AddHead(L, p);
  53. }
  54. else
  55. {
  56. if (x == 8)
  57. {
  58. if (L.pHead != NULL)
  59. {
  60. p = L.pHead;
  61. L.pHead = L.pHead->pNext;
  62. delete p;
  63. putch(8);
  64. putch(' ');
  65. putch(8);
  66. }
  67. }
  68. }
  69. } while (x != 13);
  70. }
  71. void Output(List &L)
  72. {
  73. Node *p = L.pHead;
  74. while (p != NULL)
  75. {
  76. cout << p->x;
  77. p = p->pNext;
  78. }
  79. }
  80. void AddTail(List &L, Node *p)
  81. {
  82. if (L.pHead == NULL)
  83. {
  84. L.pHead = L.pTail = p;
  85. }
  86. else
  87. {
  88. L.pTail->pNext = p;
  89. L.pTail = p;
  90. }
  91. }
  92. int ListLen(List L)
  93. {
  94. int dem = 0;
  95. Node *p = L.pHead;
  96. while (p != NULL)
  97. {
  98. dem++;
  99. p = p->pNext;
  100. }
  101. return dem;
  102. }
  103. int SoSanhListCungDoDai(Node *p1, Node *p2)
  104. {
  105. int t;
  106. if (p1->pNext != NULL && p2->pNext != NULL)
  107. {
  108. t = SoSanhListCungDoDai(p1->pNext, p2->pNext);
  109. if (t != 0) return t;
  110. }
  111. if ((p1->pNext == NULL && p2->pNext == NULL) || t == 0)
  112. {
  113. if (p1->x > p2->x) return 1;
  114. if (p1->x < p2->x) return -1;
  115. return 0;
  116. }
  117. }
  118. int SoSanhList(List L1, List L2)
  119. {
  120. int len1 = ListLen(L1), len2 = ListLen(L2);
  121. if (len1 > len2) return 1;
  122. if (len1 == len2) return SoSanhListCungDoDai(L1.pHead, L2.pHead);
  123. if (len1 < len2) return -1;
  124. }
  125. void CongList(List L1, List L2, List &L3)
  126. {
  127. Node *p1 = L1.pHead, *p2 = L2.pHead;
  128. Node *p3 = NULL;
  129. int r = 0;
  130. int x;
  131. while (p1 != NULL && p2 != NULL)
  132. {
  133. x = p1->x + p2->x + r;
  134. if (x >= 10)
  135. {
  136. r = 1;
  137. x = x - 10;
  138. }
  139. else
  140. r = 0;
  141. p3 = CreateNode(x);
  142. AddHead(L3, p3);
  143. p1 = p1->pNext;
  144. p2 = p2->pNext;
  145. }
  146. while (p1 != NULL)
  147. {
  148. x = p1->x + r;
  149. if (x >= 10)
  150. {
  151. r = 1;
  152. x = x - 10;
  153. }
  154. else
  155. r = 0;
  156. p3 = CreateNode(x);
  157. AddHead(L3, p3);
  158. p1 = p1->pNext;
  159. }
  160. while (p2 != NULL)
  161. {
  162. x = p2->x + r;
  163. if (x >= 10)
  164. {
  165. r = 1;
  166. x = x - 10;
  167. }
  168. else
  169. r = 0;
  170. p3 = CreateNode(x);
  171. AddHead(L3, p3);
  172. p2 = p2->pNext;
  173. }
  174. if (r == 1)
  175. {
  176. p3 = CreateNode(1);
  177. AddHead(L3, p3);
  178. }
  179. }
  180. void TruList(List L1, List L2, List &L3)
  181. {
  182. List L;
  183. int dau = 1;
  184. if (SoSanhList(L1, L2) == -1)
  185. {
  186. L.pHead = L1.pHead;
  187. L1.pHead = L2.pHead;
  188. L2.pHead = L.pHead;
  189. dau = -1;
  190.  
  191. }
  192. Node *p1 = L1.pHead, *p2 = L2.pHead, *p3 = NULL;
  193. int r = 0, x;
  194. while (p1 != NULL && p2 != NULL)
  195. {
  196. x = p1->x - p2->x - r;
  197. if (x < 0)
  198. {
  199. r = 1;
  200. x = x + 10;
  201. }
  202. else
  203. r = 0;
  204. p3 = CreateNode(x);
  205. AddHead(L3, p3);
  206.  
  207. p1 = p1->pNext;
  208. p2 = p2->pNext;
  209. }
  210. while (p1 != NULL)
  211. {
  212. x = p1->x - r;
  213. if (x < 0)
  214. {
  215. r = 1;
  216. x = x + 10;
  217. }
  218. else
  219. r = 0;
  220. p3 = CreateNode(x);
  221. AddHead(L3, p3);
  222. p1 = p1->pNext;
  223. }
  224. p3 = L3.pHead;
  225. while (L3.pHead != NULL)
  226. {
  227. if (L3.pHead->x == 0)
  228. {
  229. p2 = L3.pHead;
  230. L3.pHead = L3.pHead->pNext;
  231. delete p2;
  232. if (ListLen(L3) == 1)
  233. {
  234. if (L3.pHead->x == 0) dau = 0;
  235. break;
  236. }
  237. }
  238. else
  239. break;
  240. }
  241. L3.pHead->x *= dau;
  242. }
  243. void Chen(List &L)
  244. {
  245. AddHead(L, CreateNode(0));
  246. }
  247. void HuyList(List &L1)
  248. {
  249. Node *p1 = L1.pHead;
  250. while (L1.pHead != NULL)
  251. {
  252. L1.pHead = L1.pHead->pNext;
  253. delete p1;
  254. p1 = L1.pHead;
  255. }
  256. L1.pTail = NULL;
  257. }
  258. void CopyList(List L, List &L1)
  259. {
  260.  
  261. Node *p = L.pHead;
  262. while (p != NULL)
  263. {
  264. AddHead(L1, CreateNode(p->x));
  265. p = p->pNext;
  266. }
  267. }
  268. void NhanListVoiMotSo(List L, List &L1, int x)
  269. {
  270. Node *p = L.pHead;
  271. int r = 0;
  272. int n;
  273. while (p != NULL)
  274. {
  275. n = p->x * x + r;
  276. if (n >= 10)
  277. {
  278. r = n / 10;
  279. n %= 10;
  280. }
  281. else
  282. r = 0;
  283. AddTail(L1, CreateNode(n));
  284. p = p->pNext;
  285. }
  286.  
  287. if (r != 0)
  288. {
  289. AddTail(L1, CreateNode(r));
  290. }
  291. }
  292. void DaoList(List &L)
  293. {
  294. Node *p1 = L.pHead;
  295. List L1;
  296. CreateList(L1);
  297. while (p1 != NULL)
  298. {
  299. AddHead(L1, CreateNode(p1->x));
  300. p1 = p1->pNext;
  301. }
  302. HuyList(L);
  303. L.pHead = L1.pHead;
  304. L.pTail = L1.pTail;
  305. CreateList(L1);
  306. }
  307. void NhanList(List L1, List L2, List &L3)
  308. {
  309. Node *p1 = L1.pHead, *p2 = L2.pHead, *p3 = NULL;
  310. int x = 0;
  311. int r = 0;
  312. int dem = 0;
  313. List L, L4;
  314. CreateList(L);
  315. CreateList(L4);
  316. while (p2 != NULL)
  317. {
  318. NhanListVoiMotSo(L1, L, p2->x);
  319. for (int i = 0; i < dem; i++) Chen(L);
  320.  
  321. CongList(L, L3, L4);
  322. HuyList(L);
  323. HuyList(L3);
  324. CopyList(L4, L3);
  325. HuyList(L4);
  326. p2 = p2->pNext;
  327. dem++;
  328. }
  329. DaoList(L3);
  330. }
  331. void main()
  332. {
  333. system("start https://www.facebook.com/pages/C%C3%B9ng-h%E1%BB%8Dc-l%E1%BA%ADp-tr%C3%ACnh/632038696941833");
  334. List L1, L2, L3, L4;
  335. int dau;
  336. CreateList(L1);
  337. CreateList(L2);
  338. CreateList(L3);
  339. CreateList(L4);
  340. Input(L1);
  341. Input(L2);
  342. TruList(L1, L2, L3);
  343. cout << endl;
  344. Output(L3);
  345. cout << endl;
  346. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement