-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.cpp
190 lines (180 loc) · 5.34 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
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
#include "eval.hpp"
#include <cstdlib> //rand
#include <ctime> //time
#include <curses.h>
#include <iomanip>
#include <iostream>
#include <stdexcept>
#include <string>
#include <sstream>
#include <unistd.h>//getpid, getopt
#include <readline/readline.h>
#include <readline/history.h>
#define TXTRED "\x1b[31;1m"
#define TXTBLD "\x1b[1m"
#define TXTDEF "\x1b[0m"
std::vector<double> globalHistory;
//Robert Jenkins' 96 bit Mix Function
//https://stackoverflow.com/a/323302
unsigned long mix(unsigned long a, unsigned long b, unsigned long c)
{
a=a-b; a=a-c; a=a^(c >> 13);
b=b-c; b=b-a; b=b^(a << 8);
c=c-a; c=c-b; c=c^(b >> 13);
a=a-b; a=a-c; a=a^(c >> 12);
b=b-c; b=b-a; b=b^(a << 16);
c=c-a; c=c-b; c=c^(b >> 5);
a=a-b; a=a-c; a=a^(c >> 3);
b=b-c; b=b-a; b=b^(a << 10);
c=c-a; c=c-b; c=c^(b >> 15);
return c;
}
int main(int argc, char** argv){
// Needed for dice rolling
unsigned long seed = mix(clock(), time(NULL), getpid());
srand(seed);
int c;
bool fmode = false;
bool imode = false;
opterr = 0; // don't print error messages.
while ((c = getopt(argc, argv, "fhi")) != -1) {
switch (c) {
case 'f':
std::cout << "financial-mode\n";
fmode = true;
break;
case 'h':
std::cout << "this is the placeholder help text.\n";
return 0;
case 'i':
std::cout << "interactive-mode\n";
imode = true;
break;
case '?':
default:
break;
}
}
if (!(imode || fmode)) {
std::string s;
// If there are commandline arguments, ignore stdin.
if (argc > 1){
for(int i = 1; i < argc; i++){
s += argv[i];
}
}
else{
std::cin >> s;
}
try{
#ifdef DRAWGRAPH
std::cerr << evaluate(s) << '\n';
#else
std::cout << std::setprecision(15) << evaluate(s) << '\n';
#endif
}
catch(std::runtime_error& e){
std::cerr << e.what() << std::endl;
return 1;
}
catch(...){
std::cerr << "An unknown error occured.\n";
return 2;
}
}
else if(imode){
unsigned int counter = 0;
double res;
std::string expr;
while(true){
expr = readline(">>> ");
if(expr == "q") break;
try{
res = evaluate(expr);
globalHistory.push_back(res);
std::cout << "[" << counter << "]\t" << std::setprecision(15) << res << "\n";
counter++;
add_history(expr.c_str());
}
catch(std::runtime_error& e){
std::cerr << e.what() << std::endl;
}
catch(...){
std::cerr << "An unknown error occured.\n";
}
}
}
else if (fmode){
int c;
std::string prev = "0";
std::string cur = "0";
std::string expr = "0";
int count = 0;
int y = 0; int x = 0;
int decimal = -1;
cbreak();
noecho();
timeout(-1);
initscr();
keypad(stdscr, TRUE);
std::stringstream ss;
const int left = 5;
const int digits = 12 + left;
move(0, left);
start_color();
init_pair(1, COLOR_RED, COLOR_BLACK);
while ((c = getchar()) != 'q'){
switch (c){
case '-':
getyx(stdscr, y, x);
move(y, 0);
chgat(20, A_NORMAL, COLOR_RED, NULL);
attron(COLOR_PAIR(1));
move(y, x);
case '+':
move(y, left);
clrtoeol();
if(decimal >=0){
mvaddstr(y, digits-decimal, cur.c_str());
}
else {
mvaddstr(y, digits-cur.length(), cur.c_str());
}
addch(c);
expr += c; expr += cur;
prev = cur;
cur = "";
move(y, 0);
ss << '[' << ++count << ']';
addstr(ss.str().c_str());
ss.str(std::string());
move(++y, left);
attrset(0);
decimal = -1;
break;
case KEY_ENTER:
case 10:
case 13:
mvaddstr(y, 0, "---------------");
ss << evaluate(expr) << std::endl;
decimal = ss.str().find('.');
attron(A_STANDOUT);
mvaddch(++y, 0, '*');
mvaddstr(y, digits-decimal, ss.str().c_str());
attroff(A_STANDOUT);
ss.str(std::string());
move(++y, left);
break;
case '.':
decimal = cur.length();
default:
addch(c);
cur += c;
break;
}
refresh();
}
endwin();
}
return 0;
}