Advertisement
Guest User

Untitled

a guest
Nov 22nd, 2017
67
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 3.68 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <string.h>
  4. #define MAX_NAME 30
  5. #define MAX_INDEX 12
  6. #define MAX_SIZE 100
  7.  
  8. typedef struct student_st {
  9.     char index[MAX_INDEX];
  10.     char ime[MAX_NAME];
  11.     char prezime[MAX_NAME];
  12.     unsigned poeni;
  13. } STUDENT;
  14.  
  15. FILE *safe_open(char*, char*);
  16. void ucitavanje (FILE *, STUDENT *, int *);
  17. void a(STUDENT *, int);
  18. void b(STUDENT *, int);
  19. void c(STUDENT *, int, char *);
  20. void d(STUDENT *, int);
  21.  
  22. int main(int brArg, char *args[]) {
  23.  
  24.     STUDENT niz[MAX_SIZE];
  25.     int n;
  26.     FILE *in;
  27.    
  28.     if (brArg != 3) {
  29.       puts("Niste dobro pozvali program.");
  30.       exit(EXIT_FAILURE);
  31.     }
  32.    
  33.     in = safe_open(args[1], "r");
  34.    
  35.     ucitavanje(in, niz, &n);
  36.     a(niz, n);
  37.     b(niz, n);
  38.     c(niz, n, args[2]);
  39.     d(niz, n);
  40.    
  41.     fclose(in);
  42.    
  43.     return 0;
  44. }
  45.  
  46. FILE *safe_open(char *name, char *mode) {
  47.     FILE *fp = fopen(name, mode);
  48.  
  49.     if(fp == NULL) {
  50.         printf("Doslo je do greske prilikom otvaranja fajla %s.\n", name);
  51.         exit(EXIT_FAILURE);
  52.     }
  53.  
  54.     return fp;
  55. }
  56.  
  57. void ucitavanje (FILE *in, STUDENT *niz, int *i) {
  58.     STUDENT temp;
  59.     int br = 0;
  60.  
  61.     while(fscanf(in, "%s %s %s %u", temp.index, temp.ime, temp.prezime, &temp.poeni)!=EOF) {
  62.         strcpy(niz[br].index, temp.index);
  63.         strcpy(niz[br].ime, temp.ime);
  64.         strcpy(niz[br].prezime, temp.prezime);
  65.         niz[br].poeni = temp.poeni;
  66.         br++;
  67.     }
  68.    
  69.     *i = br;
  70. }
  71.  
  72. void a (STUDENT *niz, int n) {
  73.     FILE *s;
  74.     int j, i;
  75.     STUDENT temp;
  76.    
  77.     s = safe_open("sortirani.txt", "w");
  78.    
  79.     for(i = 0; i< n -1; ++i)
  80.         for(j = i + 1; j < n ; ++j) {
  81.             if(strcmp(niz[i].index, niz[j].index)>0) {
  82.                 strcpy(temp.index, niz[i].index);
  83.                 strcpy(temp.ime, niz[i].ime);
  84.                 strcpy(temp.prezime, niz[i].prezime);
  85.                 temp.poeni = niz[i].poeni;
  86.                
  87.                 strcpy(niz[i].index, niz[j].index);
  88.                 strcpy(niz[i].ime, niz[j].ime);
  89.                 strcpy(niz[i].prezime, niz[j].prezime);
  90.                 niz[i].poeni = niz[j].poeni;
  91.                
  92.                 strcpy(niz[j].index, temp.index);
  93.                 strcpy(niz[j].ime, temp.ime);
  94.                 strcpy(niz[j].prezime, temp.prezime);
  95.                 niz[j].poeni = temp.poeni;
  96.             }
  97.         }
  98.    
  99.     for(i = 0; i <n; i++) {
  100.         fprintf(s, "%s %s %s %u\n", niz[i].index, niz[i].ime, niz[i].prezime, niz[i].poeni);
  101.     }
  102.    
  103.     fclose(s);
  104. }
  105.  
  106. void b (STUDENT *niz, int n) {
  107.     int i = 1, maxs, mins;;
  108.    
  109.     maxs = strlen(niz[0].prezime);
  110.     mins = strlen(niz[0].prezime);
  111.    
  112.     for(; i < n; i++) {
  113.         if(strlen(niz[i].prezime) > maxs) {
  114.             maxs = strlen(niz[i].prezime);
  115.         } else if(strlen(niz[i].prezime) < mins) {
  116.             mins = strlen(niz[i].prezime);
  117.         }
  118.     }
  119.    
  120.     for(i = 0; i < n; i++) {
  121.         if(strlen(niz[i].prezime) == maxs) {
  122.             printf("Najduze prezime je : %s.\n", niz[i].prezime);
  123.         }
  124.        
  125.         if(strlen(niz[i].prezime) == mins) {
  126.             printf("Najkrace prezime je : %s.\n", niz[i].prezime);
  127.         }
  128.     }
  129. }
  130.  
  131. void c (STUDENT *niz, int n, char *ppoeni) {
  132.     FILE *p;
  133.     int i = 0;
  134.    
  135.     char naziv[100];
  136.     strcpy(naziv, "preko_");
  137.     strcat(naziv, ppoeni);
  138.     strcat(naziv, "_poena.txt");
  139.    
  140.     p = safe_open(naziv, "w");
  141.    
  142.     for(; i < n; i++) {
  143.         if(niz[i].poeni > atoi(ppoeni))
  144.             fprintf(p, "%s %s %s %u\n", niz[i].index, niz[i].ime, niz[i].prezime, niz[i].poeni);
  145.     }
  146.    
  147.     fclose(p);
  148. }
  149.  
  150. void d (STUDENT *niz, int n) {
  151.     FILE *p;
  152.     int i = 0, dimena = 0;
  153.    
  154.     p = safe_open("izlaz.txt", "w");
  155.    
  156.     for(; i < n; i++) {
  157.         dimena += strlen(niz[i].ime);
  158.     }
  159.    
  160.     fprintf(p, "Prosecna duzina prezimena je: %d.\n", dimena/n);
  161.    
  162.     fclose(p);
  163. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement