Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdlib.h>
- #include <string.h>
- #include <stdio.h>
- #define MAX_NIZ 30
- #define MAX_IME 20+1
- struct prevoznik_st{
- char operater[MAX_IME];
- double cena_karte;
- double maksimalna_masa;
- double doplata;
- };
- struct cena_st{
- double ukupna_cena;
- char operater[MAX_IME];
- };
- FILE *safe_fopen(char in_filename[], char mode[], int error_code);
- void ucitaj_fajl(FILE *in, struct prevoznik_st prevoznici[], int *n);
- void transform(struct prevoznik_st revoznici[], struct cena_st cene[], int n, double masa,struct cena_st temp[]);
- double racunaj_cenu(double masa,struct prevoznik_st prevoznici);
- void cuvaj_fajl(FILE *out,struct cena_st cene[], int n);
- int main(int arg_num, char **args){
- if(arg_num!=4){
- printf("Uneli ste pogresnu komandu da startate program!");
- exit(404);
- }
- double masa = atof(args[1]);
- char *in_filename=args[2];
- char *out_filename=args[3];
- FILE *in= safe_fopen(in_filename, "r", 2);
- FILE *out= safe_fopen(out_filename, "w", 3);
- struct prevoznik_st prevoznici[MAX_NIZ];
- struct cena_st cene[MAX_NIZ];
- struct cena_st tempst[MAX_NIZ];
- int n;
- ucitaj_fajl(in, prevoznici, &n);
- transform(prevoznici, cene, n, masa,tempst);
- cuvaj_fajl(out, cene, n);
- fclose(in);
- fclose(out);
- return 0;
- }
- FILE *safe_fopen(char filename[], char mode[], int error_code){
- FILE *fp=fopen(filename,mode);
- if (fp==NULL){
- printf("Can't open '%s'!\n", filename);
- exit(error_code);
- }
- return fp;
- }
- void ucitaj_fajl(FILE *in, struct prevoznik_st prevoznici[], int *n){
- (*n)=0;
- while(fscanf(in, "%s %lf %lf %lf",
- prevoznici[*n].operater,
- &prevoznici[*n].cena_karte,
- &prevoznici[*n].maksimalna_masa,
- &prevoznici[*n].doplata)!=EOF){
- (*n)++;
- }
- }
- void transform(struct prevoznik_st prevoznici[], struct cena_st cene[], int n, double masa,struct cena_st tempst[]){
- int i;
- for(i=0;i<n;i++){
- strcpy(cene[i].operater,prevoznici[i].operater);
- cene[i].ukupna_cena=racunaj_cenu(masa,prevoznici[i]);
- }
- //sortiranje moze i drugoj funkciji
- for(i=0;i<(n-1);i++){
- for(j=0;j<(n-i-1);j++){
- if(cene[j].ukupna_cena>cene[j+1].ukupna_cena){
- tempst[i]=cene[j];
- cene[j]=cene[j+1];
- cene[j+1]=tempst[i];
- }
- }
- }
- }
- double racunaj_cenu(double masa,struct prevoznik_st prevoznici){
- double maks=prevoznici.maksimalna_masa;
- double cena=prevoznici.cena_karte;
- double doplata=prevoznici.doplata;
- double suma;
- if (maks<masa)
- suma=cena+((masa-maks)*doplata);
- else
- suma=cena;
- return suma;
- }
- void cuvaj_fajl(FILE *out,struct cena_st cene[], int n){
- int i;
- for(i=0;i<n;i++){
- fprintf(out, "%7.2f %s \n",
- cene[i].ukupna_cena,
- cene[i].operater);
- }}
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement