fork download
  1. #include<bits/stdc++.h>
  2. using namespace std;
  3. #define MAX 8
  4. struct SV{
  5. int msv;
  6. char hodem[30];
  7. char ten[10];
  8. char gioitinh[10];
  9. int namsinh;
  10. double diemtk;
  11. };
  12. struct List{
  13. int count;
  14. SV e[MAX];
  15. };
  16. SV taosv(int ma,char* hd,char*t,char*gt,int y,double d){
  17. SV sv;
  18. sv.msv=ma;
  19. strcpy(sv.hodem,hd);
  20. strcpy(sv.ten,t);
  21. strcpy(sv.gioitinh,gt);
  22. sv.namsinh=y;
  23. sv.diemtk=d;
  24. return sv;
  25. }
  26. void create(List&L){
  27. L.count=-1;
  28. }
  29. int empty(List L){
  30. return(L.count==-1 );
  31. }
  32. int full(List L){
  33. return(L.count==MAX-1);
  34. }
  35. int add(List& L,SV sv){
  36. if(full(L)){
  37. return 0;
  38. }
  39. else{
  40. L.count++;
  41. L.e[L.count]=sv;
  42. return 1;
  43. }
  44. }
  45. void input(List &L){
  46. add(L,taosv(1001,"tran vam ","thanh","nam",1997,7.5));
  47. add(L,taosv(1002,"nguyen thi ","bich","nu",1998,7.2));
  48. add(L,taosv(1003,"nguyen van ","giang","nam",1996,6.4));
  49. add(L,taosv(1004,"bui thi","hong","nu",1998,8.6));
  50. add(L,taosv(1005,"duong van ","hung","nu",1997,6.8));
  51. }
  52. void showSV(SV sv){
  53. cout<<fixed;
  54. cout<<setw(8)<<left<<sv.msv;
  55. cout<<setw(10)<<left<<sv.hodem;
  56. cout<<setw(10)<<left<<sv.ten;
  57. cout<<setw(9)<<left<<sv.gioitinh;
  58. cout<<setw(9)<<left<<sv.namsinh;
  59. cout<<setw(8)<<setprecision<<sv.diemtk;
  60. cout<<endl;
  61. }
  62. void showList(List L ){
  63. cout<<fixed;
  64. cout<<setw(8)<<left<<"Ma sv";
  65. cout<<setw(10)<<left<<"Ho dem";
  66. cout<<setw(10)<<left<<"Ten";
  67. cout<<setw(9)<<left<<"Gioi tinh";
  68. cout<<setw(9)<<left<<"Nam sinh";
  69. cout<<setw(8)<<setprecision<<"Diem tk";
  70. cout<<endl;
  71. for(int i=0;i<=L.count;i++){
  72. showSV(L.e[i]);
  73. }
  74. }
  75. int remove(int k,List&L){
  76. if(k<=L.count+1 && k>0){
  77. for(int i=k;i<=L.count;i++){
  78. L.e[i-1]=L.e[i];
  79. }
  80. L.count--;
  81. return 1;
  82. }
  83. return 0;
  84. }
  85. int insert(int k,List& L, SV sv){
  86. if(k< L.count+1 && k>0 && !full(L)){
  87. for(int i=L.count;i>=k-1;--i){
  88. L.e[i+1]=L.e[i];
  89. }
  90. L.e[k-1]=sv;
  91. L.count++;
  92. return 1;
  93. }
  94. return 0;
  95. }
  96. void selectionSort(List &L){
  97. for(int i=0;i<L.count;i++){
  98. int min=i;
  99. for(int j=i+1;j<=L.count;j++){
  100. if(strcmp(L.e[i].ten,L.e[j].ten)<0){
  101. min=j;
  102. }
  103. }
  104. if(min!=i){
  105. SV temp=L.e[i];
  106. L.e[i]=L.e[min];
  107. L.e[min]=temp;
  108. }
  109. }
  110. }
  111. int main(int argc,char const*argv[]){
  112. List L;
  113. create(L);
  114. input(L);
  115. cout<<"==============DANH SACH SINH VIEN==============="<<endl;
  116. showList(L);
  117. if(remove(1,L)){
  118. cout<<"Xoa thanh cong...!Phan tu thu nhat da bi xoa khoi ds"<<endl;
  119. cout<<"===============DANH SACH SINH VIEN============="<<endl;
  120. showList(L);
  121. }
  122. else{
  123. cout<<"loi xu ly ,ds rong hoac ko hop le"<<endl;
  124. }
  125. if(insert(3,L,taosv(1006,"le thi","doan","nu",1998,7.6))){
  126. cout<<"chen sv thanh cong"<<endl;
  127. cout<<"=================DANH SACH SINH VIEN=============="<<endl;
  128. showList(L);
  129. }
  130. else{
  131. cout<<"DS full or vi tri ko hop le!"<<endl;
  132. }
  133. selectionSort(L);
  134. cout<<"============DANH SACH SINH VIEN sau khi sap xep==========="<<endl;
  135. showList(L);
  136. return 0;
  137. }
  138.  
Success #stdin #stdout 0.01s 5284KB
stdin
Standard input is empty
stdout
==============DANH SACH SINH VIEN===============
Ma sv   Ho dem    Ten       Gioi tinhNam sinh 1       Diem tk
1001    tran vam  thanh     nam      1997     1       7.500000
1002    nguyen thi bich      nu       1998     1       7.200000
1003    nguyen van giang     nam      1996     1       6.400000
1004    bui thi   hong      nu       1998     1       8.600000
1005    duong van hung      nu       1997     1       6.800000
Xoa thanh cong...!Phan tu thu nhat da bi xoa khoi ds
===============DANH SACH SINH VIEN=============
Ma sv   Ho dem    Ten       Gioi tinhNam sinh 1       Diem tk
1002    nguyen thi bich      nu       1998     1       7.200000
1003    nguyen van giang     nam      1996     1       6.400000
1004    bui thi   hong      nu       1998     1       8.600000
1005    duong van hung      nu       1997     1       6.800000
chen sv thanh cong
=================DANH SACH SINH VIEN==============
Ma sv   Ho dem    Ten       Gioi tinhNam sinh 1       Diem tk
1002    nguyen thi bich      nu       1998     1       7.200000
1003    nguyen van giang     nam      1996     1       6.400000
1006    le thi    doan      nu       1998     1       7.600000
1004    bui thi   hong      nu       1998     1       8.600000
1005    duong van hung      nu       1997     1       6.800000
============DANH SACH SINH VIEN sau khi sap xep===========
Ma sv   Ho dem    Ten       Gioi tinhNam sinh 1       Diem tk
1005    duong van hung      nu       1997     1       6.800000
1004    bui thi   hong      nu       1998     1       8.600000
1003    nguyen van giang     nam      1996     1       6.400000
1006    le thi    doan      nu       1998     1       7.600000
1002    nguyen thi bich      nu       1998     1       7.200000