feat: Initial commit
This commit is contained in:
17
Laboratorio 6/Esercizio 2/easy_test_set.txt
Normal file
17
Laboratorio 6/Esercizio 2/easy_test_set.txt
Normal file
@@ -0,0 +1,17 @@
|
||||
15
|
||||
5 7 4 10
|
||||
6 8 10 1
|
||||
3 1 6 10
|
||||
4 4 2 1
|
||||
7 2 4 10
|
||||
8 8 4 6
|
||||
6 1 4 4
|
||||
8 3 6 4
|
||||
7 7 4 2
|
||||
1 3 7 1
|
||||
10 10 9 10
|
||||
8 8 8 6
|
||||
9 6 10 5
|
||||
6 10 9 5
|
||||
9 6 9 9
|
||||
|
||||
45
Laboratorio 6/Esercizio 2/easy_test_set_result.txt
Normal file
45
Laboratorio 6/Esercizio 2/easy_test_set_result.txt
Normal file
@@ -0,0 +1,45 @@
|
||||
TEST #1
|
||||
zaffiro = 5, rubino = 7, topazio = 4, smeraldo = 10, TOT = 26
|
||||
Collana massima di lunghezza 24
|
||||
TEST #2
|
||||
zaffiro = 6, rubino = 8, topazio = 10, smeraldo = 1, TOT = 25
|
||||
Collana massima di lunghezza 24
|
||||
TEST #3
|
||||
zaffiro = 3, rubino = 1, topazio = 6, smeraldo = 10, TOT = 20
|
||||
Collana massima di lunghezza 16
|
||||
TEST #4
|
||||
zaffiro = 4, rubino = 4, topazio = 2, smeraldo = 1, TOT = 11
|
||||
Collana massima di lunghezza 10
|
||||
TEST #5
|
||||
zaffiro = 7, rubino = 2, topazio = 4, smeraldo = 10, TOT = 23
|
||||
Collana massima di lunghezza 22
|
||||
TEST #6
|
||||
zaffiro = 8, rubino = 8, topazio = 4, smeraldo = 6, TOT = 26
|
||||
Collana massima di lunghezza 23
|
||||
TEST #7
|
||||
zaffiro = 6, rubino = 1, topazio = 4, smeraldo = 4, TOT = 15
|
||||
Collana massima di lunghezza 13
|
||||
TEST #8
|
||||
zaffiro = 8, rubino = 3, topazio = 6, smeraldo = 4, TOT = 21
|
||||
Collana massima di lunghezza 19
|
||||
TEST #9
|
||||
zaffiro = 7, rubino = 7, topazio = 4, smeraldo = 2, TOT = 20
|
||||
Collana massima di lunghezza 18
|
||||
TEST #10
|
||||
zaffiro = 1, rubino = 3, topazio = 7, smeraldo = 1, TOT = 12
|
||||
Collana massima di lunghezza 9
|
||||
TEST #11
|
||||
zaffiro = 10, rubino = 10, topazio = 9, smeraldo = 10, TOT = 39
|
||||
Collana massima di lunghezza 39
|
||||
TEST #12
|
||||
zaffiro = 8, rubino = 8, topazio = 8, smeraldo = 6, TOT = 30
|
||||
Collana massima di lunghezza 30
|
||||
TEST #13
|
||||
zaffiro = 9, rubino = 6, topazio = 10, smeraldo = 5, TOT = 30
|
||||
Collana massima di lunghezza 27
|
||||
TEST #14
|
||||
zaffiro = 6, rubino = 10, topazio = 9, smeraldo = 5, TOT = 30
|
||||
Collana massima di lunghezza 30
|
||||
TEST #15
|
||||
zaffiro = 9, rubino = 6, topazio = 9, smeraldo = 9, TOT = 33
|
||||
Collana massima di lunghezza 31
|
||||
16
Laboratorio 6/Esercizio 2/hard_test_set.txt
Normal file
16
Laboratorio 6/Esercizio 2/hard_test_set.txt
Normal file
@@ -0,0 +1,16 @@
|
||||
15
|
||||
19 6 8 5
|
||||
19 14 5 11
|
||||
14 14 5 8
|
||||
12 16 5 14
|
||||
12 17 20 20
|
||||
13 11 14 18
|
||||
7 14 12 10
|
||||
13 20 12 17
|
||||
17 18 6 9
|
||||
5 5 15 19
|
||||
18 12 20 17
|
||||
13 6 19 6
|
||||
17 20 15 19
|
||||
12 18 17 8
|
||||
7 20 12 7
|
||||
45
Laboratorio 6/Esercizio 2/hard_test_set_result.txt
Normal file
45
Laboratorio 6/Esercizio 2/hard_test_set_result.txt
Normal file
@@ -0,0 +1,45 @@
|
||||
TEST #1
|
||||
zaffiro = 19, rubino = 6, topazio = 8, smeraldo = 5, TOT = 38
|
||||
Collana massima di lunghezza 37
|
||||
TEST #2
|
||||
zaffiro = 19, rubino = 14, topazio = 5, smeraldo = 11, TOT = 49
|
||||
Collana massima di lunghezza 41
|
||||
TEST #3
|
||||
zaffiro = 14, rubino = 14, topazio = 5, smeraldo = 8, TOT = 41
|
||||
Collana massima di lunghezza 33
|
||||
TEST #4
|
||||
zaffiro = 12, rubino = 16, topazio = 5, smeraldo = 14, TOT = 47
|
||||
Collana massima di lunghezza 37
|
||||
TEST #5
|
||||
zaffiro = 12, rubino = 17, topazio = 20, smeraldo = 20, TOT = 69
|
||||
Collana massima di lunghezza 67
|
||||
TEST #6
|
||||
zaffiro = 13, rubino = 11, topazio = 14, smeraldo = 18, TOT = 56
|
||||
Collana massima di lunghezza 54
|
||||
TEST #7
|
||||
zaffiro = 7, rubino = 14, topazio = 12, smeraldo = 10, TOT = 43
|
||||
Collana massima di lunghezza 42
|
||||
TEST #8
|
||||
zaffiro = 13, rubino = 20, topazio = 12, smeraldo = 17, TOT = 62
|
||||
Collana massima di lunghezza 55
|
||||
TEST #9
|
||||
zaffiro = 17, rubino = 18, topazio = 6, smeraldo = 9, TOT = 50
|
||||
Collana massima di lunghezza 39
|
||||
TEST #10
|
||||
zaffiro = 5, rubino = 5, topazio = 15, smeraldo = 19, TOT = 44
|
||||
Collana massima di lunghezza 35
|
||||
TEST #11
|
||||
zaffiro = 18, rubino = 12, topazio = 20, smeraldo = 17, TOT = 67
|
||||
Collana massima di lunghezza 60
|
||||
TEST #12
|
||||
zaffiro = 13, rubino = 6, topazio = 19, smeraldo = 6, TOT = 44
|
||||
Collana massima di lunghezza 32
|
||||
TEST #13
|
||||
zaffiro = 17, rubino = 20, topazio = 15, smeraldo = 19, TOT = 71
|
||||
Collana massima di lunghezza 67
|
||||
TEST #14
|
||||
zaffiro = 12, rubino = 18, topazio = 17, smeraldo = 8, TOT = 55
|
||||
Collana massima di lunghezza 55
|
||||
TEST #15
|
||||
zaffiro = 7, rubino = 20, topazio = 12, smeraldo = 7, TOT = 46
|
||||
Collana massima di lunghezza 39
|
||||
201
Laboratorio 6/Esercizio 2/main.c
Normal file
201
Laboratorio 6/Esercizio 2/main.c
Normal file
@@ -0,0 +1,201 @@
|
||||
// Laboratorio 6 - Esercizio 2
|
||||
// Matteo Schiff - s295565
|
||||
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
|
||||
int fZ(int z, int r, int t, int s, int len);
|
||||
int fR(int z, int r, int t, int s, int len);
|
||||
int fS(int z, int r, int t, int s, int len);
|
||||
int fT(int z, int r, int t, int s, int len);
|
||||
|
||||
enum Pietra{zaffiro, rubino, topazio, smeraldo};
|
||||
|
||||
int *cache;
|
||||
int mz, mr, mt, ms;
|
||||
|
||||
// Funzioni helper per semplificare la memorizzazione
|
||||
// NOTA se `getCache` restituisce 0 vuol dire che la collana non è ancora stata analizzata
|
||||
int getCache(int p, int z, int r, int t, int s) {
|
||||
return *(cache + p*mz*mr*mt*ms + z*mr*mt*ms + r*mt*ms + t*ms + s);
|
||||
}
|
||||
|
||||
void saveCache(int p, int z, int r, int t, int s, int val) {
|
||||
*(cache + p*mz*mr*mt*ms + z*mr*mt*ms + r*mt*ms + t*ms + s) = val;
|
||||
}
|
||||
|
||||
int fZ(int z, int r, int t, int s, int len) {
|
||||
if (len != 0 && getCache(zaffiro, z,r,t,s) != 0) {
|
||||
return getCache(zaffiro,z,r,t,s);
|
||||
}
|
||||
|
||||
int x = -1;
|
||||
int m;
|
||||
if (z > 0) {
|
||||
m = fZ(z-1,r,t,s,len +1);
|
||||
if (m > x)
|
||||
x = m;
|
||||
}
|
||||
|
||||
if (r > 0) {
|
||||
m = fR(z,r-1,t,s,len +1);
|
||||
if (m > x)
|
||||
x = m;
|
||||
}
|
||||
|
||||
// se non è possibile più aggiungere pietre, ho raggiunto la lunghezza massima ottenibile dal ramo
|
||||
if (x == -1) {
|
||||
saveCache(zaffiro,z,r,t,s,len);
|
||||
return len;
|
||||
}
|
||||
|
||||
// se no restituisco il massimo fin'ora
|
||||
saveCache(zaffiro,z,r,t,s,x);
|
||||
return x;
|
||||
}
|
||||
|
||||
int fR(int z, int r, int t, int s, int len) {
|
||||
if (len != 0 && getCache(rubino, z,r,t,s) != 0) {
|
||||
return getCache(rubino,z,r,t,s);
|
||||
}
|
||||
|
||||
int x = -1;
|
||||
int m;
|
||||
if (s > 0) {
|
||||
m = fS(z,r,t,s-1,len +1);
|
||||
if (m > x)
|
||||
x = m;
|
||||
}
|
||||
|
||||
if (t > 0) {
|
||||
m = fT(z,r,t-1,s,len +1);
|
||||
if (m > x)
|
||||
x = m;
|
||||
}
|
||||
|
||||
// se non è possibile più aggiungere pietre, ho raggiunto la lunghezza massima ottenibile dal ramo
|
||||
if (x == -1) {
|
||||
saveCache(rubino,z,r,t,s,len);
|
||||
return len;
|
||||
}
|
||||
|
||||
// se no restituisco il massimo fin'ora
|
||||
saveCache(rubino,z,r,t,s,x);
|
||||
return x;
|
||||
}
|
||||
|
||||
int fT(int z, int r, int t, int s, int len) {
|
||||
if (len != 0 && getCache(topazio, z,r,t,s) != 0) {
|
||||
return getCache(topazio,z,r,t,s);
|
||||
}
|
||||
|
||||
int x = -1;
|
||||
int m;
|
||||
if (z > 0) {
|
||||
m = fZ(z-1,r,t,s,len +1);
|
||||
if (m > x)
|
||||
x = m;
|
||||
}
|
||||
|
||||
if (r > 0) {
|
||||
m = fR(z,r-1,t,s,len +1);
|
||||
if (m > x)
|
||||
x = m;
|
||||
}
|
||||
|
||||
// se non è possibile più aggiungere pietre, ho raggiunto la lunghezza massima ottenibile dal ramo
|
||||
if (x == -1) {
|
||||
saveCache(topazio,z,r,t,s,len);
|
||||
return len;
|
||||
}
|
||||
|
||||
// se no restituisco il massimo fin'ora
|
||||
saveCache(topazio,z,r,t,s,x);
|
||||
return x;
|
||||
}
|
||||
|
||||
int fS(int z, int r, int t, int s, int len) {
|
||||
if (len != 0 && getCache(smeraldo, z,r,t,s) != 0) {
|
||||
return getCache(smeraldo,z,r,t,s);
|
||||
}
|
||||
|
||||
int x = -1;
|
||||
int m;
|
||||
if (s > 0) {
|
||||
m = fS(z,r,t,s-1,len +1);
|
||||
if (m > x)
|
||||
x = m;
|
||||
}
|
||||
|
||||
if (t > 0) {
|
||||
m = fT(z,r,t-1,s,len +1);
|
||||
if (m > x)
|
||||
x = m;
|
||||
}
|
||||
|
||||
// se non è possibile più aggiungere pietre, ho raggiunto la lunghezza massima ottenibile dal ramo
|
||||
if (x == -1) {
|
||||
saveCache(smeraldo,z,r,t,s,len);
|
||||
return len;
|
||||
}
|
||||
|
||||
// se no restituisco il massimo fin'ora
|
||||
saveCache(smeraldo,z,r,t,s,x);
|
||||
return x;
|
||||
}
|
||||
|
||||
void solveCase(int z, int r, int t, int s) {
|
||||
mz = z+1;
|
||||
mr = r+1;
|
||||
mt = t+1;
|
||||
ms = s+1;
|
||||
cache = calloc(4*mz*mr*mt*ms, sizeof(int));
|
||||
|
||||
int max = z+r+t+s;
|
||||
|
||||
int res = -1, x;
|
||||
|
||||
printf("zaffiri = %d, rubini = %d, topazi = %d, smeraldi = %d; numero pietre = %d\n", z,r,t,s,max);
|
||||
|
||||
x = fZ(z-1,r,t,s,1);
|
||||
if (x > res)
|
||||
res = x;
|
||||
|
||||
x = fR(z,r-1,t,s,1);
|
||||
if (x > res)
|
||||
res = x;
|
||||
|
||||
x = fS(z,r,t,s-1,1);
|
||||
if (x > res)
|
||||
res = x;
|
||||
|
||||
x = fT(z,r,t-1,s,1);
|
||||
if (x > res)
|
||||
res = x;
|
||||
free(cache);
|
||||
|
||||
printf("Collana massima di lunghezza %d\n", res);
|
||||
fflush(stdout);
|
||||
}
|
||||
|
||||
int main() {
|
||||
int z, r, t, s, N = 0;
|
||||
|
||||
FILE *fp;
|
||||
|
||||
if ((fp = fopen("hard_test_set.txt", "r")) == NULL)
|
||||
{
|
||||
puts("Impossibile aprire il file");
|
||||
return 1;
|
||||
}
|
||||
|
||||
fscanf(fp, "%d", &N);
|
||||
|
||||
for (int i = 0; i < N; i++) {
|
||||
fscanf(fp, " %d %d %d %d", &z, &r, &t, &s);
|
||||
printf("Test case %d\n", i+1);
|
||||
solveCase(z,r,t,s);
|
||||
}
|
||||
|
||||
fclose(fp);
|
||||
}
|
||||
108
Laboratorio 6/Esercizio 3/equipArray.c
Normal file
108
Laboratorio 6/Esercizio 3/equipArray.c
Normal file
@@ -0,0 +1,108 @@
|
||||
// Laboratorio 6 - Esercizio 3 - equipArray.c
|
||||
// Matteo Schiff - s295565
|
||||
|
||||
#ifndef EQUIPARRAY_C_DEFINED
|
||||
#define EQUIPARRAY_C_DEFINED
|
||||
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
#include <stdio.h>
|
||||
|
||||
#include "invArray.h"
|
||||
#include "equipArray.h"
|
||||
#include "inv.h"
|
||||
|
||||
/* ADT di prima classe collezione di oggetti di equipaggiamento */
|
||||
typedef struct equipArray_s {
|
||||
int inUse;
|
||||
inv_t *items[EQUIP_SLOT];
|
||||
} *equipArray_t;
|
||||
|
||||
/* creatore e disruttore */
|
||||
equipArray_t equipArray_init() {
|
||||
equipArray_t new = malloc(sizeof(struct equipArray_s));
|
||||
new->inUse = 0;
|
||||
return new;
|
||||
}
|
||||
void equipArray_free(equipArray_t equipArray) {
|
||||
free(equipArray);
|
||||
}
|
||||
|
||||
/* quanti equipaggiamenti sono in uso */
|
||||
int equipArray_inUse(equipArray_t equipArray) {
|
||||
return equipArray->inUse;
|
||||
}
|
||||
|
||||
void equipArray_apply_modifier(stat_t *stats, equipArray_t equipArray) {
|
||||
for (int i = 0; i < equipArray_inUse(equipArray); i++) {
|
||||
stat_sum(stats, &equipArray->items[i]->stat);
|
||||
}
|
||||
}
|
||||
|
||||
/* scrittura su file */
|
||||
void equipArray_print(FILE *fp, equipArray_t equipArray, invArray_t invArray) {
|
||||
for (int i = 0; i < equipArray->inUse; i++) {
|
||||
printf(" %d: ", i);
|
||||
inv_print(fp, equipArray->items[i]);
|
||||
}
|
||||
}
|
||||
|
||||
void equipArray_insert(equipArray_t equipArray, invArray_t invArray) {
|
||||
char name[LEN+1];
|
||||
|
||||
if (equipArray->inUse >= EQUIP_SLOT) {
|
||||
puts("Tutti gli slot sono occupati");
|
||||
return;
|
||||
}
|
||||
|
||||
puts("Inserisci il nome dell'oggetto da equipaggiare: ");
|
||||
scanf(" %50s", name);
|
||||
int index = invArray_searchByName(invArray, name);
|
||||
|
||||
if (index == -1) {
|
||||
puts("Oggetto non trovato");
|
||||
return;
|
||||
}
|
||||
|
||||
inv_t *selected = invArray_getByIndex(invArray, index);
|
||||
|
||||
equipArray->items[(equipArray->inUse)++] = selected;
|
||||
}
|
||||
|
||||
void equipArray_remove(equipArray_t equipArray, invArray_t invArray) {
|
||||
int index;
|
||||
|
||||
if (equipArray->inUse <= 0) {
|
||||
puts("Tutti gli slot sono vuoti");
|
||||
return;
|
||||
}
|
||||
|
||||
puts("Oggetti equipaggiati: ");
|
||||
equipArray_print(stdout, equipArray, invArray);
|
||||
|
||||
puts("Inserisci l'indice dell'oggetto da rimuovere: ");
|
||||
scanf(" %d", &index);
|
||||
if (index < 0 || index >= equipArray->inUse) {
|
||||
puts("Indice oggetto non valido");
|
||||
return;
|
||||
}
|
||||
|
||||
equipArray->items[index] = equipArray->items[--(equipArray->inUse)];
|
||||
}
|
||||
|
||||
/* modifica equipaggiamento scegliendo un oggetto da inventario */
|
||||
void equipArray_update(equipArray_t equipArray, invArray_t invArray) {
|
||||
char selection;
|
||||
puts("Vuoi inserire o rimuovere un oggetto? Rispondi (i) o (r): ");
|
||||
scanf(" %c", &selection);
|
||||
|
||||
if (selection == 'i') {
|
||||
equipArray_insert(equipArray, invArray);
|
||||
} else {
|
||||
equipArray_remove(equipArray, invArray);
|
||||
}
|
||||
}
|
||||
|
||||
/* Si possono aggiungere altre funzioni se ritenute necessarie */
|
||||
|
||||
#endif
|
||||
34
Laboratorio 6/Esercizio 3/equipArray.h
Normal file
34
Laboratorio 6/Esercizio 3/equipArray.h
Normal file
@@ -0,0 +1,34 @@
|
||||
// Laboratorio 6 - Esercizio 3 - equipArray.h
|
||||
// Matteo Schiff - s295565
|
||||
|
||||
#ifndef EQUIPARRAY_H_DEFINED
|
||||
#define EQUIPARRAY_H_DEFINED
|
||||
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
#include <stdio.h>
|
||||
|
||||
#define EQUIP_SLOT 8
|
||||
|
||||
#include "invArray.h"
|
||||
|
||||
/* ADT di prima classe collezione di oggetti di equipaggiamento */
|
||||
typedef struct equipArray_s *equipArray_t;
|
||||
|
||||
/* creatore e disruttore */
|
||||
equipArray_t equipArray_init();
|
||||
void equipArray_free(equipArray_t equipArray);
|
||||
|
||||
/* quanti equipaggiamenti sono in uso */
|
||||
int equipArray_inUse(equipArray_t equipArray);
|
||||
|
||||
void equipArray_apply_modifier(stat_t *stats, equipArray_t equipArray);
|
||||
|
||||
/* scrittura su file */
|
||||
void equipArray_print(FILE *fp, equipArray_t equipArray, invArray_t invArray);
|
||||
/* modifica equipaggiamento scegliendo un oggetto da inventario */
|
||||
void equipArray_update(equipArray_t equipArray, invArray_t invArray);
|
||||
|
||||
/* Si possono aggiungere altre funzioni se ritenute necessarie */
|
||||
|
||||
#endif
|
||||
122
Laboratorio 6/Esercizio 3/gdr.c
Normal file
122
Laboratorio 6/Esercizio 3/gdr.c
Normal file
@@ -0,0 +1,122 @@
|
||||
// Laboratorio 6 - Esercizio 3 - gdr.c
|
||||
// Matteo Schiff - s295565
|
||||
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
#include <stdio.h>
|
||||
|
||||
#include "pgList.h"
|
||||
#include "invArray.h"
|
||||
#include "pg.h"
|
||||
|
||||
#define N_SCELTE 7
|
||||
#define DBG 0
|
||||
|
||||
enum { falso, vero };
|
||||
typedef int bool;
|
||||
|
||||
void stampaMenu(char *scelte[], int *selezione){
|
||||
int i=0;
|
||||
printf("\nMENU'\n");
|
||||
for(i=0;i<N_SCELTE;i++)
|
||||
printf("%2d > %s\n",i,scelte[i]);
|
||||
scanf(" %d",selezione);
|
||||
}
|
||||
|
||||
int main(int argc, char **argv) {
|
||||
char *scelte[] = {
|
||||
"Uscita",
|
||||
"Stampa personaggi",
|
||||
"Stampa inventario",
|
||||
"Cerca personaggio",
|
||||
"Aggiungi personaggio",
|
||||
"Elimina personaggio",
|
||||
"Modifica equip"
|
||||
};
|
||||
|
||||
char codiceRicerca[LEN];
|
||||
int selezione;
|
||||
FILE *fin;
|
||||
bool fineProgramma;
|
||||
|
||||
pgList_t pgList = pgList_init();
|
||||
invArray_t invArray = invArray_init();
|
||||
pg_t *pgp, pg;
|
||||
|
||||
fin = fopen("pg.txt","r");
|
||||
pgList_read(fin, pgList);
|
||||
fclose(fin);
|
||||
#if DBG
|
||||
pgList_print(stdout, pgList);
|
||||
#endif /* DBG */
|
||||
|
||||
fin = fopen("inventario.txt","r");
|
||||
invArray_read(fin, invArray);
|
||||
fclose(fin);
|
||||
#if DBG
|
||||
invArray_print(stdout, invArray);
|
||||
#endif /* DBG */
|
||||
|
||||
fineProgramma = falso;
|
||||
|
||||
do {
|
||||
stampaMenu(scelte, &selezione);
|
||||
switch(selezione){
|
||||
|
||||
case 0: {
|
||||
fineProgramma = vero;
|
||||
} break;
|
||||
|
||||
case 1: {
|
||||
pgList_print(stdout, pgList, invArray);
|
||||
} break;
|
||||
|
||||
case 2: {
|
||||
invArray_print(stdout, invArray);
|
||||
} break;
|
||||
|
||||
case 3: {
|
||||
printf("Inserire codice personaggio: ");
|
||||
scanf("%s", codiceRicerca);
|
||||
pgp = pgList_searchByCode(pgList, codiceRicerca);
|
||||
if (pgp!=NULL) {
|
||||
pg_print(stdout, pgp, invArray);
|
||||
} else {
|
||||
puts("Personaggio non trovato");
|
||||
}
|
||||
} break;
|
||||
|
||||
case 4: {
|
||||
printf("Cod Nome Classe HP MP ATK DEF MAG SPR: ");
|
||||
if (pg_read(stdin, &pg) != 0) {
|
||||
pgList_insert(pgList, pg);
|
||||
}
|
||||
} break;
|
||||
|
||||
case 5: {
|
||||
printf("Inserire codice personaggio: ");
|
||||
scanf("%s", codiceRicerca);
|
||||
pgList_remove(pgList, codiceRicerca);
|
||||
} break;
|
||||
|
||||
case 6: {
|
||||
printf("Inserire codice personaggio: ");
|
||||
scanf("%s", codiceRicerca);
|
||||
pgp = pgList_searchByCode(pgList, codiceRicerca);
|
||||
if (pgp!=NULL) {
|
||||
pg_updateEquip(pgp, invArray);
|
||||
} else {
|
||||
puts("Personaggio non trovato");
|
||||
}
|
||||
} break;
|
||||
|
||||
default:{
|
||||
printf("Scelta non valida\n");
|
||||
} break;
|
||||
}
|
||||
} while(!fineProgramma);
|
||||
|
||||
pgList_free(pgList);
|
||||
|
||||
return 0;
|
||||
}
|
||||
71
Laboratorio 6/Esercizio 3/inv.c
Normal file
71
Laboratorio 6/Esercizio 3/inv.c
Normal file
@@ -0,0 +1,71 @@
|
||||
// Laboratorio 6 - Esercizio 3 - inv.c
|
||||
// Matteo Schiff - s295565
|
||||
|
||||
#ifndef INV_C_DEFINED
|
||||
#define INV_C_DEFINED
|
||||
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
#include <stdio.h>
|
||||
|
||||
#include <limits.h>
|
||||
#include "inv.h"
|
||||
|
||||
/* funzioni di input/output delle statistiche */
|
||||
void stat_read(FILE *fp, stat_t *statp) {
|
||||
fscanf(fp, " %d %d %d %d %d %d", &(statp->hp), &(statp->mp), &(statp->atk), &(statp->def), &(statp->mag), &(statp->spr));
|
||||
}
|
||||
|
||||
int zero_se_soglia(int a, int soglia) {
|
||||
if (a < soglia)
|
||||
return 0;
|
||||
return a;
|
||||
}
|
||||
|
||||
void stat_print(FILE *fp, stat_t *statp, int soglia) {
|
||||
fprintf(fp, "HP: %d, MP: %d, ATK: %d, DEF: %d, MAG: %d, SPR: %d\n",
|
||||
zero_se_soglia(statp->hp, soglia),
|
||||
zero_se_soglia(statp->mp, soglia),
|
||||
zero_se_soglia(statp->atk, soglia),
|
||||
zero_se_soglia(statp->def, soglia),
|
||||
zero_se_soglia(statp->mag, soglia),
|
||||
zero_se_soglia(statp->spr, soglia));
|
||||
}
|
||||
|
||||
void stat_copy(stat_t *a, stat_t *b) {
|
||||
a->atk = b->atk;
|
||||
a->def = b->def;
|
||||
a->hp = b->hp;
|
||||
a->mag = b->mag;
|
||||
a->mp = b->mp;
|
||||
a->spr = b->spr;
|
||||
}
|
||||
|
||||
void stat_sum(stat_t *a, stat_t *b) {
|
||||
a->atk += b->atk;
|
||||
a->def += b->def;
|
||||
a->hp += b->hp;
|
||||
a->mag += b->mag;
|
||||
a->mp += b->mp;
|
||||
a->spr += b->spr;
|
||||
}
|
||||
|
||||
/* funzioni di input/output di un oggetto dell'inventario */
|
||||
void inv_read(FILE *fp, inv_t *invp) {
|
||||
fscanf(fp, " %49s %49s", invp->nome, invp->tipo);
|
||||
stat_read(fp, &(invp->stat));
|
||||
}
|
||||
|
||||
void inv_print(FILE *fp, inv_t *invp) {
|
||||
fprintf(fp, " %s (%s) - ", invp->nome, invp->tipo);
|
||||
stat_print(fp, &(invp->stat),INT_MIN);
|
||||
}
|
||||
|
||||
/* ritorna il campo stat di un oggetto dell'inventario */
|
||||
stat_t inv_getStat(inv_t *invp) {
|
||||
return invp->stat;
|
||||
}
|
||||
|
||||
/* Si possono aggiungere altre funzioni se ritenute necessarie */
|
||||
|
||||
#endif
|
||||
42
Laboratorio 6/Esercizio 3/inv.h
Normal file
42
Laboratorio 6/Esercizio 3/inv.h
Normal file
@@ -0,0 +1,42 @@
|
||||
// Laboratorio 6 - Esercizio 3 - inv.h
|
||||
// Matteo Schiff - s295565
|
||||
|
||||
#ifndef INV_H_DEFINED
|
||||
#define INV_H_DEFINED
|
||||
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
#include <stdio.h>
|
||||
|
||||
#define LEN 50
|
||||
#define MIN_STAT 1
|
||||
|
||||
/* quasi ADT statistiche */
|
||||
typedef struct stat_s {
|
||||
int hp, mp, atk, def, mag, spr;
|
||||
} stat_t;
|
||||
|
||||
/* quasi ADT oggetto di inventario */
|
||||
typedef struct inv_s {
|
||||
char nome[LEN];
|
||||
char tipo[LEN];
|
||||
stat_t stat;
|
||||
} inv_t;
|
||||
|
||||
/* funzioni di input/output delle statistiche */
|
||||
void stat_read(FILE *fp, stat_t *statp);
|
||||
void stat_print(FILE *fp, stat_t *statp, int soglia);
|
||||
|
||||
void stat_copy(stat_t *a, stat_t *b);
|
||||
void stat_sum(stat_t *a, stat_t *b);
|
||||
|
||||
/* funzioni di input/output di un oggetto dell'inventario */
|
||||
void inv_read(FILE *fp, inv_t *invp);
|
||||
void inv_print(FILE *fp, inv_t *invp);
|
||||
|
||||
/* ritorna il campo stat di un oggetto dell'inventario */
|
||||
stat_t inv_getStat(inv_t *invp);
|
||||
|
||||
/* Si possono aggiungere altre funzioni se ritenute necessarie */
|
||||
|
||||
#endif
|
||||
67
Laboratorio 6/Esercizio 3/invArray.c
Normal file
67
Laboratorio 6/Esercizio 3/invArray.c
Normal file
@@ -0,0 +1,67 @@
|
||||
// Laboratorio 6 - Esercizio 3 - invArray.c
|
||||
// Matteo Schiff - s295565
|
||||
|
||||
#ifndef INVARRAY_C_DEFINED
|
||||
#define INVARRAY_C_DEFINED
|
||||
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
#include <stdio.h>
|
||||
|
||||
#include "inv.h"
|
||||
|
||||
typedef struct invArray_s {
|
||||
int N;
|
||||
inv_t *array;
|
||||
} *invArray_t;
|
||||
|
||||
/* creatore e disruttore */
|
||||
invArray_t invArray_init() {
|
||||
invArray_t new = malloc(sizeof(struct invArray_s));
|
||||
new->N = 0;
|
||||
new->array = NULL;
|
||||
return new;
|
||||
}
|
||||
|
||||
void invArray_free(invArray_t invArray) {
|
||||
free(invArray->array);
|
||||
free(invArray);
|
||||
}
|
||||
|
||||
/* lettura e scrittura su file */
|
||||
void invArray_read(FILE *fp, invArray_t invArray) {
|
||||
if (fscanf(fp, " %d", &invArray->N) != 1)
|
||||
return;
|
||||
|
||||
invArray->array = malloc(invArray->N * sizeof(inv_t));
|
||||
|
||||
for (int i = 0; i < invArray->N; i++) {
|
||||
inv_read(fp, &invArray->array[i]);
|
||||
}
|
||||
}
|
||||
void invArray_print(FILE *fp, invArray_t invArray) {
|
||||
for (int i = 0; i < invArray->N; i++) {
|
||||
inv_print(fp, &invArray->array[i]);
|
||||
}
|
||||
}
|
||||
|
||||
/* ritorna puntatore a oggetto selezionato da indice (nel vettore) */
|
||||
inv_t *invArray_getByIndex(invArray_t invArray, int index) {
|
||||
if (index >= invArray->N)
|
||||
return NULL;
|
||||
|
||||
return &(invArray->array[index]);
|
||||
}
|
||||
/* ritorna indice (nel vettore) a oggetto selezionato da nome */
|
||||
int invArray_searchByName(invArray_t invArray, char *name) {
|
||||
for (int i = 0; i < invArray->N; i++) {
|
||||
if (!strcmp(invArray->array[i].nome, name))
|
||||
return i;
|
||||
}
|
||||
|
||||
return -1;
|
||||
}
|
||||
|
||||
/* Si possono aggiungere altre funzioni se ritenute necessarie */
|
||||
|
||||
#endif
|
||||
30
Laboratorio 6/Esercizio 3/invArray.h
Normal file
30
Laboratorio 6/Esercizio 3/invArray.h
Normal file
@@ -0,0 +1,30 @@
|
||||
// Laboratorio 6 - Esercizio 3 - invArray.h
|
||||
// Matteo Schiff - s295565
|
||||
|
||||
#ifndef INVARRAY_H_DEFINED
|
||||
#define INVARRAY_H_DEFINED
|
||||
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
#include <stdio.h>
|
||||
|
||||
#include "inv.h"
|
||||
|
||||
/* ADT di prima classe collezione di oggetti di inventario */
|
||||
typedef struct invArray_s *invArray_t;
|
||||
|
||||
/* creatore e disruttore */
|
||||
invArray_t invArray_init();
|
||||
void invArray_free(invArray_t invArray);
|
||||
|
||||
/* lettura e scrittura su file */
|
||||
void invArray_read(FILE *fp, invArray_t invArray);
|
||||
void invArray_print(FILE *fp, invArray_t invArray);
|
||||
/* ritorna puntatore a oggetto selezionato da indice (nel vettore) */
|
||||
inv_t *invArray_getByIndex(invArray_t invArray, int index);
|
||||
/* ritorna indice (nel vettore) a oggetto selezionato da nome */
|
||||
int invArray_searchByName(invArray_t invArray, char *name);
|
||||
|
||||
/* Si possono aggiungere altre funzioni se ritenute necessarie */
|
||||
|
||||
#endif
|
||||
16
Laboratorio 6/Esercizio 3/inventario.txt
Normal file
16
Laboratorio 6/Esercizio 3/inventario.txt
Normal file
@@ -0,0 +1,16 @@
|
||||
15
|
||||
Excalibur Spada2M 0 0 120 0 0 50
|
||||
Filatterio Accessorio -100 20 -30 -30 25 25
|
||||
ArmillaDiDiamante Accessorio 0 0 25 0 25
|
||||
Escutcheon Scudo 1000 50 0 75 0 0
|
||||
MantoElfico Vesti 0 50 0 10 10 10
|
||||
Pendragon Scudo 250 0 0 45 0 30
|
||||
Oricalco Reliquia 0 0 0 0 15 15
|
||||
TalismanoNero Accessorio -500 150 0 0 100 50
|
||||
AmmazzaDraghi Spada2M 0 0 80 -20 0 -20
|
||||
Fiocco Accessorio 0 0 1 1 1 1
|
||||
CorazzaAdamantina ArmaturaPesante 3333 333 -15 33 -15 33
|
||||
DagaRunica Pugnale 0 50 10 0 35 0
|
||||
Tempesta Spada1M 0 0 40 0 -5 0
|
||||
Maximillian ArmaturaLeggera 500 0 0 30 0 15
|
||||
Ametista Reliquia 0 0 15 15 0 0
|
||||
49
Laboratorio 6/Esercizio 3/pg.c
Normal file
49
Laboratorio 6/Esercizio 3/pg.c
Normal file
@@ -0,0 +1,49 @@
|
||||
// Laboratorio 6 - Esercizio 3 - pg.c
|
||||
// Matteo Schiff - s295565
|
||||
|
||||
#ifndef PG_C_DEFINED
|
||||
#define PG_C_DEFINED
|
||||
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
#include <stdio.h>
|
||||
|
||||
#include "equipArray.h"
|
||||
#include "pg.h"
|
||||
|
||||
void pg_calc_stats(pg_t *pgp) {
|
||||
stat_copy(&pgp->eq_stat, &pgp->b_stat);
|
||||
equipArray_apply_modifier(&pgp->eq_stat, pgp->equip);
|
||||
}
|
||||
|
||||
/* lettura e scrittura su file */
|
||||
int pg_read(FILE *fp, pg_t *pgp) {
|
||||
if (fscanf(fp, " %49s %49s %49s", pgp->cod, pgp->nome, pgp->classe) != 3)
|
||||
return 0;
|
||||
|
||||
stat_read(fp, &(pgp->b_stat));
|
||||
pgp->equip = equipArray_init();
|
||||
pg_calc_stats(pgp);
|
||||
|
||||
return 1;
|
||||
}
|
||||
/* non essendo struct dinamica, pulisce chiamando il distruttore di equipArray */
|
||||
void pg_clean(pg_t *pgp) {
|
||||
free(pgp->equip);
|
||||
}
|
||||
|
||||
void pg_print(FILE *fp, pg_t *pgp, invArray_t invArray) {
|
||||
fprintf(fp, "%s (%s)\n Classe: %s\n Statistiche - ", pgp->nome, pgp->cod, pgp->classe);
|
||||
stat_print(fp, &(pgp->eq_stat), 0);
|
||||
equipArray_print(fp, pgp->equip, invArray);
|
||||
}
|
||||
/* modifica personaggio aggiungendo/togliendo un equipaggiamento selezionato da inventario:
|
||||
di fatto e' sufficiente chiamare l'opportuna funzione dal modulo equipArray */
|
||||
void pg_updateEquip(pg_t *pgp, invArray_t invArray) {
|
||||
equipArray_update(pgp->equip, invArray);
|
||||
pg_calc_stats(pgp);
|
||||
}
|
||||
|
||||
/* Si possono aggiungere altre funzioni se ritenute necessarie */
|
||||
|
||||
#endif
|
||||
35
Laboratorio 6/Esercizio 3/pg.h
Normal file
35
Laboratorio 6/Esercizio 3/pg.h
Normal file
@@ -0,0 +1,35 @@
|
||||
// Laboratorio 6 - Esercizio 3 - pg.h
|
||||
// Matteo Schiff - s295565
|
||||
|
||||
#ifndef PG_H_DEFINED
|
||||
#define PG_H_DEFINED
|
||||
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
#include <stdio.h>
|
||||
|
||||
#include "equipArray.h"
|
||||
|
||||
/* quasi ADT personaggio; si noti che si tratta di un composto con riferimento
|
||||
a un equipArray di proprieta' */
|
||||
typedef struct pg_s {
|
||||
char cod[LEN];
|
||||
char nome[LEN];
|
||||
char classe[LEN];
|
||||
stat_t b_stat, eq_stat;
|
||||
equipArray_t equip;
|
||||
} pg_t;
|
||||
|
||||
/* lettura e scrittura su file */
|
||||
int pg_read(FILE *fp, pg_t *pgp);
|
||||
/* non essendo struct dinamica, pulisce chiamando il distruttire di equipArray */
|
||||
void pg_clean(pg_t *pgp);
|
||||
|
||||
void pg_print(FILE *fp, pg_t *pgp, invArray_t invArray);
|
||||
/* modifica personaggio aggiungendo/togliendo un equipaggiamento selezionato da inventario:
|
||||
di fatto e' sufficiente chiamare l'opportuna funzione dal modulo equipArray */
|
||||
void pg_updateEquip(pg_t *pgp, invArray_t invArray);
|
||||
|
||||
/* Si possono aggiungere altre funzioni se ritenute necessarie */
|
||||
|
||||
#endif
|
||||
10
Laboratorio 6/Esercizio 3/pg.txt
Normal file
10
Laboratorio 6/Esercizio 3/pg.txt
Normal file
@@ -0,0 +1,10 @@
|
||||
PG0001 Sephiroth Guerriero 1832 71 116 65 41 49
|
||||
PG0002 Aerith MagoBianco 976 144 12 39 121 140
|
||||
PG0003 Vivi MagoNero 1001 136 17 36 131 115
|
||||
PG0004 Beatrix Templare 1654 99 85 35 70 61
|
||||
PG0005 Orlandeau Paladino 1701 84 81 68 34 48
|
||||
PG0006 Basch Capitano 2199 46 32 98 33 89
|
||||
PG0007 Lulu MagoNero 999 139 21 33 119 117
|
||||
PG0008 Yuna Evocatrice 1019 100 32 17 97 99
|
||||
PG0009 DarkCecil CavaliereNero 1901 48 106 90 12 18
|
||||
PG0010 Kefka Arcimago 1271 89 39 47 89 64
|
||||
131
Laboratorio 6/Esercizio 3/pgList.c
Normal file
131
Laboratorio 6/Esercizio 3/pgList.c
Normal file
@@ -0,0 +1,131 @@
|
||||
// Laboratorio 6 - Esercizio 3 - pgList.c
|
||||
// Matteo Schiff - s295565
|
||||
|
||||
#ifndef PGLIST_C_DEFINED
|
||||
#define PGLIST_C_DEFINED
|
||||
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
#include <stdio.h>
|
||||
|
||||
#include "pgList.h"
|
||||
|
||||
typedef struct node_s *link;
|
||||
|
||||
typedef struct node_s {
|
||||
pg_t val;
|
||||
link next;
|
||||
} node;
|
||||
|
||||
/* ADT di prima classe collezione di personaggi */
|
||||
typedef struct pgList_s {
|
||||
link head;
|
||||
link tail;
|
||||
int nPg;
|
||||
} *pgList_t;
|
||||
|
||||
link new_node(pg_t val, link next) {
|
||||
link t = malloc(sizeof(node));
|
||||
if (t == NULL) {
|
||||
printf("ERROR: out of memory");
|
||||
exit(2);
|
||||
}
|
||||
t->val = val;
|
||||
t->next = next;
|
||||
|
||||
return t;
|
||||
}
|
||||
|
||||
void free_node(link p) {
|
||||
pg_clean(&p->val);
|
||||
free(p);
|
||||
}
|
||||
|
||||
/* creatore e distruttore */
|
||||
pgList_t pgList_init() {
|
||||
pgList_t new = malloc(sizeof(struct pgList_s));
|
||||
new->head = NULL;
|
||||
new->tail = NULL;
|
||||
new->nPg = 0;
|
||||
return new;
|
||||
}
|
||||
|
||||
void pgList_free(pgList_t pgList) {
|
||||
link p, x;
|
||||
for (p = pgList->head, x = pgList->head->next; x != NULL; p = x, x = x->next){
|
||||
free_node(p);
|
||||
}
|
||||
free(pgList);
|
||||
}
|
||||
|
||||
/* lettura e scrittura su file */
|
||||
void pgList_read(FILE *fp, pgList_t pgList) {
|
||||
while (!feof(fp))
|
||||
{
|
||||
pg_t pgp;
|
||||
if (pg_read(fp, &pgp) != 0) {
|
||||
pgList_insert(pgList, pgp);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void pgList_print(FILE *fp, pgList_t pgList, invArray_t invArray) {
|
||||
link p;
|
||||
|
||||
for (p = pgList->head; p != NULL; p = p->next) {
|
||||
pg_print(fp, &(p->val), invArray);
|
||||
}
|
||||
}
|
||||
|
||||
/* inserimento di un nuovo personaggio */
|
||||
void pgList_insert(pgList_t pgList, pg_t pg) {
|
||||
link new = new_node(pg, NULL);
|
||||
if (pgList->head == NULL) {
|
||||
pgList->head = new;
|
||||
} else {
|
||||
pgList->tail->next = new;
|
||||
}
|
||||
|
||||
pgList->tail = new;
|
||||
}
|
||||
/* cancellazione con rimozione */
|
||||
void pgList_remove(pgList_t pgList, char* cod) {
|
||||
link p, x;
|
||||
|
||||
if (pgList->head == NULL)
|
||||
return;
|
||||
|
||||
for (x = pgList->head, p = NULL; x != NULL; p = x, x = x->next) {
|
||||
if (!strcmp(x->val.cod, cod)) {
|
||||
if (p == NULL) {
|
||||
pgList->head = x->next;
|
||||
} else {
|
||||
p->next = x->next;
|
||||
|
||||
// se tolgo l'ultimo elemento devo aggiornare `tail`
|
||||
if (x->next == NULL)
|
||||
pgList->tail = p;
|
||||
}
|
||||
|
||||
free_node(x);
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/* ricerca per codice, ritornando il puntatore */
|
||||
pg_t *pgList_searchByCode(pgList_t pgList, char* cod) {
|
||||
link p;
|
||||
|
||||
for (p = pgList->head; p != NULL; p = p->next) {
|
||||
if (!strcmp(p->val.cod, cod)) {
|
||||
return &(p->val);
|
||||
}
|
||||
}
|
||||
|
||||
return NULL;
|
||||
}
|
||||
|
||||
/* Si possono aggiungere altre funzioni se ritenute necessarie */
|
||||
|
||||
#endif
|
||||
33
Laboratorio 6/Esercizio 3/pgList.h
Normal file
33
Laboratorio 6/Esercizio 3/pgList.h
Normal file
@@ -0,0 +1,33 @@
|
||||
// Laboratorio 6 - Esercizio 3 - pgList.h
|
||||
// Matteo Schiff - s295565
|
||||
|
||||
#ifndef PGLIST_H_DEFINED
|
||||
#define PGLIST_H_DEFINED
|
||||
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
#include <stdio.h>
|
||||
|
||||
#include "pg.h"
|
||||
|
||||
/* ADT di prima classe collezione di personaggi */
|
||||
typedef struct pgList_s *pgList_t;
|
||||
|
||||
/* creatore e distruttore */
|
||||
pgList_t pgList_init();
|
||||
void pgList_free(pgList_t pgList);
|
||||
|
||||
/* lettura e scrittura su file */
|
||||
void pgList_read(FILE *fp, pgList_t pgList);
|
||||
void pgList_print(FILE *fp, pgList_t pgList, invArray_t invArray);
|
||||
|
||||
/* inserimento di un nuovo personaggio */
|
||||
void pgList_insert(pgList_t pgList, pg_t pg);
|
||||
/* cancellazione con rimozione */
|
||||
void pgList_remove(pgList_t pgList, char* cod);
|
||||
/* ricerca per codice, ritornando il puntatore */
|
||||
pg_t *pgList_searchByCode(pgList_t pgList, char* cod);
|
||||
|
||||
/* Si possono aggiungere altre funzioni se ritenute necessarie */
|
||||
|
||||
#endif
|
||||
Reference in New Issue
Block a user