Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // compiled with clang because of macOS
- /* $ gcc --v version
- Configured with: --prefix=/Applications/Xcode.app/Contents/Developer/usr --with-gxx-include-dir=/usr/include/c++/4.2.1
- Apple LLVM version 9.0.0 (clang-900.0.38)
- Target: x86_64-apple-darwin16.7.0
- Thread model: posix
- InstalledDir: /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin
- */
- #include <stdio.h>
- #include <stdlib.h>
- #include <string.h>
- struct Student {
- char nume[30], optionale[4][3];
- };
- typedef struct Student Student; // so i can use `Student var` instead of `struct Student var`
- enum Order {
- Ascending = 1, Descending = 0
- };
- typedef enum Order Order; // so i can use `Order var` instead of `enum Order var`
- /*
- strcmp(a,b) returns >0 if a>b and <0 if a<b
- Ascending = 1, Descending = 0
- So if I want to order the array in ascending order i need to
- verify that a > b which is (strcmp(a, b) > 0) and than compar it with
- order
- */
- int compare(char *a, char* b, Order order)
- {
- return (strcmp(a, b) > 0) == order;
- }
- void shakersort(Student *a[], int n, int* sortComparisons, Order order)
- {
- int j,ultim,sus,jos;
- Student *temp;
- sus= 1; jos= n-1; ultim= n-1;
- do {
- for( j= jos; j >= sus; j --)
- {
- if (compare(a[j-1]-> nume, a[j]->nume, order))
- {
- (*sortComparisons)++;
- temp = a[j-1];
- a[j-1]= a[j];
- a[j]= temp;
- ultim= j;
- }
- }
- sus= ultim+1;
- for( j=sus; j <= jos; j ++)
- {
- if (compare(a[j-1]-> nume, a[j]->nume, order))
- {
- (*sortComparisons)++;
- temp=a[j-1]; a[j-1]=a[j]; a[j]=temp;
- ultim=j;
- }
- }
- jos=ultim-1;
- } while (!(sus>jos));
- }
- Student* insertie(Student *a, Order order)
- {
- int i,j, n = 4;
- for( i= n-2; i >= 0 ;i--)
- {
- strcpy(a->optionale[n], a->optionale[i]);
- j=i+1;
- while(compare(a->optionale[j],a->optionale[n], !order) && j>= 0) {
- strcpy(a->optionale[j-1], a->optionale[j]);
- j++;
- }
- strcpy(a->optionale[j-1], a->optionale[n]);
- }
- return a;
- }
- Student** loadStudents(FILE *f, int *studentCount)
- {
- int size = 0;
- Student** students = malloc(sizeof(Student *));
- students[0] = malloc(sizeof(Student));
- while( fgets (students[size]->nume, 30, f) !=NULL )
- {
- fscanf(f, "%s %s %s %s\n", students[size]->optionale[0], students[size]->optionale[1], students[size]->optionale[2] , students[size]->optionale[3]);
- students[size]->nume[strlen(students[size]->nume) - 2] = 0; // remove the \r\n at the end of the string because of fgets()
- size++;
- students = realloc(students, sizeof(Student*) * (size + 1));
- students[size] = malloc(sizeof(Student));
- }
- *studentCount = size;
- return students;
- }
- int main(int argc, const char * argv[]) {
- FILE *f = fopen("/Users/dariuscostolas/school/SDA/test/lab3_a2.txt", "r");
- int studentsCount, sortComparisons;
- Student** students = loadStudents(f, &studentsCount);
- // order ascending
- sortComparisons = 0;
- shakersort(students, studentsCount, &sortComparisons, Ascending);
- printf("Total comparisons for Ascending order: %d\n\n", sortComparisons);
- for(int i=0;i<studentsCount;i++)
- {
- printf("%s\n", students[i]->nume);
- }
- // order descending
- sortComparisons = 0;
- shakersort(students, studentsCount, &sortComparisons, Descending);
- printf("Total comparisons for Descending order: %d\n\n", sortComparisons);
- for(int i=0;i<studentsCount;i++)
- {
- printf("%s\n", students[i]->nume);
- }
- // this is because of my ocd to see the students ordered ascending when printing them
- shakersort(students, studentsCount, &sortComparisons, Ascending);
- for(int i = 0; i < studentsCount; i++)
- {
- printf("%s = ", students[i]->nume);
- insertie(students[i], Ascending);
- for(int j = 0;j < 4; j++)
- {
- printf("%s ", students[i]->optionale[j]);
- }
- printf("\n");
- }
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement