feat: Initial commit
This commit is contained in:
127
Laboratorio 7/esercizio1.c
Normal file
127
Laboratorio 7/esercizio1.c
Normal file
@@ -0,0 +1,127 @@
|
||||
// Laboratorio 7 - Esercizio 1
|
||||
// Matteo Schiff - s295565
|
||||
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#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;
|
||||
}
|
||||
191
Laboratorio 7/esercizio2.c
Normal file
191
Laboratorio 7/esercizio2.c
Normal file
@@ -0,0 +1,191 @@
|
||||
// Laboratorio 7 - Esercizio 2
|
||||
// Matteo Schiff - s295565
|
||||
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#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;
|
||||
}
|
||||
6
Laboratorio 7/mappa.txt
Normal file
6
Laboratorio 7/mappa.txt
Normal file
@@ -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
|
||||
5
Laboratorio 7/sort.txt
Normal file
5
Laboratorio 7/sort.txt
Normal file
@@ -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
|
||||
Reference in New Issue
Block a user