Advertisement
stev4

Untitled

Dec 18th, 2019
343
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 2.74 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <string.h>
  4.  
  5. #define MAX_SIMBOL 2+1
  6. #define MAX_IME 20+1
  7. #define MAX_VRSTA 20+1
  8.  
  9. typedef struct element_st{
  10.     char simbol[MAX_SIMBOL];
  11.     char ime[MAX_IME];
  12.     unsigned tezina;
  13.     char vrsta[MAX_VRSTA];
  14.    
  15.     struct element_st *next;
  16. } ELEMENT;
  17.  
  18. void init_list(ELEMENT **head){
  19.     *head=NULL;
  20. }
  21.  
  22. FILE *file_sopen(char filename[], char mode[], int error_code){
  23.     FILE *fp=fopen(filename,mode);
  24.     if(fp==NULL){
  25.     printf("Niste uspeli da otvorite fajl %s", filename);
  26.     exit(error_code);
  27.     }
  28.     return fp;
  29. }
  30.  
  31. ELEMENT* kreiraj_novi_item(char simbol[],char ime[],unsigned tezina,char vrsta[]){
  32.     ELEMENT *new=(ELEMENT *)malloc(sizeof(ELEMENT));
  33.     if(new==NULL){
  34.     exit(21);
  35.     }
  36.    
  37.     strcpy(new->simbol, simbol);
  38.     strcpy(new->ime, ime);
  39.     new->tezina=tezina;
  40.     strcpy(new->vrsta, vrsta);
  41.  
  42.     new->next=NULL;
  43.  
  44.     return new;
  45. }
  46.  
  47. void add_to_list(ELEMENT *new,ELEMENT **head){
  48.     if(*head == NULL) { // list is empty
  49.         *head = new;
  50.         return;
  51.     }
  52.  
  53.     add_to_list(new, &((*head)->next));
  54. }
  55.  
  56.  
  57. void read_list_from(FILE *in,ELEMENT **head){
  58.     char simbol[MAX_SIMBOL];
  59.     char ime[MAX_IME];
  60.     unsigned tezina;
  61.     char vrsta[MAX_VRSTA];
  62.  
  63.     while(fscanf(in,"%s %s %u %s",simbol, ime, &tezina, vrsta)!=EOF){
  64.     ELEMENT *new=kreiraj_novi_item(simbol,ime,tezina,vrsta);
  65.     add_to_list(new,head);
  66.     }  
  67. }
  68. void save_item_to(FILE *out, ELEMENT *x){
  69.     fprintf(out, "%-2s %3u %s \n",
  70.     x->simbol, x->tezina, x->vrsta);
  71.  
  72. }
  73.  
  74. void save_list_to(FILE *out,ELEMENT *head){
  75.     if(head !=NULL){
  76.     save_item_to(out, head);
  77.     save_list_to(out, head->next);
  78. }
  79. }
  80.  
  81.  
  82.  
  83. void destroy_list(ELEMENT **head){
  84.     if(*head!=NULL){
  85.     destroy_list(&((*head)->next));
  86.     free(*head);
  87.     *head=NULL;    
  88.     }
  89. }
  90.  
  91. ELEMENT *nadji_najbolji(ELEMENT *head,char vrsta[]){
  92. if(head==NULL){
  93.     return NULL;}
  94. ELEMENT *best=NULL;
  95. while(head!=NULL){
  96.     if(strcmp(head->vrsta,vrsta)==0){
  97.     if(best==NULL)
  98.     best=head;
  99.     else if(head->tezina>best->tezina)
  100.     best=head;
  101.     }
  102.    
  103.     head=head->next;
  104. }
  105. return best;
  106. }
  107.  
  108. int main(int arg_num, char *args[]){
  109.  
  110. if(arg_num!=4){
  111.     printf("Niste lepo pokrenuli program.");
  112.     exit(212);
  113. }
  114.  
  115. char *vrsta=args[1];
  116. char *in_filename=args[2];
  117. char *out_filename=args[3];
  118.  
  119. FILE *in=file_sopen(in_filename, "r", 1);
  120. FILE *out=file_sopen(out_filename, "w", 2);
  121.  
  122. ELEMENT *head;
  123. init_list(&head);
  124.  
  125. read_list_from(in, &head);
  126. save_list_to(out, head);
  127.  
  128. ELEMENT *best=nadji_najbolji(head,vrsta);
  129. if (best == NULL) {
  130.         fprintf(out, "\nU uzorku nije pronadjen nijedan %s!\n", vrsta);
  131.     } else {
  132.         fprintf(
  133.             out, "\nNajtezi %s je %s (%s), atomska tezina %u\n",
  134.             best->vrsta, best->ime, best->simbol, best->tezina
  135.         );
  136.     }
  137.  
  138. destroy_list(&head);
  139.  
  140. fclose(in);
  141. fclose(out);
  142.  
  143. return 0;}
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement