Advertisement
Guest User

Untitled

a guest
Oct 18th, 2017
100
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 4.25 KB | None | 0 0
  1. // compiled with clang because of macOS
  2. /* $ gcc --v version
  3.  Configured with: --prefix=/Applications/Xcode.app/Contents/Developer/usr --with-gxx-include-dir=/usr/include/c++/4.2.1
  4.  Apple LLVM version 9.0.0 (clang-900.0.38)
  5.  Target: x86_64-apple-darwin16.7.0
  6.  Thread model: posix
  7.  InstalledDir: /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin
  8.  */
  9.  
  10. #include <stdio.h>
  11. #include <stdlib.h>
  12. #include <string.h>
  13.  
  14. struct Student {
  15.     char nume[30], optionale[4][3];
  16. };
  17.  
  18. typedef struct Student Student; // so i can use  `Student var` instead of `struct Student var`
  19.  
  20. enum Order {
  21.     Ascending = 1, Descending = 0
  22. };
  23. typedef enum Order Order; // so i can use  `Order var` instead of `enum Order var`
  24.  
  25. /*
  26.  strcmp(a,b) returns >0 if a>b and <0 if a<b
  27.  Ascending = 1, Descending = 0
  28.  So if I want to order the array in ascending order i need to
  29.  verify that  a > b which is (strcmp(a, b) > 0) and than compar it with
  30.  order
  31. */
  32. int compare(char *a, char* b, Order order)
  33. {
  34.     return (strcmp(a, b) > 0)  == order;
  35. }
  36.  
  37. void shakersort(Student *a[], int n, int* sortComparisons, Order order)
  38. {
  39.     int j,ultim,sus,jos;
  40.     Student *temp;
  41.     sus= 1; jos= n-1; ultim= n-1;
  42.     do {
  43.         for( j= jos; j >= sus; j --)
  44.         {
  45.             if (compare(a[j-1]-> nume, a[j]->nume, order))
  46.             {
  47.                 (*sortComparisons)++;
  48.                 temp = a[j-1];
  49.                 a[j-1]= a[j];
  50.                 a[j]= temp;
  51.                 ultim= j;
  52.             }
  53.         }
  54.         sus= ultim+1;
  55.         for( j=sus; j <= jos; j ++)
  56.         {
  57.             if (compare(a[j-1]-> nume, a[j]->nume, order))
  58.             {
  59.                 (*sortComparisons)++;
  60.                 temp=a[j-1]; a[j-1]=a[j]; a[j]=temp;
  61.                 ultim=j;
  62.             }
  63.         }
  64.         jos=ultim-1;
  65.     } while (!(sus>jos));
  66. }
  67.  
  68. Student* insertie(Student *a, Order order)
  69. {
  70.     int i,j, n = 4;
  71.     for( i= n-2; i >= 0 ;i--)
  72.     {
  73.         strcpy(a->optionale[n], a->optionale[i]);
  74.         j=i+1;
  75.         while(compare(a->optionale[j],a->optionale[n], !order) && j>= 0) {
  76.             strcpy(a->optionale[j-1], a->optionale[j]);
  77.             j++;
  78.         }
  79.         strcpy(a->optionale[j-1], a->optionale[n]);
  80.     }
  81.     return a;
  82. }
  83. Student** loadStudents(FILE *f, int *studentCount)
  84. {
  85.     int size = 0;
  86.     Student** students = malloc(sizeof(Student *));
  87.     students[0] = malloc(sizeof(Student));
  88.     while( fgets (students[size]->nume, 30, f) !=NULL )
  89.     {
  90.         fscanf(f, "%s %s %s %s\n", students[size]->optionale[0], students[size]->optionale[1],  students[size]->optionale[2] , students[size]->optionale[3]);
  91.         students[size]->nume[strlen(students[size]->nume) - 2] = 0; // remove the \r\n at the end of the string because of fgets()
  92.         size++;
  93.         students = realloc(students, sizeof(Student*) * (size + 1));
  94.         students[size] = malloc(sizeof(Student));
  95.     }
  96.     *studentCount = size;
  97.     return students;
  98. }
  99.  
  100. int main(int argc, const char * argv[]) {
  101.     FILE *f = fopen("/Users/dariuscostolas/school/SDA/test/lab3_a2.txt", "r");
  102.     int studentsCount, sortComparisons;
  103.     Student** students = loadStudents(f, &studentsCount);
  104.    
  105.     // order ascending
  106.     sortComparisons = 0;
  107.     shakersort(students, studentsCount, &sortComparisons, Ascending);
  108.     printf("Total comparisons for Ascending order: %d\n\n", sortComparisons);
  109.     for(int i=0;i<studentsCount;i++)
  110.     {
  111.         printf("%s\n", students[i]->nume);
  112.     }
  113.    
  114.     // order descending
  115.     sortComparisons = 0;
  116.     shakersort(students, studentsCount, &sortComparisons, Descending);
  117.     printf("Total comparisons for Descending order: %d\n\n", sortComparisons);
  118.     for(int i=0;i<studentsCount;i++)
  119.     {
  120.         printf("%s\n", students[i]->nume);
  121.     }
  122.    
  123.    
  124.     // this is because of my ocd to see the students ordered ascending when printing them
  125.     shakersort(students, studentsCount, &sortComparisons, Ascending);
  126.     for(int i = 0; i < studentsCount; i++)
  127.     {
  128.         printf("%s = ", students[i]->nume);
  129.         insertie(students[i], Ascending);
  130.         for(int j = 0;j < 4; j++)
  131.         {
  132.             printf("%s ", students[i]->optionale[j]);
  133.         }
  134.         printf("\n");
  135.     }
  136.    
  137.     return 0;
  138. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement