-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.cpp
More file actions
175 lines (143 loc) · 5.08 KB
/
main.cpp
File metadata and controls
175 lines (143 loc) · 5.08 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
// /******************************************************************************
// Welcome to Zed Sandbox.
// Zed Sandbox is a coding tool for C, C++, Python, PHP, Ruby,
// C#, OCaml, VB, Perl, Swift, Prolog, Javascript, Pascal, COBOL, HTML, CSS, JS, and more.
// Code, Compile, and Run online from anywhere in world.
// *******************************************************************************/
#include "calculator.h"
using namespace std;
// /*********************************
// SETUP
// *********************************/
// #include <iostream>
// #include <string>
// #include <vector>
// using namespace std;
// double evaluate(string a, string op, string b);
// vector<string> lex(string input);
// vector<string> parse(vector<string> tokens);
// double interpret(vector<string> tokens);
// /*********************************
// HELPERS
// *********************************/
// string qlog(string t, string m) {
// return "[" + t + "] " + m;
// }
// double evaluate(string a, string op, string b) {
// double da = stod(a);
// double db = stod(b);
// // cout << qlog("EVALUATE", "Operation: " + a + " " + op + " " + b + '\n');
// // cout << qlog("EVALUATE", "Result: ");
// if (op == "+") return da + db;
// if (op == "-") return da - db;
// if (op == "*") return da * db;
// if (op == "/") return da / db;
// return 1;
// }
// /*********************************
// LEXER
// *********************************/
// vector<string> lex(string input) {
// vector<string> tokens;
// string current = "";
// for (char c : input) {
// // digit checking
// if (isdigit(c) || c == '.') current.push_back(c);
// else if (c == ' ') {
// if (current != "") { tokens.push_back(current); current = ""; }
// }
// // operator checking
// else if (c == '+' || c == '-' || c == '*' || c == '/') {
// if (current != "") { tokens.push_back(current); current = ""; }
// current.push_back(c);
// tokens.push_back(current);
// current = "";
// }
// else if (c == '(' || c == ')'){
// if (current != "") { tokens.push_back(current); current = ""; }
// current.push_back(c);
// tokens.push_back(current);
// current = "";
// }
// }
// if (current != "") tokens.push_back(current);
// return tokens;
// }
// /*********************************
// PARSER
// *********************************/
// vector<string> parse(vector<string> tokens) {
// vector<string> parsedTokens;
// int i = 0;
// while (i < tokens.size()) {
// if (tokens[i] == "(") {
// int start = i + 1, j = start;
// int count = 1;
// // Find matching paren
// while (j < tokens.size() && count > 0) {
// if (tokens[j] == "(") count++;
// else if (tokens[j] == ")") count--;
// j++;
// }
// vector<string> paranTokens(tokens.begin() + start, tokens.begin() + j - 1);
// string result = to_string(interpret(parse(paranTokens)));
// parsedTokens.push_back(result);
// i = j;
// }
// else {
// parsedTokens.push_back(tokens[i]);
// i++;
// }
// }
// // Now handle multiplication and division with proper precedence
// vector<string> finalTokens;
// i = 0;
// while (i < parsedTokens.size()) {
// if (i + 2 < parsedTokens.size() &&
// (parsedTokens[i + 1] == "*" || parsedTokens[i + 1] == "/")) {
// string left = parsedTokens[i];
// string op = parsedTokens[i + 1];
// string right = parsedTokens[i + 2];
// double result = evaluate(left, op, right);
// finalTokens.push_back(to_string(result));
// i += 3;
// } else {
// finalTokens.push_back(parsedTokens[i]);
// i++;
// }
// }
// return finalTokens;
// }
// /*********************************
// INTERPRETER
// *********************************/
// double interpret(vector<string> tokens) {
// double result = stod(tokens[0]);
// int i = 1;
// while (i < tokens.size()) {
// string op = tokens[i];
// string nextNum = tokens[i + 1];
// result = evaluate(to_string(result), op, nextNum);
// i += 2;
// }
// return result;
// }
/*********************************
ENTRY POINT
*********************************/
int main() {
string input;
cout << "========CALCULATOR=======\n";
cout << "This calculator is made in C++ and uses manual processing with lexing, parsing, and interpreting.\n";
cout << "Made by Razka Rizaldi, tyydev1.\n";
cout << "Evaluate: ";
getline(cin, input);
// The process (long for bigger picture)
// vector<string> v_input;
// v_input = lex(input);
// v_input = parse(v_input);
// double result = interpret(v_input);
// cout << result;
cout << "Result: " << eval(input) << endl;
return 0;
}