Advertisement
Guest User

there_you_go

a guest
Nov 18th, 2019
97
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 3.66 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <fcntl.h>
  3. #include <unistd.h>
  4. #include <stdlib.h>
  5. #include <string.h>
  6. char ** readFile(int fd);
  7. int * split_grades(char * stri);
  8. char * split_name(char * stri);
  9. double average(int * nr);
  10. int myCompare(const void*a, const void *b);
  11. int main(int argc, char** argv)
  12. {
  13.     if(argc!=2){
  14.         printf("Enter one argument which is the name of the file\n");
  15.         return 0;
  16.     }
  17.     char *buf;
  18.     int bytesRead;
  19.     int fp;
  20.     //printf("%s\n",argv[1]);
  21.     fp=open(argv[1], O_RDONLY);
  22.     char **students=readFile(fp);
  23.     int i,pid2,n;
  24.     printf("Students are: \n");
  25.     n=atoi(students[0]);
  26.     for(i=1; i<n; i++)
  27.         printf("%s",*(students+i));
  28.     //printf("%d\n",n);
  29.     int pid=fork();
  30.     if(pid==0)
  31.     {
  32.         printf("Averages:\n");
  33.         for(i=1; i<n; i++)
  34.             printf("%s average [%d]: %f\n",split_name(*(students+i)),i,average(split_grades(*(students+i))));
  35.     }
  36.     else
  37.     {
  38.         pid2=fork();
  39.         if(pid2==0)
  40.         {
  41.             printf("Ordered:\n");
  42.             qsort(students+1,n-1,sizeof(char*),myCompare);
  43.             int j;
  44.             for(j=1; j<n; j++)
  45.                 printf("Ord [%d]: %s",j,*(students+j));
  46.         }
  47.     }
  48.     close(fp);
  49.     return 0;
  50. }
  51. double average(int * nr)
  52. {
  53.     int i;
  54.     int sum=0;
  55.     for(i=1; i<*(nr+0); i++)
  56.         sum+=*(nr+i);
  57.     return sum*1.0/((*(nr+0)-1)*1.0);
  58. }
  59. int * split_grades(char * stri)
  60. {
  61.     char * string=(char*)malloc(sizeof(char)*(strlen(stri)+1));
  62.     strcpy(string,stri);
  63.     char * pch;
  64.     int *numbers=(int*) malloc(sizeof(int)*2);
  65.     char nr_count=1;
  66.     char *splitted_string;
  67.     splitted_string=(char*)malloc(sizeof(char)*(strlen(string)+1));
  68.     pch = strtok (string," ,abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ\n");
  69.     while (pch != NULL)
  70.     {
  71.         numbers[nr_count++]=atoi(pch);
  72.         numbers=(int*)realloc(numbers, (nr_count+1)*sizeof(int));
  73.         numbers[0]=nr_count;
  74.         pch = strtok (NULL, " ,abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ\n");
  75.     }
  76.     return numbers;
  77. }
  78. char * split_name(char * stri)
  79. {
  80.     char * string=(char*)malloc(sizeof(char)*(strlen(stri)+1));
  81.     strcpy(string,stri);
  82.     char * pch;
  83.     char *splitted_string;
  84.     int n;
  85.     splitted_string=(char*)malloc(sizeof(char)*(strlen(string)+1));
  86.     pch = strtok (string," ,0123456789\n");
  87.     while (pch != NULL)
  88.     {
  89.         strcat(splitted_string,pch);
  90.         n=strlen(splitted_string);
  91.         if(pch = strtok (NULL, " ,0123456879\n"))
  92.         {
  93.             splitted_string[n]=' ';
  94.             splitted_string[n+1]='\0';
  95.         }
  96.         //pch = strtok (NULL, " ,0123456879\n");
  97.     }
  98.     return splitted_string;
  99. }
  100. char ** readFile(int fd)
  101. {
  102.     char **student_grades=(char**)malloc(sizeof(char*)*2);
  103.     int nr_count=1;
  104.     char buffer[1000];
  105.     int bytes_read;
  106.     int k = 0;
  107.     char chr;
  108.     *(student_grades+0)=(char*)malloc(sizeof(char*)*11);
  109.     while((bytes_read=read(fd,&chr,1))>0)
  110.     {
  111.         // bytes_read = read(fd, &chr, 1);
  112.         buffer[k++]=chr;
  113.         if(chr == '\n' || chr == '\0')
  114.         {
  115.             buffer[k]='\0';
  116.             *(student_grades+nr_count)=(char*)malloc(sizeof(char)*(strlen(buffer)+1));
  117.             strcpy(student_grades[nr_count++],buffer);
  118.             student_grades=(char**)realloc(student_grades, (nr_count+1)*sizeof(char*));
  119.             sprintf(*(student_grades+0), "%d", nr_count);
  120.             memset(buffer,0,k);
  121.             k = 0;
  122.         }
  123.     }
  124.     return student_grades;
  125. }
  126. int myCompare (const void * a, const void * b )
  127. {
  128.     const char **ia = (const char **)a;
  129.     const char **ib = (const char **)b;
  130.     return strcmp(*ia, *ib);
  131. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement