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.
- Loading branch information
1 parent
94debc1
commit cf641ae
Showing
4 changed files
with
234 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,53 @@ | ||
#include <iostream> | ||
|
||
using namespace std; | ||
|
||
enum Ordinamento {CRESCENTE, DECRESCENTE}; | ||
|
||
void stampa_vettore(int v[], int dim) { | ||
for (int i=0; i<dim; i++) { | ||
cout << v[i] << " "; | ||
} | ||
cout << endl; | ||
} | ||
|
||
void ordina(int v[], int dim, Ordinamento ord) { | ||
int s_index; | ||
// insertion sort | ||
for (int i=0; i<dim-1; i++) { | ||
s_index = i; | ||
for (int j=i+1; j<dim; j++) { | ||
if (ord == CRESCENTE && v[j] > v[s_index]) | ||
s_index = j; | ||
else if (ord == DECRESCENTE && v[j] < v[s_index]) | ||
s_index = j; | ||
} | ||
int aux = v[s_index]; | ||
v[s_index] = v[i]; | ||
v[i] = aux; | ||
} | ||
} | ||
|
||
int main() { | ||
const short int DIM = 10; | ||
int v[] = {100, 222, 3, 41, 54, 6, 7, 8, 9, 10}; | ||
|
||
stampa_vettore(v, DIM); | ||
|
||
// ricerca massimo e minimo | ||
int max, min; | ||
max = min = v[0]; | ||
for (int i=1; i<DIM; i++) { | ||
v[i] > max ? max = v[i] : 0; | ||
v[i] < min ? min = v[i] : 0; | ||
} | ||
cout << "Min: " << min << " Max: " << max << endl; | ||
|
||
ordina(v, DIM, CRESCENTE); | ||
stampa_vettore(v, DIM); | ||
|
||
ordina(v, DIM, DECRESCENTE); | ||
stampa_vettore(v, DIM); | ||
|
||
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,5 @@ | ||
#include <iostream> | ||
|
||
using namespace std; | ||
|
||
|
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,150 @@ | ||
/* | ||
* file: contenitore_no_struct.cpp | ||
* author: francesco mecatti | ||
* | ||
* Seconda simulazione prova programmazione 1 | ||
* | ||
*/ | ||
#include <iostream> | ||
|
||
using namespace std; | ||
|
||
const char MIN_CHAR = 'a'; | ||
const char MAX_CHAR = 'z'; | ||
|
||
enum errore {OK = 0, ELE_NOT_VALID, NOT_ENOUGH_SPACE, TOO_SMALL_CAP, NUM_ERR}; | ||
const char error_msgs[NUM_ERR][50] = {"", "Elemento non valido", "Spazio finito", "Capienza troppo piccola"}; | ||
|
||
/* | ||
* stampa l'error message ed esce con errore | ||
* @param msg: string, esce con errore | ||
*/ | ||
void gestisci_errore(errore e) { | ||
cerr << error_msgs[e] << endl; | ||
// exit(1); | ||
} | ||
|
||
/* | ||
* inizializza tutti i contatori a 0 e ritorna | ||
* @param v: vettore | ||
* @param dim: dimensione del vettore (numero lettere minuscole) | ||
* @return: void | ||
*/ | ||
void inizializza(int v[], int dim) { | ||
for (int i=0; i<dim; i++) | ||
v[i] = 0; | ||
} | ||
|
||
/* | ||
* incrementa il contatore di v[ele] e aggiorna il numero di elementi | ||
* @param num_ele: numero di elementi prsenti nel contenitore | ||
* @param ele: elemento da inserire | ||
* @return: tipo di errore se occurred, OK altrimenti | ||
*/ | ||
errore inserisci(char ele, int v[], int &num_ele, int capienza) { | ||
if (ele<MIN_CHAR || ele>MAX_CHAR) | ||
return ELE_NOT_VALID; | ||
// cout << "valid" << endl; | ||
|
||
if (num_ele >= capienza) | ||
return NOT_ENOUGH_SPACE; | ||
// cout << "space" << endl; | ||
|
||
// cout << v[ele-MIN_CHAR] << endl; | ||
v[ele-MIN_CHAR]++; | ||
num_ele++; | ||
return OK; | ||
} // se esce senza errori ritorna 0 => OK | ||
|
||
errore estrai(char ele, int v[], int &num_ele) { | ||
if (ele<MIN_CHAR || ele>MAX_CHAR) | ||
return ELE_NOT_VALID; | ||
|
||
num_ele -= v[ele-MIN_CHAR]; | ||
v[ele-MIN_CHAR] = 0; | ||
return OK; | ||
} // se esce senza errori ritorna 0 => OK | ||
|
||
errore modifica_capienza(int nuova_capienza, int num_ele, int &capienza) { | ||
if (nuova_capienza < num_ele) | ||
return TOO_SMALL_CAP; | ||
|
||
capienza = nuova_capienza; | ||
return OK; | ||
} | ||
|
||
void stampa(int v[], int dim) { | ||
cout << "<"; | ||
for (int i=0; i<dim; i++) { | ||
if (v[i] != 0) | ||
cout << char(i+MIN_CHAR) << ":" << v[i] << ", "; | ||
} | ||
cout << ">" << endl; | ||
} | ||
|
||
int main() { | ||
const char menu[] = | ||
"i - inserisci elemento\n" | ||
"e - estrai elementi\n" | ||
"m - modifica capienza\n" | ||
"c - stampa capienza\n" | ||
"q - stampa numero elementi\n" | ||
"s - stampa vettore\n" | ||
"t - termina programma\n"; | ||
|
||
|
||
// struttura dati | ||
// cout << int(MAX_CHAR) - int(MIN_CHAR) + 1; | ||
const int DIM = int(MAX_CHAR)-int(MIN_CHAR)+1; | ||
int capienza = 0; // numero totale di elementi | ||
int dimensione_attuale = 0; | ||
int contenitore[DIM]; | ||
errore err; | ||
|
||
// inizializzazione | ||
cout << "Capienza: "; cin >> capienza; | ||
inizializza(contenitore, DIM); | ||
|
||
char ch; // scelta utente | ||
char ele; int aux; | ||
while (true) { | ||
cout << menu << endl; | ||
cout << ">> "; cin >> ch; | ||
switch (ch) { | ||
case 'i': | ||
cout << "Nome elemento: "; cin >> ele; | ||
err = inserisci(ele, contenitore, dimensione_attuale, capienza); | ||
if (err != OK) { | ||
cout << err << endl; | ||
gestisci_errore(err); | ||
} | ||
break; | ||
case 'e': | ||
cout << "Nome elemento: "; cin >> ele; | ||
err = estrai(ele, contenitore, dimensione_attuale); | ||
if (err != OK) | ||
gestisci_errore(err); | ||
break; | ||
case 'm': | ||
cout << "Nuova capienza: "; cin >> aux; | ||
err = modifica_capienza(aux, dimensione_attuale, capienza); | ||
if (err != OK) | ||
gestisci_errore(err); | ||
break; | ||
case 'c': | ||
cout << "Capienza: " << capienza << endl; | ||
break; | ||
case 'q': | ||
cout << "Quanti elementi sono nel vettore? " << dimensione_attuale << endl; | ||
break; | ||
case 's': | ||
stampa(contenitore, DIM); | ||
break; | ||
case 't': | ||
return 0; | ||
break; | ||
default: | ||
cout << "Scelta errata" << endl; | ||
} | ||
} | ||
} |
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; | ||
|
||
void stampa_occorrenze (int v[], int dim) { | ||
int c; | ||
for (int i=0; i<dim; i++) { | ||
c = 0; | ||
if (v[i] > 0) { | ||
c++; | ||
for (int j=i+1; j<dim; j++) { | ||
if (v[j] == v[i]) { | ||
c++; | ||
v[j] = 0; | ||
} | ||
} | ||
cout << "Valore " << v[i] << " presente " << c << " volte" << endl; | ||
} | ||
} | ||
} | ||
|
||
int main() { | ||
int a[12] = {9, 8, 1, 1, 1, 2, 3, 4, 2, 3, 3, 9}; | ||
stampa_occorrenze(a, 12); | ||
return 0; | ||
} |