Advertisement
Mazamin

Città cap

Jan 20th, 2019
126
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 2.81 KB | None | 0 0
  1. /*
  2.  Si scriva un programma che legge da un FILE contenetente per ogni riga il nome di una citta',
  3.  la sua provincia di appartennenza e il codice di avviamento postale.
  4.  Il programma calcola poi un codice relativo ad ogni riga e formato dalle prime 2 lettere del
  5.  nome della citta', dalle prime 2 lettere della provincia e dalle prime sue lettere del codice
  6.  postale e le stampa su un file come rioportato nell'esempio.
  7.  
  8.  ESEMPIO:
  9.  Con riferimento al file "testo.txt" contenente le seguenti citta':
  10.  
  11.             Eboli Salerno 84025
  12.             Rho Milano 20017
  13.             Pompei Napoli 80045
  14.             Pomezia Roma 00040
  15.             Battipaglia Salerno 84091
  16.             SanGennaroVesuviano Napoli 80040
  17.  
  18.  il programma calcola i codici e produce ESATTAMENTE il seguente output in un file:
  19.  
  20.             1.  (EbSa84) Eboli Salerno 84025
  21.             2.  (RhMi02) Rho Milano 20017
  22.             3.  (PoNa80) Pompei Napoli 80045
  23.             4.  (PoRo06) Pomezia Roma 00040
  24.             5.  (BaSa84) Battipaglia Salerno 84091
  25.             6.  (SaNa80) SanGennaroVesuviano Napoli 80040
  26. */
  27.  
  28. #include <stdio.h>
  29. #include <stdlib.h>
  30. #include <string.h>
  31. #define MAXSTR 100
  32. #define MAXCAR 100
  33. #define MAXCIT 32
  34.  
  35. /* Prototipi di funzione */
  36. int LeggiElenco(FILE *fp, char [MAXCIT][MAXCAR]);
  37. void GeneraCodici(char [MAXCIT][MAXCAR], char [MAXCIT][MAXCAR], int);
  38. void SalvaSuFile(char [MAXCIT][MAXCAR], char [MAXCIT][MAXCAR], int);
  39. FILE * my_fopen(char *);
  40.  
  41. /* programma principale */
  42. int main()
  43. {
  44.     int n;
  45.     FILE * fp;
  46.     char string[MAXCIT][MAXCAR], code[MAXCIT][MAXCAR];
  47.     fp=my_fopen("r");
  48.     n=LeggiElenco(fp, string);
  49.     GeneraCodici(string, code, n);
  50.     fclose(fp);
  51.     SalvaSuFile(string, code, n);
  52.     return 0;
  53. }
  54.  
  55. /* FUNZIONE DA COMPLETARE */
  56. int LeggiElenco(FILE *fp, char string[][MAXCAR])
  57. {
  58.     int n=0;
  59.     while(feof(fp)==0){
  60.         fgets(string[n++], MAXCAR-1, fp);
  61.     }
  62.     return n;
  63. }
  64.  
  65. /* FUNZIONE DA COMPLETARE */
  66. void GeneraCodici(char string[][MAXCAR], char code[][MAXCAR], int r)
  67. {
  68.     int i, numtemp;
  69.     char strtemp[2][MAXSTR];
  70.     for(i=0;i<r;i++){
  71.         sscanf(string[i], "%s %s %d", strtemp[0], strtemp[1], &numtemp);
  72.         strtemp[0][2]='\0';
  73.         strtemp[1][2]='\0';
  74.         sprintf(code[i], "%s%s%.2d", strtemp[0], strtemp[1], numtemp/1000);
  75.     }
  76. }
  77.  
  78. /* FUNZIONE DA COMPLETARE */
  79. void SalvaSuFile(char string[][MAXCAR], char code[][MAXCAR], int r)
  80. {
  81.     int i;
  82.     FILE * fp;
  83.     fp=my_fopen("w");
  84.     for(i=0;i<r;i++)
  85.         fprintf(fp, "%d. (%s) %s", i+1, code[i], string[i]);
  86. }
  87.  
  88. FILE * my_fopen(char * mode){
  89.     FILE * fp;
  90.     char filename[MAXSTR];
  91.     printf("Inserisci il nome del file di %s\n", (mode[0]=='r')?("input"):("output"));
  92.     scanf("%s", filename);
  93.     if((fp=fopen(filename, mode))==NULL)
  94.         abort();
  95.     return fp;
  96. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement