Holey_yan

20141125_KUAS_VisualC++_Exam07

Nov 24th, 2014
278
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.89 KB | None | 0 0
  1. /************************************/
  2. /** 20141028 Exam 07               **/
  3. /** Maker  : Yan                   **/
  4. /** E-Mail : [email protected] **/
  5. /** Date   : 2014/11/25 pm.12:00   **/
  6. /************************************/
  7.  
  8. #include <stdio.h>
  9. #include <stdlib.h>
  10. #include <string.h>
  11.  
  12. struct MyStruct
  13. {
  14.     char name[5];
  15.     int Cht;
  16.     int Eng;
  17.     int Math;
  18. };
  19.  
  20. int main(int argc, char const *argv[])
  21. {
  22.     struct MyStruct ss1[100];
  23.     FILE *fptr = fopen("input.txt", "r");
  24.     if(fptr == NULL)
  25.     {
  26.         printf("Read file ERROR!\n");
  27.         return 0;
  28.     }
  29.  
  30.     int counter = 0;
  31.     while(!feof(fptr))
  32.     {
  33.         fscanf(fptr, "%s%d%d%d", ss1[counter].name, &ss1[counter].Cht, \
  34.                                  &ss1[counter].Eng, &ss1[counter].Math);
  35.         counter++;
  36.     }
  37.     for(int i = 0; i < counter; i++)
  38.         printf("%s: %3d, %3d, %3d\n", ss1[i].name, ss1[i].Cht, ss1[i].Eng, ss1[i].Math);
  39.     printf("------------------------------------------------\n");
  40.  
  41.     printf("/* Search Mode: -1 = Exit program.\n");
  42.     printf("                 0 = Search by NAME (key word)\n");
  43.     printf("                 1 = Search by SCORE           */\n");
  44.  
  45.     while(1)
  46.     {
  47.         printf("\nWhat do you want to search: ");
  48.         int search = 0;
  49.         scanf("%d", &search);
  50.  
  51.         if(search == -1)
  52.         {
  53.             break;
  54.         }
  55.         else if(search == 0)
  56.         {
  57.             printf("Input Name: ");
  58.             char name[5] = {NULL};
  59.             scanf("%s", name);
  60.  
  61.             for(int i = 0; i < counter; i++)
  62.             {
  63.                 if(strstr(ss1[i].name, name))
  64.                     printf("\t%s: %3d, %3d, %3d\n", ss1[i].name, ss1[i].Cht, ss1[i].Eng, ss1[i].Math);
  65.             }
  66.         }
  67.         else if(search == 1)
  68.         {
  69.             printf("Input Score: ");
  70.             int score = 0;
  71.             scanf("%d", &score);
  72.             for(int i = 0; i < counter; i++)
  73.             {
  74.                 if((score == ss1[i].Cht) || (score == ss1[i].Eng) || (score == ss1[i].Math))
  75.                 {
  76.                     printf("\t%s: %3d, %3d, %3d\n", ss1[i].name, ss1[i].Cht, ss1[i].Eng, ss1[i].Math);
  77.                 }
  78.             }
  79.         }
  80.         else
  81.         {
  82.             continue;
  83.         }
  84.     }
  85.     return 0;
  86. }
Advertisement
Add Comment
Please, Sign In to add comment