arif334

Points Table

Oct 15th, 2020
707
9 days
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <string.h>
  4. #include <math.h>
  5.  
  6. struct Team{
  7.     char name[30];
  8.     int match, won, lost, drawn, point;
  9.     int runs, opp_runs;
  10.     double overs, opp_overs, nrr;
  11. };
  12.  
  13. typedef struct Team team;
  14.  
  15. int main()
  16. {
  17.     freopen("in2.txt", "r", stdin);
  18.     freopen("out2.txt", "w", stdout);
  19.    
  20.     char s[500];
  21.     team teams[10];
  22.     char team_names[3][30] = {"Australia", "Bangladesh", "England"};
  23.     int t1, t2, i, j;
  24.    
  25.     for(i = 0; i < 3; i++) {
  26.         strcpy(teams[i].name, team_names[i]);
  27.         teams[i].match = teams[i].won = teams[i].lost = teams[i].drawn = teams[i].point = 0;
  28.         teams[i].runs = teams[i].opp_runs = 0;
  29.         teams[i].overs = teams[i].opp_overs = teams[i].nrr = 0.0;
  30.     }
  31.    
  32.    
  33.     while(scanf("%[^\n]", s) == 1) {
  34.         getchar();
  35.         //fprintf(stderr, "%s\n", s);
  36.        
  37.         char t[30];
  38.         int t1_run, t2_run;
  39.         double t1_over, t2_over;
  40.         int is_allout = 1;
  41.         for(i = 0, j = 0; s[i]; i++) {
  42.             if(s[i] == ' ') {
  43.                 t[j] = '\0';
  44.                 if(!strcmp(t, "Australia")) t1 = 0;
  45.                 else if(!strcmp(t, "Bangladesh")) t1 = 1;
  46.                 else t1 = 2;
  47.                 j = 0;
  48.             }
  49.             else if(s[i] == '/') {
  50.                 t[j] = '\0';
  51.                 t1_run = atoi(t);
  52.                 is_allout = 0;
  53.             }
  54.             else {
  55.                 t[j++] = s[i];
  56.             }
  57.         }
  58.         t[j] = '\0';
  59.         if(is_allout) t1_run = atoi(t);
  60.         t1_over = 50;
  61.        
  62.         //fprintf(stderr, "%d %lf\n", t1_run, t1_over);
  63.        
  64.         scanf("%[^\n]", s);
  65.         getchar();
  66.        
  67.         int spaces = 0;
  68.         is_allout = 1;
  69.         for(i = 0, j = 0; s[i]; i++) {
  70.             if(s[i] == ' ') {
  71.                 if(spaces == 0) {
  72.                     t[j] = '\0';
  73.                     if(!strcmp(t, "Australia")) t2 = 0;
  74.                     else if(!strcmp(t, "Bangladesh")) t2 = 1;
  75.                     else t2 = 2;
  76.                     spaces++;
  77.                     j = 0;
  78.                 }
  79.                 else {
  80.                     t[j] = '\0';
  81.                     if(is_allout) t2_run = atoi(t);
  82.                     spaces++;
  83.                     j = 0;
  84.                 }
  85.             }
  86.             else if(s[i] == '/') {
  87.                 t[j] = '\0';
  88.                 t2_run = atoi(t);
  89.                 is_allout = 0;
  90.             }
  91.             else {
  92.                 t[j++] = s[i];
  93.             }
  94.         }
  95.         t[j] = '\0';
  96.         if(is_allout) t2_over= 50;
  97.         else t2_over = atof(t);
  98.        
  99.         //fprintf(stderr, "%d %lf\n", t2_run, t2_over);
  100.        
  101.         teams[t1].runs += t1_run;
  102.         teams[t1].overs += t1_over;
  103.         if( fabs((teams[t1].overs - (int)teams[t1].overs) - 0.6) < 0.00001 ) teams[t1].overs = (int)teams[t1].overs + 1;
  104.         teams[t1].opp_runs += t2_run;
  105.         teams[t1].opp_overs += t2_over;
  106.         if( fabs((teams[t1].opp_overs - (int)teams[t1].opp_overs) - 0.6) < 0.00001 ) teams[t1].opp_overs = (int)teams[t1].opp_overs + 1;
  107.        
  108.         teams[t2].runs += t2_run;
  109.         teams[t2].overs += t2_over;
  110.         if( fabs((teams[t2].overs - (int)teams[t2].overs) - 0.6) < 0.00001 ) teams[t2].overs = (int)teams[t2].overs + 1;
  111.         teams[t2].opp_runs += t1_run;
  112.         teams[t2].opp_overs += t1_over;
  113.         if( fabs((teams[t2].opp_overs - (int)teams[t2].opp_overs) - 0.6) < 0.00001 ) teams[t2].opp_overs = (int)teams[t2].opp_overs + 1;
  114.        
  115.         teams[t1].match++;
  116.         teams[t2].match++;
  117.         if(t1_run > t2_run) {
  118.             teams[t1].point += 2;
  119.             teams[t1].won++;
  120.             teams[t2].lost++;
  121.         }
  122.         else if(t2_run > t1_run) {
  123.             teams[t2].point += 2;
  124.             teams[t2].won++;
  125.             teams[t1].lost++;
  126.         }
  127.         else {
  128.             teams[t1].point += 1;
  129.             teams[t1].drawn++;
  130.             teams[t2].point += 1;
  131.             teams[t2].drawn++;
  132.         }
  133.     }
  134.    
  135.     for(i = 0; i < 3; i++) {
  136.         teams[i].overs = (int)teams[i].overs + ( teams[i].overs - (int)teams[i].overs ) * 0.17;
  137.         teams[i].opp_overs = (int)teams[i].overs + (teams[i].opp_overs - (int)teams[i].opp_overs) * 0.17;
  138.         teams[i].nrr = (teams[i].runs / teams[i].overs) - (teams[i].opp_runs / teams[i].opp_overs);
  139.     }
  140.    
  141.     team temp;
  142.     for(i = 0; i < 3; i++) {
  143.         for(j = 1; j < (3 - i); j++) {
  144.             if( (teams[j].point > teams[j-1].point) || ( (teams[j].point == teams[j-1].point) && (teams[j].nrr > teams[j-1].nrr) )) {
  145.                 temp = teams[j];
  146.                 teams[j] = teams[j-1];
  147.                 teams[j-1] = temp;
  148.             }
  149.         }
  150.     }
  151.    
  152.     printf("%-15s%5s%5s%5s%7s%9s%8s\n", "Team", "Match", "Won", "Lost", "Drawn", "NRR", "Points");
  153.     for(i = 0; i < 3; i++) {
  154.         printf("%-15s%5d%5d%5d%7d%+9.3lf%8d\n", teams[i].name, teams[i].match, teams[i].won, teams[i].lost, teams[i].drawn, teams[i].nrr, teams[i].point);
  155.     }
  156.    
  157.     return 0;
  158. }
  159.  
RAW Paste Data