Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //https://www.facebook.com/pages/C%C3%B9ng-h%E1%BB%8Dc-l%E1%BA%ADp-tr%C3%ACnh/632038696941833
- #include <iostream>
- #include <conio.h>
- using namespace std;
- struct Node
- {
- int x;
- Node *pNext;
- };
- struct List
- {
- Node *pHead, *pTail;
- };
- void CreateList(List &L)
- {
- L.pHead = L.pTail = NULL;
- }
- Node* CreateNode(int x)
- {
- Node *p = new Node;
- if (p == NULL) exit(1);
- p->x = x;
- p->pNext = NULL;
- return p;
- }
- void AddHead(List &L, Node *p)
- {
- if (L.pHead == NULL)
- {
- L.pHead = L.pTail = p;
- }
- else
- {
- p->pNext = L.pHead;
- L.pHead = p;
- }
- }
- void Input(List &L)
- {
- int x;
- Node *p = NULL;
- cout << endl;
- cout << "nhap so nguyen: ";
- do
- {
- x = getch();
- if (x >= 48 && x <= 57)
- {
- x = x - 48;
- cout << x;
- p = CreateNode(x);
- AddHead(L, p);
- }
- else
- {
- if (x == 8)
- {
- if (L.pHead != NULL)
- {
- p = L.pHead;
- L.pHead = L.pHead->pNext;
- delete p;
- putch(8);
- putch(' ');
- putch(8);
- }
- }
- }
- } while (x != 13);
- }
- void Output(List &L)
- {
- Node *p = L.pHead;
- while (p != NULL)
- {
- cout << p->x;
- p = p->pNext;
- }
- }
- void AddTail(List &L, Node *p)
- {
- if (L.pHead == NULL)
- {
- L.pHead = L.pTail = p;
- }
- else
- {
- L.pTail->pNext = p;
- L.pTail = p;
- }
- }
- int ListLen(List L)
- {
- int dem = 0;
- Node *p = L.pHead;
- while (p != NULL)
- {
- dem++;
- p = p->pNext;
- }
- return dem;
- }
- int SoSanhListCungDoDai(Node *p1, Node *p2)
- {
- int t;
- if (p1->pNext != NULL && p2->pNext != NULL)
- {
- t = SoSanhListCungDoDai(p1->pNext, p2->pNext);
- if (t != 0) return t;
- }
- if ((p1->pNext == NULL && p2->pNext == NULL) || t == 0)
- {
- if (p1->x > p2->x) return 1;
- if (p1->x < p2->x) return -1;
- return 0;
- }
- }
- int SoSanhList(List L1, List L2)
- {
- int len1 = ListLen(L1), len2 = ListLen(L2);
- if (len1 > len2) return 1;
- if (len1 == len2) return SoSanhListCungDoDai(L1.pHead, L2.pHead);
- if (len1 < len2) return -1;
- }
- void CongList(List L1, List L2, List &L3)
- {
- Node *p1 = L1.pHead, *p2 = L2.pHead;
- Node *p3 = NULL;
- int r = 0;
- int x;
- while (p1 != NULL && p2 != NULL)
- {
- x = p1->x + p2->x + r;
- if (x >= 10)
- {
- r = 1;
- x = x - 10;
- }
- else
- r = 0;
- p3 = CreateNode(x);
- AddHead(L3, p3);
- p1 = p1->pNext;
- p2 = p2->pNext;
- }
- while (p1 != NULL)
- {
- x = p1->x + r;
- if (x >= 10)
- {
- r = 1;
- x = x - 10;
- }
- else
- r = 0;
- p3 = CreateNode(x);
- AddHead(L3, p3);
- p1 = p1->pNext;
- }
- while (p2 != NULL)
- {
- x = p2->x + r;
- if (x >= 10)
- {
- r = 1;
- x = x - 10;
- }
- else
- r = 0;
- p3 = CreateNode(x);
- AddHead(L3, p3);
- p2 = p2->pNext;
- }
- if (r == 1)
- {
- p3 = CreateNode(1);
- AddHead(L3, p3);
- }
- }
- void TruList(List L1, List L2, List &L3)
- {
- List L;
- int dau = 1;
- if (SoSanhList(L1, L2) == -1)
- {
- L.pHead = L1.pHead;
- L1.pHead = L2.pHead;
- L2.pHead = L.pHead;
- dau = -1;
- }
- Node *p1 = L1.pHead, *p2 = L2.pHead, *p3 = NULL;
- int r = 0, x;
- while (p1 != NULL && p2 != NULL)
- {
- x = p1->x - p2->x - r;
- if (x < 0)
- {
- r = 1;
- x = x + 10;
- }
- else
- r = 0;
- p3 = CreateNode(x);
- AddHead(L3, p3);
- p1 = p1->pNext;
- p2 = p2->pNext;
- }
- while (p1 != NULL)
- {
- x = p1->x - r;
- if (x < 0)
- {
- r = 1;
- x = x + 10;
- }
- else
- r = 0;
- p3 = CreateNode(x);
- AddHead(L3, p3);
- p1 = p1->pNext;
- }
- p3 = L3.pHead;
- while (L3.pHead != NULL)
- {
- if (L3.pHead->x == 0)
- {
- p2 = L3.pHead;
- L3.pHead = L3.pHead->pNext;
- delete p2;
- if (ListLen(L3) == 1)
- {
- if (L3.pHead->x == 0) dau = 0;
- break;
- }
- }
- else
- break;
- }
- L3.pHead->x *= dau;
- }
- void Chen(List &L)
- {
- AddHead(L, CreateNode(0));
- }
- void HuyList(List &L1)
- {
- Node *p1 = L1.pHead;
- while (L1.pHead != NULL)
- {
- L1.pHead = L1.pHead->pNext;
- delete p1;
- p1 = L1.pHead;
- }
- L1.pTail = NULL;
- }
- void CopyList(List L, List &L1)
- {
- Node *p = L.pHead;
- while (p != NULL)
- {
- AddHead(L1, CreateNode(p->x));
- p = p->pNext;
- }
- }
- void NhanListVoiMotSo(List L, List &L1, int x)
- {
- Node *p = L.pHead;
- int r = 0;
- int n;
- while (p != NULL)
- {
- n = p->x * x + r;
- if (n >= 10)
- {
- r = n / 10;
- n %= 10;
- }
- else
- r = 0;
- AddTail(L1, CreateNode(n));
- p = p->pNext;
- }
- if (r != 0)
- {
- AddTail(L1, CreateNode(r));
- }
- }
- void DaoList(List &L)
- {
- Node *p1 = L.pHead;
- List L1;
- CreateList(L1);
- while (p1 != NULL)
- {
- AddHead(L1, CreateNode(p1->x));
- p1 = p1->pNext;
- }
- HuyList(L);
- L.pHead = L1.pHead;
- L.pTail = L1.pTail;
- CreateList(L1);
- }
- void NhanList(List L1, List L2, List &L3)
- {
- Node *p1 = L1.pHead, *p2 = L2.pHead, *p3 = NULL;
- int x = 0;
- int r = 0;
- int dem = 0;
- List L, L4;
- CreateList(L);
- CreateList(L4);
- while (p2 != NULL)
- {
- NhanListVoiMotSo(L1, L, p2->x);
- for (int i = 0; i < dem; i++) Chen(L);
- CongList(L, L3, L4);
- HuyList(L);
- HuyList(L3);
- CopyList(L4, L3);
- HuyList(L4);
- p2 = p2->pNext;
- dem++;
- }
- DaoList(L3);
- }
- void main()
- {
- system("start https://www.facebook.com/pages/C%C3%B9ng-h%E1%BB%8Dc-l%E1%BA%ADp-tr%C3%ACnh/632038696941833");
- List L1, L2, L3, L4;
- int dau;
- CreateList(L1);
- CreateList(L2);
- CreateList(L3);
- CreateList(L4);
- Input(L1);
- Input(L2);
- TruList(L1, L2, L3);
- cout << endl;
- Output(L3);
- cout << endl;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement