Advertisement
Guest User

readssss

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