Skip to content
This repository has been archived by the owner on Jun 16, 2023. It is now read-only.

Commit

Permalink
esperimenti stringhe, matrici, vettori dinamici, struct e file binari
Browse files Browse the repository at this point in the history
  • Loading branch information
mc-cat-tty committed Dec 21, 2021
1 parent 14789bf commit 19dcf0f
Show file tree
Hide file tree
Showing 8 changed files with 173 additions and 0 deletions.
32 changes: 32 additions & 0 deletions esercitazioni/carica_struct_in_ram.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
#include <iostream>
#include <fstream>

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<char *>(&p), sizeof(partita_t));
f.close();
return static_cast<bool>(f);
}

int main() {
partita_t p1;
carica(p1, "partita.dat");
stampa(p1);
return 0;
}
41 changes: 41 additions & 0 deletions esercitazioni/copia_struct.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
#include <iostream>

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);
}
21 changes: 21 additions & 0 deletions esercitazioni/copia_vettore.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
#include <iostream>

using namespace std;

void copia_vett(const int* const src, int n, int* &dst) {
dst = new int[n];
for (int i=0; i<n; i++)
dst[i] = src[i];
return;
}

int main() {
const int a[3] = {1, 2, 3};
int* b = NULL;
copia_vett(a, 3, b);
for (int i=0; i<3; i++)
cout << b[i] << " ";
cout << endl;
delete[] b;
return 0;
}
11 changes: 11 additions & 0 deletions esercitazioni/len_stringa.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
#include <iostream>

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;
}
Binary file added esercitazioni/partita.dat
Binary file not shown.
32 changes: 32 additions & 0 deletions esercitazioni/salva_struct_in_memoria.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
#include <iostream>
#include <fstream>

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<const char *>(&p), sizeof(p));

f.close();
return static_cast<bool>(f);
}

int main() {
partita_t p1_g1 =
{
"Francesco",
10,
5
};
salva(p1_g1, "partita.dat");
return 0;
}
26 changes: 26 additions & 0 deletions esercitazioni/test_matrici.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
#include <iostream>

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<dim; i++) {
d_princ += m[i][i];
d_sec += m[i][dim-i-1];
// cout << i << " " << dim-i-1 << endl;
}
cout << d_princ << " " << d_sec << endl;
return d_princ-d_sec;
}

int main() {
const int m[][DIM]= {{1, 2, 3, 4},
{2, 3, 4, 5},
{2, 3, 4, 5},
{2, 3, 4, 5},
};
cout << calcola_diff_diag(m, DIM);
}
10 changes: 10 additions & 0 deletions esercitazioni/test_stringa.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
#include <iostream>

using namespace std;

int main() {
char stringa[] = "parolamoltolungaallocamemoria";
cout << "stringa contiene: " << stringa << endl;
cout << "Inerisci una parola: "; cin >> stringa;
cout << "Hai inserito \""<<stringa<<"\" come parola" << endl;
}

0 comments on commit 19dcf0f

Please sign in to comment.