-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.cpp
240 lines (206 loc) · 7.64 KB
/
main.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
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
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
#include <iostream>
using namespace std;
//variabile per la lunghezza dei vettori (matrici)
#define DIMENSIONE 3
//funzione per il menu
int menu(){
int selezione;
cout << "-----------Menu principale-----------" << endl;
cout << "|->[1] Moltiplicazione (matrici 3x3)" << endl;
cout << "|->[2] Addizione (matrici 3x3)" << endl;
cout << "|->[3] Sottrazione (matrici 3x3)" << endl;
cout << "|->[4] Trasversale (matrici 3x3)" << endl;
cout << "|->[5] Determinante (matrici 3x3)" << endl;
cout << "|->[0] Esci dal programma" << endl;
cout << ">>>Selezione: ";
cin >> selezione;
return selezione;
} //-123456
/* stampa spazi bianchi per la grafica delle matrici.
* Dopo la prima cifra del numero ci si sposta di 5 spazi per il carattere che divide i due numeri
*/
void spaces(float number){
// NUMERI POSITIVI
if (number >= 0) {
// qui sotto: numeri ad una cifra (0;9)
if ((number < 10) && (number >= 0)) {
for (int i = 0; i < 5; i++) {
cout << " ";
} // qui sotto: numeri a due cifre (10;99)
} else if ((number < 100) && (number >= 10)) {
for (int i = 0; i < 4; i++) {
cout << " ";
} // qui sotto: numeri a tre cifre (100;999)
} else if ((number < 1000) && (number >= 100)){
for (int i = 0; i < 3; i++) {
cout << " ";
}
}
} else {
// qui sotto: numeri ad una cifra (0;9)
if ((number > -10) && (number < 0)) {
for (int i = 0; i < 4; i++) {
cout << " ";
} // qui sotto: numeri a due cifre (10;99)
} else if ((number > -100) && (number <= 10)) {
for (int i = 0; i < 3; i++) {
cout << " ";
} // qui sotto: numeri a tre cifre (100;999)
} else if ((number > 1000) && (number <= 100)){
for (int i = 0; i < 2; i++) {
cout << " ";
}
}
}
}
// addizione tra due matrici
void addizioneMatrici(float matrice1[DIMENSIONE][DIMENSIONE], float matrice2[DIMENSIONE][DIMENSIONE]){
float s;
for (int i = 0; i < DIMENSIONE; i++) {
for (int j = 0; j < DIMENSIONE; j++) {
cout << "| ";
cout << matrice1[i][j] + matrice2[i][j];
s = matrice1[i][j] + matrice2[i][j];
spaces(s);
}
cout << "|" << endl;
}
}
// addizione tra due matrici (matrici 3x3)
void sottrazioneMatrici(float matrice1[DIMENSIONE][DIMENSIONE], float matrice2[DIMENSIONE][DIMENSIONE]){
float s;
for (int i = 0; i < DIMENSIONE; i++) {
for (int j = 0; j < DIMENSIONE; j++) {
cout << "| ";
cout << matrice1[i][j] + (-(matrice2[i][j]));
s = matrice1[i][j] + (-(matrice2[i][j]));
spaces(s);
}
cout << "|" << endl;
}
}
// matrice trasversale (matrice 3x3) (trasforma le colonne in righe)
void matriceTrasversale(float matrice[DIMENSIONE][DIMENSIONE]){
for (int i = 0; i < DIMENSIONE; i++) {
for (int j = 0; j < DIMENSIONE; j++) {
cout << "| ";
cout << matrice[j][i];
spaces(matrice[j][i]);
}
cout << "|" << endl;
}
}
// calcolo del determinante di una matrice
void determinanteMatrice(float matrice[DIMENSIONE][DIMENSIONE]){
int diaForward = 1, diaBack = 1;
float determinante = 0;
for (int i=0; i<DIMENSIONE; i++)
{
for (int j=0; j<DIMENSIONE; j++)
{
diaBack *= matrice[j][j+i<DIMENSIONE ? j+i : j+i-DIMENSIONE];
diaForward *= matrice[j][DIMENSIONE-j-1-i>=0 ? DIMENSIONE-j-1-i : 2*DIMENSIONE-j-i-1];
}
determinante += diaBack-diaForward;
diaForward = diaBack = 1;
}
cout << "Determinante della matrice: " << determinante << endl << endl;
}
// moltiplicazione tra due matrici
void moltiplicaMatrici(float matrice1[DIMENSIONE][DIMENSIONE], float matrice2[DIMENSIONE][DIMENSIONE]){
float matriceMoltiplicazione[DIMENSIONE][DIMENSIONE] = {{0, 0, 0}, {0, 0, 0}, {0, 0, 0}};
for (int i = 0; i < DIMENSIONE; i++) {
for (int j = 0; j < DIMENSIONE; j++) {
for (int k = 0; k < DIMENSIONE; k++) {
matriceMoltiplicazione[i][j] += matrice1[i][k] * matrice2[k][j];
}
}
}
for (int i = 0; i < DIMENSIONE; i++) {
for (int j = 0; j < DIMENSIONE; j++) {
cout << "|";
cout << matriceMoltiplicazione[i][j];
spaces(matriceMoltiplicazione[i][j]);
}
cout << "|" << endl;
}
}
// funzione principale del programma
int main() {
float matrice1[DIMENSIONE][DIMENSIONE];
float matrice2[DIMENSIONE][DIMENSIONE];
int selezione, elemento;
// acquisizione della prima matrice
cout << "Inserisci la prima matrice seguendo questo ordine: " << endl;
cout << "1 | 2 | 3" << endl;
cout << "4 | 5 | 6" << endl;
cout << "7 | 8 | 9" << endl;
elemento = 1;
for(int i = 0; i < DIMENSIONE; i++){
for (int j = 0; j < DIMENSIONE; j++) {
cout << "Inserisci elemento [" << elemento << "] della prima matrice: ";
cin >> matrice1[i][j];
elemento++;
}
}
// acquisizione della seconda matrice
cout << "Inserisci la seconda matrice seguendo questo ordine: " << endl;
cout << "1 | 2 | 3" << endl;
cout << "4 | 5 | 6" << endl;
cout << "7 | 8 | 9" << endl;
elemento = 1;
for(int i = 0; i < DIMENSIONE; i++){
for (int j = 0; j < DIMENSIONE; j++) {
cout << "Inserisci elemento [" << elemento << "] della seconda matrice: ";
cin >> matrice2[i][j];
elemento++;
}
}
// gestione del menu e della funzione scelta dall'utente
do{
selezione = menu();
switch(selezione){
default:
cout << "Selezione non valida, riprova..." << endl << endl;
break;
case 0:
cout << "Termino il programma..." << endl << endl;
break;
case 1:
cout << "Moltiplicazione tra le due matrici (prima * seconda): " << endl << endl;
moltiplicaMatrici(matrice1, matrice2);
break;
case 2:
cout << "Addizione tra le due matrici: " << endl << endl;
addizioneMatrici(matrice1, matrice2);
break;
case 3:
cout << "Sottrazione tra le due matrici: " << endl << endl;
sottrazioneMatrici(matrice1, matrice2);
break;
case 4:
cout << "Trasversale di una matrice. Di quale matrice eseguire il trasversale? (1 o 2): ";
int sceltaMatriceTrasv;
cin >> sceltaMatriceTrasv;
if (sceltaMatriceTrasv == 1)
matriceTrasversale(matrice1);
else if (sceltaMatriceTrasv == 2)
matriceTrasversale(matrice2);
else
cout << "Matrice non valida. Torno al menu principale..." << endl << endl;
break;
case 5:
cout << "Determinante di una matrice. Di quale matrice eseguire il trasversale? (1 o 2): ";
int sceltaMatriceDet;
cin >> sceltaMatriceDet;
if (sceltaMatriceDet == 1)
determinanteMatrice(matrice1);
else if (sceltaMatriceDet == 2)
determinanteMatrice(matrice2);
else
cout << "Matrice non valida. Torno al menu principale..." << endl << endl;
break;
}
}while(selezione!=0);
return 0;
}