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
a662ca6
commit 9c6ed65
Showing
4 changed files
with
153 additions
and
71 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 |
---|---|---|
@@ -1,2 +1,2 @@ | ||
esempi/*.out | ||
esercutazioni/*.out | ||
|
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
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; | ||
} |