-
Notifications
You must be signed in to change notification settings - Fork 0
/
Exp.cpp
129 lines (107 loc) · 2.96 KB
/
Exp.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
#include "Parser.h"
#include <iostream>
#include <math.h>
int getType(int** ptr);
double num(int** ptr);
int i = 0;
EndRes* result(int** ptr, int start, bool bracket, int length)
{
i = start;
double res = 0;
double temp = 0;
int def = plus;
int last = plus;
for(; i < length; i++)
{
//gets type of current element
def = getType(ptr);
if(def == 0) continue;
if(def == leftb) //if left bracket, fire up a new "instance" of this function until rightb or next leftb
{
EndRes* end = result(ptr, ++i, true, length);
temp = end->result;
i = end->index;
}
if(def == number)
{
if(ptr[i+1][number] != 0 || ptr[i+1][dot] == 1) // if next element is a number or a floating point
//executes function, which processes sequence of digits as a number(+floating point)
{
temp = num(ptr);
}
else
{
temp = ptr[i][number];
}
}
if(temp != 0)
{
switch(last) //decides what to do with operands, based on current operator
{
case plus:
res += temp;
break;
case minus:
res -= temp;
break;
case times:
res *= temp;
break;
case over:
res /= temp;
break;
case power:
res = pow(res,temp);
break;
}
}
if(def == rightb && bracket)
{
return new EndRes(res, i);
}
temp = 0;
last = def;
}
return new EndRes(res, i);
}
//gets the type of the element
int getType(int** ptr){
for(int index = 0; index < 12; index++) //checks what kind of element we are dealing with
{
if(ptr[i][index] == 1)
{
if(index != number)
return index;
}
}
if(ptr[i][number] != 0) // if it is a number...
{
return number;
}
else{
return 0;
}
}
//checks if next element is also number, if so, then combines them properly
double num(int** ptr)
{
double res = ptr[i][number];
int index = i;
//checks if next element is also number
if (ptr[++i][number] != 0)
{
res = num(ptr);
res = res + ptr[index][number]*pow(10, (int)log10(res)+1);
}
else if(ptr[i][dot] == 1) //checks if there's floating point, if is, treats it as normal number
//Then, after completing putting together that number behind floating point, multiplies it by number of digits
{
res = num(ptr);
res = res*pow(10,-((int)log10(res)+1)) + ptr[index][number];
}
else //if not, returns current value
{
res = ptr[--i][number];
}
return res;
}