Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- using namespace std;
- struct PhanSo
- {
- int Tu, Mau;
- };
- struct Node
- {
- PhanSo ps;
- Node *pNext;
- };
- struct List
- {
- Node *pHead, *pTail;
- };
- void CreateList(List &L)
- {
- L.pTail = L.pHead = NULL;
- }
- Node *CreateNode(PhanSo ps)
- {
- Node *p = new Node;
- if (p)
- {
- p->ps.Mau = ps.Mau;
- p->ps.Tu = ps.Tu;
- p->pNext = NULL;
- }
- return p;
- }
- void AddTail(List &L, Node *p)
- {
- if (L.pHead == NULL)
- {
- L.pTail = L.pHead = p;
- }
- {
- L.pTail->pNext = p;
- L.pTail = p;
- }
- }
- void Input(List &L)
- {
- PhanSo ps;
- do
- {
- system("cls");
- cout << "Tu: ";
- cin >> ps.Tu;
- cout << "Mau: ";
- cin >> ps.Mau;
- if (ps.Mau == 0) return;
- AddTail(L, CreateNode(ps));
- } while (1);
- }
- void Output(List L)
- {
- Node *p = L.pHead;
- while (p)
- {
- cout << p->ps.Tu;
- if (p->ps.Tu == 0 || p->ps.Mau == 1);
- else
- cout << " / " << p->ps.Mau;
- cout << endl;
- p = p->pNext;
- }
- }
- Node *Max(List L)
- {
- if (L.pHead == NULL) return NULL;
- Node *max = L.pHead;
- Node *p = max->pNext;
- while (p)
- {
- if ((float(p->ps.Tu) / p->ps.Mau) > (float(max->ps.Tu) / max->ps.Mau)) max = p;
- p = p->pNext;
- }
- return max;
- }
- void LietKe(List L)
- {
- Node *p = L.pHead;
- while (p)
- {
- if (p->ps.Tu > p->ps.Mau)
- {
- cout << p->ps.Tu;
- if (p->ps.Tu == 0 || p->ps.Mau == 1);
- else
- cout << " / " << p->ps.Mau << endl;
- }
- p = p->pNext;
- }
- }
- int SNT(int n)
- {
- if (n < 2) return 0;
- for (int i = 2; i <= sqrt(float(n)); i++)
- {
- if (n % i == 0) return 0;
- }
- return 1;
- }
- int DemTuLaSNT(List L)
- {
- Node *p = L.pHead;
- int dem = 0;
- while (p)
- {
- if (SNT(p->ps.Tu) == 1)
- {
- dem++;
- }
- p = p->pNext;
- }
- return dem;
- }
- PhanSo Tong(List L)
- {
- Node *p = L.pHead;
- PhanSo s = {0, 1};
- while (p)
- {
- if (p->ps.Tu > p->ps.Mau)
- {
- s.Tu = s.Tu * p->ps.Mau + s.Mau * p->ps.Tu;
- s.Mau = s.Mau * p->ps.Mau;
- }
- p = p->pNext;
- }
- return s;
- }
- void Chen(List &L, Node *p, Node *q)
- {
- if (p == L.pTail) AddTail(L, q);
- else
- {
- q->pNext = p->pNext;
- p->pNext = q;
- }
- }
- void ChenSauMax(List &L, Node *p)
- {
- if (L.pHead)
- Chen(L, Max(L), p);
- }
- void main()
- {
- List L;
- CreateList(L);
- //-------------------------------------------------//
- Input(L);
- //-------------------------------------------------//
- Output(L);
- //-------------------------------------------------//
- PhanSo tong = Tong(L);
- cout << "Tong: ";
- cout << tong.Tu;
- if (tong.Tu == 0 || tong.Mau == 1);
- else
- cout << " / " << tong.Mau;
- cout << endl;
- //-------------------------------------------------//
- PhanSo a = { 5, 6 };
- ChenSauMax(L, CreateNode(a));
- //-------------------------------------------------//
- Output(L);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement