Advertisement
stev4

Untitled

Dec 5th, 2019
470
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 2.64 KB | None | 0 0
  1. #include <stdlib.h>
  2. #include <string.h>
  3. #include <stdio.h>
  4.  
  5. #define MAX_NIZ 30
  6. #define MAX_IME 20+1
  7.  
  8. struct prevoznik_st{
  9.     char operater[MAX_IME];
  10.     double cena_karte;
  11.     double maksimalna_masa;
  12.     double doplata;
  13. };
  14.  
  15. struct cena_st{
  16.     double ukupna_cena;
  17.     char operater[MAX_IME];
  18. };
  19.  
  20.  
  21. FILE *safe_fopen(char in_filename[], char mode[], int error_code);
  22. void ucitaj_fajl(FILE *in, struct prevoznik_st prevoznici[], int *n);
  23. void transform(struct prevoznik_st revoznici[], struct cena_st cene[], int n, double masa,struct cena_st temp[]);
  24. double racunaj_cenu(double masa,struct prevoznik_st prevoznici);
  25. void cuvaj_fajl(FILE *out,struct cena_st cene[], int n);
  26.  
  27. int main(int arg_num, char **args){
  28.    if(arg_num!=4){
  29.     printf("Uneli ste pogresnu komandu da startate program!");
  30.     exit(404);
  31.    }
  32.  
  33. double masa = atof(args[1]);
  34. char *in_filename=args[2];
  35. char *out_filename=args[3];
  36.  
  37. FILE *in= safe_fopen(in_filename, "r", 2);
  38. FILE *out= safe_fopen(out_filename, "w", 3);
  39.  
  40. struct prevoznik_st prevoznici[MAX_NIZ];
  41. struct cena_st cene[MAX_NIZ];
  42. struct cena_st tempst[MAX_NIZ];
  43. int n;
  44.  
  45. ucitaj_fajl(in, prevoznici, &n);
  46. transform(prevoznici, cene, n, masa,tempst);
  47.  
  48. cuvaj_fajl(out, cene, n);
  49.  
  50. fclose(in);
  51. fclose(out);
  52. return 0;
  53. }
  54.  
  55. FILE *safe_fopen(char filename[], char mode[], int error_code){
  56.  
  57. FILE *fp=fopen(filename,mode);
  58. if (fp==NULL){
  59.         printf("Can't open '%s'!\n", filename);
  60.         exit(error_code);
  61. }
  62. return fp;
  63. }
  64.  
  65.  
  66.  
  67. void ucitaj_fajl(FILE *in, struct prevoznik_st prevoznici[], int *n){
  68.  
  69. (*n)=0;
  70.  
  71. while(fscanf(in, "%s %lf %lf %lf",
  72.     prevoznici[*n].operater,
  73.     &prevoznici[*n].cena_karte,
  74.     &prevoznici[*n].maksimalna_masa,
  75.     &prevoznici[*n].doplata)!=EOF){
  76.     (*n)++;
  77.     }
  78. }
  79.  
  80.  
  81. void transform(struct prevoznik_st prevoznici[], struct cena_st cene[], int n, double masa,struct cena_st tempst[]){
  82.  
  83. int i;
  84. for(i=0;i<n;i++){
  85.     strcpy(cene[i].operater,prevoznici[i].operater);
  86.     cene[i].ukupna_cena=racunaj_cenu(masa,prevoznici[i]);
  87.  
  88. }
  89.  
  90. //sortiranje moze i drugoj funkciji
  91. for(i=0;i<(n-1);i++){
  92.     for(j=0;j<(n-i-1);j++){
  93.     if(cene[j].ukupna_cena>cene[j+1].ukupna_cena){
  94.    
  95.     tempst[i]=cene[j];
  96.     cene[j]=cene[j+1];
  97.     cene[j+1]=tempst[i];
  98.    
  99.     }
  100.     }
  101. }
  102. }
  103.  
  104. double racunaj_cenu(double masa,struct prevoznik_st prevoznici){
  105.     double maks=prevoznici.maksimalna_masa;
  106.     double cena=prevoznici.cena_karte;
  107.     double doplata=prevoznici.doplata;
  108.     double suma;
  109.  
  110.     if (maks<masa)
  111.     suma=cena+((masa-maks)*doplata);
  112.      else
  113.     suma=cena;
  114.  
  115. return suma;
  116.  
  117. }
  118.  
  119.  
  120. void cuvaj_fajl(FILE *out,struct cena_st cene[], int n){
  121.  
  122. int i;
  123. for(i=0;i<n;i++){
  124.  
  125. fprintf(out, "%7.2f %s \n",
  126.     cene[i].ukupna_cena,
  127.     cene[i].operater);
  128.  
  129. }}
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement