This repository has been archived by the owner on May 25, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
interface.cpp
330 lines (266 loc) · 6.42 KB
/
interface.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
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
#include <iostream>
#include <string>
#include <limits>
#include <boost/format.hpp>
#include "base.h"
#include "sequence.h"
#include "matrix.h"
enum Type {INT, COMPLEX};
struct TypedDiagonalMatrix{
Type type;
void *value;
DiagonalMatrix<int>* asInt() const {
return static_cast<DiagonalMatrix<int>*>(value);
}
DiagonalMatrix<complexf>* asComplex() const {
return static_cast<DiagonalMatrix<complexf>*>(value);
}
TypedDiagonalMatrix(Type type, int width, int height, int diag) : type(type){
if(type == INT)
value = new DiagonalMatrix<int>(width, height, diag);
else if(type == COMPLEX)
value = new DiagonalMatrix<complexf>(width, height, diag);
}
TypedDiagonalMatrix(Type type, void *value)
: type(type), value(value){}
~TypedDiagonalMatrix(){
if(type == INT)
delete asInt();
else if(type == COMPLEX)
delete asComplex();
}
int getWidth() const {
if(type == INT)
return asInt()->getWidth();
else if(type == COMPLEX)
return asComplex()->getWidth();
else
return -1;
}
int getHeight() const {
if(type == INT)
return asInt()->getHeight();
else if(type == COMPLEX)
return asComplex()->getHeight();
else
return -1;
}
int getDiag() const {
if(type == INT)
return asInt()->getDiag();
else if(type == COMPLEX)
return asComplex()->getDiag();
else
return -1;
}
void print() const {
for(int y = 0; y < this->getHeight(); y++){
for(int x = 0; x < this->getWidth(); x++){
if(this->type == INT){
std::cout << this->asInt()->get(x, y) << " ";
}
else if(this->type == COMPLEX){
std::cout << boost::format("%1$.1f%2$+.1fi\t") % this->asComplex()->get(x, y).real() % this->asComplex()->get(x, y).imag();
}
}
std::cout << "\n";
}
std::cout << "\n\n";
}
TypedDiagonalMatrix* operator+(const TypedDiagonalMatrix &matrix) const {
TypedDiagonalMatrix *newMatrix;
Type type;
void *value;
if(this->type == INT && matrix.type == INT){
type = INT;
value = *asInt() + *matrix.asInt();
}
else if(this->type == INT && matrix.type == COMPLEX){
type = COMPLEX;
value = *matrix.asComplex() + *asInt();
}
else if(this->type == COMPLEX && matrix.type == INT){
type = COMPLEX;
value = *asComplex() + *matrix.asInt();
}
else if(this->type == COMPLEX && matrix.type == COMPLEX){
type = COMPLEX;
value = *asComplex() + *matrix.asComplex();
}
newMatrix = new TypedDiagonalMatrix(type, value);
return newMatrix;
}
TypedDiagonalMatrix* operator*(float val) const {
TypedDiagonalMatrix *newMatrix;
Type type;
void *value;
if(this->type == INT){
type = INT;
value = *asInt() * val;
}
else if(this->type == COMPLEX){
type = COMPLEX;
value = *asComplex() * val;
}
newMatrix = new TypedDiagonalMatrix(type, value);
return newMatrix;
}
float getNorm(){
if(this->type == INT){
return this->asInt()->getNorm<float>();
}
else if(this->type == COMPLEX){
return this->asComplex()->getNorm<float>();
}
return -1;
}
};
void print(std::string str){
if(str.empty()) return;
if(str.back() == ':')
std::cout << str << "\n";
else
std::cout << str << "\n\n";
std::cout.flush();
}
int readInt(int from=std::numeric_limits<int>::min(), int to=std::numeric_limits<int>::max()){
while(true){
try{
std::string line;
std::getline(std::cin, line);
if(line.empty()) throw std::exception();
int read = std::stoi(line);
if(read < from || read > to) throw std::exception();
std::cout << "\n";
return read;
}catch(...){
print("please, enter a number from " + std::to_string(from) + " to " + std::to_string(to) + ":");
}
}
}
float readFloat(float from=std::numeric_limits<float>::lowest(), float to=std::numeric_limits<float>::max()){
while(true){
try{
std::string line;
std::getline(std::cin, line);
if(line.empty()) throw std::exception();
float read = std::stof(line);
if(read < from || read > to) throw std::exception();
std::cout << "\n";
return read;
}catch(...){
print("please, enter a float number from " + std::to_string(from) + " to " + std::to_string(to) + ":");
}
}
}
TypedDiagonalMatrix* readMatrix(){
print("1. integer\n2. complex");
int choice = readInt(1,2);
print("enter width:");
int width = readInt(1);
print("enter height:");
int height = readInt(1);
int diag = 0;
while(true){
print("enter diagonality:");
diag = readInt(1, (std::min(width, height) - 1) * 2 + 1);
if(diag % 2 != 1)
print("diagonality must be odd");
else
break;
}
if(choice == 1){
TypedDiagonalMatrix *matrix = new TypedDiagonalMatrix(INT, width, height, diag);
for(int y = 0; y < height; y++)
for(int x = 0; x < width; x++){
print("enter element (" + std::to_string(y+1) + "," + std::to_string(x+1) + "):");
int read = readInt();
matrix->asInt()->set(read, x, y);
}
return matrix;
}
else if(choice == 2){
TypedDiagonalMatrix *matrix = new TypedDiagonalMatrix(COMPLEX, width, height, diag);
for(int y = 0; y < height; y++)
for(int x = 0; x < width; x++){
print("enter element (" + std::to_string(y+1) + "," + std::to_string(x+1) + ") real part:");
float real = readFloat();
print("enter element (" + std::to_string(y+1) + "," + std::to_string(x+1) + ") imag part:");
float imag = readFloat();
complexf read(real, imag);
matrix->asComplex()->set(read, x, y);
}
return matrix;
}
return nullptr;
}
void printMenu(){
print("1. set A\n2. set B\n3. set c\n4. get A+B\n5. get c*A\n6. get norm of A\n7. exit");
}
void menu(){
TypedDiagonalMatrix *A = nullptr;
TypedDiagonalMatrix *B = nullptr;
float c = 0;
while(true){
printMenu();
int choice = readInt(1,7);
switch(choice){
case 1:
delete A;
A = readMatrix();
break;
case 2:
delete B;
B = readMatrix();
break;
case 3:
print("enter c:");
c = readFloat();
break;
case 4:
{
if(!A || !B){
print("A or B isn't set");
break;
}
TypedDiagonalMatrix *D = nullptr;
try{
D = *A + *B;
D->print();
}catch(...){
print("A and B have different size");
}
delete D;
break;
}
case 5:
{
if(!A){
print("A isn't set");
break;
}
TypedDiagonalMatrix *D = nullptr;
D = *A * c;
D->print();
delete D;
break;
}
case 6:
{
if(!A){
print("A isn't set");
break;
}
float norm = A->getNorm();
print(std::to_string(norm));
break;
}
case 7:
return;
}
}
}
int main(){
menu();
return 0;
}