124 lines
3.4 KiB
C++
124 lines
3.4 KiB
C++
#include <iostream>
|
|
#include <stack>
|
|
#include <cctype>
|
|
#include <cstring>
|
|
|
|
// 1. how to read and store the input?
|
|
std::string readInput()
|
|
{
|
|
std::string input;
|
|
char c;
|
|
while (std::cin.get(c)) {
|
|
if (c == '\n')
|
|
break;
|
|
input += c;
|
|
}
|
|
return input;
|
|
}
|
|
|
|
// 2. pseudo code of the algorithm
|
|
// convert to postfix expression use stack,
|
|
// 1. if it is an operand, push it into stack,
|
|
// 2. if it is an operator, pop the top two elements and push the operator result,
|
|
// 3. if it is a bracket, pop until the matched bracket, then push it into stack,
|
|
// 4. if the input is end, pop all the remaining elements,
|
|
// 5. if the last element is a bracket, return error,
|
|
// else return the result
|
|
std::string infixToPostfix(const std::string& input)
|
|
{
|
|
std::stack<char> stack;
|
|
std::string postfix;
|
|
const char* precedence = "+-*/()";
|
|
for (const auto& c : input) {
|
|
if (std::isalpha(c) || std::isdigit(c)) {
|
|
postfix += c;
|
|
} else if (c == '(') {
|
|
stack.push(c);
|
|
} else if (c == ')') {
|
|
while (!stack.empty() && stack.top() != '(') {
|
|
postfix += stack.top();
|
|
stack.pop();
|
|
}
|
|
if (!stack.empty()) {
|
|
stack.pop();
|
|
}
|
|
} else {
|
|
while (!stack.empty() && precedence[c] <= precedence[stack.top()]) {
|
|
postfix += stack.top();
|
|
stack.pop();
|
|
}
|
|
stack.push(c);
|
|
}
|
|
}
|
|
while (!stack.empty()) {
|
|
postfix += stack.top();
|
|
stack.pop();
|
|
}
|
|
return postfix;
|
|
}
|
|
|
|
// 3. data structures used in the algorithm
|
|
// stack
|
|
// string
|
|
|
|
// 4. time complexity
|
|
// O(n), where n is the length of input
|
|
|
|
// 5. space complexity
|
|
// O(n), where n is the length of input
|
|
|
|
// 6. how to store the postfix expression?
|
|
// using string
|
|
|
|
int main()
|
|
{
|
|
std::string input = readInput();
|
|
std::string postfix = infixToPostfix(input);
|
|
std::cout << postfix << std::endl;
|
|
|
|
// 1. how to read the postfix expression
|
|
// read postfix expression from standard input stream and store them into string
|
|
|
|
// 2. pseudo code of the algorithm
|
|
// evaluate a postfix expression use stack,
|
|
// if it is an operand, push it into stack,
|
|
// if it is an operator, pop the top two elements, apply the operator, and push the result,
|
|
// if it is a bracket, pop until the matched bracket, then push it into stack,
|
|
// if the input is end, return the last element in stack,
|
|
// else error
|
|
std::stack<int> stack;
|
|
const char* operators = "+-*/";
|
|
for (const auto& c : postfix) {
|
|
if (std::isalpha(c) || std::isdigit(c)) {
|
|
stack.push(c - '0');
|
|
} else if (std::strchr(operators, c)) {
|
|
int operand2 = stack.top();
|
|
stack.pop();
|
|
int operand1 = stack.top();
|
|
stack.pop();
|
|
|
|
switch (c) {
|
|
case '+':
|
|
stack.push(operand1 + operand2);
|
|
break;
|
|
case '-':
|
|
stack.push(operand1 - operand2);
|
|
break;
|
|
case '*':
|
|
stack.push(operand1 * operand2);
|
|
break;
|
|
case '/':
|
|
stack.push(operand1 / operand2);
|
|
break;
|
|
default:
|
|
return -1;
|
|
}
|
|
} else {
|
|
return -1;
|
|
}
|
|
}
|
|
std::cout << stack.top() << std::endl;
|
|
|
|
return 0;
|
|
}
|