diff --git a/esercitazioni/carica_struct_in_ram.cpp b/esercitazioni/carica_struct_in_ram.cpp new file mode 100644 index 0000000..6e36313 --- /dev/null +++ b/esercitazioni/carica_struct_in_ram.cpp @@ -0,0 +1,32 @@ +#include +#include + +using namespace std; + +struct partita_t { + char nome[20]; + int tempo; + int progresso; +}; + +void stampa(const partita_t &p) { + cout << "Nome: " << p.nome << endl; + cout << "Tempo: " << p.tempo << endl; + cout << "Progresso: " << p.progresso << endl; +} + +bool carica(partita_t &p, const char filename[]) { + ifstream f(filename); + if (!f) + return false; + f.read(reinterpret_cast(&p), sizeof(partita_t)); + f.close(); + return static_cast(f); +} + +int main() { + partita_t p1; + carica(p1, "partita.dat"); + stampa(p1); + return 0; +} diff --git a/esercitazioni/copia_struct.cpp b/esercitazioni/copia_struct.cpp new file mode 100644 index 0000000..e5b1f2e --- /dev/null +++ b/esercitazioni/copia_struct.cpp @@ -0,0 +1,41 @@ +#include + +using namespace std; + +struct point { + int x; + int y; +}; + +void stampa (point &p) { + cout << p.x << " " << p.y << endl; +} + +struct vect { + int _[10]; +}; + +void stampa(vect &v) { + for (int i=0; i<10; i++) + cout << v._[i] << " "; + cout << endl; + return; +} + +int main() { + point p1, p2; + p1 = {1, 2}; + stampa(p1); + stampa(p2); + cout << endl; + p2 = p1; + stampa(p1); + stampa(p2); + cout << endl; + + vect v = {{1, 2, 3, 4, 5}}; + vect c; + c = v; + stampa(v); + stampa(c); +} diff --git a/esercitazioni/copia_vettore.cpp b/esercitazioni/copia_vettore.cpp new file mode 100644 index 0000000..fe3d82b --- /dev/null +++ b/esercitazioni/copia_vettore.cpp @@ -0,0 +1,21 @@ +#include + +using namespace std; + +void copia_vett(const int* const src, int n, int* &dst) { + dst = new int[n]; + for (int i=0; i + +using namespace std; + +int main() { + const char s[] = "test_prova"; // 10 caratteri + int len; + for (len=0; s[len] != '\0'; len++) ; + cout << "La stringa ha " << len << " caratteri" << endl; + return 0; +} diff --git a/esercitazioni/partita.dat b/esercitazioni/partita.dat new file mode 100644 index 0000000..d7eac87 Binary files /dev/null and b/esercitazioni/partita.dat differ diff --git a/esercitazioni/salva_struct_in_memoria.cpp b/esercitazioni/salva_struct_in_memoria.cpp new file mode 100644 index 0000000..e726a9e --- /dev/null +++ b/esercitazioni/salva_struct_in_memoria.cpp @@ -0,0 +1,32 @@ +#include +#include + +using namespace std; + +struct partita_t { + char nome[20]; + int tempo; + int progresso; +}; + +bool salva(const partita_t &p, const char filename[]) { + ofstream f(filename); + if (!f) + return false; + + f.write(reinterpret_cast(&p), sizeof(p)); + + f.close(); + return static_cast(f); +} + +int main() { + partita_t p1_g1 = + { + "Francesco", + 10, + 5 + }; + salva(p1_g1, "partita.dat"); + return 0; +} diff --git a/esercitazioni/test_matrici.cpp b/esercitazioni/test_matrici.cpp new file mode 100644 index 0000000..1f4b888 --- /dev/null +++ b/esercitazioni/test_matrici.cpp @@ -0,0 +1,26 @@ +#include + +using namespace std; + +const int DIM = 4; + +int calcola_diff_diag(const int m[][DIM], int dim) { + int d_princ = 0; + int d_sec = 0; + for (int i = 0; i + +using namespace std; + +int main() { + char stringa[] = "parolamoltolungaallocamemoria"; + cout << "stringa contiene: " << stringa << endl; + cout << "Inerisci una parola: "; cin >> stringa; + cout << "Hai inserito \""<