-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfunzioni.cpp
More file actions
160 lines (147 loc) · 3.68 KB
/
funzioni.cpp
File metadata and controls
160 lines (147 loc) · 3.68 KB
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
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
#include "funzioni.h"
Sezioni stoe(const string str) {
switch (str[0]) {
case 'a':
case 'A': {
return Sezioni::A;
break;
}
case 'b':
case 'B': {
return Sezioni::B;
break;
}
case 'c':
case 'C': {
return Sezioni::C;
break;
}
case 'd':
case 'D': {
return Sezioni::D;
break;
}
case 'e':
case 'E': {
return Sezioni::E;
break;
}
default: {
cout << "Input non valido" << endl;
throw BambinoNonValido("Sezione errata");
}
}
}
void stampaBambini(const Bambino b[]) {
for (int i = 0; i < Bambino::nBambini; ++i) {
cout << "[ " << endl;
cout << "\tNome: " << b[i].getNome() << endl
<< "\tCognome: " << b[i].getCognome() << endl
<< "\tCitta: " << b[i].getCitta() << endl
<< "\tSezione: " << b[i].getSezione() << endl
<< "\tEta: " << b[i].getEta() << endl;
cout << "[ " << endl;
}
}
void stampaBambino(const int INDEX, const Bambino b[]) {
cout << "[ " << endl;
cout << "\tNome: " << b[INDEX].getNome() << endl
<< "\tCognome: " << b[INDEX].getCognome() << endl
<< "\tCitta: " << b[INDEX].getCitta() << endl
<< "\tSezione: " << b[INDEX].getSezione() << endl
<< "\tEta: " << b[INDEX].getEta() << endl;
cout << "[ " << endl;
}
string toupperString(string str) {
for (int i = 0; i < str.length(); ++i) {
toupper(str[i]);
}
return str;
}
int cercaBambino(string query, const Bambino b[]) {
for (int i = 0; i < Bambino::nBambini; ++i) {
if (toupperString(b[i].getCognome()) == toupperString(query) ||
toupperString(b[i].getNome()) == toupperString(query)) {
return i;
} else {
return -1;
}
}
return -1;
}
void cercaBambino(string query, const Bambino b[], int result[]) {
for (int i = 0; i < Bambino::nBambini; ++i) {
if (toupperString(b[i].getCitta()) == toupperString(query)) {
result[i] = i;
}
}
return;
}
int cercaBambino(Sezioni query, const Bambino b[]) {
for (int i = 0; i < Bambino::nBambini; ++i) {
if (b[i].getSezione() == query) {
return i;
} else {
return -1;
}
}
return -1;
}
int cercaBambino(int query, const Bambino b[], int result[]) {
for (int i = 0; i < Bambino::nBambini; ++i) {
if (b[i].getEta() == query) {
result[i] = i;
}
}
return -1;
}
string splitString(string str, const char DELIMITER, const int OFFSET) {
int pos = -1, posPre = 0;
for (int i = 0; i < OFFSET; ++i) {
posPre = pos;
pos = str.find(DELIMITER, pos + 1);
cout << "pos: " << pos << endl << "posPre: " << posPre << endl;
}
return str.substr(posPre + 1, pos - posPre - 1);
}
int importaBambini(ifstream &in, Bambino b[]) {
in.clear();
string line;
while (getline(in, line)) {
if (in.bad()) {
return -1;
} else if (in.fail()) {
return -2;
} else if (in.eof()) {
return 0;
}
string dati[3];
Sezioni s;
int e;
for (int i = 0; i < 5; ++i) {
dati[0] = splitString(line, ';', 0);
dati[1] = splitString(line, ';', 1);
dati[2] = splitString(line, ';', 2);
s = stoe(splitString(line, ';', 3));
e = stoi(splitString(line, ';', 4));
}
b[Bambino::nBambini](dati[0], dati[1], dati[2], s, e);
++Bambino::nBambini;
}
return 0;
}
int esportaBambini(ofstream &out, Bambino b[]) {
for (int i = 0; i < Bambino::nBambini; ++i) {
if (out.bad()) {
return -1;
} else if (out.fail()) {
return -2;
} else {
out << b[i].getNome() << ';' << b[i].getCognome() << ';'
<< b[i].getCitta() << ';' << b[i].getSezione() << ';' << b[i].getEta()
<< ';' << endl;
}
}
out.close();
return 0;
}