This repository was archived by the owner on Dec 4, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.cpp
More file actions
89 lines (72 loc) · 1.85 KB
/
main.cpp
File metadata and controls
89 lines (72 loc) · 1.85 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
// main.cpp
// Copyright (c) 2018 Ni Kesu. All rights reserved.
#include <iostream>
#include <string>
#include <vector>
#include <ctime>
#include "bigint.h"
#include "token.h"
#include "expression.h"
#include "variable.h"
#ifdef TIME
#include <ctime>
#endif
using std::cin;
using std::cout;
using std::endl;
using std::cerr;
using std::string;
#define PROMPT '$'
std::vector<Variable> variableList;
void title() {
cout << "MyCalc v1.5.0\n";
cout << "[Type \"exit\" to exit]\n";
}
int main()
{
title();
variableList.push_back(Variable("ans", V_NA, ZERO, 0.0));
variableList.push_back(Variable("pi", V_DOUBLE, ZERO, 3.1415926535897));
variableList.push_back(Variable("e", V_DOUBLE, ZERO, 2.7182818284590));
variableList.push_back(Variable("c", V_BIGINT, BigInt(299792458), 0.0));
cout.precision(10);
Value ans;
while (1) {
cout << PROMPT << ' ';
string s;
getline(cin, s);
if (s == "exit") {
break;
}
bool showAnswer = (s[s.size() - 1] != ';');
if (!showAnswer) {
s.erase(s.end()-1);
}
#ifdef TIME
auto startTime = clock();
#endif
ans = Expression(s).evaluate();
if (showAnswer) {
cout << "ans =\n\n ";
}
if (ans.type() == V_DOUBLE) {
if (showAnswer) {
cout << ans.doubleValue() << endl << endl;
}
*(variableList[0].doubleValue()) = ans.doubleValue();
}
else if(ans.type() == V_BIGINT) {
if (showAnswer) {
cout << ans.intValue().toString() << endl << endl;
#ifdef TIME
auto endTime = clock();
cout << "total use " << (double)(endTime - startTime) / CLOCKS_PER_SEC
<< 's' << endl << endl;
#endif
}
*(variableList[0].intValue()) = ans.intValue();
}
variableList[0].setType(ans.type());
}
return 0;
}