This repository has been archived by the owner on Jun 16, 2023. It is now read-only.
-
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
esperimenti stringhe, matrici, vettori dinamici, struct e file binari
- Loading branch information
1 parent
14789bf
commit 19dcf0f
Showing
8 changed files
with
173 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 not shown.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} |