frain8

Untitled

Nov 16th, 2019
172
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 1.92 KB | None | 0 0
  1. /* Dasproc C - 2019
  2. William Handi Wijaya
  3. 0087
  4.    
  5. Program untuk mengurutkan rank nilai dari yang terbesar
  6. dengan urutan prioritas desain > gameplay > ui > nama.
  7. */
  8.  
  9.  
  10. #include <stdio.h>
  11. #include <string.h>
  12. #include <stdlib.h>
  13. #include <stdbool.h>
  14.  
  15. typedef struct
  16. {
  17.     char nama[11];
  18.     int desain;
  19.     int gameplay;
  20.     int ui;
  21. } apps;
  22.  
  23. apps *data = NULL;
  24. bool swapped;
  25. int t;
  26.  
  27. void sort(char prompt[9]);
  28. void swap(int j);
  29.  
  30. int main(void)
  31. {
  32.     // Input banyak aplikasi
  33.     scanf("%d", &t);
  34.  
  35.     // Input keterangan aplikasi
  36.     data = (apps*) malloc(sizeof(apps) * t);
  37.     for (int i = 0; i < t; i++)
  38.     {
  39.         scanf("%s %d %d %d", data[i].nama, &data[i].desain, &data[i].gameplay, &data[i].ui);
  40.     }
  41.  
  42.     // Sort
  43.     sort((char*) "desain");
  44.     sort((char*) "gameplay");
  45.     sort((char*) "UI");
  46.     sort((char*) "nama");
  47.  
  48.     // Print
  49.     for (int i = 0; i < t; i++)
  50.     {
  51.         printf("%s\n", data[i].nama);
  52.     }
  53.     free(data);
  54.     return 0;
  55. }
  56.  
  57. void sort(char prompt[9])
  58. {
  59.     for (int i = 0; i < t-1; i++)
  60.     {
  61.         swapped = false;
  62.         for (int j = 0; j < t-i-1; j++)
  63.         {
  64.             if (strncmp(prompt, "desain", 1) == 0)
  65.             {
  66.                 if (data[j].desain < data[j + 1].desain)
  67.                 {
  68.                     swap(j);
  69.                 }
  70.             }
  71.             else if (strncmp(prompt, "gameplay", 1) == 0)
  72.             {
  73.                 if (data[j].desain == data[j + 1].desain && data[j].gameplay < data[j + 1].gameplay)
  74.                 {
  75.                     swap(j);
  76.                 }
  77.             }
  78.             else if (strncmp(prompt, "UI", 1) == 0)
  79.             {
  80.                 if (data[j].desain == data[j + 1].desain && data[j].gameplay == data[j + 1].gameplay && data[j].ui < data[j + 1].ui)
  81.                 {
  82.                     swap(j);
  83.                 }
  84.             }
  85.             else
  86.             {
  87.                 if (data[j].desain == data[j + 1].desain && data[j].gameplay == data[j + 1].gameplay && data[j].ui == data[j + 1].ui && strcmp(data[j].nama, data[j + 1].nama) > 0)
  88.                 {
  89.                     swap(j);
  90.                 }
  91.             }
  92.         }
  93.         if (swapped == false) break;
  94.     }
  95. }
  96.  
  97. void swap(int j)
  98. {
  99.     apps tmp = data[j];
  100.     data[j] = data[j + 1];
  101.     data[j + 1] = tmp;
  102.     swapped = true;
  103.     return;
  104. }
Advertisement
Add Comment
Please, Sign In to add comment