-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.cpp
More file actions
283 lines (246 loc) · 7.91 KB
/
Copy pathmain.cpp
File metadata and controls
283 lines (246 loc) · 7.91 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
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
#include <iostream>
#include <cmath>
#define nl cout<<endl;
using namespace std;
/* */
/* TIPOS E ESTRUTURAS */
/* */
enum tipo {OPERANDO, OPERADOR, TRIGONOMETRICO, ABPARENTESE, FEPARENTESE, NADA};
union atom {double num; char oper;};
struct mol {atom val;tipo t;};
// typedef mol protein[4] :NÃO FUNCIONA //
// Então faça: //
// "Se não pode passar um elefante por baixo da porta" //
// "Coloque-o em um envelope e passe" //
struct protein{mol* tRNA;int posicao_O, posicao_I; int fim;};
/* */
/* PROTOTIPO DE */
/* SUB-ROTINAS */
/* */
void DimAloc(protein*, int); //Alocação dinamica do tamanho total da proteina
void R_RNA(string, protein*, int*); //Traduz a Expressão (M_RNA - string) para 'Proteinas' (-atomos)
double Calc(double, char, double); //Opera com 2 numeros
void ReducaoProtein(protein*, char, char, int); //Reduz a proteina calculando os caracteres adicionados
void VarreduraCrescente(protein*, char, char, int, int); //Segue ordem crescente de varredura
void VarreduraDecrescente(protein*, char, char, int, int); //Segue ordem decrescente de varredura
double CalcProteinCresc(protein*, int, int); //Calcula toda a Proteina de modo Crescente
double CalcProteinDecres(protein*, int, int); //Calcula toda a Proteina de modo Decrescente
void ShowProtein(protein*); //Mostra toda a proteina
int main(){
string mRNA;
protein proteina;
double resultado = 0;
int tamanhoM_RNA;
char operando;
int pos=0;
cout<<"Entre com a expressão";nl
cin>>mRNA;
tamanhoM_RNA = 2*mRNA.length();
DimAloc(&proteina, tamanhoM_RNA); //Aloca proteina
R_RNA(mRNA, &proteina, &pos); //Traduz Proteina
proteina.posicao_O = 0;
proteina.posicao_I = proteina.fim;
cout<<"Tamanho da proteina: "<<proteina.fim;nl
nl
cout<<"Antes: ";nl
cout<<"Expressao: "; ShowProtein(&proteina);cout<<" = ";nl //Mostra Proteina
cout<<"Variavel = "<<resultado;nl
resultado = CalcProteinCresc(&proteina, proteina.posicao_O, proteina.posicao_I); //Calcula Proteina
cout<<"Depois: ";nl
cout<<"Expressao: "; ShowProtein(&proteina);cout<<" = ";nl //Mostra Resultado final
cout<<"Resultado = "<<resultado;nl
delete[] proteina.tRNA; //Libera Proteina
return 0;
}
void DimAloc(protein* prot, int tamanho){
prot->tRNA = new mol[tamanho];
}
void R_RNA(string m_RNA, protein* prot, int* p){
double numb=0;
int expInt=1, expDec=0, fatExpDec=0;
//Varre toda a espressão mRNA
for(int j=0; j<50;j++){
if((*p)<m_RNA.length()){ //Condição de parada por tamanho
while(m_RNA[*p] == ' '){ //Retira todos os espaços em branco do M_RNA
(*p)++;
}
// Verifica e traduz Parenteses //
if((m_RNA[*p] == '(') || (m_RNA[*p] == ')')){
prot->tRNA[j].t = (m_RNA[*p] == '(') ? ABPARENTESE:FEPARENTESE;
prot->tRNA[j].val.oper = m_RNA[(*p)++];
}
// Verifica e traduz Operadores //
else if((m_RNA[*p]=='+')||(m_RNA[*p]=='-')||(m_RNA[*p]=='*')||(m_RNA[*p]=='/')||(m_RNA[*p]=='^')||(m_RNA[*p]=='l')){
prot->tRNA[j].t = OPERADOR;
prot->tRNA[j].val.oper = m_RNA[(*p)++];
}
// Verifica e traduz Reais //
else if( (('9'>=m_RNA[*p]) && (m_RNA[*p]>='0')) || (m_RNA[*p] == ',') || (m_RNA[*p] == '.')){
expInt=1, expDec=0, fatExpDec=0;
for(int k=(*p);k<m_RNA.length(); k++){
if(( '9' >= m_RNA[k]) && (m_RNA[k] >= '0')){
numb = numb*pow(10,expInt) + (m_RNA[k]-48)*pow(10,expDec*fatExpDec);
expDec--;
}
else if ((m_RNA[k] == ',') || (m_RNA[k] == '.')){
expInt=0;
fatExpDec=1;
expDec=-1;
} else {
(*p) = k;
numb = 0;
break;
};
prot->tRNA[j].t = OPERANDO;
prot->tRNA[j].val.num = numb;
};
}
// Verifica e traduz o sinal '=' //
else if(m_RNA[*p]== '='){
prot->fim = j;
prot->tRNA[j].t = NADA;
prot->tRNA[j].val.oper = m_RNA[*p];
} else cout<<"Expressão invalida\n";
};
//Para de traduzir a expressao ao encontrar o '='
if(prot->tRNA[j].val.oper == '=') break; //Condição de parada por '='
};
}
double Calc(double term1, char oper, double term2){
switch (oper)
{
case '+':
return (term1+term2);
case '-':
return (term1-term2);
case '*':
return (term1*term2);
case '/':
return (term1/term2);
case '^':
return (pow(term1,term2));
case 'l':
return (log(term1)/log(term2));
default:
cout<<"Nao é um operando\n";
return 0;
}
}
void ReducaoProtein(protein* prot, char posit, char negati, int i){
int pre, pos;
double operador_1, operador_2;
if(prot->tRNA[i].t == OPERADOR){
if((prot->tRNA[i].val.oper == posit)||(prot->tRNA[i].val.oper == negati)){
pre=i-1;
do
{
if(prot->tRNA[pre].t == OPERANDO){
operador_1 = prot->tRNA[pre].val.num;
prot->tRNA[pre].t = NADA;
break;
};
if(prot->tRNA[pre].t == FEPARENTESE){
prot->posicao_I = pre;
prot->tRNA[pre].t = NADA;
int count=0;
for(int j = (prot->posicao_I-1); j >= 0; j--){
if(prot->tRNA[j].t == FEPARENTESE) count++;
else if((prot->tRNA[j].t == ABPARENTESE))
if(count != 0){
count--;
}
else if(count == 0){
prot->posicao_O = j;
prot->tRNA[j].t = NADA;
break;
}
}
operador_1 = CalcProteinDecres(prot, prot->posicao_O, prot->posicao_I);
break;
}
} while (prot->tRNA[pre--].t != OPERANDO);
pos=i+1;
do
{
if(prot->tRNA[pos].t == OPERANDO){
operador_2 = prot->tRNA[pos].val.num;
prot->tRNA[pos].t = NADA;
break;
};
if(prot->tRNA[pos].t == ABPARENTESE){
prot->posicao_O = pos;
prot->tRNA[pos].t = NADA;
int count=0;
for(int j = (prot->posicao_O+1); j < prot->fim; j++){
if(prot->tRNA[j].t == ABPARENTESE) count++;
else if((prot->tRNA[j].t == FEPARENTESE)){
if(count != 0){
count--;
}
else if(count == 0){
prot->posicao_I = j;
prot->tRNA[j].t = NADA;
break;
}
}
}
operador_2 = CalcProteinCresc(prot, prot->posicao_O, prot->posicao_I);
break;
}
} while (prot->tRNA[pos++].t != OPERANDO);
prot->tRNA[i].t = OPERANDO;
prot->tRNA[i].val.num = Calc(operador_1,prot->tRNA[i].val.oper,operador_2);
}
}
}
void VarreduraCrescente(protein* prot, char positivo, char negativo, int inicio, int final){
for(int i = (inicio); i<final; i++){
ReducaoProtein(prot, positivo, negativo, i);
}
}
void VarreduraDecrescente(protein* prot, char positivo, char negativo, int inicio, int final){
for(int i = (final -1) ; i>=inicio; i--){
ReducaoProtein(prot, positivo, negativo, i);
}
}
double CalcProteinCresc(protein* proteina, int inicio, int final){
double res;
VarreduraCrescente(proteina, '^','l', inicio, final);
VarreduraCrescente(proteina, '*', '/', inicio, final);
VarreduraCrescente(proteina, '+','-', inicio, final);
for(int i=inicio; i<final; i++){
if(proteina->tRNA[i].t == OPERANDO){
res = proteina->tRNA[i].val.num;
proteina->tRNA[i].t = NADA;
}
}
return res;
}
double CalcProteinDecres(protein* proteina, int inicio, int final){
double res;
VarreduraDecrescente(proteina, '^','l', inicio, final);
VarreduraDecrescente(proteina, '*', '/', inicio, final);
VarreduraDecrescente(proteina, '+','-', inicio, final);
for(int i=inicio; i<final; i++){
if(proteina->tRNA[i].t == OPERANDO){
res = proteina->tRNA[i].val.num;
proteina->tRNA[i].t = NADA;
}
}
return res;
}
void ShowProtein(protein* prot){
double a;
char b;
for(int i=0; i<prot->fim; i++){
if((prot->tRNA[i].t == OPERADOR)||(prot->tRNA[i].t == ABPARENTESE)||(prot->tRNA[i].t == FEPARENTESE)) {
b = prot->tRNA[i].val.oper;
cout<<b;
}
else if(prot->tRNA[i].t == OPERANDO) {
a = prot->tRNA[i].val.num;
cout<<a;
}
else continue;
}
}