frain8

Untitled

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