Advertisement
Guest User

Untitled

a guest
Jun 13th, 2018
61
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.78 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <string.h>
  4.  
  5. typedef struct {
  6. char tenHang[25];
  7. int donGia;
  8. int soLuong;
  9. float thanhTien;
  10. }ThongKe;
  11.  
  12. typedef struct NODE {
  13. ThongKe SoLieu;
  14. struct NODE* next;
  15. }NODE;
  16.  
  17. typedef struct {
  18. NODE* first;
  19. NODE* last;
  20. }DS;
  21.  
  22. NODE* taoSoLieu()
  23. {
  24. NODE* node = (NODE*)calloc(1 ,sizeof(NODE));
  25. fflush(stdin);
  26. printf("\t\tTen don hang: ");
  27. gets(node->SoLieu.tenHang);
  28. fflush(stdin);
  29. do
  30. {
  31. printf("\t\tDon gia: ");
  32. scanf("%d", &node->SoLieu.donGia);
  33. }while (node->SoLieu.donGia < 0);
  34. fflush(stdin);
  35. do
  36. {
  37. printf("\t\tSo luong: ");
  38. scanf("%d", &node->SoLieu.soLuong);
  39. }while (node->SoLieu.soLuong < 0);
  40. fflush(stdin);
  41. node->next = NULL;
  42. return node;
  43. }
  44.  
  45. DS* taoDanhSach(int n)
  46. {
  47. int i;
  48. DS* ds = (DS*)calloc(1, sizeof(DS));
  49. ds->first = NULL;
  50. ds->last = NULL;
  51. NODE* node = NULL;
  52. for (i=0; i<n; i++)
  53. {
  54. printf("\t\tDon hang thu %d\n", i+1);
  55. node = taoSoLieu();
  56. if (ds->first == NULL)
  57. {
  58. ds->first = node;
  59. ds->last = node;
  60. }
  61. else
  62. {
  63. ds->last->next = node;
  64. ds->last = node;
  65. }
  66. }
  67. }
  68.  
  69. void inDanhSach(DS* ds)
  70. {
  71. int i=1;
  72. NODE* tmp = ds->first;
  73. printf("\t\t\t SO LIEU BAN HANG\n");
  74. printf("\t\t_%5s_%25s_%10s_%10s_%15s_\n", "_____", "_________________________", "__________", "__________", "_______________");
  75. printf("\t\t|%5s|%25s|%10s|%10s|%15s|\n", "STT", "Ten Hang", "Don Gia", "So Luong", "Thanh Tien");
  76. printf("\t\t|%5s+%25s+%10s+%10s+%15s|\n", "-----", "-------------------------", "----------", "----------", "---------------");
  77. while (tmp != NULL)
  78. {
  79. printf("\t\t|%5d|%25s|%10d|%10d|%15.3f|\n", i++, tmp->SoLieu.tenHang, tmp->SoLieu.donGia, tmp->SoLieu.soLuong, (float)(tmp->SoLieu.donGia * tmp->SoLieu.soLuong));
  80. tmp = tmp->next;
  81. }
  82. printf("\t\t|%5s_%25s_%10s_%10s_%15s|\n", "_____", "_________________________", "__________", "__________", "_______________");
  83. }
  84.  
  85. void giaiPhong(DS* ds)
  86. {
  87. NODE* tmp = NULL;
  88. NODE* del = NULL;
  89. del = tmp = ds->first;
  90. while (tmp != NULL)
  91. {
  92. tmp = tmp->next;
  93. free(del);
  94. del = tmp;
  95. }
  96. }
  97.  
  98. void themHang(DS* ds, NODE* node)
  99. {
  100. ds->last->next = node;
  101. ds->last = node;
  102. }
  103.  
  104. int main()
  105. {
  106. int n, chon;
  107. char select;
  108. DS* ds = NULL;
  109. label:
  110. printf("\t\t\t Bang chon\n");
  111. printf("\t\t ______________________________ \n");
  112. printf("\t\t|1. Nhap so lieu ban hang |\n");
  113. printf("\t\t|2. Bo sung so lieu ban hang |\n");
  114. printf("\t\t|3. Hien thi so lieu ban hang |\n");
  115. printf("\t\t|4. Ket thuc |\n");
  116. printf("\t\t|______________________________|\n");
  117. printf("\t\tMoi chon: ");
  118. scanf("%d", &chon);
  119. switch (chon)
  120. {
  121. case 1:
  122. {
  123. printf("\t\tNhap so luong hang: ");
  124. scanf("%d", &n);
  125. ds = taoDanhSach(n);
  126. goto label;
  127. }
  128. case 2:
  129. {
  130. NODE* node = NULL;
  131. do
  132. {
  133. node = taoSoLieu();
  134. printf("\n\t\tNhap nua khong (y/n): ");
  135. scanf("%s", &select);
  136. }
  137. while (select != 'n');
  138. printf("\n");
  139. goto label;
  140. }
  141. case 3:
  142. {
  143. inDanhSach(ds);
  144. goto label;
  145. }
  146. case 4:
  147. {
  148. giaiPhong(ds);
  149. exit(0);
  150. }
  151. default:
  152. {
  153. printf("\t\tNhap sai, nhap lai\n");
  154. goto label;
  155. }
  156. }
  157. return 0;
  158. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement