Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <fcntl.h>
- #include <unistd.h>
- #include <stdlib.h>
- #include <string.h>
- char ** rf(int f)
- {
- char **student_grades=(char**)malloc(sizeof(char*)*2);
- int n_count=1;
- char buffer[1000];
- int bytes_read;
- int k = 0;
- char chr;
- *(student_grades+0)=(char*)malloc(sizeof(char*)*11);
- while((bytes_read=read(f,&chr,1))>0)
- {
- // bytes_read = read(f, &chr, 1);
- buffer[k++]=chr;
- if(chr == '\n' || chr == '\0')
- {
- buffer[k]='\0';
- *(student_grades+n_count)=(char*)malloc(sizeof(char)*(strlen(buffer)+1));
- strcpy(student_grades[n_count++],buffer);
- student_grades=(char**)realloc(student_grades, (n_count+1)*sizeof(char*));
- sprintf(*(student_grades+0), "%d", n_count);
- memset(buffer,0,k);
- k = 0;
- }
- }
- return student_grades;
- }
- int * s_grades(char * s)
- {
- char * sng=(char*)malloc(sizeof(char)*(strlen(s)+1));
- strcpy(sng,s);
- char * pch;
- int *numbers=(int*) malloc(sizeof(int)*2);
- char n_count=1;
- char *splitted_sng;
- splitted_sng=(char*)malloc(sizeof(char)*(strlen(sng)+1));
- pch = strtok (sng," ,abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ\n");
- while (pch != NULL)
- {
- numbers[n_count++]=atoi(pch);
- numbers=(int*)realloc(numbers, (n_count+1)*sizeof(int));
- numbers[0]=n_count;
- pch = strtok (NULL, " ,abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ\n");
- }
- return numbers;
- }
- char * s_name(char * s)
- {
- char * sng=(char*)malloc(sizeof(char)*(strlen(s)+1));
- strcpy(sng,s);
- char * pch;
- char *splitted_sng;
- int n;
- splitted_sng=(char*)malloc(sizeof(char)*(strlen(sng)+1));
- pch = strtok (sng," ,0123456789\n");
- while (pch != NULL)
- {
- strcat(splitted_sng,pch);
- n=strlen(splitted_sng);
- if(pch = strtok (NULL, " ,0123456879\n"))
- {
- splitted_sng[n]=' ';
- splitted_sng[n+1]='\0';
- }
- //pch = strtok (NULL, " ,0123456879\n");
- }
- return splitted_sng;
- }
- double avr(int * n)
- {
- int i;
- int sum=0;
- for(i=1; i<*(n+0); i++)
- sum+=*(n+i);
- return sum*1.0/((*(n+0)-1)*1.0);
- }
- int myCompare (const void * a, const void * b )
- {
- const char **ia = (const char **)a;
- const char **ib = (const char **)b;
- return strcmp(*ia, *ib);
- }
- int main(int argc, char** argv)
- {
- if(argc!=2)
- {
- printf("Enter one argument which is the name of the file\n");
- return 0;
- }
- char *buf;
- int bytesRead;
- int fp;
- //printf("%s\n",argv[1]);
- fp=open(argv[1], O_RDONLY);
- char **students=rf(fp);
- int i,pid2,n;
- printf("Students are: \n");
- n=atoi(students[0]);
- for(i=1; i<n; i++)
- printf("%s",*(students+i));
- //printf("%d\n",n);
- int pid=fork();
- if(pid==0)
- {
- printf("avrs:\n");
- for(i=1; i<n; i++)
- printf("%s avr %f\n",s_name(*(students+i)),avr(s_grades(*(students+i))));
- }
- else
- {
- pid2=fork();
- if(pid2==0)
- {
- printf("Ordered:\n");
- qsort(students+1,n-1,sizeof(char*),myCompare);
- int j;
- for(j=1; j<n; j++)
- printf("Ord %d: %s",j,*(students+j));
- }
- }
- close(fp);
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement