Advertisement
huutho_96

Hocsinh

Apr 11th, 2015
410
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.93 KB | None | 0 0
  1. #include <iostream>
  2. #include <conio.h>
  3. using namespace std;
  4. struct NgaySinh
  5. {
  6. int d, m, y;
  7. };
  8. struct HocSinh
  9. {
  10. char Ten[25];
  11. float DTB;
  12. float Toan, Ly, Hoa;
  13. NgaySinh NS;
  14. };
  15. void NhapNgaySinh(NgaySinh &NS)
  16. {
  17. cout << "Ngay : ";
  18. cin >> NS.d;
  19. cout << "Thang: ";
  20. cin >> NS.m;
  21. cout << "Nam : ";
  22. cin >> NS.y;
  23. fflush(stdin);
  24. if (NS.d < 0 || NS.d > 31)
  25. {
  26. cout << "ERROR";
  27. NhapNgaySinh(NS);
  28. }
  29. else
  30. {
  31. if (NS.m == 4 || NS.m == 6 || NS.m == 9 || NS.m == 11)
  32. {
  33. if (NS.d == 31)
  34. {
  35. cout << "ERROR";
  36. NhapNgaySinh(NS);
  37. }
  38. }
  39. if (NS.m == 2)
  40. {
  41. if (NS.y % 4 == 0 && NS.y % 100 != 0)
  42. {
  43. if (NS.d > 29)
  44. {
  45. cout << "ERROR";
  46. NhapNgaySinh(NS);
  47. }
  48. }
  49. else
  50. {
  51. if (NS.y % 400 == 0)
  52. {
  53. if (NS.d > 29)
  54. {
  55. cout << "ERROR";
  56. NhapNgaySinh(NS);
  57. }
  58. }
  59. }
  60. }
  61. }
  62. }
  63. void XuatNgaySinh(NgaySinh NS)
  64. {
  65. cout << "Ngay : " << NS.d << endl;
  66. cout << "Thang: " << NS.m << endl;
  67. cout << "Nam : " << NS.y << endl;
  68. }
  69. void NhapThongTin(HocSinh &Hs)
  70. {
  71. fflush(stdin);
  72. cout << "Nhap Ten: ";
  73. cin.getline(Hs.Ten, 25);
  74. do
  75. {
  76. cout << "Toan: ";
  77. cin >> Hs.Toan;
  78. } while (Hs.Toan < 0.0 || Hs.Toan > 10.0);
  79. do
  80. {
  81. cout << "Ly : ";
  82. cin >> Hs.Ly;
  83. } while (Hs.Ly < 0.0 || Hs.Ly > 10.0);
  84. do
  85. {
  86. cout << "Hoa : ";
  87. cin >> Hs.Hoa;
  88. } while (Hs.Hoa < 0.0 || Hs.Hoa > 10.0);
  89. Hs.DTB = (Hs.Toan * 2 + Hs.Ly + Hs.Hoa) / 4;
  90. fflush(stdin);
  91. NhapNgaySinh(Hs.NS);
  92. }
  93. void XuatThongTin(HocSinh &Hs)
  94. {
  95. cout << "Ten : " << Hs.Ten << endl;
  96. cout << "Toan: " << Hs.Toan << endl;
  97. cout << "Ly : " << Hs.Ly << endl;
  98. cout << "Hoa : " << Hs.Hoa << endl;
  99. cout << "DTB : " << Hs.DTB << endl;
  100. XuatNgaySinh(Hs.NS);
  101. }
  102. void NhapDanhSach(HocSinh *Hs, int n)
  103. {
  104. for (int i = 0; i < n; i++)
  105. NhapThongTin(Hs[i]);
  106. }
  107. void XuatDanhSach(HocSinh *Hs, int n)
  108. {
  109. for (int i = 0; i < n; i++)
  110. XuatThongTin(Hs[i]);
  111. }
  112. void Swap(HocSinh &a, HocSinh &b)
  113. {
  114. HocSinh t;
  115. t = a;
  116. a = b;
  117. b = t;
  118. }
  119. void QuickSort(HocSinh Hs[], int l, int r)
  120. {
  121. int i = l, j = r;
  122. float x = Hs[(i + j) / 2].DTB;
  123. do
  124. {
  125. while (Hs[i].DTB > x) i++;
  126. while (Hs[j].DTB < x) j--;
  127. if (i <= j)
  128. {
  129. Swap(Hs[i], Hs[j]);
  130. i++;
  131. j--;
  132. }
  133. } while (i <= j);
  134. if (i < r) QuickSort(Hs, i, r);
  135. if (j > l) QuickSort(Hs, l, j);
  136. }
  137. void main()
  138. {
  139. int n;
  140. HocSinh *Hs = NULL;
  141. cout << "So sinh vien can nhap: ";
  142. cin >> n;
  143. Hs = new HocSinh[n];
  144. NhapDanhSach(Hs, n);
  145. QuickSort(Hs, 0, n - 1);
  146. int luachon;
  147. do
  148. {
  149. system("cls");
  150. cout << "1. Sap xep theo diem trung binh\n2. Danh sach nguoi co DTB < 5.0";
  151. luachon = getch();
  152. luachon -= 48;
  153. switch (luachon)
  154. {
  155. case 1:
  156. XuatDanhSach(Hs, n);
  157. break;
  158. case 2:
  159. for (int i = n - 1; i >= 0; i--)
  160. {
  161. if (Hs[i].DTB < 5.0) XuatThongTin(Hs[i]);
  162. else break;
  163. }
  164. break;
  165. default:
  166. break;
  167. }
  168. } while (luachon != 1 || luachon != 2);
  169. delete[] Hs;
  170. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement