-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMenu_de_Matrices.cpp
More file actions
206 lines (180 loc) · 6.7 KB
/
Menu_de_Matrices.cpp
File metadata and controls
206 lines (180 loc) · 6.7 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
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
#include <iostream>
#include <vector>
#include <limits>
using namespace std;
typedef vector<vector<int>> Matrix;
void printMatrix(const Matrix& matrix) {
for (const auto& row : matrix) {
for (const auto& elem : row) {
cout << elem << " ";
}
cout << endl;
}
}
Matrix transpose(const Matrix& matrix) {
Matrix trans(matrix[0].size(), vector<int>(matrix.size()));
for (size_t i = 0; i < matrix.size(); ++i) {
for (size_t j = 0; j < matrix[0].size(); ++j) {
trans[j][i] = matrix[i][j];
}
}
return trans;
}
bool canMultiply(const Matrix& mat1, const Matrix& mat2) {
return mat1[0].size() == mat2.size();
}
Matrix matMult(const Matrix& mat1, const Matrix& mat2) {
int rows1 = mat1.size();
int cols1 = mat1[0].size();
int cols2 = mat2[0].size();
Matrix result(rows1, vector<int>(cols2, 0));
for (int i = 0; i < rows1; ++i) {
for (int j = 0; j < cols2; ++j) {
for (int k = 0; k < cols1; ++k) {
result[i][j] += mat1[i][k] * mat2[k][j];
}
}
}
return result;
}
Matrix readMatrix(int rows, int cols) {
Matrix matrix(rows, vector<int>(cols));
for (int i = 0; i < rows; ++i) {
for (int j = 0; j < cols; ++j) {
cout << "Ingrese el elemento (" << i + 1 << ", " << j + 1 << "): ";
while (!(cin >> matrix[i][j])) {
cin.clear();
cin.ignore(numeric_limits<streamsize>::max(), '\n');
cout << "Entrada no valida. Introduce un numero valido para la posicion (" << i + 1 << ", " << j + 1 << "): ";
}
}
}
return matrix;
}
int main() {
int rows, cols;
cout << "Ingrese el numero de filas de la matriz A: ";
while (!(cin >> rows) || rows <= 0) {
cin.clear();
cin.ignore(numeric_limits<streamsize>::max(), '\n');
cout << "Entrada no valida. Introduce un numero de filas valido: ";
}
cout << "Ingrese el numero de columnas de la matriz A: ";
while (!(cin >> cols) || cols <= 0) {
cin.clear();
cin.ignore(numeric_limits<streamsize>::max(), '\n');
cout << "Entrada no valida. Introduce un numero de columnas valido: ";
}
Matrix A = readMatrix(rows, cols);
cout << "Ingrese el numero de filas de la matriz x: ";
while (!(cin >> rows) || rows <= 0) {
cin.clear();
cin.ignore(numeric_limits<streamsize>::max(), '\n');
cout << "Entrada no valida. Introduce un numero de filas valido: ";
}
cout << "Ingrese el numero de columnas de la matriz x: ";
while (!(cin >> cols) || cols <= 0) {
cin.clear();
cin.ignore(numeric_limits<streamsize>::max(), '\n');
cout << "Entrada no valida. Introduce un numero de columnas valido: ";
}
Matrix x = readMatrix(rows, cols);
int choice;
do {
cout << "Menu:\n";
cout << "1. Muestre las matrices originales ingresadas\n";
cout << "2. Escriba la transpuesta de la matriz original ingresada\n";
cout << "3. Calcule (Ax)^T\n";
cout << "4. Calcule x^TA^T\n";
cout << "5. Calcule xx^T\n";
cout << "6. Calcule x^Tx\n";
cout << "7. Salir\n";
cout << "Ingrese su eleccion: ";
while (!(cin >> choice) || choice < 1 || choice > 7) {
cin.clear();
cin.ignore(numeric_limits<streamsize>::max(), '\n');
cout << "Entrada no valida. Por favor, ingrese una opcion entre 1 y 7: ";
}
switch (choice) {
case 1:
cout << "Matriz A:\n";
printMatrix(A);
cout << "Matriz x:\n";
printMatrix(x);
break;
case 2: {
cout << "¿De que matriz desea obtener la transpuesta? (A/x): ";
char matChoice;
cin >> matChoice;
if (matChoice == 'A' || matChoice == 'a') {
printMatrix(transpose(A));
} else if (matChoice == 'x' || matChoice == 'X') {
printMatrix(transpose(x));
} else {
cout << "Opcion no valida." << endl;
}
break;
}
case 3: {
cout << "Paso 1: Calcule Ax\n";
Matrix Ax = matMult(A, x);
printMatrix(Ax);
cout << "Paso 2: Transponga el resultado para obtener (Ax)^T\n";
Matrix result = transpose(Ax);
printMatrix(result);
break;
}
case 4: {
cout << "Paso 1: Calcule A^T\n";
Matrix AT = transpose(A);
printMatrix(AT);
cout << "Paso 2: Calcule x^T\n";
Matrix xT = transpose(x);
printMatrix(xT);
cout << "Paso 3: Multiplique x^T y A^T para obtener x^TA^T\n";
Matrix result = matMult(xT, AT);
printMatrix(result);
break;
}
case 5: {
cout << "Paso 1: Calcule x^T\n";
Matrix xT = transpose(x);
printMatrix(xT);
cout << "Paso 2: Multiplique x y x^T para obtener xx^T\n";
Matrix result = matMult(x, xT);
printMatrix(result);
break;
}
case 6: {
cout << "Paso 1: Calcule x^T\n";
Matrix xT = transpose(x);
printMatrix(xT);
cout << "Paso 2: Multiplique x^T y x para obtener x^Tx\n";
Matrix result = matMult(xT, x);
printMatrix(result);
break;
}
case 7:
cout << "Saliendo...\n";
break;
default:
cout << "Opcion no valida." << endl;
}
if (choice != 7) {
char continueChoice;
do {
cout << "¿Desea volver al menu? (s/n): ";
cin >> continueChoice;
if (continueChoice == 's' || continueChoice == 'S') {
system("CLS"); // Usa "clear" si estas en Linux/Unix
} else if (continueChoice == 'n' || continueChoice == 'N') {
choice = 7;
cout << "Saliendo...\n";
} else {
cout << "Opcion no valida. Por favor, ingrese 's' para si o 'n' para no.\n";
}
} while (continueChoice != 's' && continueChoice != 'S' && continueChoice != 'n' && continueChoice != 'N');
}
} while (choice != 7);
return 0;
}