riccardinofuffolo

Algoritmi e programmazione - Totocalcio

Nov 12th, 2012
136
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 1.92 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3.  
  4. #define ESITI 3
  5. #define MAXNOME 64
  6.  
  7. int conta_righe(char nomefile[]);
  8. void leggi(char** *matrice, char nomefile[], int N);
  9. void espandi(char** matrice, char* combinazione, int i, int j, int N);
  10.  
  11. main()
  12. {
  13.     char sistema[MAXNOME+1];
  14.     int partite, i, j;
  15.     char** pronostici;
  16.     char* combinazione;
  17.  
  18.     printf("File input? ");
  19.     scanf("%s", sistema);
  20.  
  21.     partite = conta_righe(sistema);
  22.     leggi(&pronostici, sistema, partite);
  23.     combinazione = (char*)malloc(partite*sizeof(char));
  24.     espandi(pronostici, combinazione, 0, 0, partite);
  25.  
  26.     system("PAUSE");   
  27.     return 0;
  28. }
  29.  
  30. int conta_righe(char nomefile[])
  31. {
  32.     FILE *input;
  33.     char car;
  34.     int righe = 0;
  35.     input = fopen(nomefile, "r");
  36.     if (input != NULL)
  37.         {
  38.         while (fscanf(input, "%c", &car)!=EOF)
  39.             {
  40.             if (car == '\n')
  41.                 righe++;
  42.             }
  43.             fclose(input);
  44.         }
  45.     else
  46.         {
  47.         printf("File non trovato. ");
  48.         system("PAUSE");
  49.         exit(EXIT_FAILURE);
  50.         }
  51.     return righe;
  52. }
  53.  
  54. void leggi(char** *matrice, char nomefile[], int N)
  55. {
  56.     char **tmpmatr, riga[ESITI+1];
  57.     FILE *input;
  58.     int i, j;
  59.  
  60.     input = fopen(nomefile, "r");
  61.     if (input != NULL)
  62.         {
  63.             tmpmatr = (char**) malloc(N*sizeof(char*));
  64.         for (i = 0; i < N; i++)
  65.             {
  66.             tmpmatr[i] = (char*) malloc((ESITI+1)*sizeof(char));
  67.             fscanf(input, "%s", tmpmatr[i]);
  68.             }
  69.         fclose(input);
  70.         *matrice = tmpmatr;
  71.         }
  72.     else
  73.         {
  74.         printf("Errore in lettura. ");
  75.         system("PAUSE");
  76.         exit(EXIT_FAILURE);
  77.         }
  78.        
  79. }
  80.  
  81. void espandi(char** matrice, char* combinazione, int i, int j, int N)
  82. {
  83.     j = 0;
  84.     if (i == N)
  85.         {
  86.         combinazione[i] = '\0';
  87.         printf("%s\n", combinazione);  
  88.         }
  89.     else
  90.         {
  91.         while (matrice[i][j]!='\0')
  92.             {
  93.             combinazione[i] = matrice[i][j];
  94.             espandi(matrice, combinazione, i+1, j, N);
  95.             j++;
  96.             }
  97.         i--;
  98.         }  
  99. }
Advertisement
Add Comment
Please, Sign In to add comment