Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include<stdio.h>
- #include<stdlib.h>
- #include<string.h>
- #define MAX 30
- #define MAXKIDS 128
- typedef struct{
- char i[MAX];
- char p[MAX];
- int g;
- } Student;
- void swap(Student niz[], int i, int j)
- {
- char tmpi[MAX], tmpp[MAX];
- int tmpg = niz[i].g;
- niz[i].g = niz[j].g;
- niz[j].g = tmpg;
- strcpy(tmpi, niz[i].i);
- strcpy(niz[i].i, niz[j].i);
- strcpy(niz[j].i, tmpi);
- strcpy(tmpp, niz[i].p);
- strcpy(niz[i].p, niz[j].p);
- strcpy(niz[j].p, tmpp);
- }
- void check(Student niz[], int n)
- {
- int i;
- for(i=1; i<n+1; i++)
- printf("%s %s %d \n",niz[i].i, niz[i].p, niz[i].g );
- }
- void sort(Student niz[], int n)
- {
- int state, i;
- do {
- state = 0;
- for(i=0; i<n; i++)
- if(niz[i].g>niz[i+1].g)
- {
- swap(niz, i, i+1);
- state=1;
- }
- } while (state);
- printf("\n");
- check(niz, n);
- }
- int printout(char *in)
- {
- FILE *in_file = fopen(in, "r");
- if(!in_file)
- {
- printf("Datoteka nije uspeno otvorena!\n", in);
- exit(-1);
- }
- int c, counter=1;
- while ((c = getc(in_file)) != EOF)
- {
- putchar(c);
- if(c=='\n')
- counter++;
- }
- putchar('\n');
- close(in_file);
- return counter;
- }
- void sortkids(char *in, char *out, int n)
- {
- FILE *in_file = fopen(in, "r");
- FILE *out_file = fopen(out, "w");
- Student niz[MAXKIDS];
- int i, tmp[MAXKIDS];
- if(in_file==NULL || out_file==NULL)
- {
- printf("Datoteka nije uspeno otvorena!\n", in);
- exit(-1);
- }
- for(i=1; i<n; i++)
- fscanf(in_file, "%s %s %d\n",&niz[i].i, &niz[i].p, &niz[i].g);
- sort(niz, n);
- for(i=1; i<n; i++)
- fprintf(out_file, "%s %s %d\n",niz[i].i, niz[i].p, niz[i].g);
- close(in_file);
- close(out_file);
- }
- int main(int argc, char *argv[])
- {
- if(argc!=3)
- {
- printf("Greska u pozivu!\n");
- system("pause");
- return -1;
- }
- char *in=argv[1], *out=argv[2];
- int counter=printout(in);
- sortkids(in, out, counter);
- system("pause");
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement