-
Notifications
You must be signed in to change notification settings - Fork 55
/
main.cpp
153 lines (132 loc) · 5.21 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
#include "src/tensor.hxx"
#include <iostream>
#include <fstream>
using namespace std;
typedef int TEST_TYPE;
void readTensor(Tensor<TEST_TYPE> &tensor, istream &file){
if (tensor.dimension() == 1){
TEST_TYPE tmp;
for (ARRAY_SIZE itr = 0; itr < tensor.shape()[0] && file >> tmp; ++itr){
tensor[itr] = Tensor<TEST_TYPE >(tmp);
}
} else {
for (ARRAY_SIZE itr = 0; itr < tensor.shape()[0]; ++itr){
readTensor(tensor[itr], file);
}
}
if (file.fail() && !file.eof()) throw domain_error("Couldn't read information from file");
}
Tensor<TEST_TYPE > &loadFromFile(istream &file){
ARRAY_SIZE dim;
if (file >> dim && dim > 0){
auto *shape = new ARRAY_SIZE[dim];
for (ARRAY_SIZE itr = 0; itr < dim && file >> shape[itr]; ++itr);
if (file.fail() && !file.eof()) throw domain_error("Couldn't read information from file");
auto res = new Tensor<TEST_TYPE >(dim, shape);
readTensor(*res, file);
return *res;
} else {
throw domain_error("Bad file");
}
}
int main() {
char mod;
ofstream outFile("output.txt", ios::out);
bool flag = true;
if (!outFile.is_open()){
cout << "Could not open the output file, output will be written only in console." << endl;
flag = false;
}
cout << "Do you want to input arrays from file?(y/n)\n--> ";
cin >> mod;
auto *testTensor1_p = new Tensor<TEST_TYPE >();
auto *testTensor2_p = new Tensor<TEST_TYPE >();
if (mod == 'y' || mod == 'Y'){
ifstream inputFile("../input.txt", ios::in); // read from file
if (!inputFile.is_open()){
cout << "Could not open the file";
return 0;
}
testTensor1_p = &loadFromFile(inputFile);
testTensor2_p = &loadFromFile(inputFile);
cout << "tensor1:" << endl;
testTensor1_p->print();
cout << "tensor2:" << endl;
testTensor2_p->print();
if (flag){
outFile << "tensor1:" << endl;
testTensor1_p->write(outFile);
outFile << "tensor2:" << endl;
testTensor2_p->write(outFile);
}
} else {
ARRAY_SIZE dim;
cout << "Please, enter 2 tensors with same shapes." << endl; // read from console
cout << "Tensor1" << endl << "Dim:\n--> ";
cin >> dim;
auto shape = new ARRAY_SIZE[dim];
ARRAY_SIZE amount = 1;
for (ARRAY_SIZE itr = 0; itr < dim; ++itr){
cout << endl << "shape[" << itr << "]: ";
cin >> shape[itr];
amount *= shape[itr];
}
cout << endl << "Please, enter the " << amount << " numbers:" << endl;
auto tmp = Tensor<TEST_TYPE >(dim, shape);
testTensor1_p = &tmp;
readTensor(*testTensor1_p, cin);
cout << "Tensor2" << endl << "Dim:\n--> ";
cin >> dim;
auto shape2 = new ARRAY_SIZE[dim];
ARRAY_SIZE amount2 = 1;
for (ARRAY_SIZE itr = 0; itr < dim; ++itr){
cout << endl << "shape[" << itr << "]: ";
cin >> shape2[itr];
amount2 *= shape2[itr];
}
cout << endl << "Please, enter the " << amount2 << " numbers:" << endl;
auto tmp2 = Tensor<TEST_TYPE >(dim, shape);
testTensor2_p = &tmp2;
readTensor(*testTensor2_p, cin);
}
auto testTensor1 = *testTensor1_p;
auto testTensor2 = *testTensor2_p;
while (true){
int workMod;
cout << "Please, enter what do you want to do with tensors:" << endl;
cout << "1 - do operation between the both of tensors" << endl;
cout << "2 - do operation between the 1-st tensor and number" << endl;
cout << "3 - do operation between the 2-nd tensor and number" << endl;
cout << "0 - exit" << endl << "--> ";
cin >> workMod;
if (workMod == 1){
cout << "Please, enter the operation (+, -, *, /)" << endl << "--> ";
cin >> mod;
if (mod == '+') (testTensor1 + testTensor2).print();
else if (mod == '-') (testTensor1 - testTensor2).print();
else if (mod == '*') (testTensor1 * testTensor2).print();
else (testTensor1 / testTensor2).print();
} else if (workMod == 2){
cout << "Please, enter the operation (+, -, *, /)" << endl << "--> ";
cin >> mod;
cout << "Please, enter the number" << endl << "--> ";
TEST_TYPE number;
cin >> number;
if (mod == '+') (testTensor1 + number).print();
else if (mod == '-') (testTensor1 - number).print();
else if (mod == '*') (testTensor1 * number).print();
else (testTensor1 / number).print();
} else if (workMod == 3){
cout << "Please, enter the operation (+, -, *, /)" << endl << "--> ";
cin >> mod;
cout << "Please, enter the number" << endl << "--> ";
TEST_TYPE number;
cin >> number;
if (mod == '+') (testTensor2 + number).print();
else if (mod == '-') (testTensor2 - number).print();
else if (mod == '*') (testTensor2 * number).print();
else (testTensor2 / number).print();
} else
break;
}
}