Advertisement
apl-mhd

final project

May 9th, 2024 (edited)
881
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 7.02 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3.  
  4. struct Player
  5. {
  6.     char name[100];
  7.     int age;
  8.     char playerRole[100];
  9.     int match;
  10.     int totalRuns;
  11.     int highestRun;
  12. };
  13.  
  14. void addPlayer(FILE *file, struct Player *playerList, int n)
  15. {
  16.     char newline;
  17.  
  18.     for (int i = 0; i < n; i++)
  19.     {
  20.  
  21.         fscanf(file, "%c", &newline);
  22.         fscanf(file, "%c", &newline);
  23.  
  24.         fgets(playerList[i].name, 100, file);
  25.         fscanf(file, "%d", &playerList[i].age);
  26.         fscanf(file, "%c", &newline);
  27.         fgets(playerList[i].playerRole, 100, file);
  28.         fscanf(file, "%d", &playerList[i].match);
  29.         fscanf(file, "%d", &playerList[i].totalRuns);
  30.         fscanf(file, "%d", &playerList[i].highestRun);
  31.     }
  32. }
  33.  
  34. void printPlayer(struct Player player)
  35. {
  36.     printf("Full Name: %s", player.name);
  37.     printf("Age: %d \n", player.age);
  38.     printf("Player Role: %s", player.playerRole);
  39.  
  40.     printf("ODI Match: %d \n", player.match);
  41.     printf("Total Runs: %d \n", player.totalRuns);
  42.     printf("Highest Run: %d \n\n", player.highestRun);
  43. }
  44.  
  45. void playerInfo(struct Player playerList[], char name[], int n)
  46. {
  47.     int index;
  48.     for (int i = 0; i < n; i++)
  49.     {
  50.         index = i;
  51.         int j = -1;
  52.         while (playerList[i].name[j] != '\0')
  53.         {
  54.             if (playerList[i].name != name[j])
  55.             {
  56.                 index = -1;
  57.                 break;
  58.             }
  59.             {
  60.             }
  61.             j++;
  62.         }
  63.  
  64.         if (index != -1)
  65.         {
  66.             break;
  67.         }
  68.     }
  69.  
  70.     printPlayer(playerList[index]);
  71. }
  72.  
  73. void showAllPlayer(struct Player playerList[], int n)
  74. {
  75.     printf("All player: \n\n");
  76.  
  77.     for (int i = 0; i < n; i++)
  78.     {
  79.         printf("\n--------Player %d--------\n", i + 1);
  80.         printPlayer(playerList[i]);
  81.     };
  82. }
  83.  
  84. void mep(struct Player playerList[], int n)
  85. {
  86.     printf("MOST Experienced Player: \n\n");
  87.     int index = -1;
  88.     int maxMatch = -1;
  89.     for (int i = 0; i < n; i++)
  90.     {
  91.         if (playerList[i].match > maxMatch)
  92.         {
  93.             maxMatch = playerList[i].match;
  94.             index = i;
  95.         }
  96.     }
  97.  
  98.     printPlayer(playerList[index]);
  99. }
  100.  
  101. void ncp(struct Player playerList[], int n)
  102. {
  103.     printf("New Commer Player: \n\n");
  104.     int index = -1;
  105.     int age = 999999;
  106.     for (int i = 0; i < n; i++)
  107.     {
  108.         if (playerList[i].age < age)
  109.         {
  110.             age = playerList[i].age;
  111.             index = i;
  112.         }
  113.     }
  114.  
  115.     printPlayer(playerList[index]);
  116. }
  117.  
  118. void tsp(struct Player playerList[], int n)
  119. {
  120.     printf("Top Scorer Player: \n\n");
  121.     int index = -1;
  122.     int totalRuns = -1;
  123.     for (int i = 0; i < n; i++)
  124.     {
  125.         if (playerList[i].totalRuns > totalRuns)
  126.         {
  127.             totalRuns = playerList[i].totalRuns;
  128.             index = i;
  129.         }
  130.     }
  131.  
  132.     printPlayer(playerList[index]);
  133. }
  134.  
  135. void hhp(struct Player playerList[], int n)
  136. {
  137.     printf("Hard Hitter Player: \n\n");
  138.     int index = -1;
  139.     int avgRun = -1;
  140.     for (int i = 0; i < n; i++)
  141.     {
  142.         int temp = playerList[i].totalRuns / playerList[i].match;
  143.         if (temp > avgRun)
  144.         {
  145.             avgRun = playerList[i].totalRuns;
  146.             index = i;
  147.         }
  148.     }
  149.  
  150.     printPlayer(playerList[index]);
  151. }
  152.  
  153. void hrtp(struct Player playerList[], int n)
  154. {
  155.     printf("Highest Run Taker PlayerL \n\n");
  156.  
  157.     int index = -1;
  158.     int highestRun = -1;
  159.     for (int i = 0; i < n; i++)
  160.     {
  161.         if (playerList[i].highestRun > highestRun)
  162.         {
  163.             highestRun = playerList[i].highestRun;
  164.             index = i;
  165.         }
  166.     }
  167.  
  168.     printPlayer(playerList[index]);
  169. }
  170.  
  171. void mvp(struct Player playerList[], int n)
  172. {
  173.     printf("MOST Valuable Player: \n\n");
  174.  
  175.     int index = -1;
  176.     int value = -1;
  177.     for (int i = 0; i < n; i++)
  178.     {
  179.         int age = playerList[i].age;
  180.         int match = playerList[i].match;
  181.         int totalRuns = playerList[i].totalRuns;
  182.         int highestRun = playerList[i].highestRun;
  183.  
  184.         int j = 0;
  185.         char playerRoll[100] = {'A', 'l', 'l', 'r', 'o', 'u', 'n', 'd', 'e', 'r'};
  186.         int allRounderValue = 50;
  187.  
  188.         while (playerList[i].playerRole[j] != '\n')
  189.         {
  190.             if (playerRoll[j] != playerList[i].playerRole[j])
  191.             {
  192.                 allRounderValue = 0;
  193.                 break;
  194.             }
  195.  
  196.             j++;
  197.         }
  198.  
  199.         int temp = age + match + totalRuns + highestRun + (totalRuns / match) + (highestRun / age) + allRounderValue;
  200.         if (temp > value)
  201.         {
  202.             value = temp;
  203.             index = i;
  204.         }
  205.     }
  206.  
  207.     printPlayer(playerList[index]);
  208. }
  209.  
  210. void allRounder(struct Player playerList[], int n)
  211. {
  212.     printf("Allrounders: \n\n");
  213.     for (int i = 0; i < n; i++)
  214.     {
  215.         int j = 0;
  216.         int index = i;
  217.         char playerRoll[100] = {'A', 'l', 'l', 'r', 'o', 'u', 'n', 'd', 'e', 'r'};
  218.  
  219.         while (playerList[i].playerRole[j] != '\n')
  220.         {
  221.             if (playerRoll[j] != playerList[i].playerRole[j])
  222.             {
  223.                 index = -1;
  224.                 break;
  225.             }
  226.  
  227.             j++;
  228.         }
  229.  
  230.         if (index != -1)
  231.         {
  232.             printPlayer(playerList[index]);
  233.         }
  234.     }
  235. }
  236.  
  237. int main()
  238. {
  239.     char fileName[100];
  240.     printf("Enter File Name: ");
  241.     scanf("%s", fileName);
  242.     FILE *file;
  243.     file = fopen(fileName, "r");
  244.  
  245.     int n;
  246.     char name[100];
  247.     struct Player playerList[100];
  248.  
  249.     fscanf(file, "%d", &n);
  250.  
  251.     addPlayer(file, playerList, n);
  252.  
  253.     int menu = 1;
  254.  
  255.     while (menu)
  256.     {
  257.         printf("1. All Players\n");
  258.         printf("2. Show A Particular player information (Provide Player Name)\n");
  259.         printf("3. Show the MOST Experienced Player(MEP) \n");
  260.         printf("4. Show the New Commer Player(NCP)\n");
  261.         printf("5. Show the Top Scorer Player(TSP)\n");
  262.         printf("6. Show the Hard Hitter Player(HHP)\n");
  263.         printf("7. Show the Highest Run Taker Player(HRTP)\n");
  264.         printf("8. Show the MOST Valuable Player(MVP)\n");
  265.         printf("9. Show the Allrounders\n");
  266.         printf("10.Exit/Quit\n");
  267.  
  268.         scanf("%d", &menu);
  269.  
  270.         if (menu == 1)
  271.         {
  272.             showAllPlayer(playerList, n);
  273.         }
  274.         if (menu == 2)
  275.         {
  276.             playerInfo(playerList, name, n);
  277.         }
  278.         if (menu == 3)
  279.         {
  280.             mep(playerList, n);
  281.         }
  282.         if (menu == 4)
  283.         {
  284.             ncp(playerList, n);
  285.         }
  286.         if (menu == 5)
  287.         {
  288.             tsp(playerList, n);
  289.         }
  290.         if (menu == 6)
  291.         {
  292.             hhp(playerList, n);
  293.         }
  294.         if (menu == 7)
  295.         {
  296.             hrtp(playerList, n);
  297.         }
  298.         if (menu == 8)
  299.         {
  300.             mvp(playerList, n);
  301.         }
  302.         if (menu == 9)
  303.         {
  304.             allRounder(playerList, n);
  305.         }
  306.         if (menu == 10)
  307.         {
  308.             menu = 0;
  309.             break;
  310.         }
  311.     }
  312.  
  313.     fclose(file);
  314.  
  315.     return 0;
  316. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement