diff --git a/Laboratorio 1/EsempioIO.c b/Laboratorio 1/EsempioIO.c new file mode 100644 index 0000000..cab6b92 --- /dev/null +++ b/Laboratorio 1/EsempioIO.c @@ -0,0 +1,43 @@ +#include +int main() +{ + FILE *fp_read, *fp_write; + char file_char, choice; + if ((fp_read = fopen("../Guide.txt", "r")) == NULL) + { + printf("Error opening file\n"); + return 1; + } + if ((fp_write = fopen("../Output.txt", "w")) == NULL) + { + printf("Error opening file\n"); + return 2; + } + printf("Print on console (C) or on file (F):"); + choice = getchar(); + while (!feof(fp_read)) + { + file_char = fgetc(fp_read); + if (!feof(fp_read)) + { + switch (choice) + { + case 'C': + printf("\nChar printed on the console: %c", + file_char); + break; + case 'F': + fputc(file_char, fp_write); + printf("\nChar saved on file: "); + putchar(file_char); + break; + default: + printf("Wrong choice\n"); + return 3; + } + } + } + fclose(fp_read); + fclose(fp_write); + return 0; +} diff --git a/Laboratorio 1/Operations.txt b/Laboratorio 1/Operations.txt new file mode 100644 index 0000000..51c111a --- /dev/null +++ b/Laboratorio 1/Operations.txt @@ -0,0 +1,4 @@ ++ 15.225 30.51 +- 42.1 10.01 +* 0.62 2.4 +/ 5.0 2.5 diff --git a/Laboratorio 1/Results.txt b/Laboratorio 1/Results.txt new file mode 100644 index 0000000..fb4ec41 --- /dev/null +++ b/Laboratorio 1/Results.txt @@ -0,0 +1,4 @@ ++ 45.74 +- 32.09 +* 1.49 +/ 2.00 diff --git a/Laboratorio 1/TestAmbiente.c b/Laboratorio 1/TestAmbiente.c new file mode 100644 index 0000000..574f86e --- /dev/null +++ b/Laboratorio 1/TestAmbiente.c @@ -0,0 +1,12 @@ +#include +int main(void) +{ + int x, y; + float z; + printf("Insert an integer number:"); + scanf("%d", &x); + y = 3; + z = (float)(x) / y; + printf("%d/%d=%.3f\n", x, y, z); + return 0; +} \ No newline at end of file diff --git a/Laboratorio 1/calcolaaree.c b/Laboratorio 1/calcolaaree.c new file mode 100644 index 0000000..bfc1089 --- /dev/null +++ b/Laboratorio 1/calcolaaree.c @@ -0,0 +1,40 @@ +#include +#define P 3.14 + +int main() +{ + char fig; + char dato; + float area; + float num; + scanf("%c %c%f", &fig, &dato, &num); + + if (fig == 'Q') + { + if (dato == 'D') + { + area = num * num / 2; + } + else if (dato == 'L') + { + area = num * num; + } + + printf("Area quadrato = %f\n", area); + } + else if (fig == 'C') + { + if (dato == 'D') + { + area = P * num * num / 4; + } + else if (dato == 'R') + { + area = P * num * num; + } + + printf("Area cerchio = %f\n", area); + } + + return 0; +} \ No newline at end of file diff --git a/Laboratorio 1/calcolatrice.c b/Laboratorio 1/calcolatrice.c new file mode 100644 index 0000000..587170e --- /dev/null +++ b/Laboratorio 1/calcolatrice.c @@ -0,0 +1,43 @@ +#include +int main() +{ + char operation; + float op1, op2, res; + + operation = getchar(); + if (scanf("%f %f", &op1, &op2) != 2) { + printf("Error: Invalid input\n"); + return 2; + } + + switch (operation) + { + case '+': + res = op1 + op2; + break; + + case '-': + res = op1 - op2; + break; + + case '*': + res = op1 * op2; + break; + + case '/': + if (op2 == 0) { + printf("Error: divide by zero\n"); + return 2; + } + res = op1 / op2; + break; + + default: + printf("Error: invalid operation\n"); + return 1; + } + + printf("%c %f\n", operation, res); + + return 0; +} \ No newline at end of file diff --git a/Laboratorio 1/calcolatricefile.c b/Laboratorio 1/calcolatricefile.c new file mode 100644 index 0000000..82a0067 --- /dev/null +++ b/Laboratorio 1/calcolatricefile.c @@ -0,0 +1,61 @@ +#include +int main() +{ + + FILE *fp_read, *fp_write; + char operation; + float op1, op2, res; + + if ((fp_read = fopen("./Operations.txt", "r")) == NULL) + { + printf("Error opening file\n"); + return 1; + } + if ((fp_write = fopen("./Results.txt", "w")) == NULL) + { + printf("Error opening file\n"); + return 2; + } + + while (!feof(fp_read)) + { + fscanf(fp_read, " %c %f %f", &operation, &op1, &op2); + if (!feof(fp_read)) + { + switch (operation) + { + case '+': + res = op1 + op2; + break; + + case '-': + res = op1 - op2; + break; + + case '*': + res = op1 * op2; + break; + + case '/': + if (op2 == 0) + { + printf("Error: divide by zero\n"); + return 3; + } + res = op1 / op2; + break; + + default: + printf("Error: invalid operation\n"); + return 1; + } + + fprintf(fp_write, "%c %.2f\n", operation, res); + } + } + + fclose(fp_read); + fclose(fp_write); + + return 0; +} \ No newline at end of file diff --git a/Laboratorio 2/compresso.txt b/Laboratorio 2/compresso.txt new file mode 100644 index 0000000..dd9caec --- /dev/null +++ b/Laboratorio 2/compresso.txt @@ -0,0 +1,4 @@ +Partenza $5Destinazione $3Costo + Parigi $9New York $410$2 + $4Roma $9 Londra $570$1 + $2Sidney $6Los Angeles $42$3 \ No newline at end of file diff --git a/Laboratorio 2/decompresso.txt b/Laboratorio 2/decompresso.txt new file mode 100644 index 0000000..f9d4b6e --- /dev/null +++ b/Laboratorio 2/decompresso.txt @@ -0,0 +1,4 @@ +Partenza Destinazione Costo + Parigi New York 1000 + Roma Londra 700 + Sidney Los Angeles 2222 \ No newline at end of file diff --git a/Laboratorio 2/esercizio1.c b/Laboratorio 2/esercizio1.c new file mode 100644 index 0000000..cb86730 --- /dev/null +++ b/Laboratorio 2/esercizio1.c @@ -0,0 +1,32 @@ +#include + +int mcd(int a, int b) { + if (a == 0) { + return b; + } + + if (a > b) { + return mcd(a % b, b); + } else { + return mcd(b % a, a); + } +} + +int main() +{ + int a, b, res; + if (scanf("%d %d", &a, &b) != 2) { + puts("Error, invalid input!"); + return 1; + } + + if (a < 0 || b < 0) { + puts("Error, you must insert two positive integers!"); + return 1; + } + + res = mcd(a, b); + printf("MCD = %d\n", res); + + return 0; +} \ No newline at end of file diff --git a/Laboratorio 2/esercizio2.c b/Laboratorio 2/esercizio2.c new file mode 100644 index 0000000..d095d04 --- /dev/null +++ b/Laboratorio 2/esercizio2.c @@ -0,0 +1,110 @@ +#include + +int comprimi(FILE *fin, FILE *fout) { + char lastChar = 0, currChar; // sono sicuro che il file non inizi con un null character + int count = 0; + + int charCount = 0; + + while (!feof(fin)) + { + currChar = fgetc(fin); + + if (feof(fin)) + break; + + + if (currChar != lastChar || count == 10) { + if (count > 1) { + fputc('$', fout); + fputc('0'+count-1, fout); + charCount += 2; + } + + count = 1; + lastChar = currChar; + fputc(currChar, fout); + charCount++; + } else { + count++; + } + } + + if (count > 1) { // Controllo se devo scrivere le ripetizioni dell'ultimo carattere + fputc('$', fout); + fputc('0'+count-1, fout); + charCount += 2; + } + + return charCount; +} + +int decomprimi(FILE *fin, FILE *fout) { + char lastChar, currChar; + int count, i; + + int charCount = 0; + while (!feof(fin)) + { + currChar = fgetc(fin); + + if (feof(fin)) + break; + + if (currChar == '$') { + count = fgetc(fin) - '0'; + + for (i = 0; i < count; i++) + fputc(lastChar, fout); + + charCount += count; + } else { + lastChar = currChar; + fputc(currChar, fout); + charCount++; + } + } + + return charCount; +} + + +int main() +{ + + FILE *fp_read, *fp_write; + char operation; + + puts("Inserisci 'c' per comprimere o qualsiasi altro carattere per decomprimere: "); + scanf(" %c", &operation); + + if (operation == 'c') { + fp_read = fopen("./sorgente.txt", "r"); + fp_write = fopen("./compresso.txt", "w"); + } else { + fp_read = fopen("./compresso.txt", "r"); + fp_write = fopen("./decompresso.txt", "w"); + } + + if (fp_read == NULL) + { + printf("Error opening input file\n"); + return 1; + } + if (fp_write == NULL) + { + printf("Error opening output file\n"); + return 2; + } + + if (operation == 'c') { + comprimi(fp_read, fp_write); + } else { + decomprimi(fp_read, fp_write); + } + + fclose(fp_read); + fclose(fp_write); + + return 0; +} \ No newline at end of file diff --git a/Laboratorio 2/sorgente.txt b/Laboratorio 2/sorgente.txt new file mode 100644 index 0000000..4982116 --- /dev/null +++ b/Laboratorio 2/sorgente.txt @@ -0,0 +1,4 @@ +Partenza Destinazione Costo + Parigi New York 1000 + Roma Londra 700 + Sidney Los Angeles 2222 \ No newline at end of file diff --git a/Laboratorio 3/elaborato.txt b/Laboratorio 3/elaborato.txt new file mode 100644 index 0000000..ece5432 --- /dev/null +++ b/Laboratorio 3/elaborato.txt @@ -0,0 +1,21 @@ +Caratterizzata da un pass| c:25 +ato turbolento, in | c:19 +epoca medievale Rouen fu | c:25 +devastata piu' volte | c:21 +da incendi ed epidemie e | c:25 +durante la Guerra | c:18 +dei Cent' Anni fu occupat| c:25 +a dagli inglesi. | c:18 +Nel **** nella sua piazza| c:25 + centrale la giovane | c:21 +Giovanna d' Arco ( Jeanne| c:25 + d' Arc) fu processata | c:23 +per eresia e arsa sul rog| c:25 +o! Durante la seconda | c:22 +guerra mondiale gli Allea| c:25 +ti bombardarono | c:16 +ampie zone della citta' ,| c:25 + soprattutto il | c:16 +quartiere che si estende | c:25 +a sud della cattedrale. | c:25 +ÿ | c:2 diff --git a/Laboratorio 3/esercizio1.c b/Laboratorio 3/esercizio1.c new file mode 100644 index 0000000..ccd8944 --- /dev/null +++ b/Laboratorio 3/esercizio1.c @@ -0,0 +1,108 @@ +#include +#include +#include +#define INPUT_FILE "./input.txt" +#define OUTPUT_FILE "./testo.txt" + +void writeChar(FILE *fout, int * charInLine, int * written, char chr, bool isOrig) { + if (chr != '\n') { + fputc(chr, fout); + } else { + for (int i = *charInLine; i <= 24; i++) { + fputc(' ', fout); + } + } + + if (*charInLine >= 24 || chr == '\n') { + fprintf(fout, "| c:%d \n", *written + 1); + *charInLine = 0; + *written = 0; + } else { + if (isOrig) { + *written += 1; + } + *charInLine += 1; + } +} + +void elabora(FILE *fin, FILE *fout) { + char lastChar, currChar, buf; + int count, i; + + int charInLine = 0, written = 0; + bool requiresSpace = false; + bool requiresCaps = false; + + while (!feof(fin)) + { + buf = fgetc(fin); + + if (feof(fin)) + break; + + currChar = buf; // This is done to avoid havind EOF char in currChar. + + if (requiresSpace) { + if (currChar != ' ') { + writeChar(fout, &charInLine, &written, ' ', false); + } + + requiresSpace = false; + } + + if (requiresCaps && isalpha(currChar)) { + if (currChar >= 97 && currChar <= 122) { + currChar -= 32; + } + + requiresCaps = false; + } + + if (isdigit(currChar)) { + writeChar(fout, &charInLine, &written, '*', true); + } else { + writeChar(fout, &charInLine, &written, currChar, true); + } + + if (currChar == '.' || currChar == ',' ||currChar == ';' ||currChar == ':' ||currChar == '!' ||currChar == '?' ||currChar == '\''||currChar == '('||currChar == ')') { + requiresSpace = true; + } + + if (currChar == '.' ||currChar == '!' ||currChar == '?') { + requiresCaps = true; + } + } + + // Add '\n' at the end of the file if not present + if (currChar != '\n') { + writeChar(fout, &charInLine, &written, '\n', false); + } +} + + +int main() +{ + + FILE *fp_read, *fp_write; + + fp_read = fopen(INPUT_FILE, "r"); + fp_write = fopen(OUTPUT_FILE, "w"); + + if (fp_read == NULL) + { + printf("Error opening file\n"); + return 1; + } + if (fp_write == NULL) + { + printf("Error opening file\n"); + return 2; + } + + elabora(fp_read, fp_write); + + fclose(fp_read); + fclose(fp_write); + + return 0; +} \ No newline at end of file diff --git a/Laboratorio 3/esercizio3.c b/Laboratorio 3/esercizio3.c new file mode 100644 index 0000000..86477c1 --- /dev/null +++ b/Laboratorio 3/esercizio3.c @@ -0,0 +1,71 @@ +#include +#include +#include +#define INPUT_FILE "./numeri.txt" + +int max(int a, int b) +{ + if (a > b) { + return a; + } + + return b; +} + +int min(int a, int b) +{ + if (a < b) { + return a; + } + + return b; +} + +int main() +{ + + FILE *fp_read; + + fp_read = fopen(INPUT_FILE, "r"); + + if (fp_read == NULL) + { + printf("Error opening file\n"); + return 1; + } + + int x, y, a; + int maxVal, minVal, scartati = 0; + + if (fscanf(fp_read, " %d", &x) == EOF || fscanf(fp_read, " %d",& y) == EOF) { + fclose(fp_read); + return 1; // Terminate if there are only two numbers + } + + minVal = min(x, y); + maxVal = max(x, y); + + while (!feof(fp_read)) + { + if (fscanf(fp_read, " %d", &a) == EOF) { + break; + } + + if (x + y != a && x - y != a && x * y != a && (y==0 || x / y != a )) { + scartati++; + continue; + } + + minVal = min(minVal, a); + maxVal = max(maxVal, a); + + x = y; + y = a; + } + + printf("Numero massimo: %d\nNumero minimo: %d\nNumeri scartati: %d\n", maxVal, minVal, scartati); + + fclose(fp_read); + + return 0; +} diff --git a/Laboratorio 3/input.txt b/Laboratorio 3/input.txt new file mode 100644 index 0000000..e37bdf0 --- /dev/null +++ b/Laboratorio 3/input.txt @@ -0,0 +1,9 @@ +Caratterizzata da un passato turbolento, in +epoca medievale Rouen fu devastata piu' volte +da incendi ed epidemie e durante la Guerra +dei Cent'Anni fu occupata dagli inglesi. +nel 1431 nella sua piazza centrale la giovane +Giovanna d'Arco (Jeanne d'Arc) fu processata +per eresia e arsa sul rogo!durante la seconda +guerra mondiale gli Alleati bombardarono +ampie zone della citta', soprattutto il quartiere che si estende a sud della cattedrale. diff --git a/Laboratorio 3/numeri.txt b/Laboratorio 3/numeri.txt new file mode 100644 index 0000000..e2776be --- /dev/null +++ b/Laboratorio 3/numeri.txt @@ -0,0 +1,10 @@ +12 +3 +4 +7 -3 +0 +4 +1 +3 +3 9 +11 diff --git a/Laboratorio 3/testo.txt b/Laboratorio 3/testo.txt new file mode 100644 index 0000000..836307b --- /dev/null +++ b/Laboratorio 3/testo.txt @@ -0,0 +1,20 @@ +Caratterizzata da un pass| c:25 +ato turbolento, in | c:19 +epoca medievale Rouen fu | c:25 +devastata piu' volte | c:21 +da incendi ed epidemie e | c:25 +durante la Guerra | c:18 +dei Cent' Anni fu occupat| c:24 +a dagli inglesi. | c:17 +Nel **** nella sua piazza| c:25 + centrale la giovane | c:21 +Giovanna d' Arco ( Jeanne| c:23 + d' Arc) fu processata | c:22 +per eresia e arsa sul rog| c:25 +o! Durante la seconda | c:21 +guerra mondiale gli Allea| c:25 +ti bombardarono | c:16 +ampie zone della citta' ,| c:24 + soprattutto il quartiere| c:25 + che si estende a sud del| c:25 +la cattedrale. | c:15 diff --git a/Laboratorio 4/matrix.txt b/Laboratorio 4/matrix.txt new file mode 100644 index 0000000..dbb6fe9 --- /dev/null +++ b/Laboratorio 4/matrix.txt @@ -0,0 +1,4 @@ +3 4 +1 2 3 4 +5 6 7 8 +9 0 1 1 diff --git a/Laboratorio 4/matrixquad.c b/Laboratorio 4/matrixquad.c new file mode 100644 index 0000000..3239701 --- /dev/null +++ b/Laboratorio 4/matrixquad.c @@ -0,0 +1,118 @@ +// Laboratorio 4 - Esercizio 3 +// Matteo Schiff - s295565 + +#include +#include +#define MAX_N 20 + +int min(int a, int b); +int stampaSottomatrice(int m[MAX_N][MAX_N], int x, int y, int dim); +void sottomatrici(int M[MAX_N][MAX_N], int nr, int nc, int dim); +int leggiMatrice(int M[MAX_N][MAX_N], int * nr, int * nc); + +int min(int a, int b) { + if (a < b) + return a; + + return b; +} + + +// La funzione stampa la sottomatrice e restituisce la somma degli elementi +int stampaSottomatrice(int m[MAX_N][MAX_N], int x, int y, int dim) { + int sum = 0; + for (int i = x; i < x + dim; i++) { + for (int j = y; j < y + dim; j++) { + printf("%d ", m[i][j]); + sum += m[i][j]; + } + putc('\n', stdout); + } + putc('\n', stdout); + + return sum; +} + +void sottomatrici(int M[MAX_N][MAX_N], int nr, int nc, int dim) { + int a, max = 0; + int x = -1, y = -1; + + printf("Le sottomatrici quadrate di dimensione %d sono:\n", dim); + + for (int i = 0; i <= nr - dim; i++) { + for (int j = 0; j <= nc - dim; j++) { + a = stampaSottomatrice(M, i, j, dim); + + if (a >= max) { + max = a; + x = i; + y = j; + } + } + } + + printf("La sottomatrice con somma degli elementi massima (%d) e':\n", max); + stampaSottomatrice(M, x, y, dim); +} + +int leggiMatrice(int M[MAX_N][MAX_N], int * nr, int * nc) { + char filename[23] = "./"; + FILE *fp_read; + + puts("Inserisci il nome del file"); + scanf("%20s", filename + 2); + + + fp_read = fopen(filename, "r"); + + if (fp_read == NULL) + { + printf("Errore nell'apertura del file\n"); + return 1; + } + + fscanf(fp_read, "%d %d\n", nr, nc); + + // Controlla dimensione matrice + if (*nr < 0 || *nc < 0 || *nr > MAX_N || *nc > MAX_N) { + printf("Dimensione della matrice non valida\n"); + fclose(fp_read); + return 1; + } + + // Leggi matrice + for (int i = 0; i < *nr; i++) { + for (int j = 0; j < *nc; j++) { + fscanf(fp_read, "%d ", &M[i][j]); + } + } + + fclose(fp_read); + return 0; +} + +int main() +{ + int M[MAX_N][MAX_N], N, P, dir, lmax, dim, nr, nc; + + if (leggiMatrice(M, &nr, &nc)) { + return 1; + } + + lmax = min(nr, nc); + + // Chiedi all'utente le dimensioni dei sottoquadrati + while (1) { + printf("Inserisci una dimensione compresa tra 1 e %d: ", lmax); + scanf("%d", &dim); + + // Se dim non è un valore valido, termina + if (dim < 1 || dim > lmax) { + break; + } + + sottomatrici(M, nr, nc, dim); + } + + return 0; +} diff --git a/Laboratorio 4/ruota.c b/Laboratorio 4/ruota.c new file mode 100644 index 0000000..dcb8f66 --- /dev/null +++ b/Laboratorio 4/ruota.c @@ -0,0 +1,58 @@ +// Laboratorio 4 - Esercizio 2 +// Matteo Schiff - s295565 + +#include +#include +#define MAX_N 30 + +void ruota(int v[MAX_N], int N, int P, int dir); + +void ruota(int v[MAX_N], int N, int P, int dir) { + int t; + + P = P % N; + + for (int x = 0; x < P; x++) { + if (dir == 1) { + t = v[0]; + for (int i = 0; i < N - 1; i++) { + v[i] = v[i+1]; + } + v[N-1] = t; + } else { + t = v[N-1]; + for (int i = N-1; i > 0; i--) { + v[i] = v[i-1]; + } + v[0] = t; + } + } + + for (int x = 0; x < N; x++) { + printf("%d ", v[x]); + } + putchar('\n'); +} + +int main() +{ + int V[MAX_N], N, P, dir; + + do { + printf("Inserisci N compreso tra 0 e %d: ", MAX_N); + } while(scanf("%d", &N) != 1 || N > MAX_N || N < 0); + + printf("Inserisci il vettore di lunghezza %d: ", N); + for(int i = 0; i < N; i++) { + scanf(" %d", &V[i]); + } + + do { + printf("Inserisci il numero di rotazioni e la direzione (formato ' '): "); + scanf(" %d %d", &P, &dir); + ruota(V, N, P, dir); + } while (P != 0); + + + return 0; +} diff --git a/Laboratorio 4/sottovettori.c b/Laboratorio 4/sottovettori.c new file mode 100644 index 0000000..b026dd0 --- /dev/null +++ b/Laboratorio 4/sottovettori.c @@ -0,0 +1,66 @@ +// Laboratorio 4 - Esercizio 1 +// Matteo Schiff - s295565 + +#include +#include +#define INPUT_FILE "./vettore.txt" +#define MAX_LEN 30 + +void sottoSequenze(int V[MAX_LEN], int N); + +void sottoSequenze(int V[MAX_LEN], int N) { + int start = 0; // memorizza l'inizio del sottovettore corrente + int maxLen = 0; // memorizza la grandezza del massimo sottovettore decrementata di uno + + int positions[30]; // memorizza gli indici dei sottovettori con lunghezza massima trovati + int lastPosition = 0; // memorizza la posizione dell'ultimo elemento salvato in `positions` + + for (int i = 0; i < N; i++) { + if (V[i] == 0) { + start = i+1; + } + + if ((i - start) == maxLen) { + positions[lastPosition] = start; + lastPosition++; + } else if ((i - start) > maxLen) { + positions[0] = start; + lastPosition = 1; + maxLen = i - start; + } + } + + printf("I sottovettori sono di dimensione massima (%d):\n", maxLen + 1); + + for (int j = 0; j < lastPosition; j++) { // Per ogni sottovettore ... + for (int x = 0; x < maxLen + 1; x++) { // ... stampa il sottovettore + printf("%d ", V[positions[j]+x]); + } + putchar('\n'); + } +} + +int main() +{ + int V[MAX_LEN], N; + FILE *fp_read; + + fp_read = fopen(INPUT_FILE, "r"); + + if (fp_read == NULL) + { + printf("Errore nell'apertura del file\n"); + return 1; + } + + for (N = 0; N < MAX_LEN && !feof(fp_read); N++) { + if (fscanf(fp_read, "%d ", &V[N]) != 1) + break; + } + + sottoSequenze(V, N); + + fclose(fp_read); + + return 0; +} diff --git a/Laboratorio 4/vettore.txt b/Laboratorio 4/vettore.txt new file mode 100644 index 0000000..0d93aad --- /dev/null +++ b/Laboratorio 4/vettore.txt @@ -0,0 +1 @@ +1 3 4 0 1 0 9 4 2 0 diff --git a/Laboratorio 5/dizionario.txt b/Laboratorio 5/dizionario.txt new file mode 100644 index 0000000..b27d49d --- /dev/null +++ b/Laboratorio 5/dizionario.txt @@ -0,0 +1,10 @@ +9 +$11$ pelle +$2$ pollo +$333$ palla +$41$ alla +$5078$ tta +$6$ tti +$7$ ll +$81$ er +$900$ ere \ No newline at end of file diff --git a/Laboratorio 5/ricodifica.c b/Laboratorio 5/ricodifica.c new file mode 100644 index 0000000..7aa8111 --- /dev/null +++ b/Laboratorio 5/ricodifica.c @@ -0,0 +1,95 @@ +// Laboratorio 5 - Esercizio 2 +// Matteo Schiff - s295565 + +#include +#include +#include +#define MAX_SIZE 30 +#define MAX_LEN 200 + +typedef struct Coppia +{ + int codice; + int len; + char sequenza[MAX_LEN]; +} coppia; + +int caricaDizionario(coppia coppie[MAX_SIZE], int * c); + +int caricaDizionario(coppia coppie[MAX_SIZE], int * c) { + FILE *fp_read; + + if ((fp_read = fopen("./dizionario.txt", "r")) == NULL) + { + printf("Errore nell'apertura del file\n"); + return 1; + } + + fscanf(fp_read, "%d\n", c); + + if (*c >= MAX_SIZE) { + printf("Troppi elementi nel dizionario"); + fclose(fp_read); + return 1; + } + + for (int i = 0; i < *c; i++) { + fscanf(fp_read, "$%d$ %200s\n", &coppie[i].codice, coppie[i].sequenza); + coppie[i].len = strlen(coppie[i].sequenza); + } + + fclose(fp_read); + + return 0; +} + + +int main() +{ + + FILE *fp_read, *fp_write; + char riga[MAX_LEN], buf[MAX_LEN]; + bool richiedeCiclo; + int c, w; + coppia coppie[MAX_SIZE]; + + if (caricaDizionario(coppie, &c)) { + return 1; + } + + if ((fp_read = fopen("./sorgente.txt", "r")) == NULL) + { + printf("Errore nell'apertura del file\n"); + return 1; + } + if ((fp_write = fopen("./ricodificato.txt", "w")) == NULL) + { + printf("Errore nell'apertura del file\n"); + return 2; + } + + while (!feof(fp_read)) { + fgets(riga, 200, fp_read); + + for (int i = 0; i < c; i++) { + richiedeCiclo = true; + while (richiedeCiclo) { + richiedeCiclo = false; + char * ptr = strstr(riga, coppie[i].sequenza); + if (ptr != NULL) { + strcpy(buf, ptr); + w = sprintf(ptr, "$%d$", coppie[i].codice); + strcpy(ptr + w, buf + coppie[i].len); + richiedeCiclo = true; + } + } + } + + fprintf(fp_write, "%s", riga); + } + + fclose(fp_read); + fclose(fp_write); + + return 0; +} \ No newline at end of file diff --git a/Laboratorio 5/ricodificato.txt b/Laboratorio 5/ricodificato.txt new file mode 100644 index 0000000..7179cc0 --- /dev/null +++ b/Laboratorio 5/ricodificato.txt @@ -0,0 +1,5 @@ +a$11$ figlio di a$2$ +fece una $333$ di $11$ di $2$ +tu$6$ i pesci venn$81$o a g$41$ +p$81$ ved$81$e la $333$ di $11$ di $2$ +fa$5078$ da a$11$ figlio di a$2$ \ No newline at end of file diff --git a/Laboratorio 5/sorgente.txt b/Laboratorio 5/sorgente.txt new file mode 100644 index 0000000..26fe61f --- /dev/null +++ b/Laboratorio 5/sorgente.txt @@ -0,0 +1,5 @@ +apelle figlio di apollo +fece una palla di pelle di pollo +tutti i pesci vennero a galla +per vedere la palla di pelle di pollo +fatta da apelle figlio di apollo \ No newline at end of file diff --git a/Laboratorio 6/gtt.c b/Laboratorio 6/gtt.c new file mode 100644 index 0000000..d3ed83f --- /dev/null +++ b/Laboratorio 6/gtt.c @@ -0,0 +1,249 @@ +// Laboratorio 6 - Esercizio 1 +// Matteo Schiff - s295565 + +#include +#include +#include +#include +#include +#define MAX_LEN 30 +#define MAX_ROWS 1000 + +const int MAXL = 50; + +typedef struct Time { + unsigned int hours; + unsigned int minutes; + unsigned int seconds; +} time; + +typedef struct Date { + unsigned int year; + unsigned int month; + unsigned int day; +} date; + +typedef struct Corsa { + char codice_tratta[MAX_LEN]; + char partenza[MAX_LEN]; + char destinazione[MAX_LEN]; + date data; + time ora_partenza; + time ora_arrivo; + unsigned int ritardo; +} corsa; + +typedef enum { + r_date, r_partenza, r_capolinea, r_ritardo, r_ritardo_tot, r_fine +} t_comandi; + +char* toLower(char* s); +t_comandi leggiComando(); +int confrontaDate(date a, date b); +void stampaCorsa(corsa corsa); +void data(char * argomenti, corsa corse[MAX_ROWS], int N); +void partenza(char * argomenti, corsa corse[MAX_ROWS], int N); +void capolinea(char * argomenti, corsa corse[MAX_ROWS], int N); +void ritardo(char * argomenti, corsa corse[MAX_ROWS], int N); +void ritardoTot(char * argomenti, corsa corse[MAX_ROWS], int N); +void menuParola (corsa corse[MAX_ROWS], int N); +int loadFile(corsa corse[MAX_ROWS], int * N, char * filename); + +// Trasforma in lowercase tutti i caratteri di una stringa +char* toLower(char* s) { + for(char *p=s; *p; p++) *p=tolower(*p); + return s; +} + +t_comandi leggiComando() { + t_comandi c; + char cmd[MAXL]; + char tabella[7][12] = { + "date", "partenza", "capolinea", "ritardo", "ritardo_tot", "fine" + }; + printf("comando (date/partenza/capolinea"); + printf("/ritardo/ritardo_tot/fine): "); + scanf("%s",cmd); toLower(cmd); + c=r_date; + while(c<7 && strcmp(cmd,tabella[c])!=0) + c++; + return (c); +} + +// restituisce true se la prima data è successiva o coincidente alla seconda +int confrontaDate(date a, date b) { + if (a.year > b.year) + return true; + else if (a.year < b.year) + return false; + + if (a.month > b.month) + return true; + else if (a.month < b.month) + return false; + + if (a.day >= b.day) + return true; + + return false; +} + +void stampaCorsa(corsa corsa) { + printf(" - %s %s %s, data: %4u/%02u/%02u, ora di partenza: %02u:%02u:%02u, ora di arrivo: %02u:%02u:%02u, ritardo %d minuto\n", + corsa.codice_tratta, corsa.partenza, corsa.destinazione, + corsa.data.year, corsa.data.month, corsa.data.day, + corsa.ora_partenza.hours, corsa.ora_partenza.minutes, corsa.ora_partenza.seconds, + corsa.ora_arrivo.hours, corsa.ora_arrivo.minutes, corsa.ora_arrivo.seconds, + corsa.ritardo); +} + +void data(char * argomenti, corsa corse[MAX_ROWS], int N) { + date data_inizio, data_fine; + if (sscanf(argomenti, " %4u/%2u/%2u %4u/%2u/%2u", &data_inizio.year, &data_inizio.month, &data_inizio.day, &data_fine.year, &data_fine.month, &data_fine.day) != 6) { + puts("Date non valide. La sintassi è 'date '"); + return; + } + + puts("Elenco corse con partenza compresa nell'intervallo specificato:\n"); + + for (int i = 0; i < N; i++) { + if (confrontaDate(corse[i].data, data_inizio) && confrontaDate(data_fine, corse[i].data)) { + stampaCorsa(corse[i]); + } + } +} + +void partenza(char * argomenti, corsa corse[MAX_ROWS], int N) { + char partenza[30]; + if (sscanf(argomenti, " %30s", partenza) != 1) { + puts("Partenza non valida. La sintassi è 'partenza '"); + return; + } + + printf("Elenco corse con partenza da %s:\n", partenza); + + for (int i = 0; i < N; i++) { + if (strcmp(partenza, corse[i].partenza) == 0) { + stampaCorsa(corse[i]); + } + } +} + +void capolinea(char * argomenti, corsa corse[MAX_ROWS], int N) { + char capolinea[30]; + if (sscanf(argomenti, " %30s", capolinea) != 1) { + puts("Capolinea non valido. La sintassi è 'capolinea '"); + return; + } + + printf("Elenco corse con capolinea da %s:\n", capolinea); + + for (int i = 0; i < N; i++) { + if (strcmp(capolinea, corse[i].destinazione) == 0) { + stampaCorsa(corse[i]); + } + } +} + +void ritardo(char * argomenti, corsa corse[MAX_ROWS], int N) { + date data_inizio, data_fine; + if (sscanf(argomenti, " %4u/%2u/%2u %4u/%2u/%2u", &data_inizio.year, &data_inizio.month, &data_inizio.day, &data_fine.year, &data_fine.month, &data_fine.day) != 6) { + puts("Date non valide. La sintassi è 'date '"); + return; + } + + puts("Eleco corse con ritardo nell'intervallo di date specificato:\n"); + + for (int i = 0; i < N; i++) { + if (confrontaDate(corse[i].data, data_inizio) && confrontaDate(data_fine, corse[i].data) && corse[i].ritardo > 0) { + stampaCorsa(corse[i]); + } + } +} + +void ritardoTot(char * argomenti, corsa corse[MAX_ROWS], int N) { + char codice_tratta[30]; + if (sscanf(argomenti, " %30s", codice_tratta) != 1) { + puts("Codice di tratta non valido. La sintassi è 'ritardo_tot '"); + return; + } + + unsigned int sum = 0; + + for (int i = 0; i < N; i++) { + if (strcmp(codice_tratta, corse[i].codice_tratta) == 0) { + sum += corse[i].ritardo; + } + } + + printf("Il ritardo complessivo accumulato sulla tratta %s è pari a %u minuti.\n", codice_tratta, sum); +} + +void menuParola (corsa corse[MAX_ROWS], int N) { + t_comandi comando; + char argomenti[MAXL]; + int i, continua=1; + while (continua) { + comando = leggiComando(); + fgets(argomenti,MAXL,stdin); /* resto della riga */ + switch (comando) { + case r_date: data(argomenti, corse, N); break; + case r_partenza: partenza(argomenti, corse, N); break; + case r_capolinea: capolinea(argomenti, corse, N); break; + case r_ritardo: ritardo(argomenti, corse, N); break; + case r_ritardo_tot: ritardoTot(argomenti, corse, N); break; + case r_fine: continua = 0; break; + default: + puts("Comando non valido\n"); + } + } +} + +int loadFile(corsa corse[MAX_ROWS], int * N, char * filename) { + FILE *fp_read; + unsigned int lines, i; + + if ((fp_read = fopen(filename, "r")) == NULL) + { + puts("Impossibile aprire il file"); + return 1; + } + + fscanf(fp_read, "%u\n", &lines); + + if (lines > MAX_ROWS) { + fclose(fp_read); + return 1; + } + + for (i = 0; i < lines; i++) { + int num_read = fscanf(fp_read, "%30s %30s %30s %4u/%2u/%2u %2u:%2u:%2u %2u:%2u:%2u %u\n", corse[i].codice_tratta, corse[i].partenza, corse[i].destinazione, &corse[i].data.year, &corse[i].data.month, &corse[i].data.day, &corse[i].ora_partenza.hours, &corse[i].ora_partenza.minutes, &corse[i].ora_partenza.seconds, &corse[i].ora_arrivo.hours, &corse[i].ora_arrivo.minutes, &corse[i].ora_arrivo.seconds, &corse[i].ritardo); + if (num_read != 13) { + // la stringa è mal formattata + printf("File non formattato correttamente. Errore a linea %d\n", i+1); + fclose(fp_read); + return 1; + } + } + + fclose(fp_read); + + *N = i; + return 0; +} + +int main() { + int N; + corsa corse[MAX_ROWS]; + + if (loadFile(corse, &N, "./log.txt")) { + return 1; + } + + if (corse == NULL) { + return 1; + } + menuParola(corse, N); + + return 0; +} diff --git a/Laboratorio 6/log.txt b/Laboratorio 6/log.txt new file mode 100644 index 0000000..52689da --- /dev/null +++ b/Laboratorio 6/log.txt @@ -0,0 +1,7 @@ +6 +GTT001 Braccini Porta_Nuova 2018/10/10 18:50:00 19:07:25 1 +GTT001 Braccini Porta_Nuova 2018/12/10 19:50:00 20:06:00 1 +GTT002 Politecnico XVIII_Dicembre 2018/10/10 10:01:23 10:12:08 4 +GTT003 Einaudi Cso_Trapani 2018/09/10 14:11:23 14:38:23 2 +GTT004 Marmolada Sebastopoli 2018/11/10 00:01:02 00:12:00 3 +GTT002 Politecnico Piazza_Statuto 2018/11/10 23:11:59 23:20:07 0 diff --git a/Laboratorio 7/esercizio1.c b/Laboratorio 7/esercizio1.c new file mode 100644 index 0000000..1b19bdb --- /dev/null +++ b/Laboratorio 7/esercizio1.c @@ -0,0 +1,127 @@ +// Laboratorio 7 - Esercizio 1 +// Matteo Schiff - s295565 + +#include +#include +#define NC 50 +#define NR 50 +#define FILENAME "mappa.txt" + +struct regione +{ + int x, y, b, h; +}; + +void leggiMatrice(int mat[][NC], int maxR, int *nrp, int *ncp); +int riconosciRegione(int mat[][NC],int nr,int nc,int r,int c,int *b,int *h); +void analizzaRegioni(int mat[][NC],int nr,int nc); + +void leggiMatrice(int mat[][NC], int maxR, int *nrp, int *ncp) { + FILE *fp_read; + + fp_read = fopen(FILENAME, "r"); + + if (fp_read == NULL) + { + printf("Errore nell'apertura del file\n"); + return; + } + + fscanf(fp_read, "%d %d\n", nrp, ncp); + + // Controlla dimensione matrice + if (*nrp < 0 || *ncp < 0 || *nrp > maxR || *ncp > NC) { + printf("Dimensione della matrice non valida\n"); + fclose(fp_read); + return; + } + + // Leggi matrice + for (int i = 0; i < *nrp; i++) { + for (int j = 0; j < *ncp; j++) { + fscanf(fp_read, "%d ", &mat[i][j]); + } + } + + fclose(fp_read); +} + +int riconosciRegione(int mat[][NC],int nr,int nc,int r,int c,int *b,int *h) { + int x, y; + + // controlla se è un vertice + if (r > 0 && mat[r-1][c] == 1 || c > 0 && mat[r][c-1] == 1) { + return 0; + } + + // misura estensione orizzontale + for (x = r; x < nr; x++) { + if (mat[x][c] != 1) { + break; + } + } + + // misura estensione verticale + for (y = c; y < nc; y++) { + if (mat[r][y] != 1) { + break; + } + } + + // calcola base ed altezza + *h = x-r; + *b = y-c; + + return 1; +} + +void analizzaRegioni(int mat[][NC],int nr,int nc) { + int b, h; + + struct regione H, B, A; + + int maxH = 0, maxB = 0, maxA = 0; + + for (int r = 0; r < nr; r++){ + for (int c = 0; c < nc; c++){ + if (riconosciRegione(mat,nr,nc,r,c,&b,&h)) { + if (h > maxH) { + maxH = h; + H.x = r; + H.y = c; + H.b = b; + H.h = h; + } + + if (b > maxB) { + maxB = b; + B.x = r; + B.y = c; + B.b = b; + B.h = h; + } + + if (h*b > maxA) { + maxA = h*b; + A.x = r; + A.y = c; + A.b = b; + A.h = h; + } + } + } + } + + printf("Max altezza: estremo=(%d,%d), altezza=%d, larghezza=%d, area=%d\n", H.x, H.y, H.h, H.b, H.h * H.b); + printf("Max larghezza: estremo=(%d,%d), altezza=%d, larghezza=%d, area=%d\n", B.x, B.y, B.h, B.b, B.h * B.b); + printf("Max area: estremo=(%d,%d), altezza=%d, larghezza=%d, area=%d\n", A.x, A.y, A.h, A.b, A.h * A.b); +} + +int main() { + int M[NR][NC]; + int nr, nc; + + leggiMatrice(M,NR,&nr,&nc); + analizzaRegioni(M, nr, nc); + return 0; +} \ No newline at end of file diff --git a/Laboratorio 7/esercizio2.c b/Laboratorio 7/esercizio2.c new file mode 100644 index 0000000..8d657e5 --- /dev/null +++ b/Laboratorio 7/esercizio2.c @@ -0,0 +1,191 @@ +// Laboratorio 7 - Esercizio 2 +// Matteo Schiff - s295565 + +#include +#include +#define MAX_LEN 100 +#define FILENAME "sort.txt" + +void leggiSequenza(int vec[MAX_LEN], int *len, FILE *fp_read); +void copy(int source[MAX_LEN], int destination[MAX_LEN], int N); +void insertionSort(int vec[MAX_LEN], int N); +void selectionSort(int vec[MAX_LEN], int N); +void shellSort(int vec[MAX_LEN], int N); + +void leggiSequenza(int vec[MAX_LEN], int *len, FILE *fp_read) +{ + fscanf(fp_read, "%d ", len); + + // Leggi vettore + for (int i = 0; i < *len; i++) + { + fscanf(fp_read, "%d ", &vec[i]); + } +} + +void copy(int source[MAX_LEN], int destination[MAX_LEN], int N) +{ + for (int i = 0; i < N; i++) + { + destination[i] = source[i]; + } +} + +void insertionSort(int vec[MAX_LEN], int N) +{ + // variabili per statistiche + int scambi = 0, iterInt = 0, iterExt = 0, iterTot = 0; + // variabili algoritmo + int i, j, l = 0, r = N - 1, x; + + printf(" -- Insertion sort\n"); + + for (i = l + 1; i <= r; i++) + { + iterInt = 0; + x = vec[i]; + j = i - 1; + while (j >= 0 && x < vec[j]) + { + vec[j + 1] = vec[j]; + j--; + + // incremento statistiche + scambi++; + iterInt++; + } + vec[j + 1] = x; + + // aggiorno statistiche + scambi++; + iterTot += iterInt; + iterExt++; + printf(" Iterazioni al ciclo %d: %d\n", i, iterInt); + } + iterTot += iterExt; + printf(" Iterazioni esterne: %d\n", iterExt); + printf(" Iterazioni totali: %d\n", iterTot); + printf(" Scambi: %d\n", scambi); +} + +void selectionSort(int vec[MAX_LEN], int N) +{ + // variabili per statistiche + int scambi = 0, iterInt = 0, iterExt = 0, iterTot = 0; + // variabili algoritmo + int i, j, l = 0, r = N - 1, min, temp; + + printf(" -- Selection sort\n"); + + for (i = l; i <= r; i++) + { + iterInt = 0; + + min = i; + for (j = i + 1; j <= r; j++) + { + if (vec[j] < vec[min]) + min = j; + + iterInt++; + } + + if (min != i) + { + temp = vec[i]; + vec[i] = vec[min]; + vec[min] = temp; + + scambi++; + } + + // aggiorno statistiche + iterTot += iterInt; + iterExt++; + printf(" Iterazioni al ciclo %d: %d\n", i, iterInt); + } + iterTot += iterExt; + printf(" Iterazioni esterne: %d\n", iterExt); + printf(" Iterazioni totali: %d\n", iterTot); + printf(" Scambi: %d\n", scambi); +} + +void shellSort(int vec[MAX_LEN], int N) +{ + // variabili per statistiche + int scambi = 0, iterInt = 0, iterExt = 0, iterTot = 0; + // variabili algoritmo + int i, j, x, l = 0, r = N - 1, h = 1; + + printf(" -- Shell sort\n"); + + while (h < N / 3) + h = 3 * h + 1; + while (h >= 1) + { + for (i = l + h; i <= r; i++) + { + iterInt = 0; + j = i; + x = vec[i]; + while (j >= l + h && x < vec[j - h]) + { + vec[j] = vec[j - h]; + + j -= h; + scambi++; + iterInt++; + } + vec[j] = x; + scambi++; + + // aggiorno statistiche + iterTot += iterInt; + iterExt++; + printf(" Iterazioni al ciclo %d: %d\n", i, iterInt); + } + h = h / 3; + } + + iterTot += iterExt; + printf(" Iterazioni esterne: %d\n", iterExt); + printf(" Iterazioni totali: %d\n", iterTot); + printf(" Scambi: %d\n", scambi); +} + +int main() +{ + FILE *fp_read; + int vec[MAX_LEN], toOrder[MAX_LEN]; + int n, len; + + if ((fp_read = fopen(FILENAME, "r")) == NULL) + { + puts("Impossibile aprire il file\n"); + return 1; + } + + fscanf(fp_read, "%d ", &n); + + for (int i = 0; i < n; i++) + { + leggiSequenza(vec, &len, fp_read); + + printf(" - Sequenza %d\n", i); + + copy(vec, toOrder, len); + insertionSort(toOrder, len); + + copy(vec, toOrder, len); + selectionSort(toOrder, len); + + copy(vec, toOrder, len); + shellSort(toOrder, len); + + putc('\n', stdout); + } + + fclose(fp_read); + + return 0; +} \ No newline at end of file diff --git a/Laboratorio 7/mappa.txt b/Laboratorio 7/mappa.txt new file mode 100644 index 0000000..d95df1f --- /dev/null +++ b/Laboratorio 7/mappa.txt @@ -0,0 +1,6 @@ +5 6 +1 1 0 0 0 0 +0 0 1 1 0 0 +0 0 1 1 0 1 +0 0 0 0 0 1 +1 0 1 0 0 1 \ No newline at end of file diff --git a/Laboratorio 7/sort.txt b/Laboratorio 7/sort.txt new file mode 100644 index 0000000..980ce77 --- /dev/null +++ b/Laboratorio 7/sort.txt @@ -0,0 +1,5 @@ +4 +5 1 2 3 4 5 +5 1 2 3 4 0 +5 5 4 3 2 1 +5 1 5 2 4 3 \ No newline at end of file diff --git a/Laboratorio 8/esercizio1.c b/Laboratorio 8/esercizio1.c new file mode 100644 index 0000000..a6985ef --- /dev/null +++ b/Laboratorio 8/esercizio1.c @@ -0,0 +1,121 @@ +// Laboratorio 8 - Esercizio 1 +// Matteo Schiff - s295565 + +#include +#include +#define NC 50 +#define NR 50 +#define FILENAME "mappa.txt" + +struct regione +{ + int x, y, b, h; +}; + +void leggiMatrice(int mat[][NC], int maxR, int *nrp, int *ncp); +int riconosciRegione(int mat[][NC],int nr,int nc,int r,int c,int *b,int *h); + +void leggiMatrice(int mat[][NC], int maxR, int *nrp, int *ncp) { + FILE *fp_read; + + fp_read = fopen(FILENAME, "r"); + + if (fp_read == NULL) + { + printf("Errore nell'apertura del file\n"); + return; + } + + fscanf(fp_read, "%d %d\n", nrp, ncp); + + // Controlla dimensione matrice + if (*nrp < 0 || *ncp < 0 || *nrp > maxR || *ncp > NC) { + printf("Dimensione della matrice non valida\n"); + fclose(fp_read); + return; + } + + // Leggi matrice + for (int i = 0; i < *nrp; i++) { + for (int j = 0; j < *ncp; j++) { + fscanf(fp_read, "%d ", &mat[i][j]); + } + } + + fclose(fp_read); +} + +int riconosciRegione(int mat[][NC],int nr,int nc,int r,int c,int *b,int *h) { + int x, y; + + // controlla se è un vertice + if (r > 0 && mat[r-1][c] == 1 || c > 0 && mat[r][c-1] == 1) { + return 0; + } + + // misura estensione orizzontale + for (x = r; x < nr; x++) { + if (mat[x][c] != 1) { + break; + } + } + + // misura estensione verticale + for (y = c; y < nc; y++) { + if (mat[r][y] != 1) { + break; + } + } + + // calcola base ed altezza + *h = x-r; + *b = y-c; + + return 1; +} + +int main() { + int M[NR][NC]; + int nr, nc, b, h; + + struct regione H, B, A; + + int maxH = 0, maxB = 0, maxA = 0; + + leggiMatrice(M,NR,&nr,&nc); + + for (int r = 0; r < nr; r++){ + for (int c = 0; c < nc; c++){ + if (riconosciRegione(M,nr,nc,r,c,&b,&h)) { + if (h > maxH) { + maxH = h; + H.x = r; + H.y = c; + H.b = b; + H.h = h; + } + + if (b > maxB) { + maxB = b; + B.x = r; + B.y = c; + B.b = b; + B.h = h; + } + + if (h*b > maxA) { + maxA = h*b; + A.x = r; + A.y = c; + A.b = b; + A.h = h; + } + } + } + } + + printf("Max altezza: estremo=(%d,%d), altezza=%d, larghezza=%d, area=%d\n", H.x, H.y, H.h, H.b, H.h * H.b); + printf("Max larghezza: estremo=(%d,%d), altezza=%d, larghezza=%d, area=%d\n", B.x, B.y, B.h, B.b, B.h * B.b); + printf("Max area: estremo=(%d,%d), altezza=%d, larghezza=%d, area=%d\n", A.x, A.y, A.h, A.b, A.h * A.b); + return 0; +} \ No newline at end of file diff --git a/Laboratorio 8/mappa.txt b/Laboratorio 8/mappa.txt new file mode 100644 index 0000000..d95df1f --- /dev/null +++ b/Laboratorio 8/mappa.txt @@ -0,0 +1,6 @@ +5 6 +1 1 0 0 0 0 +0 0 1 1 0 0 +0 0 1 1 0 1 +0 0 0 0 0 1 +1 0 1 0 0 1 \ No newline at end of file diff --git a/TemiEsame/campionato.c b/TemiEsame/campionato.c new file mode 100644 index 0000000..bd3d4b0 --- /dev/null +++ b/TemiEsame/campionato.c @@ -0,0 +1,43 @@ +#include +#include +#define MAXN 10 +#define MAXM 10 + +int indMax(int ranking[MAXN], int n) { + int ind = 0, max = ranking[0]; + + for (int i = 1; i < n; i++) { + if (max < ranking[i]) { + ind = i; + max = ranking[i]; + } + } + + return ind; +} + +void displRanking(int C[MAXN][MAXM], int n, int m) { + int ranking[MAXM]; + + puts("la squadra capolista in ognuna delle nelle 3 giornate è:"); + + for (int i = 0; i < n; i++) { + ranking[i] = 0; + } + + for (int j = 0; j < m; j++) { + for (int i = 0; i < n; i++) { + ranking[i] += C[i][j]; + } + + printf(" %d", indMax(ranking, n)); + } + putc('\n', stdout); +} + +int main(){ + int arr[MAXN][MAXM] = {{3,1,0},{0,1,1},{1,1,1},{1,1,3}}; + displRanking(arr, 4, 3); + return 0; +} + diff --git a/TemiEsame/carte.txt b/TemiEsame/carte.txt new file mode 100644 index 0000000..01ad86f --- /dev/null +++ b/TemiEsame/carte.txt @@ -0,0 +1,2 @@ +1 2 4 4 +3 3 5 8 diff --git a/TemiEsame/charErase.c b/TemiEsame/charErase.c new file mode 100644 index 0000000..78b3726 --- /dev/null +++ b/TemiEsame/charErase.c @@ -0,0 +1,38 @@ +#include +#include + +int charErase (char str[], int pos[]) { + int l = strlen(str); + int * atp = pos; + int steps = 0; + + // segno con -1 i caratteri da eliminare + while (*atp != -1) { + if (*atp < l) + str[*atp] = -1; // nessun carattere ascii ha codice -1, è una flag valida + + atp++; + } + + for (int i = 0; i < l; i++) { + while (str[i + steps] == -1) { + steps++; + + if (i + steps >= l) + break; + } + + str[i] = str[i + steps]; + } + + str[l-steps] = 0; + return steps; +} + +int main(){ + char sus[] = "ThisIsAString"; + int arr[] = {7, 4, 2, 0, 11, -1}; + printf("%d",charErase(sus, arr)); + puts(sus); + return 0; +} diff --git a/TemiEsame/conta.c b/TemiEsame/conta.c new file mode 100644 index 0000000..6288b15 --- /dev/null +++ b/TemiEsame/conta.c @@ -0,0 +1,26 @@ +#include +#include + +int conta(char *parole[], int nparole, char *cerca) { + int s = 0; + int l = strlen(cerca); + for (int i = 0; i < nparole; i++) { + char * prt = parole[i]; + do { + prt = strstr(prt, cerca); + + if (prt != NULL) { + prt += l; + s++; + } + + } while (prt != NULL); + } + + return s; +} + +int main(int argc, char ** argv) { + printf("%d", conta(argv, argc-1, argv[argc-1])); + return 0; +} \ No newline at end of file diff --git a/TemiEsame/countAndPrint.c b/TemiEsame/countAndPrint.c new file mode 100644 index 0000000..f649018 --- /dev/null +++ b/TemiEsame/countAndPrint.c @@ -0,0 +1,40 @@ +#include +#include +#include + + +int isVocal(char c) { + char * vowels = "aeiou"; + + c = tolower(c); + return strchr(vowels, c) != NULL; +} + +void countAndPrint(char str[], int n) { + int l = strlen(str), v, s = 0; + + for (int i = 0; i < l - n; i++) { + v = 0; + for (int j = i; j < i + n; j++) { + if (isVocal(str[j])) { + v++; + } + } + + if (v == 2) { + s++; + for (int j = i; j < i + n; j++) + putchar(str[j]); + + putchar('\n'); + } + + } + + printf("%d", s); +} + +int main() { + countAndPrint("forExample",4); + return 0; +} diff --git a/TemiEsame/matriceRowMajor.c b/TemiEsame/matriceRowMajor.c new file mode 100644 index 0000000..4ad62f3 --- /dev/null +++ b/TemiEsame/matriceRowMajor.c @@ -0,0 +1,46 @@ +#include +#include +#define MAXR 10 +#define MAXC 10 + +int buildMatrix(int V[], int N, int M[MAXR][MAXC], int nr, int nc) { + int p = 0, x, y; + + if (N % 2 == 1) { + return 1; + } + + for (int i = 0; i < N; i += 2) { + for (int j = 0; j < (V[i+1]); j++) { + x = p / nc; + y = p % nc; + + if (x >= nr) { + return 1; + } + + if (y == 0) { + putchar('\n'); + } + + M[x][y] = V[i]; + printf("%d\t", V[i]); + p++; + } + } + putchar('\n'); + + p--; + if (p / nc != (nr - 1) || p % nc != (nc - 1)) { + return 1; + } + + return 0; +} + +int main(){ + int arr[MAXR][MAXC]; + int vec[] = {1, 2, 17, 2, 3, 1, 8, 4, 6, 1, 7, 3, 5, 2}; + printf("%d", buildMatrix(vec, 14, arr, 3, 5)); + return 0; +} \ No newline at end of file diff --git a/TemiEsame/maxOdd.c b/TemiEsame/maxOdd.c new file mode 100644 index 0000000..b345800 --- /dev/null +++ b/TemiEsame/maxOdd.c @@ -0,0 +1,36 @@ +#include +#include + +void maxOdd(int v[], int N) { + int max = 0, li = 0; + + for (int i = 0; i <= N; i++) { + if (i == N || v[i] % 2 == 0) { + if (i - li > max) { + max = i - li; + } + li = i+1; + } + } + + li = 0; + + for (int i = 0; i <= N; i++) { + if (i == N || v[i] % 2 == 0) { + if (i - li == max) { + for (int j = li; j < i; j++){ + printf("%i", v[j]); + } + puts(""); + } + li = i+1; + } + } +} + +int main(){ + int arr[] = {1, 3, 7, 1, 0, 1, 9, 3, 1, 0}; + maxOdd(arr, 10); + return 0; +} + diff --git a/TemiEsame/overslapping.c b/TemiEsame/overslapping.c new file mode 100644 index 0000000..366889c --- /dev/null +++ b/TemiEsame/overslapping.c @@ -0,0 +1,46 @@ +#include +#include +#define MAXN 10 + +int areaTot(FILE *fp) { + unsigned int carte[MAXN][MAXN]; + unsigned int lx, ly, rx, ry, somma = 0; + + for (int i = 0; i < MAXN; i++) { + for (int j = 0; j < MAXN; j++) { + carte[i][j] = 0; + } + } + + while (!feof(fp)) { + if (fscanf(fp, "%u %u %u %u ", &lx, &ly, &rx, &ry) != 4) { + return -1; + } + + if (lx >= MAXN || ly >= MAXN || rx >= MAXN || ry >= MAXN) { + return -1; + } + + for (int i = lx; i < rx; i++) { + for (int j = ly; j < ry; j++) { + carte[i][j] = 1; + } + } + } + + for (int i = 0; i < MAXN; i++) { + for (int j = 0; j < MAXN; j++) { + somma += carte[i][j]; + } + } + + return somma; +} + +int main() { + FILE * fin; + fin = fopen("./carte.txt", "r"); + printf("%d", areaTot(fin)); + fclose(fin); + return 0; +} diff --git a/TemiEsame/prodCartOrd.c b/TemiEsame/prodCartOrd.c new file mode 100644 index 0000000..83d34a7 --- /dev/null +++ b/TemiEsame/prodCartOrd.c @@ -0,0 +1,26 @@ +#include +#include + +void insertionSort(int *v1, int d1) { + int t = 0; + + for (int i = 1; i < d1; i++) { + t = v1[i]; + + int j = i -1; + + while (j >= 0 && v1[j]>t) { + v1[j+1] = v1[j]; + j--; + } + + v1[j+1] = t; + } +} + +void prodCartOrd(int *v1, int d1, int *v2, int d2) { + insertionSort(v1, d1); + insertionSort(v2, d2); + + +} \ No newline at end of file diff --git a/TemiEsame/sommaCornici.c b/TemiEsame/sommaCornici.c new file mode 100644 index 0000000..d210839 --- /dev/null +++ b/TemiEsame/sommaCornici.c @@ -0,0 +1,19 @@ +#include +#include +#define MAXN 10 + +int sommaCornici(int mat[MAXN][MAXN], int N, int vet[]) { + int c = N / 2; + + for (int i = 0; i < c; i++) { + vet[i] = mat[i][i] + mat[N-i-1][i] + mat[N-i-1][N-i-1] + mat[N-i-1][N-i-1]; + + for (int j = i; j < N-i-1; j++) { + vet[i] += mat[i][j] + mat[j][i] + mat[N-i-1][j] + mat[j][N-i-1]; + } + } + + if (N % 2 == 1) { + vet[c+1] = mat[c][c]; + } +} \ No newline at end of file