forked from D-F-D-P/ASUMathLabG09
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmath_lab.cpp
More file actions
173 lines (150 loc) · 4.26 KB
/
math_lab.cpp
File metadata and controls
173 lines (150 loc) · 4.26 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
#include "math_lab.h"
#define MATRIX array_matrix.get_matrix()
using namespace std;
//----------------------------------------------------------------------------------------------//trim spaces fun->public fun
string trim_spaces(string right_string)
{
if(right_string.find(' ')==-1)//recursion base case
{
return right_string;
}
string left_string="";
int i=right_string.find(' ')+1;
left_string=right_string.substr(0,i-1);
return left_string+trim_spaces(right_string.substr(i,right_string.length()-1));
}
//-----------------------------------------------------------------------------------------------// decode
void math_lab::decode(string operation)
{
bool print_=0;
if(operation[operation.length()-1]==';')
{
print_=1;
}
string first_operand,second_operand,third_operand;
int st,nd,th;
int operator1;
int x=0;
for(int i=0;i<operation.length();i++)
{char a=operation[i];
if(operation[i]=='=')
{
first_operand=operation.substr(0,i);
x=i+1;
}
if(operation[i]=='+'||operation[i]=='-'||operation[i]=='*'||operation[i]=='/'||operation[i]=='\'')
{
operator1=i;
second_operand=operation.substr(x,i-x);
if(print_==1)
{
third_operand=operation.substr(i+1,operation.length()-(i+2));
}
else
{
third_operand=operation.substr(i+1,operation.length()-(i+1));
}
break;
}
}
st= array_matrix.find_matrix(first_operand);
nd=array_matrix.find_matrix(second_operand);
th=array_matrix.find_matrix(third_operand);
// cout<<endl<<second_operand<<third_operand<<first_operand;
if(operation[operator1]=='+')
{
MATRIX[st]=MATRIX[nd]+MATRIX[th];
if(!print_)
{
MATRIX[st].print_matrix();
}
}
else if(operation[operator1]=='-')
{
MATRIX[st]=MATRIX[nd]-MATRIX[th];
if(!print_)
{
MATRIX[st].print_matrix();
}
}
else if(operation[operator1]=='*')
{
MATRIX[st]=MATRIX[nd]*MATRIX[th];
if(!print_)
{
MATRIX[st].print_matrix();
}
}
else if(operation[operator1]=='/')
{
MATRIX[st]=MATRIX[nd]/MATRIX[th];
if(!print_)
{
MATRIX[st].print_matrix();
}
}
else if(operation[operator1]=='\'')
{
MATRIX[st]=MATRIX[nd].inverse();
if(!print_)
{
MATRIX[st].print_matrix();
}
}
array_matrix.print();
return;
}
//-----------------------------------------------------------------------------------------------//load_file
void math_lab:: load_file(string file_path)
{
array_matrix.set_size(10);
std::ifstream infile;
string data_string;
string operation_string;
infile.open(file_path);
while(!infile.eof())
{
getline(infile,data_string);
if(data_string.find('[') != -1)
{
MATRIX[array_matrix.valid_size].fill_matrix(data_string);
array_matrix.valid_size++;
if (array_matrix.valid_size==array_matrix.get_size())
{
array_matrix.set_size(array_matrix.valid_size+5);
}
}
else
{
operation_string=trim_spaces(data_string);
//cout<<operation_string;
decode(operation_string);
}
}
return;
}
//----------------------------------------------------------------------------------------------------//open_commands
void math_lab::open_command()
{
string data_string;
string operation_string;
while(1)
{
getline(cin,data_string);
if(data_string.find('[') != -1)
{
MATRIX[array_matrix.valid_size].fill_matrix(data_string);
array_matrix.valid_size++;
if (array_matrix.valid_size==array_matrix.get_size())
{
array_matrix.set_size(array_matrix.valid_size+5);
}
}
else
{
operation_string=trim_spaces(data_string);
decode(operation_string);
}
}
return;
}