Skip to content

Commit

Permalink
fix minor bug
Browse files Browse the repository at this point in the history
  • Loading branch information
littlemight committed Mar 23, 2020
1 parent 27682d8 commit f624900
Show file tree
Hide file tree
Showing 8 changed files with 23 additions and 9 deletions.
Binary file modified bin/Calculator.o
Binary file not shown.
4 changes: 3 additions & 1 deletion bin/Makefile

Large diffs are not rendered by default.

Binary file added bin/OOPerator.exe
Binary file not shown.
Binary file modified bin/Parser.o
Binary file not shown.
Binary file modified deploy/OOPerator.exe
Binary file not shown.
4 changes: 2 additions & 2 deletions src/Calculator/Calculator.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -177,9 +177,9 @@ void Calculator::mrClicked() {
} else {
ui->display->setText(dspTxt + QString::number(val));
if (!isdigit(this->tokens.back()) && this->tokens.back() != '.') {
this->tokens += " " + to_string(ans);
this->tokens += " " + to_string(val);
} else {
this->tokens += to_string(ans);
this->tokens += to_string(val);
}
}
}
Expand Down
6 changes: 3 additions & 3 deletions src/OOPerator.pro.user
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE QtCreatorProject>
<!-- Written by QtCreator 4.11.1, 2020-03-22T23:25:59. -->
<!-- Written by QtCreator 4.11.1, 2020-03-23T07:18:01. -->
<qtcreator>
<data>
<variable>EnvironmentId</variable>
Expand Down Expand Up @@ -381,7 +381,7 @@
<value type="int" key="Qt4ProjectManager.Qt4BuildConfiguration.BuildConfiguration">2</value>
</valuemap>
<valuemap type="QVariantMap" key="ProjectExplorer.Target.BuildConfiguration.1">
<value type="QString" key="ProjectExplorer.BuildConfiguration.BuildDirectory">C:/Users/nomight/Desktop/Codes/OOP/Tubes/y</value>
<value type="QString" key="ProjectExplorer.BuildConfiguration.BuildDirectory">C:/Users/nomight/Desktop/Codes/OOP/Tubes/OOPerator/bin</value>
<valuemap type="QVariantMap" key="ProjectExplorer.BuildConfiguration.BuildStepList.0">
<valuemap type="QVariantMap" key="ProjectExplorer.BuildStepList.Step.0">
<value type="bool" key="ProjectExplorer.BuildStep.Enabled">true</value>
Expand Down Expand Up @@ -562,7 +562,7 @@
<value type="bool" key="RunConfiguration.UseQmlDebugger">false</value>
<value type="bool" key="RunConfiguration.UseQmlDebuggerAuto">true</value>
<value type="QString" key="RunConfiguration.WorkingDirectory"></value>
<value type="QString" key="RunConfiguration.WorkingDirectory.default">C:/Users/nomight/Desktop/Codes/OOP/Tubes/y</value>
<value type="QString" key="RunConfiguration.WorkingDirectory.default">C:/Users/nomight/Desktop/Codes/OOP/Tubes/OOPerator/bin</value>
</valuemap>
<value type="int" key="ProjectExplorer.Target.RunConfigurationCount">1</value>
</valuemap>
Expand Down
18 changes: 15 additions & 3 deletions src/Parser/Parser.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,7 @@ bool Parser::isUnaryStmt(string stmt, double &val, string &unaryOp) {
stringstream ss(stmt);
string curToken;

cout << stmt << '\n';
vector<string> tmp;
while (ss >> curToken) {
tmp.push_back(curToken);
Expand All @@ -125,7 +126,7 @@ bool Parser::isUnaryStmt(string stmt, double &val, string &unaryOp) {
if (!isUnaryOp((unaryOp))) {
if (unaryOp == ".") {
throw new InvalidDecimalException(stmt);
} else {
} else if (unaryOp != "-") {
throw new UndefinedOperatorException(unaryOp);
}
}
Expand Down Expand Up @@ -231,6 +232,9 @@ double Parser::evalExpression(string strTokens) {
int sz = tokens.size();

Expression* val;
for (int i = 0; i < sz; i++) {
cout << i << ": " << tokens[i] << '\n';
}
for (int i = 0; i < sz; i++) {
if (tokens[i] == "(") {
operators.push(tokens[i]);
Expand All @@ -249,7 +253,8 @@ double Parser::evalExpression(string strTokens) {
throw new ImbalancedParanthesisException;
}
} else if (isdigit(tokens[i][0])||
((i == 0 || (isBinaryOp(tokens[i - 1]) || tokens[i - 1] == "(")) && tokens[i] == "-" && i < sz - 1 && isdigit(tokens[i + 1][0]))
((i == 0 || (isBinaryOp(tokens[i - 1]) || tokens[i - 1] == "(")) && tokens[i] == "-" && i < sz - 1 && isdigit(tokens[i + 1][0])) ||
(tokens[i][0] == '-' && tokens[i].size() > 1 && isdigit(tokens[i][1]))
) {
if ((i == 0 || (isBinaryOp(tokens[i - 1]) || tokens[i - 1] == "(")) &&
tokens[i] == "-" &&
Expand All @@ -261,12 +266,19 @@ double Parser::evalExpression(string strTokens) {
} else {
throw new InvalidDecimalException(tokens[i] + tokens[i + 1]);
}
} else {
} else if (isdigit(tokens[i][0])){
if (isValidNum(tokens[i])) {
val = new TerminalExpression(stod(tokens[i]));
} else {
throw new InvalidDecimalException(tokens[i]);
}
} else {
string tmp = tokens[i].substr(1);
if (isValidNum(tmp)) {
val = new NegativeExpression(new TerminalExpression(stod(tmp)));
} else {
throw new InvalidDecimalException(tokens[i]);
}
}
values.push(val);
} else if (isBinaryOp(tokens[i])) {
Expand Down

0 comments on commit f624900

Please sign in to comment.