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
5ab2073
commit e9f9e3d
Showing
5 changed files
with
206 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,48 @@ | ||
# Notazione posizionale | ||
## Rappresentazione binaria | ||
L'insieme dei possibili valori utilizzati da un calcolatore è {0, 1}; quindi la base è 2. | ||
|
||
Numero: sequenza di cifre. | ||
|
||
Cifra: simbolo che rappresenta un singolo numero. | ||
|
||
Il valore della base-1 è il massimo valore assumibile da una cifra. Quindi la base rappresenta il numero di possibili cifre. | ||
|
||
Notazione parentesi quadre, con cui si indica la base del numero, posto al pedice della parentesi. | ||
|
||
## Base esadecimale | ||
Base = 16. | ||
|
||
Ogni cifra in base 16 rappresenta una possibile combinazione di 4 cifre in base 2. | ||
|
||
Le cifre della base 10 vengono estese da A, B, C, D, E, F. | ||
|
||
## Algoritmo di conversione | ||
Il passaggio da una base all'altra è sempre possibile attraverso un algoritmo di conversione. | ||
|
||
Prendiamo un numero natuale N memorizzato in memoria (non importa dove). L'elaboratore è in grado di eseguire calcoli su questo numero. | ||
Data una qualsiasi base _b_, il valore dela cifra i-esima è dato da *(N/b^i)%b* | ||
|
||
Ogni C++ literal è interpretato come un numero in base 10. | ||
|
||
## Shifting | ||
n\*2^i -> shifta a sx tutte le cifre del numero | ||
n/2^i -> shifta a dx tutte le cifre del numero | ||
|
||
TODO: link | ||
|
||
## Complemento a 2 | ||
=> metodo per rappresentare i numeri negativi sulla macchina. | ||
|
||
La prima soluzione potrebbe essere dedicare 1 bit per segnalare la presenza di segno positivo o negativo. Si sprecherebbe una configurazione (il valore zero resta invariato indipendentemente dal segno) | ||
|
||
Quindi: utilizzo il primo bit per segnalare se il numero è negativo o positivo. Se il numero è negativo sommo una costante (2^N con N numero di bit disponibili). | ||
|
||
I nuovi range diventano: | ||
- [0, 2^(N-1)-1] | ||
- [-2(N-1)] | ||
|
||
Vantaggi: | ||
- una sola rappresentazione per lo zero: tutti i bit a 0 | ||
|
||
Gli interi in C++ sono rappresentati in complemento a 2. |
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,98 @@ | ||
#include <iostream> | ||
|
||
using namespace std; | ||
|
||
int main() { | ||
int scelta, // scelta del menù | ||
num_anelli_ferro = 0, | ||
num_anelli_rame = 0, | ||
pos1_rame, // prima posizione anello rame. 0 non presente. | ||
pos2_rame, // seconda posizione anello rame. 0 non presente. | ||
pos, // posizione inserimento o rimozione | ||
tipo; // tipo di anello: 1 - ferro, 2 - rame | ||
|
||
bool ok = true; // flag che segnala se l'operazione è andata a buon fine | ||
while (true) { | ||
cout << "1. Inserimento\n" | ||
<< "2. Estrazione\n" | ||
<< "3. Stampa\n" | ||
<< "4. Terminazione\n" | ||
<< "Scelta: "; | ||
|
||
cin >> scelta; | ||
|
||
switch(scelta) { | ||
case 1: | ||
cout << "Tipo anello (1-Ferro, 2-Rame): "; cin >> tipo; | ||
cout << "Posizione: "; cin >> pos; | ||
if (pos>=1 && pos<=num_anelli_rame+num_anelli_ferro+1) { // Range check | ||
if (tipo == 2 && num_anelli_rame < 2) { // check num max rame | ||
num_anelli_rame++; | ||
if (pos1_rame == 0) | ||
pos1_rame = pos; | ||
else if (pos2_rame == 0) | ||
pos2_rame = pos; | ||
} | ||
else if (tipo == 1) { | ||
num_anelli_ferro++; | ||
} | ||
else { | ||
ok = false; | ||
break; | ||
} | ||
if (pos <= pos1_rame && !(tipo==2 && num_anelli_rame==1)) | ||
pos1_rame++; | ||
if (pos <= pos2_rame && !(tipo==2 && num_anelli_rame==2)) | ||
pos2_rame++; | ||
} | ||
else { | ||
ok = false; | ||
} | ||
break; | ||
case 2: | ||
cout << "Posizione: "; cin >> pos; | ||
if (pos>=1 && pos<=num_anelli_rame+num_anelli_ferro) { // Range check | ||
// Rimuovo rame se presente, altrimenti ferro | ||
if (pos == pos1_rame && pos1_rame != 0) { | ||
num_anelli_rame--; | ||
pos1_rame = 0; | ||
} | ||
else if (pos == pos2_rame && pos2_rame != 0) { | ||
num_anelli_rame--; | ||
pos2_rame = 0; | ||
} | ||
else { | ||
num_anelli_ferro--; | ||
} | ||
|
||
// Scalo | ||
if (pos <= pos1_rame) | ||
pos1_rame--; | ||
if (pos <= pos2_rame) | ||
pos2_rame--; | ||
} | ||
else { | ||
ok = false; | ||
} | ||
break; | ||
case 3: | ||
for (int i=1; i<=num_anelli_ferro+num_anelli_rame; i++) { | ||
if ((i == pos1_rame && pos1_rame != 0) || (i == pos2_rame && pos2_rame != 0)) | ||
cout << "R"; | ||
else | ||
cout << "F"; | ||
} | ||
cout << endl; | ||
break; | ||
case 4: | ||
return 0; | ||
default: | ||
cout << "Scelta non valida" << endl; | ||
} | ||
if (!ok) | ||
cout << "Operazione non consentita" << endl; | ||
ok = true; | ||
} | ||
|
||
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,42 @@ | ||
#include <iostream> | ||
|
||
using namespace std; | ||
|
||
int main() { | ||
int scelta, num_anelli = 0, j; | ||
while (true) { | ||
cout << "1. Inserimento\n" | ||
<< "2. Estrazione\n" | ||
<< "3. Stampa\n" | ||
<< "4. Terminazione\n" | ||
<< "Scelta: "; | ||
|
||
cin >> scelta; | ||
|
||
switch(scelta) { | ||
case 1: | ||
cout << "Posizione: "; | ||
cin >> j; | ||
if (j>=1 && j<=num_anelli+1) | ||
num_anelli++; | ||
break; | ||
case 2: | ||
cout << "Posizione: "; | ||
cin >> j; | ||
if (j>=1 && j<=num_anelli) | ||
num_anelli--; | ||
break; | ||
case 3: | ||
for (int i=0; i<num_anelli; i++) { | ||
cout << "F"; | ||
} | ||
cout << endl; | ||
break; | ||
case 4: | ||
return 0; | ||
default: | ||
cout << "Non valido" << endl; | ||
} | ||
} | ||
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,18 @@ | ||
#include <iostream> | ||
|
||
using namespace std; | ||
|
||
|
||
int main() { | ||
int n, | ||
b = 1; // base | ||
const int base_originale = 2; | ||
cout << "Numero intero positivo: "; | ||
cin >> n; | ||
while (b < n) { | ||
cout << (n/b)%base_originale; | ||
b *= base_originale; | ||
} | ||
cout << endl; | ||
return 0; | ||
} |