-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Директно пресмятане на израз в обратен полски запис
- Loading branch information
Showing
4 changed files
with
63 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
#include "rpn_direct_calculator.hpp" | ||
|
||
void RPNDirectCalculator::processOperation(char op) { | ||
double rightop = resultStack.pop(); | ||
double leftop = resultStack.pop(); | ||
resultStack.push(applyOperation(leftop, op, rightop)); | ||
} | ||
|
||
double RPNDirectCalculator::calculate(std::string const& expr) { | ||
for(char c : expr) { | ||
if (isdigit(c)) | ||
resultStack.push(digitToValue(c)); | ||
else if (isop(c) || c == '(') { | ||
char op; | ||
while (!operationStack.empty() && | ||
operationStack.peek() != '(' && | ||
priority(operationStack.peek()) >= priority(c)) | ||
processOperation(operationStack.pop()); | ||
operationStack.push(c); | ||
} | ||
else if (c == ')') { | ||
char op; | ||
while ((op = operationStack.pop()) != '(') | ||
processOperation(op); | ||
} else | ||
throw std::runtime_error("Некоректен израз: непознат символ!"); | ||
} | ||
while (!operationStack.empty()) | ||
processOperation(operationStack.pop()); | ||
|
||
double result = resultStack.pop(); | ||
if (!resultStack.empty()) | ||
throw std::runtime_error("Некоректен израз: повече цифри от операции!"); | ||
return result; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
#ifndef __RPN_DIRECT_CALCULATOR_HPP | ||
#define __RPN_DIRECT_CALCULATOR_HPP | ||
|
||
#include "lstack.hpp" | ||
#include "calculator.hpp" | ||
|
||
class RPNDirectCalculator : public Calculator { | ||
private: | ||
LinkedStack<double> resultStack; | ||
LinkedStack<char> operationStack; | ||
|
||
void processOperation(char op); | ||
|
||
public: | ||
double calculate(std::string const& s); | ||
}; | ||
|
||
#endif |