108 lines
3.4 KiB
C
108 lines
3.4 KiB
C
#include <stdio.h>
|
|
#include <stdlib.h>
|
|
#include <string.h>
|
|
#include <ctype.h>
|
|
#include <math.h>
|
|
|
|
#define MAX_LINE 1024
|
|
// Funzione per contare le cifre dispari
|
|
int conta_cifre_dispari(FILE *file)
|
|
{
|
|
unsigned counter = 0;
|
|
unsigned ch;
|
|
while ((ch = fgetc(file))!=EOF) {
|
|
if (isdigit(ch)) {
|
|
if (ch % 2 != 0)
|
|
counter++;
|
|
}
|
|
}
|
|
return counter;
|
|
}
|
|
|
|
|
|
// Conteggio numeri (non cifre) nella linea 6 (la prima linea la 1)
|
|
int conta_numeri_linea(FILE *file, int n)
|
|
{
|
|
char riga[MAX_LINE];
|
|
unsigned counter = 0;
|
|
unsigned linecounter = 1;
|
|
while(fgets(riga,sizeof(riga),file)) {
|
|
if (linecounter == n) {
|
|
char prova[MAX_LINE];
|
|
size_t provacounter = 0;
|
|
int digitFound = 0;
|
|
for (size_t i = 0; i < strlen(riga); i++) {
|
|
if (isdigit(riga[i])) {
|
|
digitFound = 1;
|
|
prova[provacounter] = riga[i];
|
|
provacounter++;
|
|
}
|
|
else if(digitFound) {
|
|
digitFound = 0;
|
|
counter++;
|
|
}
|
|
}
|
|
// printf("%s\n",riga);
|
|
linecounter++;
|
|
}
|
|
else
|
|
linecounter++;
|
|
}
|
|
return counter;
|
|
}
|
|
|
|
|
|
// visualizza la linea piu corta
|
|
char * stampaLineaCorta(char * const strmin, FILE* file)
|
|
{
|
|
char riga[MAX_LINE] = "";
|
|
char corta[MAX_LINE] = "gggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggg";
|
|
while(fgets(riga,sizeof(riga),file)) {
|
|
if (strlen(riga) < strlen(corta)) {
|
|
strcpy(corta,riga);
|
|
}
|
|
}
|
|
// printf("%s",corta);
|
|
strcpy(strmin,corta);
|
|
return strmin;
|
|
}
|
|
|
|
|
|
int main()
|
|
{
|
|
FILE *file = fopen("file.txt", "r");
|
|
if (!file)
|
|
{
|
|
perror("Errore nell'apertura del file");
|
|
return 1;
|
|
}
|
|
|
|
// Conteggio cifre dispari
|
|
rewind(file);
|
|
printf("Numero di cifre dispari: %d\n", conta_cifre_dispari(file));
|
|
|
|
// Conteggio numeri (non cifre) nella linea 6 (la prima linea la 1)
|
|
rewind(file);
|
|
printf("Numero di numeri nella linea 6: %d\n", conta_numeri_linea(file, 6));
|
|
|
|
// visualizza la linea pi corta
|
|
rewind(file);
|
|
char result[500+1];
|
|
*(result+500) = '\0';
|
|
stampaLineaCorta(result, file);
|
|
printf("Linea piu' corta: %s %u\n", result, strlen(result));
|
|
|
|
//salvare i risultati suL file di testo "tuoCognomeProgResult.txt" //esempio se fossi io "zoliProgResult.txt"
|
|
//PROGETTO LIBERO
|
|
rewind(file);
|
|
FILE* outputFile = fopen("vichiProgResult.txt","w");
|
|
fprintf(outputFile,"%d\n",conta_cifre_dispari(file));
|
|
rewind(file);
|
|
fprintf(outputFile,"%d\n",conta_numeri_linea(file, 6));
|
|
rewind(file);
|
|
fprintf(outputFile,"%s %u\n", result, strlen(result));
|
|
fclose(outputFile);
|
|
fclose(file);
|
|
return 0;
|
|
}
|