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
/
Copy pathcatena_disomogenea.cpp
82 lines (75 loc) · 1.71 KB
/
catena_disomogenea.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
#include <iostream>
using namespace std;
int main() {
// Struttura dati
int n, // numero anelli
pos_1 = 0, // posizione rame 1. 0 se libera (fuori dal range)
pos_2 = 0; // posizione rame 2. 0 se libera (fuori dal range)
// Variabili di appoggio
int scelta, // scelta switch
materiale, // 1 ferro, 2 rame
pos, // posizione scelta per inserimento
error = false;
while (true) {
cout << "1. inserimento" << endl
<< "2. rimozione" << endl
<< "3. stampa" << endl
<< "4. exit" << endl;
cout << ">> "; cin >> scelta;
switch (scelta) {
case 1:
cout << "Ferro (1) o rame (2)? "; cin >> materiale;
cout << "Posizione? "; cin >> pos;
if (!(pos>=1 && pos<=n+1) || (materiale == 2 && pos_1!=0 && pos_2!=0)) {
error = true;
break; // esco se pos fuori dal range o inserimento rame ma posto finito
}
n++;
if (pos <= pos_1) // se inserisco un anello a sinistra di un rame shifto la sua posizione a destra
pos_1++;
if (pos <= pos_2)
pos_2++;
if (materiale == 2) {
if (pos_1 == 0)
pos_1 = pos;
else if (pos_2 == 0)
pos_2 = pos;
}
break;
case 2:
cout << "Posizione? "; cin >> pos;
if (!(pos>=1 && pos<=n)) {
error = true;
break;
}
n--;
if (pos == pos_1)
pos_1 = 0;
if (pos == pos_2)
pos_2 = 0;
if (pos < pos_1)
pos_1--;
if (pos < pos_2)
pos_2--;
break;
case 3:
for (int i = 1; i <= n; i++) {
if (i == pos_1 || i == pos_2)
cout << "R";
else
cout << "F";
}
cout << endl;
break;
case 4:
return 0;
default:
error = true;
break;
}
if (error)
cout << "Errore" << endl;
error = false;
}
return 0;
}