hamaXD

Final 58 ตอนที่ 3 ยังสมบูรณ์ (ranking)

Dec 9th, 2016
62
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 2.18 KB | None | 0 0
  1. #include<stdio.h>
  2. #include<string.h>
  3. #define line "- - - - - - - - - - - - - - - - - - - - - -"
  4.  
  5. typedef struct{
  6.     int d,m,y;
  7. }date;
  8. typedef struct{
  9.     int playerID;
  10.     char name[20];
  11.     char nationality[50];
  12.     int age;
  13.     int point;
  14.     date updated;
  15. }player;
  16.  
  17. int inputPlayers (player p[]) {
  18.     int n=0;
  19.     printf("Input Player Information\n");
  20.     printf("Number of players: ");
  21.     scanf("%d",&n);
  22.     int i=0;
  23.     while(i<n){
  24.         printf("Player#%d",i+1);
  25.         printf("\nPlayerID: ");
  26.         scanf("%d",&p[i].playerID); fflush(stdin);
  27.         printf("Name: ");
  28.         scanf("%[^\n]s",p[i].name); fflush(stdin);
  29.         printf("Nationality:");
  30.         scanf("%[^\n]s",p[i].nationality); fflush(stdin);
  31.         printf("Age: ");
  32.         scanf("%d",&p[i].age); fflush(stdin);
  33.         i++;
  34.     }
  35.     return n;
  36. }
  37.  
  38. void updatePoints( player p[] , int n) {
  39.     int round=0,i,j;
  40.     player input;
  41.     int newpoint;
  42.     printf("\nUpdate Points of Players\n");
  43.     printf("Number of players to update points: ");
  44.     scanf("%d",&round);
  45.    
  46.     for(i=0;i<round;i++){
  47.         printf("PlayerID: ");
  48.         scanf("%d",&input.playerID);
  49.         for(j=0;j<round;j++){
  50.             if(input.playerID==p[j].playerID){
  51.                 printf("%s Current points = %d",p[j].name,p[j].point);
  52.                 printf("\nEnter new points: ");
  53.                 scanf("%d",&newpoint);
  54.                 p[j].point=newpoint;
  55.                 printf("Updated date (dd/mm/yyyy) : ");
  56.                 scanf("%d/%d/%d",&p[j].updated.d,&p[j].updated.m,&p[j].updated.y); fflush(stdin);
  57.             }
  58.         }
  59.     }
  60. }
  61.  
  62. void showPlayers (player p[],int n){
  63.     printf("\nShow Player Information\n");
  64.     printf("# PlayerlD Name  Nationality Age Points  Updated\n");
  65.     printf(line);
  66.     int i=0;
  67.     while(i<n){
  68.         printf("\n%d    %d %s %s %d %d %d/%d/%d\n",i+1,p[i].playerID,p[i].name,p[i].nationality,p[i].age,p[i].point,p[i].updated.d,p[i].updated.m,p[i].updated.y);
  69.         i++;
  70.     }
  71.     printf("\n");
  72.     printf(line);  
  73.    
  74. }
  75.  
  76. void ranking ( player p [] , int n){
  77.     player rank[100];
  78.     int point = p[0].point;
  79.     int i=0,j=0;
  80.     for(i=0;i<n;i++){
  81.         if(point>p[i].point){
  82.             point=p[i].point;
  83.             rank[j]=p[i];
  84.             j++;
  85.         }
  86.     }
  87.     i=0;
  88.     while(i<n){
  89.         p[i]=rank[i];
  90.         i++;
  91.     }
  92. }
  93.  
  94. int main(){
  95.     player input[500];
  96.     int n;
  97.     n=inputPlayers(input);
  98.     updatePoints (input,n);
  99.     ranking (input,n);
  100.     showPlayers (input,n);
  101.     return 0;
  102. }
Add Comment
Please, Sign In to add comment