Untitled
unknown
plain_text
3 years ago
2.7 kB
5
Indexable
//Code by: Kien KK //date: 03/06/2015 //visual studio 2013 #include<iostream> #include<conio.h> #include<stdio.h> #include<stdlib.h> typedef struct { int code; char name[30]; int amount; float price; }mathang; typedef struct Node{ struct Node *lc, *rc; mathang data; }node; typedef node *Tree; node *getnode() { node *p; p = (node*)malloc(sizeof(node)); p->lc = NULL; p->rc = NULL; return p; } int insert(node *tr, node *root) { if (tr->data.code == root->data.code) return 0; if (tr->data.code > root->data.code) { if (tr->lc == NULL) tr->lc = root; else{ insert(tr->lc, root); } } if (tr->data.code < root->data.code) { if (tr->rc == NULL) tr->rc = root; else{ insert(tr->rc, root); } } return 1; } void LNR(node *tr) { if (tr != NULL){ LNR(tr->lc); printf("\nTen mat hang %s\nMa hang %d\nSo luong %d\nGia tien %f ", tr->data.name, tr->data.code, tr->data.amount, tr->data.price); LNR(tr->rc); } } void HangDongGia(node *tr, float gia) { if (tr != NULL) { if (tr->data.price == gia) printf("\nTen mat hang %s\nMa hang %d\nSo luong %d\nGia tien %f ", tr->data.name, tr->data.code, tr->data.amount, tr->data.price); HangDongGia(tr->lc, gia); HangDongGia(tr->rc, gia); } } int TinhTongMatHang(Tree c){ if (c != NULL){ int a = TinhTongMatHang(c->lc); int b = TinhTongMatHang(c->rc); return c->data.amount + a + b; } return 0; } float TinhTongGiaTien(Tree c){ if (c != NULL){ float a = TinhTongGiaTien(c->lc); float b = TinhTongGiaTien(c->rc); return c->data.price + a + b; } return 0; } int main() { node *root, *p; root = NULL; int n, x, i, kt = 0; printf("Nhap so luong mat hang: "); scanf("%d", &n); printf("--------------------------------------\n"); for (i = 0; i < n; i++) { p = getnode(); do{ printf("Nhap mat hang thu %d: \n", i + 1); fflush(stdin); printf("nhap ten mat hang: "); gets(p->data.name); printf("Nhap ma hang: "); scanf("%d", &x); p->data.code = x; printf("Nhap so luong hang: "); scanf("%d", &x); p->data.amount = x; printf("Nhap gia tien hang: "); scanf("%d", &x); p->data.price = x; if (root == NULL) { root = p; kt = 1; } else{ kt = insert(root, p); } } while (kt == 0); } printf("Danh sach LNR: \n"); LNR(root); printf("\n Nhap vao gia tien:"); float k; scanf("%f", &k); HangDongGia(root, k); printf("\n Tong so luong hang: %d", TinhTongMatHang(root)); printf("\n Tong gia tiet cac mat hang la: %f", TinhTongGiaTien(root)); getch(); return 0; }
Editor is loading...