Nguythang

danhsachlop

Nov 30th, 2015
96
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 11.13 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <conio.h>
  3. #include <stdlib.h>
  4. #include <string.h>
  5. #include <ctype.h>
  6. struct hs
  7.     {
  8.         int CODEstudent;
  9.         char Name[26];
  10.         float ws,pt,pe,fe;
  11.     };
  12. void trim(char a[])
  13.     {
  14.         int i,j,k,h,n;
  15.         char b[100];
  16.         n = strlen(a);
  17.         i=0;
  18.         j=n-1;
  19.         while(a[i]==' ')
  20.           i++;
  21.         while(a[j]==' ')
  22.           j--;
  23.         k=0;
  24.         for(h=i;h<=j;h++)
  25.             b[k++]=a[h];
  26.         b[k]='\0';
  27.         strcpy(a,b);
  28.     }
  29. void InputValidf (float *n)
  30. {
  31.     char c;
  32.     int re, valid=0;
  33.     do{
  34.         fflush(stdin);
  35.         re= scanf ("%f%c",n,&c);
  36.         if (re<0 || re>10)
  37.             printf (" Data type errox !!\n");
  38.         else if (c != '\n')
  39.             printf ("Integer musted be inputted !!\n");
  40.         else
  41.             valid =1;
  42.         if (valid==0)
  43.             printf ("Repeat input again !\n");
  44.     } while (!valid);
  45. }
  46. int FindStd(hs a[], int &RollNo, int size)
  47. {
  48.     bool found = false;
  49.     printf("Enter RollNo to find: "); scanf("%d", &RollNo);
  50.     printf ("STT | MSV | Ten            | Workshop | Progress Test | PE   |   FE\n");
  51.     for(int i=1; i<=size; i++)
  52.         if(a[i].CODEstudent == RollNo)
  53.         {
  54.             found = true;
  55.             printf("\n%2d.  %5d %15s   %6.2f    %6.2f          %6.2f %6.2f\n", i, a[i].CODEstudent, a[i].Name, a[i].ws, a[i].pt, a[i].pe, a[i].fe);
  56.             return i;
  57.         }
  58.     if (!found)
  59.     {
  60.         printf("Roll number not found\n");
  61.         return -1;
  62.     }
  63. }
  64. void EditStd(hs a[], int &CODEstudent, int size)
  65. {
  66.     int i_found;
  67.     i_found = FindStd(a, CODEstudent, size);
  68.     if(i_found!=-1)
  69.     {
  70.         printf("MSV: "); scanf("%d", &a[i_found].CODEstudent);
  71.         fflush(stdin);
  72.         printf("FullName: "); gets(a[i_found].Name);
  73.         printf("workshop: "); scanf("%f", &a[i_found].ws);
  74.         fflush(stdin);
  75.         printf("progress test: "); scanf("%f", &a[i_found].pt);
  76.         fflush(stdin);
  77.         printf("pratical exam: "); scanf("%f", &a[i_found].pe);
  78.         fflush(stdin);
  79.         printf("final exam: "); scanf("%f", &a[i_found].fe);
  80.         fflush(stdin);
  81.     }
  82.     else
  83.         printf("Not found\n");
  84. }
  85. void DeleteStd(hs a[], int &CODEstudent, int *size)
  86. {
  87.     int i_found;
  88.     int i;
  89.     i_found = FindStd(a, CODEstudent, *size);
  90.     if(i_found!=-1)
  91.     {
  92.         for (i=i_found;i<*size;i++)
  93.         a[i]=a[i+1];
  94.         (*size)--;
  95.     }
  96.     else
  97.         printf("Not found\n");
  98. }
  99. int LoadFile(char *filename, hs a[], int *size)
  100.     {
  101.         char ch;
  102.         int i;
  103.         char ten[50], msv[50], workshop[50],pract[50], parte[50],finale[50];
  104.        
  105.         FILE *fp = fopen(filename,"r+");
  106.         if(fp==NULL)
  107.         {
  108.             printf(" file %s not found\n",filename);
  109.             return(0);
  110.         }
  111.         rewind(fp);
  112.         i=1;
  113.        
  114.         while(fscanf(fp,"%[^|]|%[^|]|%[^|]|%[^|]|%[^|]|%[^\n]\n",msv,ten,workshop,pract,parte,finale)==6)
  115.         {
  116.             trim(ten);
  117.             trim(msv);
  118.             trim(workshop);
  119.             trim(pract);
  120.             trim(parte);
  121.             trim(finale);
  122.             a[i].CODEstudent = atoi(msv);
  123.             strcpy(a[i].Name,ten);
  124.             a[i].ws = atof(workshop);
  125.             a[i].pt = atof(pract);
  126.             a[i].pe = atof(parte);
  127.             a[i].fe = atof(finale);
  128.             i++;
  129.         }
  130.         *size=i-1;
  131.         fclose(fp);
  132.         return(1);
  133.     }
  134. int SaveFile( char *filename, hs a[], int size)
  135.     {
  136.         char ch;
  137.         int i;
  138.         FILE *fp=NULL;
  139.        
  140.         fp = fopen(filename, "r+");
  141.         if(fp!=NULL)
  142.         {
  143.             printf("Replace file %s (y/n)?", filename);
  144.             fclose(fp);
  145.             ch = getche();
  146.             if(ch!='y')
  147.                 return(0);
  148.         }
  149.        
  150.         fp = fopen(filename, "w+");
  151.         if(fp!=NULL)
  152.         {
  153.             for(i=1; i<=size; i++)
  154.             {
  155.                 fprintf(fp, "\n%5d | %15s | %2.2f | %2.2f | %2.2f | %2.2f", a[i].CODEstudent, a[i].Name, a[i].ws, a[i].pt, a[i].pe, a[i].fe);
  156.             }
  157.             fclose(fp);
  158.         }
  159.         else
  160.             printf("failed");
  161.         return(1);
  162.     }
  163. int main()
  164. {
  165.     char ch1='\0',ch2='\0',ch3='\0',ch4='\0';
  166.     char ma[10];FILE *fp;char buff[255];
  167.     int stdcount=0,i,j,CODEstudent;
  168.     hs a[60];
  169.     system("cls");
  170.     fflush(stdin);
  171.     printf("********************************\n");
  172.     printf("*                              *\n");  
  173.     printf("*HE THONG QUAN LI LOP HOC      *\n");
  174.     printf("*1. Quan li danh sach sinh vien*\n");
  175.     printf("*2. Thoat                      *\n");
  176.     printf("*Moi ban nhap lua chon:        *\n");
  177.     printf("*                              *\n");
  178.     printf("*                              *\n");  
  179.     printf("********************************\n");
  180.     scanf("%c",&ch1);
  181.     fflush(stdin);
  182.     while (ch1!='2')
  183.     {      
  184.         switch (ch1)
  185.         {
  186.             case '1':
  187.             {
  188.                 system("cls");
  189.                 fflush(stdin);
  190.                 printf("******************************************************\n");
  191.                 printf("*1. In ra danh sach sinh vien                        *\n");
  192.                 printf("*2. Them mot sinh vien moi vao lop                   *\n");
  193.                 printf("*3. Tim Sinh Vien Theo Ma                            *\n");
  194.                 printf("*4. Sua, xoa mot sinh vien theo ma sinh vien         *\n");
  195.                 printf("*5. Nap danh sach tu file                            *\n");
  196.                 printf("*6. In danh sach ra file                             *\n");
  197.                 printf("*7. Bang diem cua lop                                *\n");
  198.                 printf("*8. Thong ke sinh vien trong lop                     *\n");
  199.                 printf("*9. Tro ve menu chinh                                *\n");
  200.                 printf("*Moi ban nhap lua chon:                              *\n");
  201.                 printf("******************************************************\n");
  202.                 scanf("%c",&ch2);
  203.                 switch (ch2)
  204.                 {
  205.                     case '1':
  206.                         {
  207.                         system("cls");
  208.                         fflush(stdin);
  209.                         printf("DANH SACH SINH VIEN\n");
  210.                         if (stdcount==0) printf ("Khong co sinh vien nao...");
  211.                         else
  212.                                         printf ("-----------------------------\n");
  213.                                         printf ("|STT  MSV   Ho Ten          |\n");
  214.                             {
  215.                                 for (i=1;i<=stdcount;i++)
  216.                                     {
  217.                                         printf("|%2d. %5d. %16s|\n" ,i,a[i].CODEstudent,a[i].Name);
  218.                                     }
  219.                             }
  220.                                         printf ("-----------------------------\n");
  221.                         system("pause");
  222.                         }
  223.                         break;
  224.                     case '2':
  225.                         {
  226.                             system("cls");
  227.                             fflush(stdin);
  228.                             int kt=0;
  229.                             printf("nhap vao ma so sinh vien: \n");
  230.                             fflush(stdin);
  231.                             scanf("%d",&a[stdcount+1].CODEstudent);
  232.                             for (i=1;i<stdcount;i++) if (a[stdcount+1].CODEstudent==a[i].CODEstudent)
  233.                             {
  234.                                 printf("MSSV ban vua nhap da bi trung\n");
  235.                                 kt=1;
  236.                                 break;
  237.                             }
  238.                             if (kt==0)
  239.                             {
  240.                                 stdcount++;
  241.                                 fflush(stdin);
  242.                                 printf("nhap vao ten sinh vien: \n");
  243.                                 fflush(stdin);
  244.                                 //InputValidC (&a[stdcount].Name);
  245.                                 scanf("%[^\n]",&a[stdcount].Name);
  246.                                 printf("nhap vao diem workshop: \n");
  247.                                 fflush(stdin);
  248.                                 InputValidf (&a[stdcount].ws);
  249.                                 printf("nhap vao diem progress test: \n");
  250.                                 fflush(stdin);
  251.                                 InputValidf (&a[stdcount].pt);
  252.                                 printf("nhap vao diem pratical exam: \n");
  253.                                 fflush(stdin);
  254.                                 InputValidf (&a[stdcount].pe);
  255.                                 printf("nhap vao diem final exam: \n");
  256.                                 fflush(stdin);
  257.                                 InputValidf (&a[stdcount].fe);
  258.                             }
  259.                             system("pause");
  260.                         }
  261.                         break;
  262.                     case '3':
  263.                         {
  264.                              FindStd(a, CODEstudent, stdcount);
  265.                              system("pause");  
  266.                         }
  267.                         break;
  268.                     case '4':
  269.                         {
  270.                         int ch;
  271.                         char m[20];
  272.                         printf("Nhap vao 1 de xoa, 2 de sua:\n");
  273.                         scanf("%d",&ch);
  274.                         if(ch>2 || ch<0) printf("Nhap sai, nhap lai!!");
  275.                         fflush(stdin);
  276.                         while (ch==2 || ch==1)
  277.                             {
  278.                                 if (ch==1)
  279.                                 {
  280.                                     DeleteStd(a, CODEstudent, &stdcount);
  281.                                 }
  282.                                 if (ch==2)
  283.                                 {
  284.                                     EditStd(a,CODEstudent,stdcount);
  285.                                 }
  286.                                 break;
  287.                             }
  288.                         system("pause");
  289.                         }
  290.                         break;
  291.                     case '5':
  292.                         if(LoadFile("text.dat", a, &stdcount)==1)
  293.                             printf("loaded\n");
  294.                         else
  295.                         printf("error\n");
  296.                         system("pause");
  297.                         break;                         
  298.                     case '6':
  299.                         if(SaveFile("text.dat", a, stdcount)==1)
  300.                             printf("saved\n");
  301.                         else
  302.                             printf("error");
  303.                         system("pause\n");             
  304.                         break;             
  305.  
  306.                     case '7' :
  307.                         {
  308.                             fflush(stdin);
  309.                             system("cls");
  310.                             printf ("BANG DIEM CUA LOP\n");
  311.                             printf("*______________________________________________________________________________*\n");
  312.                             printf ("STT     Full Name      Workshop    Progress Test    Practical Exam   Final Exam\n");
  313.                             for (i=1;i<stdcount;i++)
  314.                             {
  315.                             printf (" %2d. |%15s | %2.2f     |   %2.2f        |    %2.2f          | %2.2f   \n", i, a[i].Name, a[i].ws, a[i].pt, a[i].pe, a[i].fe);
  316.                             }
  317.                             printf("*______________________________________________________________________________*\n");
  318.                             system("pause");
  319.                         }
  320.                         break;
  321.                     case '8':
  322.                         {
  323.                         float tb=0,max=0,min=10;
  324.                         int cc,cc3;
  325.                         char cc2[26],cc4[26];
  326.                         system("cls");
  327.                         printf ("THONG KE CU THE SINH VIEN\n");
  328.                         printf("Passed: \n");
  329.                         for (i=1;i<=stdcount;i++)
  330.                         {
  331.                             if ((a[i].ws>0) && (a[i].pt>0) && (a[i].pe>4) && (a[i].fe>4) && (a[i].ws*0.1+a[i].pt*0.2+a[i].pe*0.4+a[i].fe*0.3) >= 5) printf("%6d - %15s \n",a[i].CODEstudent,a[i].Name);
  332.                         }
  333.                         printf("\n");
  334.                         printf("Sinh vien truot: \n");
  335.                         for (i=1;i<=stdcount;i++)
  336.                         {
  337.                             if ((a[i].ws=0) || (a[i].pt=0) || (a[i].pe<4) || (a[i].fe<4) || (a[i].ws*0.1+a[i].pt*0.2+a[i].pe*0.4+a[i].fe*0.3) < 5) printf("%6d - %15s \n",a[i].CODEstudent,a[i].Name); 
  338.                         }
  339.                         printf("\n");
  340.                         printf("Sinh vien duoc hoc bong: \n");
  341.                         for (i=1;i<=stdcount;i++)
  342.                         {
  343.                             if ((a[i].ws/10+a[i].pt/5+a[i].pe*2/5+a[i].fe*3/10 +2)>= 9) printf("%6d - %15s \n",a[i].CODEstudent,a[i].Name);
  344.                         }
  345.                         printf("\n");
  346.                         printf("Sinh vien diem cao nhat la: \n");
  347.                         for (i=1;i<=stdcount;i++)
  348.                         {
  349.                             tb=(a[i].ws/10+a[i].pt/5+a[i].pe*2/5+a[i].fe*3/10 +3);
  350.                             if (tb>=max)
  351.                             {
  352.                             max=tb;
  353.                             cc=a[i].CODEstudent;
  354.                             strcpy(cc2,a[i].Name);
  355.                             }
  356.                         }
  357.                         printf("%6d - %15s Trung binh mon %2.2f\n",cc,cc2,max);
  358.                         printf("Sinh vien diem thap nhat la: \n");
  359.                         for (i=1;i<=stdcount;i++)
  360.                         {
  361.                             tb=(a[i].ws*0.1+a[i].pt*0.2+a[i].pe*0.4+a[i].fe*0.3);
  362.                             if (tb<=min)
  363.                             {
  364.                             min=tb;
  365.                             cc3=a[i].CODEstudent;
  366.                             strcpy(cc4,a[i].Name);
  367.                             }
  368.                         }
  369.                         printf("%6d - %15s Trung binh mon %2.2f\n",cc3,cc4,min);
  370.                         system("pause");
  371.                         }
  372.                         break;
  373.                     case '9':
  374.                         {
  375.                                 system("cls");
  376.                                 fflush(stdin);
  377.                                 printf("******************************************************\n");
  378.                                 printf("*Cam on ban da su dung phan mem                      *\n");
  379.                                 printf("*Moi chi tiet va gop y xin lien he                   *\n");
  380.                                 printf("*                                                    *\n");
  381.                                 printf("*                                                    *\n");
  382.                                 printf("*                                                    *\n");
  383.                                 printf("*                                                    *\n");
  384.                                 printf("*                                                    *\n");
  385.                                 printf("*                                       Trieu Nam Hai*\n");
  386.                                 printf("*                                    SDT: 01683574189*\n");
  387.                                 printf("*                       Mail: [email protected]*\n");
  388.                                 printf("******************************************************\n");
  389.                                 system("pause");
  390.                         }
  391.                         break;
  392.                 }
  393.             }
  394.         }      
  395.     }
  396. }
Advertisement
Add Comment
Please, Sign In to add comment