Skip to content
This repository
Browse code

refactor code, agrupate

  • Loading branch information...
commit 9b6bf2eee8d59cc36676a77d79fcc1cf34997538 1 parent 1cd6cf3
Ximo joatumas authored committed

Showing 2 changed files with 39 additions and 31 deletions. Show diff stats Hide diff stats

  1. +39 30 joatumas/C/katalonja.c
  2. +0 1  joatumas/C/test.sh
69 joatumas/C/katalonja.c
... ... @@ -1,54 +1,63 @@
1 1 #include <stdio.h>
2 2 #include <stdlib.h>
3 3
4   -#define DIST_BCN 1100
5   -#define DIST_MAD 800
6   -#define DIST_LSB 600
7   -
8 4 void usage(char *prog_name){
9 5 printf("%s Kilos_Vieiras Kilos_Pulpo Kilos_Centollos\n", prog_name);
10 6 }
11 7 int main(int argc, char **argv){
12 8 long int kilos;
13   - int valors_mad[3] = {500, 0, 450};
14   - int valors_bcn[3] = {450, 120, 0};
15   - int valors_lsb[3] = {600, 100, 500};
16   - int perdues_mad = -5 - 2 * 2 * DIST_MAD;
17   - int perdues_bcn = -5 - 2 * 2 * DIST_BCN;
18   - int perdues_lsb = -5 - 2 * 2 * DIST_LSB;
19   - double ganancies_mad;
20   - double ganancies_bcn;
21   - double ganancies_lsb;
22   - int i;
  9 + int valors[3][3] = {{500, 0, 450},{450, 120, 0},{600, 100, 500}};
  10 + int dists[3] = {800, 1100, 600};
  11 + int perdues[3];
  12 + double ganancies[3];
  13 + int i, j, max;
  14 +
23 15 if(argc != 4){
24 16 usage(argv[0]);
25 17 exit(-1);
26 18 }
27 19
  20 + //Read for load
28 21 for(i = 0; i<3; i++){
29 22 kilos = strtol(argv[i+1], (char **)NULL, 10);
30 23 //printf("kilos: %ld\n", kilos);
31   - ganancies_mad += kilos * valors_mad[i];
32   - ganancies_bcn += kilos * valors_bcn[i];
33   - ganancies_lsb += kilos * valors_lsb[i];
  24 + //And calculate neat value for each market:
  25 + for(j = 0; j<3; j++){
  26 + ganancies[j] += kilos * valors[j][i];
  27 + }
34 28 }
35 29 //printf("%f %f %f\n", ganancies_mad, ganancies_bcn, ganancies_lsb);
36   - ganancies_mad = ganancies_mad * (1 - (0.01 * (int)(DIST_MAD / 100)));
37   - ganancies_bcn = ganancies_bcn * (1 - (0.01 * (int)(DIST_BCN / 100)));
38   - ganancies_lsb = ganancies_lsb * (1 - (0.01 * (int)(DIST_LSB / 100)));
  30 +
  31 + //Devaluate the neat value
  32 + for(j = 0; j<3; j++){
  33 + ganancies[j] = ganancies[j] * (1 - (0.01 * (int)(dists[j] / 100)));
  34 + }
39 35 //printf("%f %f %f\n", ganancies_mad, ganancies_bcn, ganancies_lsb);
40 36 //printf("%d %d %d\n", perdues_mad, perdues_bcn, perdues_lsb);
41   - ganancies_mad -= perdues_mad;
42   - ganancies_bcn -= perdues_bcn;
43   - ganancies_lsb -= perdues_lsb;
44   - //printf("%f %f %f\n", ganancies_mad, ganancies_bcn, ganancies_lsb);
45   - if(ganancies_mad > ganancies_bcn && ganancies_mad > ganancies_lsb){
46   - printf("MADRID");
47   - }else if(ganancies_bcn > ganancies_mad && ganancies_bcn > ganancies_lsb){
48   - printf("BARCELONA");
49   - }else if(ganancies_lsb > ganancies_mad && ganancies_lsb > ganancies_bcn){
50   - printf("LISBOA");
  37 +
  38 + //Loses for transport
  39 + for(j = 0; j<3; j++){
  40 + ganancies[j] -= 5 + 2*2*dists[j];
51 41 }
52 42
  43 + //Search for greatest:
  44 + max = 0;
  45 + for(j = 1; j<3; j++){
  46 + if(ganancies[j]>ganancies[max]){
  47 + max = j;
  48 + }
  49 + }
  50 + switch(max){
  51 + case 0:
  52 + printf("MADRID");
  53 + break;
  54 + case 1:
  55 + printf("BARCELONA");
  56 + break;
  57 + case 2:
  58 + default:
  59 + printf("LISBOA");
  60 + break;
  61 + }
53 62 exit(0);
54 63 }
1  joatumas/C/test.sh
@@ -7,7 +7,6 @@ else
7 7 fi
8 8
9 9 RES=`./katalonja 0 200 0`;
10   -RES=`./katalonja 50 100 50`;
11 10 if [ $RES = "BARCELONA" ];then
12 11 echo Segona prova correcta;
13 12 else

0 comments on commit 9b6bf2e

Please sign in to comment.
Something went wrong with that request. Please try again.