update,
This commit is contained in:
BIN
task2/41807bc9-5583-4771-9a94-3c314843429a_thumbnail.jpeg
Normal file
BIN
task2/41807bc9-5583-4771-9a94-3c314843429a_thumbnail.jpeg
Normal file
Binary file not shown.
After Width: | Height: | Size: 23 KiB |
BIN
task2/Project requirement.pdf
Normal file
BIN
task2/Project requirement.pdf
Normal file
Binary file not shown.
BIN
task2/_ref/Lecture 1 Introduction.pdf
Normal file
BIN
task2/_ref/Lecture 1 Introduction.pdf
Normal file
Binary file not shown.
BIN
task2/_ref/Lecture 10 Heap.pdf
Normal file
BIN
task2/_ref/Lecture 10 Heap.pdf
Normal file
Binary file not shown.
BIN
task2/_ref/Lecture 11 Hash.pdf
Normal file
BIN
task2/_ref/Lecture 11 Hash.pdf
Normal file
Binary file not shown.
BIN
task2/_ref/Lecture 2 Vector.pdf
Normal file
BIN
task2/_ref/Lecture 2 Vector.pdf
Normal file
Binary file not shown.
BIN
task2/_ref/Lecture 3 Stack and Queue.pdf
Normal file
BIN
task2/_ref/Lecture 3 Stack and Queue.pdf
Normal file
Binary file not shown.
BIN
task2/_ref/Lecture 4 Sorting.pdf
Normal file
BIN
task2/_ref/Lecture 4 Sorting.pdf
Normal file
Binary file not shown.
BIN
task2/_ref/Lecture 5 Searching Algorithms.pdf
Normal file
BIN
task2/_ref/Lecture 5 Searching Algorithms.pdf
Normal file
Binary file not shown.
BIN
task2/_ref/Lecture 6 Tree.pdf
Normal file
BIN
task2/_ref/Lecture 6 Tree.pdf
Normal file
Binary file not shown.
BIN
task2/_ref/Lecture 7 BST.pdf
Normal file
BIN
task2/_ref/Lecture 7 BST.pdf
Normal file
Binary file not shown.
BIN
task2/_ref/Lecture 8 Graph.pdf
Normal file
BIN
task2/_ref/Lecture 8 Graph.pdf
Normal file
Binary file not shown.
BIN
task2/_ref/Lecture 9 Graph algorithms.pdf
Normal file
BIN
task2/_ref/Lecture 9 Graph algorithms.pdf
Normal file
Binary file not shown.
10
task2/build.sh
Normal file
10
task2/build.sh
Normal file
@@ -0,0 +1,10 @@
|
||||
#!/usr/bin/env bash
|
||||
|
||||
set -ex
|
||||
|
||||
# g++ -o output helloworld.cpp
|
||||
g++ -o output main.cpp
|
||||
|
||||
echo "build done"
|
||||
|
||||
./output
|
BIN
task2/chrome_RlDTbVJXwU.png
(Stored with Git LFS)
Normal file
BIN
task2/chrome_RlDTbVJXwU.png
(Stored with Git LFS)
Normal file
Binary file not shown.
BIN
task2/deliver/1721723283615.jpg
(Stored with Git LFS)
Normal file
BIN
task2/deliver/1721723283615.jpg
(Stored with Git LFS)
Normal file
Binary file not shown.
BIN
task2/deliver/chrome_OQtOQkTD0a.png
(Stored with Git LFS)
Normal file
BIN
task2/deliver/chrome_OQtOQkTD0a.png
(Stored with Git LFS)
Normal file
Binary file not shown.
185
task2/deliver/main.cpp
Normal file
185
task2/deliver/main.cpp
Normal file
@@ -0,0 +1,185 @@
|
||||
#include <iostream>
|
||||
#include <stack>
|
||||
#include <string>
|
||||
#include <cctype>
|
||||
#include <map>
|
||||
|
||||
// 2. pseudo code of the algorithm
|
||||
// convert to postfix expression use stack,
|
||||
// 1. if it is an operand, add to postfix,
|
||||
// 2. if it is an operator,
|
||||
// 2.1 if it is the first operator, push into `s` stack
|
||||
// 2.2 if `s` stack already got element, compare the weight of opreators
|
||||
// 2.3 pop `s` stack until the top most one got smaller precedence
|
||||
// 3. if it is a bracket, push into stack `s`,
|
||||
// 3. if it is a closing bracket, pop all element from `s` stack back to postfix,
|
||||
// else return the result
|
||||
|
||||
// Function to convert infix expression to postfix expression
|
||||
std::string infixToPostfix(const std::string &infix)
|
||||
{
|
||||
std::stack<char> s;
|
||||
std::string postfix;
|
||||
std::map<char, int> precedence = {{'+', 1}, {'-', 1}, {'*', 2}, {'/', 2}, {'(', 0}};
|
||||
|
||||
for (char c : infix)
|
||||
{
|
||||
if (std::isalnum(c))
|
||||
{ // 1. if operand, add to postfix
|
||||
postfix += c;
|
||||
}
|
||||
|
||||
// 其實括號入面同括號出面嘅數你當佢兩條式計就得
|
||||
else if (c == '(')
|
||||
{ // 2. if '(', push to stack
|
||||
s.push(c);
|
||||
}
|
||||
|
||||
// 咁佢呢度關括號呀嘛,
|
||||
// 咁佢咪會由個 stack 嗰度 port 返曬啲operator出嚟囉, 變咗可以 14+5*
|
||||
else if (c == ')')
|
||||
{ // 3. if ')', pop until '('
|
||||
while (!s.empty() && s.top() != '(')
|
||||
{
|
||||
postfix += s.top();
|
||||
s.pop();
|
||||
}
|
||||
if (!s.empty())
|
||||
s.pop(); // pop '('
|
||||
}
|
||||
else
|
||||
{ // 4. if operator, pop operators with greater or equal precedence
|
||||
while (!s.empty() && precedence[c] <= precedence[s.top()])
|
||||
{
|
||||
postfix += s.top();
|
||||
s.pop();
|
||||
}
|
||||
s.push(c); // push current operator
|
||||
}
|
||||
}
|
||||
|
||||
// 5. pop remaining operators, LIFO
|
||||
while (!s.empty())
|
||||
{
|
||||
postfix += s.top();
|
||||
s.pop();
|
||||
}
|
||||
|
||||
return postfix;
|
||||
}
|
||||
|
||||
// Function to evaluate postfix expression
|
||||
|
||||
// 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
|
||||
|
||||
int evaluatePostfix(const std::string &postfix)
|
||||
{
|
||||
std::stack<int> s;
|
||||
|
||||
for (char c : postfix)
|
||||
{
|
||||
if (std::isdigit(c))
|
||||
{ // 1. if operand, push to stack
|
||||
s.push(c - '0');
|
||||
}
|
||||
else
|
||||
{ // 2. if operator, pop two operands, evaluate and push result
|
||||
int op2 = s.top();
|
||||
s.pop();
|
||||
int op1 = s.top();
|
||||
s.pop();
|
||||
switch (c)
|
||||
{
|
||||
case '+':
|
||||
s.push(op1 + op2);
|
||||
break;
|
||||
case '-':
|
||||
s.push(op1 - op2);
|
||||
break;
|
||||
case '*':
|
||||
s.push(op1 * op2);
|
||||
break;
|
||||
case '/':
|
||||
s.push(op1 / op2);
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
return s.top(); // 3. result is the only element in stack
|
||||
}
|
||||
|
||||
int main()
|
||||
{
|
||||
std::string infix;
|
||||
// std::cout << "Enter an arithmetic expression: ";
|
||||
// std::getline(std::cin, infix);
|
||||
|
||||
// --- part 2 iteration:
|
||||
// 1,4,5,*,+
|
||||
// 1,20,+
|
||||
// 21
|
||||
std::cout << "e.g. 1 1+4*5" << std::endl;
|
||||
infix = "1+4*5";
|
||||
std::string postfix = infixToPostfix(infix);
|
||||
std::cout << "Postfix expression: " << postfix << std::endl;
|
||||
|
||||
int result = evaluatePostfix(postfix);
|
||||
std::cout << "Result: " << result << std::endl;
|
||||
|
||||
// --- part 2 iteration:
|
||||
// 1,4,+,5,*
|
||||
// 5,5,*
|
||||
// 25
|
||||
std::cout << "e.g. 2 (1+4)*5" << std::endl;
|
||||
infix = "(1+4)*5";
|
||||
postfix = infixToPostfix(infix);
|
||||
std::cout << "Postfix expression: " << postfix << std::endl;
|
||||
result = evaluatePostfix(postfix);
|
||||
std::cout << "Result: " << result << std::endl;
|
||||
|
||||
// --- part 2 iteration:
|
||||
// 1,4,+,5,-
|
||||
// 5,5,-
|
||||
// 0
|
||||
std::cout << "e.g. 3 1+4-5" << std::endl;
|
||||
infix = "1+4-5";
|
||||
postfix = infixToPostfix(infix);
|
||||
std::cout << "Postfix expression: " << postfix << std::endl;
|
||||
result = evaluatePostfix(postfix);
|
||||
std::cout << "Result: " << result << std::endl;
|
||||
|
||||
// --- part 2 iteration:
|
||||
// 1,4,5,*,+,5,*
|
||||
// 1,20,+,5,*
|
||||
// 21,5,*
|
||||
// 105
|
||||
std::cout << "e.g. 4 (1+4*5)*5" << std::endl;
|
||||
infix = "(1+4*5)*5";
|
||||
postfix = infixToPostfix(infix);
|
||||
std::cout << "Postfix expression: " << postfix << std::endl;
|
||||
|
||||
result = evaluatePostfix(postfix);
|
||||
std::cout << "Result: " << result << std::endl;
|
||||
|
||||
// --- part 2 iteration:
|
||||
// 1,4,5,*,+,2,5,*,-
|
||||
// =>1,20,+,2,5,*,-
|
||||
// =>21,2,5,*,-
|
||||
// =>21,10,-
|
||||
// =>11
|
||||
std::cout << "e.g. 5 (1+4*5)-2*5" << std::endl;
|
||||
infix = "(1+4*5)-2*5";
|
||||
postfix = infixToPostfix(infix);
|
||||
std::cout << "Postfix expression: " << postfix << std::endl;
|
||||
|
||||
result = evaluatePostfix(postfix);
|
||||
std::cout << "Result: " << result << std::endl;
|
||||
|
||||
return 0;
|
||||
}
|
BIN
task2/deliver_1/1721723283615.jpg
(Stored with Git LFS)
Normal file
BIN
task2/deliver_1/1721723283615.jpg
(Stored with Git LFS)
Normal file
Binary file not shown.
BIN
task2/deliver_1/chrome_RlDTbVJXwU.png
(Stored with Git LFS)
Normal file
BIN
task2/deliver_1/chrome_RlDTbVJXwU.png
(Stored with Git LFS)
Normal file
Binary file not shown.
363
task2/deliver_1/main.cpp
Normal file
363
task2/deliver_1/main.cpp
Normal file
@@ -0,0 +1,363 @@
|
||||
#include <iostream>
|
||||
#include <stack>
|
||||
#include <string>
|
||||
#include <cctype>
|
||||
#include <map>
|
||||
#include <string.h>
|
||||
#include <stdio.h>
|
||||
#include <queue>
|
||||
#include <algorithm>
|
||||
#include <cassert>
|
||||
|
||||
// 1. Part 1 can create correct post-fix expression from the input
|
||||
// (The operators are addition and multiplication.
|
||||
// All the operands are positive integers between 1 and 9) (10%)
|
||||
|
||||
// 2. Part 2 can evaluate the simple post-fix expression correctly
|
||||
// (The operators are addition and multiplication,
|
||||
// all the operands are positive integers between 1 and 9.
|
||||
// In the post-fix expression, all the operands and operators are separated by a “ ”.) (10%)
|
||||
|
||||
// 3. The operands can be any positive integers. (5%)
|
||||
// 4. The operands can be negative integers. (5%)
|
||||
// 5. The operands can be float numbers. (5%)
|
||||
// 6. The operators can be “-, /, ^, (, )”. (5%)
|
||||
|
||||
// 2. pseudo code of the algorithm
|
||||
// convert to postfix expression use stack,
|
||||
// 1. if it is an operand, add to postfix,
|
||||
// 2. if it is an operator,
|
||||
// 2.1 if it is the first operator, push into `s` stack
|
||||
// 2.2 if `s` stack already got element, compare the weight of opreators
|
||||
// 2.3 pop `s` stack until the top most one got smaller precedence
|
||||
// 3. if it is a bracket, push into stack `s`,
|
||||
// 3. if it is a closing bracket, pop all element from `s` stack back to postfix,
|
||||
// else return the result
|
||||
|
||||
/**
|
||||
* check if a string is a number
|
||||
* @param str_test the string to check
|
||||
* @return true if the string is a number, false otherwise
|
||||
*/
|
||||
bool checkIsNumber(std::string &str_test)
|
||||
{
|
||||
bool output = true;
|
||||
|
||||
// check if the string is longer than 1 character
|
||||
if (str_test.length() > 1)
|
||||
{
|
||||
// check each character in the string
|
||||
for (auto &c : str_test)
|
||||
{
|
||||
// if the character is not a digit, a dot or a minus sign
|
||||
if (!(('1' <= c && c <= '9') || c == '.' || c == '-'))
|
||||
{
|
||||
// set the output to false
|
||||
output = false;
|
||||
// break the loop
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
// if the string is only one character long
|
||||
char c = str_test[0];
|
||||
// if the character is not a digit
|
||||
if (!(('1' <= c && c <= '9')))
|
||||
{
|
||||
// set the output to false
|
||||
output = false;
|
||||
}
|
||||
}
|
||||
|
||||
return output;
|
||||
}
|
||||
|
||||
// 1. Part 1 can create correct post-fix expression from the input
|
||||
std::queue<std::string> infixToPostfix(const std::string &infix)
|
||||
{
|
||||
std::queue<std::string> output;
|
||||
std::stack<std::string> ss;
|
||||
std::queue<std::string> q_postfix;
|
||||
std::string postfix_temp;
|
||||
std::map<std::string, int> q_precedence = {{"+", 1}, {"-", 1}, {"*", 2}, {"/", 2}, {"(", 0}, {"^", 3}};
|
||||
std::stack<char *> sc;
|
||||
std::queue<std::string> q;
|
||||
|
||||
const char s_delimit[2] = " ";
|
||||
char *token;
|
||||
// seperated by delimiter space
|
||||
token = strtok(const_cast<char *>(infix.c_str()), s_delimit);
|
||||
while (token != NULL)
|
||||
{
|
||||
q.push(token);
|
||||
token = strtok(NULL, s_delimit);
|
||||
}
|
||||
|
||||
while (!q.empty())
|
||||
{
|
||||
std::string temp = q.front();
|
||||
q.pop();
|
||||
|
||||
if (checkIsNumber(temp))
|
||||
{
|
||||
q_postfix.push(temp);
|
||||
}
|
||||
|
||||
// 其實括號入面同括號出面嘅數你當佢兩條式計就得
|
||||
else if (temp.compare("(") == 0)
|
||||
{
|
||||
// 2. if '(', push to stack
|
||||
ss.push(temp);
|
||||
}
|
||||
|
||||
// 咁佢呢度關括號呀嘛,
|
||||
// 咁佢咪會由個 stack 嗰度 port 返曬啲operator出嚟囉, 1+4*5 變咗 145*+
|
||||
// 永遠解開 operator都係會向由左向右
|
||||
else if (temp.compare(")") == 0)
|
||||
{
|
||||
while (!ss.empty() && ss.top() != "(")
|
||||
{
|
||||
q_postfix.push(ss.top());
|
||||
ss.pop();
|
||||
}
|
||||
|
||||
if (!ss.empty())
|
||||
ss.pop(); // pop '('
|
||||
}
|
||||
else
|
||||
{
|
||||
// 4. if operator, pop operators with greater or equal precedence
|
||||
// 比較兩個 operand 之間個先後次序, 數字大嘅計先
|
||||
while (!ss.empty() && q_precedence[temp] <= q_precedence[ss.top()])
|
||||
{
|
||||
q_postfix.push(ss.top());
|
||||
ss.pop();
|
||||
}
|
||||
ss.push(temp); // push current operator
|
||||
}
|
||||
}
|
||||
|
||||
// 5. pop last operator
|
||||
while (!ss.empty())
|
||||
{
|
||||
q_postfix.push(ss.top());
|
||||
ss.pop();
|
||||
}
|
||||
|
||||
return q_postfix;
|
||||
}
|
||||
|
||||
float evaluatePostfix(std::queue<std::string> &postfix)
|
||||
{
|
||||
std::stack<float> s;
|
||||
std::string element = postfix.front();
|
||||
|
||||
std::cout << "Corresponding postfix expression: ";
|
||||
|
||||
while (!postfix.empty())
|
||||
{
|
||||
// 獨立 pop 番佢出嚟
|
||||
std::string element = postfix.front();
|
||||
postfix.pop();
|
||||
std::cout << element << ' ';
|
||||
|
||||
if (checkIsNumber(element))
|
||||
{ // 1. if operand, push to stack
|
||||
s.push(std::stof(element));
|
||||
}
|
||||
else
|
||||
{
|
||||
// 如果見到係operator, 攞返嗰兩粒數字出嚟計計佢
|
||||
float op2 = s.top();
|
||||
s.pop();
|
||||
float op1 = s.top();
|
||||
s.pop();
|
||||
|
||||
switch (element[0])
|
||||
{
|
||||
case '+':
|
||||
s.push(op1 + op2);
|
||||
break;
|
||||
case '-':
|
||||
s.push(op1 - op2);
|
||||
break;
|
||||
case '*':
|
||||
s.push(op1 * op2);
|
||||
break;
|
||||
case '/':
|
||||
s.push(op1 / op2);
|
||||
break;
|
||||
case '^':
|
||||
float temp = op1;
|
||||
for (float i = 0; i < op2 - 1; i++)
|
||||
{
|
||||
op1 = op1 * temp;
|
||||
}
|
||||
s.push(op1);
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
std::cout << std::endl;
|
||||
|
||||
return s.top();
|
||||
}
|
||||
|
||||
int main()
|
||||
{
|
||||
std::queue<std::string> q_temp;
|
||||
std::string infix;
|
||||
float result = 0;
|
||||
|
||||
infix = "1 + 2";
|
||||
std::cout << "e.g. " << infix << std::endl;
|
||||
q_temp = infixToPostfix(infix);
|
||||
result = evaluatePostfix(q_temp);
|
||||
std::cout << "result: " << result << std::endl;
|
||||
assert(result == 3);
|
||||
|
||||
// 3. The operands can be any positive integers. (5%)
|
||||
// 6. The operators can be “-, /, ^, (, )”. (5%)
|
||||
infix = "1 + 2 - 3 + 4 - 5 + 6 - 7 + 8 - 9";
|
||||
std::cout << "e.g. " << infix << std::endl;
|
||||
q_temp = infixToPostfix(infix);
|
||||
result = evaluatePostfix(q_temp);
|
||||
std::cout << "result: " << result << std::endl;
|
||||
assert(result == -3);
|
||||
|
||||
// 3. The operands can be any positive integers. (5%)
|
||||
// 6. The operators can be “-, /, ^, (, )”. (5%)
|
||||
infix = "1 + 2 - 3 * 4 / 5 + 6 - 7 * 8 + 9";
|
||||
std::cout << "e.g. " << infix << std::endl;
|
||||
q_temp = infixToPostfix(infix);
|
||||
result = evaluatePostfix(q_temp);
|
||||
std::cout << "result: " << result << std::endl;
|
||||
assert(std::abs(result - -40.4) < 0.0001);
|
||||
|
||||
// 4. The operands can be negative integers. (5%)
|
||||
infix = "-1 + 2";
|
||||
std::cout << "e.g. " << infix << std::endl;
|
||||
q_temp = infixToPostfix(infix);
|
||||
result = evaluatePostfix(q_temp);
|
||||
std::cout << "result: " << result << std::endl;
|
||||
assert(result == 1);
|
||||
|
||||
// 4. The operands can be negative integers. (5%)
|
||||
infix = "1 + -2";
|
||||
std::cout << "e.g. " << infix << std::endl;
|
||||
q_temp = infixToPostfix(infix);
|
||||
result = evaluatePostfix(q_temp);
|
||||
std::cout << "result: " << result << std::endl;
|
||||
assert(result == -1);
|
||||
|
||||
// 4. The operands can be negative integers. (5%)
|
||||
infix = "-1 + -2";
|
||||
std::cout << "e.g. " << infix << std::endl;
|
||||
q_temp = infixToPostfix(infix);
|
||||
result = evaluatePostfix(q_temp);
|
||||
std::cout << "result: " << result << std::endl;
|
||||
assert(result == -3);
|
||||
|
||||
// 5. The operands can be float numbers. (5%)
|
||||
infix = "1.1 + 2.1";
|
||||
std::cout << "e.g. " << infix << std::endl;
|
||||
q_temp = infixToPostfix(infix);
|
||||
result = evaluatePostfix(q_temp);
|
||||
std::cout << "result: " << result << std::endl;
|
||||
assert(std::abs(result - 3.2) < 0.001);
|
||||
|
||||
// 5. The operands can be float numbers. (5%)
|
||||
// 4. The operands can be negative integers. (5%)
|
||||
infix = "-1.1 + 2.3";
|
||||
std::cout << "e.g. " << infix << std::endl;
|
||||
q_temp = infixToPostfix(infix);
|
||||
result = evaluatePostfix(q_temp);
|
||||
std::cout << "result: " << result << std::endl;
|
||||
assert(std::abs(result - 1.2) < 0.001);
|
||||
|
||||
// 5. The operands can be float numbers. (5%)
|
||||
infix = "1.1 * 6";
|
||||
std::cout << "e.g. " << infix << std::endl;
|
||||
q_temp = infixToPostfix(infix);
|
||||
result = evaluatePostfix(q_temp);
|
||||
std::cout << "result: " << result << std::endl;
|
||||
assert(std::abs(result - 6.6) < 0.001);
|
||||
|
||||
//
|
||||
// 6. The operators can be “-, /, ^, (, )”. (5%)
|
||||
infix = "1 + 2 ^ 3";
|
||||
std::cout << "e.g. " << infix << std::endl;
|
||||
q_temp = infixToPostfix(infix);
|
||||
result = evaluatePostfix(q_temp);
|
||||
std::cout << "result: " << result << std::endl;
|
||||
assert(result == 9);
|
||||
|
||||
//
|
||||
// 6. The operators can be “-, /, ^, (, )”. (5%)
|
||||
infix = "( 1 + 4 ) * 5";
|
||||
std::cout << "e.g. " << infix << std::endl;
|
||||
q_temp = infixToPostfix(infix);
|
||||
result = evaluatePostfix(q_temp);
|
||||
assert(result == 25);
|
||||
std::cout << "result: " << result << std::endl;
|
||||
|
||||
//
|
||||
// 6. The operators can be “-, /, ^, (, )”. (5%)
|
||||
infix = "( 1 + 4 * 5 ) * 5";
|
||||
std::cout << "e.g. " << infix << std::endl;
|
||||
q_temp = infixToPostfix(infix);
|
||||
result = evaluatePostfix(q_temp);
|
||||
assert(result == 105);
|
||||
std::cout << "result: " << result << std::endl;
|
||||
|
||||
//
|
||||
// 6. The operators can be “-, /, ^, (, )”. (5%)
|
||||
infix = "( 1 + 4 * 5 ) - 2 * 5";
|
||||
std::cout << "e.g. " << infix << std::endl;
|
||||
q_temp = infixToPostfix(infix);
|
||||
result = evaluatePostfix(q_temp);
|
||||
assert(result == 11);
|
||||
std::cout << "result: " << result << std::endl;
|
||||
|
||||
//
|
||||
// 6. The operators can be “-, /, ^, (, )”. (5%)
|
||||
infix = "( 11 + 4 * 5 ) - 6 * 5";
|
||||
std::cout << "e.g. " << infix << std::endl;
|
||||
q_temp = infixToPostfix(infix);
|
||||
result = evaluatePostfix(q_temp);
|
||||
assert(result == 1);
|
||||
std::cout << "result: " << result << std::endl;
|
||||
|
||||
//
|
||||
// 5. The operands can be float numbers. (5%)
|
||||
// 6. The operators can be “-, /, ^, (, )”.
|
||||
infix = "( 1.1 + 4 ) * 2";
|
||||
std::cout << "e.g. " << infix << std::endl;
|
||||
q_temp = infixToPostfix(infix);
|
||||
result = evaluatePostfix(q_temp);
|
||||
assert(std::abs(result - 10.2) < 0.001);
|
||||
std::cout << "result: " << result << std::endl;
|
||||
|
||||
//
|
||||
// 5. The operands can be float numbers. (5%)
|
||||
// 6. The operators can be “-, /, ^, (, )”.
|
||||
infix = "( 1 + 2 * 3 ) * 2";
|
||||
std::cout << "e.g. " << infix << std::endl;
|
||||
q_temp = infixToPostfix(infix);
|
||||
result = evaluatePostfix(q_temp);
|
||||
assert(std::abs(result - 10.2) < 0.001);
|
||||
std::cout << "result: " << result << std::endl;
|
||||
|
||||
std::cout << "Enter an arithmetic expression: ";
|
||||
std::getline(std::cin, infix);
|
||||
q_temp = infixToPostfix(infix);
|
||||
result = evaluatePostfix(q_temp);
|
||||
std::cout << "result: " << result << std::endl;
|
||||
|
||||
std::cout << "done" << std::endl;
|
||||
|
||||
return 0;
|
||||
}
|
7
task2/gitUpdate.bat
Normal file
7
task2/gitUpdate.bat
Normal file
@@ -0,0 +1,7 @@
|
||||
git status .
|
||||
|
||||
@pause
|
||||
|
||||
git add .
|
||||
git commit -m"update marissa.sam task2,"
|
||||
start git push
|
8
task2/helloworld.cpp
Normal file
8
task2/helloworld.cpp
Normal file
@@ -0,0 +1,8 @@
|
||||
#include <iostream>
|
||||
|
||||
int main()
|
||||
{
|
||||
std::cout<<"Hello World";
|
||||
|
||||
return 0;
|
||||
}
|
363
task2/main.cpp
Normal file
363
task2/main.cpp
Normal file
@@ -0,0 +1,363 @@
|
||||
#include <iostream>
|
||||
#include <stack>
|
||||
#include <string>
|
||||
#include <cctype>
|
||||
#include <map>
|
||||
#include <string.h>
|
||||
#include <stdio.h>
|
||||
#include <queue>
|
||||
#include <algorithm>
|
||||
#include <cassert>
|
||||
|
||||
// 1. Part 1 can create correct post-fix expression from the input
|
||||
// (The operators are addition and multiplication.
|
||||
// All the operands are positive integers between 1 and 9) (10%)
|
||||
|
||||
// 2. Part 2 can evaluate the simple post-fix expression correctly
|
||||
// (The operators are addition and multiplication,
|
||||
// all the operands are positive integers between 1 and 9.
|
||||
// In the post-fix expression, all the operands and operators are separated by a “ ”.) (10%)
|
||||
|
||||
// 3. The operands can be any positive integers. (5%)
|
||||
// 4. The operands can be negative integers. (5%)
|
||||
// 5. The operands can be float numbers. (5%)
|
||||
// 6. The operators can be “-, /, ^, (, )”. (5%)
|
||||
|
||||
// 2. pseudo code of the algorithm
|
||||
// convert to postfix expression use stack,
|
||||
// 1. if it is an operand, add to postfix,
|
||||
// 2. if it is an operator,
|
||||
// 2.1 if it is the first operator, push into `s` stack
|
||||
// 2.2 if `s` stack already got element, compare the weight of opreators
|
||||
// 2.3 pop `s` stack until the top most one got smaller precedence
|
||||
// 3. if it is a bracket, push into stack `s`,
|
||||
// 3. if it is a closing bracket, pop all element from `s` stack back to postfix,
|
||||
// else return the result
|
||||
|
||||
/**
|
||||
* check if a string is a number
|
||||
* @param str_test the string to check
|
||||
* @return true if the string is a number, false otherwise
|
||||
*/
|
||||
bool checkIsNumber(std::string &str_test)
|
||||
{
|
||||
bool output = true;
|
||||
|
||||
// check if the string is longer than 1 character
|
||||
if (str_test.length() > 1)
|
||||
{
|
||||
// check each character in the string
|
||||
for (auto &c : str_test)
|
||||
{
|
||||
// if the character is not a digit, a dot or a minus sign
|
||||
if (!(('1' <= c && c <= '9') || c == '.' || c == '-'))
|
||||
{
|
||||
// set the output to false
|
||||
output = false;
|
||||
// break the loop
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
// if the string is only one character long
|
||||
char c = str_test[0];
|
||||
// if the character is not a digit
|
||||
if (!(('1' <= c && c <= '9')))
|
||||
{
|
||||
// set the output to false
|
||||
output = false;
|
||||
}
|
||||
}
|
||||
|
||||
return output;
|
||||
}
|
||||
|
||||
// 1. Part 1 can create correct post-fix expression from the input
|
||||
std::queue<std::string> infixToPostfix(const std::string &infix)
|
||||
{
|
||||
std::queue<std::string> output;
|
||||
std::stack<std::string> ss;
|
||||
std::queue<std::string> q_postfix;
|
||||
std::string postfix_temp;
|
||||
std::map<std::string, int> q_precedence = {{"+", 1}, {"-", 1}, {"*", 2}, {"/", 2}, {"(", 0}, {"^", 3}};
|
||||
std::stack<char *> sc;
|
||||
std::queue<std::string> q;
|
||||
|
||||
const char s_delimit[2] = " ";
|
||||
char *token;
|
||||
// seperated by delimiter space
|
||||
token = strtok(const_cast<char *>(infix.c_str()), s_delimit);
|
||||
while (token != NULL)
|
||||
{
|
||||
q.push(token);
|
||||
token = strtok(NULL, s_delimit);
|
||||
}
|
||||
|
||||
while (!q.empty())
|
||||
{
|
||||
std::string temp = q.front();
|
||||
q.pop();
|
||||
|
||||
if (checkIsNumber(temp))
|
||||
{
|
||||
q_postfix.push(temp);
|
||||
}
|
||||
|
||||
// 其實括號入面同括號出面嘅數你當佢兩條式計就得
|
||||
else if (temp.compare("(") == 0)
|
||||
{
|
||||
// 2. if '(', push to stack
|
||||
ss.push(temp);
|
||||
}
|
||||
|
||||
// 咁佢呢度關括號呀嘛,
|
||||
// 咁佢咪會由個 stack 嗰度 port 返曬啲operator出嚟囉, 1+4*5 變咗 145*+
|
||||
// 永遠解開 operator都係會向由左向右
|
||||
else if (temp.compare(")") == 0)
|
||||
{
|
||||
while (!ss.empty() && ss.top() != "(")
|
||||
{
|
||||
q_postfix.push(ss.top());
|
||||
ss.pop();
|
||||
}
|
||||
|
||||
if (!ss.empty())
|
||||
ss.pop(); // pop '('
|
||||
}
|
||||
else
|
||||
{
|
||||
// 4. if operator, pop operators with greater or equal precedence
|
||||
// 比較兩個 operand 之間個先後次序, 數字大嘅計先
|
||||
while (!ss.empty() && q_precedence[temp] <= q_precedence[ss.top()])
|
||||
{
|
||||
q_postfix.push(ss.top());
|
||||
ss.pop();
|
||||
}
|
||||
ss.push(temp); // push current operator
|
||||
}
|
||||
}
|
||||
|
||||
// 5. pop last operator
|
||||
while (!ss.empty())
|
||||
{
|
||||
q_postfix.push(ss.top());
|
||||
ss.pop();
|
||||
}
|
||||
|
||||
return q_postfix;
|
||||
}
|
||||
|
||||
float evaluatePostfix(std::queue<std::string> &postfix)
|
||||
{
|
||||
std::stack<float> s;
|
||||
std::string element = postfix.front();
|
||||
|
||||
std::cout << "Corresponding postfix expression: ";
|
||||
|
||||
while (!postfix.empty())
|
||||
{
|
||||
// 獨立 pop 番佢出嚟
|
||||
std::string element = postfix.front();
|
||||
postfix.pop();
|
||||
std::cout << element << ' ';
|
||||
|
||||
if (checkIsNumber(element))
|
||||
{ // 1. if operand, push to stack
|
||||
s.push(std::stof(element));
|
||||
}
|
||||
else
|
||||
{
|
||||
// 如果見到係operator, 攞返嗰兩粒數字出嚟計計佢
|
||||
float op2 = s.top();
|
||||
s.pop();
|
||||
float op1 = s.top();
|
||||
s.pop();
|
||||
|
||||
switch (element[0])
|
||||
{
|
||||
case '+':
|
||||
s.push(op1 + op2);
|
||||
break;
|
||||
case '-':
|
||||
s.push(op1 - op2);
|
||||
break;
|
||||
case '*':
|
||||
s.push(op1 * op2);
|
||||
break;
|
||||
case '/':
|
||||
s.push(op1 / op2);
|
||||
break;
|
||||
case '^':
|
||||
float temp = op1;
|
||||
for (float i = 0; i < op2 - 1; i++)
|
||||
{
|
||||
op1 = op1 * temp;
|
||||
}
|
||||
s.push(op1);
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
std::cout << std::endl;
|
||||
|
||||
return s.top();
|
||||
}
|
||||
|
||||
int main()
|
||||
{
|
||||
std::queue<std::string> q_temp;
|
||||
std::string infix;
|
||||
float result = 0;
|
||||
|
||||
infix = "1 + 2";
|
||||
std::cout << "e.g. " << infix << std::endl;
|
||||
q_temp = infixToPostfix(infix);
|
||||
result = evaluatePostfix(q_temp);
|
||||
std::cout << "result: " << result << std::endl;
|
||||
assert(result == 3);
|
||||
|
||||
// 3. The operands can be any positive integers. (5%)
|
||||
// 6. The operators can be “-, /, ^, (, )”. (5%)
|
||||
infix = "1 + 2 - 3 + 4 - 5 + 6 - 7 + 8 - 9";
|
||||
std::cout << "e.g. " << infix << std::endl;
|
||||
q_temp = infixToPostfix(infix);
|
||||
result = evaluatePostfix(q_temp);
|
||||
std::cout << "result: " << result << std::endl;
|
||||
assert(result == -3);
|
||||
|
||||
// 3. The operands can be any positive integers. (5%)
|
||||
// 6. The operators can be “-, /, ^, (, )”. (5%)
|
||||
infix = "1 + 2 - 3 * 4 / 5 + 6 - 7 * 8 + 9";
|
||||
std::cout << "e.g. " << infix << std::endl;
|
||||
q_temp = infixToPostfix(infix);
|
||||
result = evaluatePostfix(q_temp);
|
||||
std::cout << "result: " << result << std::endl;
|
||||
assert(std::abs(result - -40.4) < 0.0001);
|
||||
|
||||
// 4. The operands can be negative integers. (5%)
|
||||
infix = "-1 + 2";
|
||||
std::cout << "e.g. " << infix << std::endl;
|
||||
q_temp = infixToPostfix(infix);
|
||||
result = evaluatePostfix(q_temp);
|
||||
std::cout << "result: " << result << std::endl;
|
||||
assert(result == 1);
|
||||
|
||||
// 4. The operands can be negative integers. (5%)
|
||||
infix = "1 + -2";
|
||||
std::cout << "e.g. " << infix << std::endl;
|
||||
q_temp = infixToPostfix(infix);
|
||||
result = evaluatePostfix(q_temp);
|
||||
std::cout << "result: " << result << std::endl;
|
||||
assert(result == -1);
|
||||
|
||||
// 4. The operands can be negative integers. (5%)
|
||||
infix = "-1 + -2";
|
||||
std::cout << "e.g. " << infix << std::endl;
|
||||
q_temp = infixToPostfix(infix);
|
||||
result = evaluatePostfix(q_temp);
|
||||
std::cout << "result: " << result << std::endl;
|
||||
assert(result == -3);
|
||||
|
||||
// 5. The operands can be float numbers. (5%)
|
||||
infix = "1.1 + 2.1";
|
||||
std::cout << "e.g. " << infix << std::endl;
|
||||
q_temp = infixToPostfix(infix);
|
||||
result = evaluatePostfix(q_temp);
|
||||
std::cout << "result: " << result << std::endl;
|
||||
assert(std::abs(result - 3.2) < 0.001);
|
||||
|
||||
// 5. The operands can be float numbers. (5%)
|
||||
// 4. The operands can be negative integers. (5%)
|
||||
infix = "-1.1 + 2.3";
|
||||
std::cout << "e.g. " << infix << std::endl;
|
||||
q_temp = infixToPostfix(infix);
|
||||
result = evaluatePostfix(q_temp);
|
||||
std::cout << "result: " << result << std::endl;
|
||||
assert(std::abs(result - 1.2) < 0.001);
|
||||
|
||||
// 5. The operands can be float numbers. (5%)
|
||||
infix = "1.1 * 6";
|
||||
std::cout << "e.g. " << infix << std::endl;
|
||||
q_temp = infixToPostfix(infix);
|
||||
result = evaluatePostfix(q_temp);
|
||||
std::cout << "result: " << result << std::endl;
|
||||
assert(std::abs(result - 6.6) < 0.001);
|
||||
|
||||
//
|
||||
// 6. The operators can be “-, /, ^, (, )”. (5%)
|
||||
infix = "1 + 2 ^ 3";
|
||||
std::cout << "e.g. " << infix << std::endl;
|
||||
q_temp = infixToPostfix(infix);
|
||||
result = evaluatePostfix(q_temp);
|
||||
std::cout << "result: " << result << std::endl;
|
||||
assert(result == 9);
|
||||
|
||||
//
|
||||
// 6. The operators can be “-, /, ^, (, )”. (5%)
|
||||
infix = "( 1 + 4 ) * 5";
|
||||
std::cout << "e.g. " << infix << std::endl;
|
||||
q_temp = infixToPostfix(infix);
|
||||
result = evaluatePostfix(q_temp);
|
||||
assert(result == 25);
|
||||
std::cout << "result: " << result << std::endl;
|
||||
|
||||
//
|
||||
// 6. The operators can be “-, /, ^, (, )”. (5%)
|
||||
infix = "( 1 + 4 * 5 ) * 5";
|
||||
std::cout << "e.g. " << infix << std::endl;
|
||||
q_temp = infixToPostfix(infix);
|
||||
result = evaluatePostfix(q_temp);
|
||||
assert(result == 105);
|
||||
std::cout << "result: " << result << std::endl;
|
||||
|
||||
//
|
||||
// 6. The operators can be “-, /, ^, (, )”. (5%)
|
||||
infix = "( 1 + 4 * 5 ) - 2 * 5";
|
||||
std::cout << "e.g. " << infix << std::endl;
|
||||
q_temp = infixToPostfix(infix);
|
||||
result = evaluatePostfix(q_temp);
|
||||
assert(result == 11);
|
||||
std::cout << "result: " << result << std::endl;
|
||||
|
||||
//
|
||||
// 6. The operators can be “-, /, ^, (, )”. (5%)
|
||||
infix = "( 11 + 4 * 5 ) - 6 * 5";
|
||||
std::cout << "e.g. " << infix << std::endl;
|
||||
q_temp = infixToPostfix(infix);
|
||||
result = evaluatePostfix(q_temp);
|
||||
assert(result == 1);
|
||||
std::cout << "result: " << result << std::endl;
|
||||
|
||||
//
|
||||
// 5. The operands can be float numbers. (5%)
|
||||
// 6. The operators can be “-, /, ^, (, )”.
|
||||
infix = "( 1.1 + 4 ) * 2";
|
||||
std::cout << "e.g. " << infix << std::endl;
|
||||
q_temp = infixToPostfix(infix);
|
||||
result = evaluatePostfix(q_temp);
|
||||
assert(std::abs(result - 10.2) < 0.001);
|
||||
std::cout << "result: " << result << std::endl;
|
||||
|
||||
//
|
||||
// 5. The operands can be float numbers. (5%)
|
||||
// 6. The operators can be “-, /, ^, (, )”.
|
||||
infix = "1 + ( 1 + ( 1 + 2 ^ 3 ) * 3 ^ 2 ) * 2";
|
||||
std::cout << "e.g. " << infix << std::endl;
|
||||
q_temp = infixToPostfix(infix);
|
||||
result = evaluatePostfix(q_temp);
|
||||
std::cout << "result: " << result << std::endl;
|
||||
assert(std::abs(result - 165) < 0.001);
|
||||
|
||||
std::cout << "Enter an arithmetic expression: ";
|
||||
std::getline(std::cin, infix);
|
||||
q_temp = infixToPostfix(infix);
|
||||
result = evaluatePostfix(q_temp);
|
||||
std::cout << "result: " << result << std::endl;
|
||||
|
||||
std::cout << "done" << std::endl;
|
||||
|
||||
return 0;
|
||||
}
|
30
task2/notes.md
Normal file
30
task2/notes.md
Normal file
@@ -0,0 +1,30 @@
|
||||
我跟住佢 part1 part2 個方向去做個 source code, 但係 report 嗰部分我就唔需要做, 我諗你係咁嘅意思係咪?
|
||||
|
||||
# NOTES
|
||||
|
||||
The project requires you to write a simple calculator to evaluate arithmetic expressions. It
|
||||
includes two parts:
|
||||
|
||||
- In the first part, you need to convert the input to a postfix expression.
|
||||
- In the second part, you need to evaluate the postfix expression that you get in the first
|
||||
part.
|
||||
- You need to implement your algorithms using c++. Submit your code to Moodle.
|
||||
- You also need to write a report explaining the following items.
|
||||
|
||||
## Part 1: Convert an expression to postfix expression using stack in STL
|
||||
|
||||
1. how to read and store the input?
|
||||
2. pseudo code of the algorithm
|
||||
3. data structures used in the algorithm
|
||||
4. time complexity
|
||||
5. space complexity
|
||||
6. how to store the postfix expression?
|
||||
|
||||
## Part 2: Use stack of STL to evaluate a postfix expression
|
||||
|
||||
1. how to read the postfix expression
|
||||
2. pseudo code of the algorithm
|
||||
3. data structures used in the algorithm
|
||||
4. time complexity
|
||||
5. space complexity
|
||||
6. how to output the final result?
|
BIN
task2/output
Normal file
BIN
task2/output
Normal file
Binary file not shown.
22
task2/strtok_tryout.cpp
Normal file
22
task2/strtok_tryout.cpp
Normal file
@@ -0,0 +1,22 @@
|
||||
#include <string.h>
|
||||
#include <stdio.h>
|
||||
|
||||
int main()
|
||||
{
|
||||
char str[80] = "This is - www.tutorialspoint.com - website";
|
||||
const char s_delimit[2] = "-";
|
||||
char *token;
|
||||
|
||||
/* get the first token */
|
||||
token = strtok(str, s_delimit);
|
||||
|
||||
/* walk through other tokens */
|
||||
while (token != NULL)
|
||||
{
|
||||
printf("%s\n", token);
|
||||
|
||||
token = strtok(NULL, s_delimit);
|
||||
}
|
||||
|
||||
return (0);
|
||||
}
|
91
task2/temp/.vscode/settings.json
vendored
Normal file
91
task2/temp/.vscode/settings.json
vendored
Normal file
@@ -0,0 +1,91 @@
|
||||
{
|
||||
"files.associations": {
|
||||
"*.mjson": "jsonc",
|
||||
"any": "cpp",
|
||||
"array": "cpp",
|
||||
"atomic": "cpp",
|
||||
"barrier": "cpp",
|
||||
"bit": "cpp",
|
||||
"*.tcc": "cpp",
|
||||
"bitset": "cpp",
|
||||
"cctype": "cpp",
|
||||
"cfenv": "cpp",
|
||||
"charconv": "cpp",
|
||||
"chrono": "cpp",
|
||||
"cinttypes": "cpp",
|
||||
"clocale": "cpp",
|
||||
"cmath": "cpp",
|
||||
"codecvt": "cpp",
|
||||
"compare": "cpp",
|
||||
"complex": "cpp",
|
||||
"concepts": "cpp",
|
||||
"condition_variable": "cpp",
|
||||
"coroutine": "cpp",
|
||||
"csetjmp": "cpp",
|
||||
"csignal": "cpp",
|
||||
"cstdarg": "cpp",
|
||||
"cstddef": "cpp",
|
||||
"cstdint": "cpp",
|
||||
"cstdio": "cpp",
|
||||
"cstdlib": "cpp",
|
||||
"cstring": "cpp",
|
||||
"ctime": "cpp",
|
||||
"cuchar": "cpp",
|
||||
"cwchar": "cpp",
|
||||
"cwctype": "cpp",
|
||||
"deque": "cpp",
|
||||
"forward_list": "cpp",
|
||||
"list": "cpp",
|
||||
"map": "cpp",
|
||||
"set": "cpp",
|
||||
"string": "cpp",
|
||||
"unordered_map": "cpp",
|
||||
"unordered_set": "cpp",
|
||||
"vector": "cpp",
|
||||
"exception": "cpp",
|
||||
"algorithm": "cpp",
|
||||
"functional": "cpp",
|
||||
"iterator": "cpp",
|
||||
"memory": "cpp",
|
||||
"memory_resource": "cpp",
|
||||
"numeric": "cpp",
|
||||
"optional": "cpp",
|
||||
"random": "cpp",
|
||||
"ratio": "cpp",
|
||||
"regex": "cpp",
|
||||
"source_location": "cpp",
|
||||
"string_view": "cpp",
|
||||
"system_error": "cpp",
|
||||
"tuple": "cpp",
|
||||
"type_traits": "cpp",
|
||||
"utility": "cpp",
|
||||
"fstream": "cpp",
|
||||
"future": "cpp",
|
||||
"initializer_list": "cpp",
|
||||
"iomanip": "cpp",
|
||||
"iosfwd": "cpp",
|
||||
"iostream": "cpp",
|
||||
"istream": "cpp",
|
||||
"latch": "cpp",
|
||||
"limits": "cpp",
|
||||
"mutex": "cpp",
|
||||
"new": "cpp",
|
||||
"numbers": "cpp",
|
||||
"ostream": "cpp",
|
||||
"ranges": "cpp",
|
||||
"scoped_allocator": "cpp",
|
||||
"semaphore": "cpp",
|
||||
"shared_mutex": "cpp",
|
||||
"span": "cpp",
|
||||
"sstream": "cpp",
|
||||
"stdexcept": "cpp",
|
||||
"stop_token": "cpp",
|
||||
"streambuf": "cpp",
|
||||
"syncstream": "cpp",
|
||||
"thread": "cpp",
|
||||
"typeindex": "cpp",
|
||||
"typeinfo": "cpp",
|
||||
"valarray": "cpp",
|
||||
"variant": "cpp"
|
||||
}
|
||||
}
|
BIN
task2/temp/1721723283615.jpg
(Stored with Git LFS)
Normal file
BIN
task2/temp/1721723283615.jpg
(Stored with Git LFS)
Normal file
Binary file not shown.
123
task2/temp/_draft/helloworld copy 2.cpp
Normal file
123
task2/temp/_draft/helloworld copy 2.cpp
Normal file
@@ -0,0 +1,123 @@
|
||||
#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;
|
||||
}
|
121
task2/temp/_draft/helloworld copy 3.cpp
Normal file
121
task2/temp/_draft/helloworld copy 3.cpp
Normal file
@@ -0,0 +1,121 @@
|
||||
#include <iostream>
|
||||
#include <stack>
|
||||
#include <string>
|
||||
#include <cctype>
|
||||
#include <map>
|
||||
|
||||
// Function to convert infix expression to postfix expression
|
||||
std::string infixToPostfix(const std::string &infix)
|
||||
{
|
||||
std::stack<char> s;
|
||||
std::string postfix;
|
||||
std::map<char, int> precedence = {{'+', 1}, {'-', 1}, {'*', 2}, {'/', 2}, {'(', 0}};
|
||||
|
||||
for (char c : infix)
|
||||
{
|
||||
if (std::isalnum(c))
|
||||
{ // 1. if operand, add to postfix
|
||||
postfix += c;
|
||||
}
|
||||
else if (c == '(')
|
||||
{ // 2. if '(', push to stack
|
||||
s.push(c);
|
||||
}
|
||||
else if (c == ')')
|
||||
{ // 3. if ')', pop until '('
|
||||
while (!s.empty() && s.top() != '(')
|
||||
{
|
||||
postfix += s.top();
|
||||
s.pop();
|
||||
}
|
||||
if (!s.empty())
|
||||
s.pop(); // pop '('
|
||||
}
|
||||
else
|
||||
{ // 4. if operator, pop operators with greater or equal precedence
|
||||
while (!s.empty() && precedence[c] <= precedence[s.top()])
|
||||
{
|
||||
postfix += s.top();
|
||||
s.pop();
|
||||
}
|
||||
s.push(c); // push current operator
|
||||
}
|
||||
}
|
||||
// 5. pop remaining operators
|
||||
while (!s.empty())
|
||||
{
|
||||
postfix += s.top();
|
||||
s.pop();
|
||||
}
|
||||
return postfix;
|
||||
}
|
||||
|
||||
// Function to evaluate postfix expression
|
||||
int evaluatePostfix(const std::string &postfix)
|
||||
{
|
||||
std::stack<int> s;
|
||||
|
||||
for (char c : postfix)
|
||||
{
|
||||
if (std::isdigit(c))
|
||||
{ // 1. if operand, push to stack
|
||||
s.push(c - '0');
|
||||
}
|
||||
else
|
||||
{ // 2. if operator, pop two operands, evaluate and push result
|
||||
int op2 = s.top();
|
||||
s.pop();
|
||||
int op1 = s.top();
|
||||
s.pop();
|
||||
switch (c)
|
||||
{
|
||||
case '+':
|
||||
s.push(op1 + op2);
|
||||
break;
|
||||
case '-':
|
||||
s.push(op1 - op2);
|
||||
break;
|
||||
case '*':
|
||||
s.push(op1 * op2);
|
||||
break;
|
||||
case '/':
|
||||
s.push(op1 / op2);
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
return s.top(); // 3. result is the only element in stack
|
||||
}
|
||||
|
||||
int main()
|
||||
{
|
||||
std::string infix;
|
||||
// std::cout << "Enter an arithmetic expression: ";
|
||||
// std::getline(std::cin, infix);
|
||||
|
||||
std::cout << "e.g. 1 1+4*5" << std::endl;
|
||||
infix = "1+4*5";
|
||||
std::string postfix = infixToPostfix(infix);
|
||||
std::cout << "Postfix expression: " << postfix << std::endl;
|
||||
|
||||
int result = evaluatePostfix(postfix);
|
||||
std::cout << "Result: " << result << std::endl;
|
||||
|
||||
std::cout << "e.g. 2 (1+4)*5" << std::endl;
|
||||
infix = "(1+4)*5";
|
||||
postfix = infixToPostfix(infix);
|
||||
std::cout << "Postfix expression: " << postfix << std::endl;
|
||||
|
||||
result = evaluatePostfix(postfix);
|
||||
std::cout << "Result: " << result << std::endl;
|
||||
|
||||
std::cout << "e.g. 3 1+4-5" << std::endl;
|
||||
infix = "1+4-5";
|
||||
postfix = infixToPostfix(infix);
|
||||
std::cout << "Postfix expression: " << postfix << std::endl;
|
||||
|
||||
result = evaluatePostfix(postfix);
|
||||
std::cout << "Result: " << result << std::endl;
|
||||
|
||||
return 0;
|
||||
}
|
103
task2/temp/_draft/helloworld copy.cpp
Normal file
103
task2/temp/_draft/helloworld copy.cpp
Normal file
@@ -0,0 +1,103 @@
|
||||
#include <iostream>
|
||||
#include <stack>
|
||||
#include <string>
|
||||
#include <cctype>
|
||||
#include <map>
|
||||
|
||||
// Function to convert infix expression to postfix expression
|
||||
std::string infixToPostfix(const std::string &infix)
|
||||
{
|
||||
std::stack<char> s;
|
||||
std::string postfix;
|
||||
std::map<char, int> precedence = {{'+', 1}, {'-', 1}, {'*', 2}, {'/', 2}, {'(', 0}};
|
||||
|
||||
for (char c : infix)
|
||||
{
|
||||
if (std::isalnum(c))
|
||||
{ // 1. if operand, add to postfix
|
||||
postfix += c;
|
||||
}
|
||||
else if (c == '(')
|
||||
{ // 2. if '(', push to stack
|
||||
s.push(c);
|
||||
}
|
||||
else if (c == ')')
|
||||
{ // 3. if ')', pop until '('
|
||||
while (!s.empty() && s.top() != '(')
|
||||
{
|
||||
postfix += s.top();
|
||||
s.pop();
|
||||
}
|
||||
if (!s.empty())
|
||||
s.pop(); // pop '('
|
||||
}
|
||||
else
|
||||
{ // 4. if operator, pop operators with greater or equal precedence
|
||||
while (!s.empty() && precedence[c] <= precedence[s.top()])
|
||||
{
|
||||
postfix += s.top();
|
||||
s.pop();
|
||||
}
|
||||
s.push(c); // push current operator
|
||||
}
|
||||
}
|
||||
// 5. pop remaining operators
|
||||
while (!s.empty())
|
||||
{
|
||||
postfix += s.top();
|
||||
s.pop();
|
||||
}
|
||||
return postfix;
|
||||
}
|
||||
|
||||
// Function to evaluate postfix expression
|
||||
int evaluatePostfix(const std::string &postfix)
|
||||
{
|
||||
std::stack<int> s;
|
||||
|
||||
for (char c : postfix)
|
||||
{
|
||||
if (std::isdigit(c))
|
||||
{ // 1. if operand, push to stack
|
||||
s.push(c - '0');
|
||||
}
|
||||
else
|
||||
{ // 2. if operator, pop two operands, evaluate and push result
|
||||
int op2 = s.top();
|
||||
s.pop();
|
||||
int op1 = s.top();
|
||||
s.pop();
|
||||
switch (c)
|
||||
{
|
||||
case '+':
|
||||
s.push(op1 + op2);
|
||||
break;
|
||||
case '-':
|
||||
s.push(op1 - op2);
|
||||
break;
|
||||
case '*':
|
||||
s.push(op1 * op2);
|
||||
break;
|
||||
case '/':
|
||||
s.push(op1 / op2);
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
return s.top(); // 3. result is the only element in stack
|
||||
}
|
||||
|
||||
int main()
|
||||
{
|
||||
std::string infix;
|
||||
std::cout << "Enter an arithmetic expression: ";
|
||||
std::getline(std::cin, infix);
|
||||
|
||||
std::string postfix = infixToPostfix(infix);
|
||||
std::cout << "Postfix expression: " << postfix << std::endl;
|
||||
|
||||
int result = evaluatePostfix(postfix);
|
||||
std::cout << "Result: " << result << std::endl;
|
||||
|
||||
return 0;
|
||||
}
|
9
task2/temp/build.sh
Normal file
9
task2/temp/build.sh
Normal file
@@ -0,0 +1,9 @@
|
||||
#!/usr/bin/env bash
|
||||
|
||||
set -ex
|
||||
|
||||
g++ -o output helloworld.cpp
|
||||
|
||||
echo "build done"
|
||||
|
||||
./output
|
BIN
task2/temp/chrome_OQtOQkTD0a.png
(Stored with Git LFS)
Normal file
BIN
task2/temp/chrome_OQtOQkTD0a.png
(Stored with Git LFS)
Normal file
Binary file not shown.
185
task2/temp/helloworld.cpp
Normal file
185
task2/temp/helloworld.cpp
Normal file
@@ -0,0 +1,185 @@
|
||||
#include <iostream>
|
||||
#include <stack>
|
||||
#include <string>
|
||||
#include <cctype>
|
||||
#include <map>
|
||||
|
||||
// 2. pseudo code of the algorithm
|
||||
// convert to postfix expression use stack,
|
||||
// 1. if it is an operand, add to postfix,
|
||||
// 2. if it is an operator,
|
||||
// 2.1 if it is the first operator, push into `s` stack
|
||||
// 2.2 if `s` stack already got element, compare the weight of opreators
|
||||
// 2.3 pop `s` stack until the top most one got smaller precedence
|
||||
// 3. if it is a bracket, push into stack `s`,
|
||||
// 3. if it is a closing bracket, pop all element from `s` stack back to postfix,
|
||||
// else return the result
|
||||
|
||||
// Function to convert infix expression to postfix expression
|
||||
std::string infixToPostfix(const std::string &infix)
|
||||
{
|
||||
std::stack<char> s;
|
||||
std::string postfix;
|
||||
std::map<char, int> precedence = {{'+', 1}, {'-', 1}, {'*', 2}, {'/', 2}, {'(', 0}};
|
||||
|
||||
for (char c : infix)
|
||||
{
|
||||
if (std::isalnum(c))
|
||||
{ // 1. if operand, add to postfix
|
||||
postfix += c;
|
||||
}
|
||||
|
||||
// 其實括號入面同括號出面嘅數你當佢兩條式計就得
|
||||
else if (c == '(')
|
||||
{ // 2. if '(', push to stack
|
||||
s.push(c);
|
||||
}
|
||||
|
||||
// 咁去呢度關括號呀嘛,
|
||||
// 咁佢咪會由個 stack 嗰度 port 返曬啲operator出嚟囉, 變咗可以 14+5*
|
||||
else if (c == ')')
|
||||
{ // 3. if ')', pop until '('
|
||||
while (!s.empty() && s.top() != '(')
|
||||
{
|
||||
postfix += s.top();
|
||||
s.pop();
|
||||
}
|
||||
if (!s.empty())
|
||||
s.pop(); // pop '('
|
||||
}
|
||||
else
|
||||
{ // 4. if operator, pop operators with greater or equal precedence
|
||||
while (!s.empty() && precedence[c] <= precedence[s.top()])
|
||||
{
|
||||
postfix += s.top();
|
||||
s.pop();
|
||||
}
|
||||
s.push(c); // push current operator
|
||||
}
|
||||
}
|
||||
|
||||
// 5. pop remaining operators, LIFO
|
||||
while (!s.empty())
|
||||
{
|
||||
postfix += s.top();
|
||||
s.pop();
|
||||
}
|
||||
|
||||
return postfix;
|
||||
}
|
||||
|
||||
// Function to evaluate postfix expression
|
||||
|
||||
// 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
|
||||
|
||||
int evaluatePostfix(const std::string &postfix)
|
||||
{
|
||||
std::stack<int> s;
|
||||
|
||||
for (char c : postfix)
|
||||
{
|
||||
if (std::isdigit(c))
|
||||
{ // 1. if operand, push to stack
|
||||
s.push(c - '0');
|
||||
}
|
||||
else
|
||||
{ // 2. if operator, pop two operands, evaluate and push result
|
||||
int op2 = s.top();
|
||||
s.pop();
|
||||
int op1 = s.top();
|
||||
s.pop();
|
||||
switch (c)
|
||||
{
|
||||
case '+':
|
||||
s.push(op1 + op2);
|
||||
break;
|
||||
case '-':
|
||||
s.push(op1 - op2);
|
||||
break;
|
||||
case '*':
|
||||
s.push(op1 * op2);
|
||||
break;
|
||||
case '/':
|
||||
s.push(op1 / op2);
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
return s.top(); // 3. result is the only element in stack
|
||||
}
|
||||
|
||||
int main()
|
||||
{
|
||||
std::string infix;
|
||||
// std::cout << "Enter an arithmetic expression: ";
|
||||
// std::getline(std::cin, infix);
|
||||
|
||||
// --- part 2 iteration:
|
||||
// 1,4,5,*,+
|
||||
// 1,20,+
|
||||
// 21
|
||||
std::cout << "e.g. 1 1+4*5" << std::endl;
|
||||
infix = "1+4*5";
|
||||
std::string postfix = infixToPostfix(infix);
|
||||
std::cout << "Postfix expression: " << postfix << std::endl;
|
||||
|
||||
int result = evaluatePostfix(postfix);
|
||||
std::cout << "Result: " << result << std::endl;
|
||||
|
||||
// --- part 2 iteration:
|
||||
// 1,4,+,5,*
|
||||
// 5,5,*
|
||||
// 25
|
||||
std::cout << "e.g. 2 (1+4)*5" << std::endl;
|
||||
infix = "(1+4)*5";
|
||||
postfix = infixToPostfix(infix);
|
||||
std::cout << "Postfix expression: " << postfix << std::endl;
|
||||
result = evaluatePostfix(postfix);
|
||||
std::cout << "Result: " << result << std::endl;
|
||||
|
||||
// --- part 2 iteration:
|
||||
// 1,4,+,5,-
|
||||
// 5,5,-
|
||||
// 0
|
||||
std::cout << "e.g. 3 1+4-5" << std::endl;
|
||||
infix = "1+4-5";
|
||||
postfix = infixToPostfix(infix);
|
||||
std::cout << "Postfix expression: " << postfix << std::endl;
|
||||
result = evaluatePostfix(postfix);
|
||||
std::cout << "Result: " << result << std::endl;
|
||||
|
||||
// --- part 2 iteration:
|
||||
// 1,4,5,*,+,5,*
|
||||
// 1,20,+,5,*
|
||||
// 21,5,*
|
||||
// 105
|
||||
std::cout << "e.g. 4 (1+4*5)*5" << std::endl;
|
||||
infix = "(1+4*5)*5";
|
||||
postfix = infixToPostfix(infix);
|
||||
std::cout << "Postfix expression: " << postfix << std::endl;
|
||||
|
||||
result = evaluatePostfix(postfix);
|
||||
std::cout << "Result: " << result << std::endl;
|
||||
|
||||
// --- part 2 iteration:
|
||||
// 1,4,5,*,+,2,5,*,-
|
||||
// =>1,20,+,2,5,*,-
|
||||
// =>21,2,5,*,-
|
||||
// =>21,10,-
|
||||
// =>11
|
||||
std::cout << "e.g. 5 (1+4*5)-2*5" << std::endl;
|
||||
infix = "(1+4*5)-2*5";
|
||||
postfix = infixToPostfix(infix);
|
||||
std::cout << "Postfix expression: " << postfix << std::endl;
|
||||
|
||||
result = evaluatePostfix(postfix);
|
||||
std::cout << "Result: " << result << std::endl;
|
||||
|
||||
return 0;
|
||||
}
|
185
task2/temp/main.cpp
Normal file
185
task2/temp/main.cpp
Normal file
@@ -0,0 +1,185 @@
|
||||
#include <iostream>
|
||||
#include <stack>
|
||||
#include <string>
|
||||
#include <cctype>
|
||||
#include <map>
|
||||
|
||||
// 2. pseudo code of the algorithm
|
||||
// convert to postfix expression use stack,
|
||||
// 1. if it is an operand, add to postfix,
|
||||
// 2. if it is an operator,
|
||||
// 2.1 if it is the first operator, push into `s` stack
|
||||
// 2.2 if `s` stack already got element, compare the weight of opreators
|
||||
// 2.3 pop `s` stack until the top most one got smaller precedence
|
||||
// 3. if it is a bracket, push into stack `s`,
|
||||
// 3. if it is a closing bracket, pop all element from `s` stack back to postfix,
|
||||
// else return the result
|
||||
|
||||
// Function to convert infix expression to postfix expression
|
||||
std::string infixToPostfix(const std::string &infix)
|
||||
{
|
||||
std::stack<char> s;
|
||||
std::string postfix;
|
||||
std::map<char, int> precedence = {{'+', 1}, {'-', 1}, {'*', 2}, {'/', 2}, {'(', 0}};
|
||||
|
||||
for (char c : infix)
|
||||
{
|
||||
if (std::isalnum(c))
|
||||
{ // 1. if operand, add to postfix
|
||||
postfix += c;
|
||||
}
|
||||
|
||||
// 其實括號入面同括號出面嘅數你當佢兩條式計就得
|
||||
else if (c == '(')
|
||||
{ // 2. if '(', push to stack
|
||||
s.push(c);
|
||||
}
|
||||
|
||||
// 咁佢呢度關括號呀嘛,
|
||||
// 咁佢咪會由個 stack 嗰度 port 返曬啲operator出嚟囉, 變咗可以 14+5*
|
||||
else if (c == ')')
|
||||
{ // 3. if ')', pop until '('
|
||||
while (!s.empty() && s.top() != '(')
|
||||
{
|
||||
postfix += s.top();
|
||||
s.pop();
|
||||
}
|
||||
if (!s.empty())
|
||||
s.pop(); // pop '('
|
||||
}
|
||||
else
|
||||
{ // 4. if operator, pop operators with greater or equal precedence
|
||||
while (!s.empty() && precedence[c] <= precedence[s.top()])
|
||||
{
|
||||
postfix += s.top();
|
||||
s.pop();
|
||||
}
|
||||
s.push(c); // push current operator
|
||||
}
|
||||
}
|
||||
|
||||
// 5. pop remaining operators, LIFO
|
||||
while (!s.empty())
|
||||
{
|
||||
postfix += s.top();
|
||||
s.pop();
|
||||
}
|
||||
|
||||
return postfix;
|
||||
}
|
||||
|
||||
// Function to evaluate postfix expression
|
||||
|
||||
// 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
|
||||
|
||||
int evaluatePostfix(const std::string &postfix)
|
||||
{
|
||||
std::stack<int> s;
|
||||
|
||||
for (char c : postfix)
|
||||
{
|
||||
if (std::isdigit(c))
|
||||
{ // 1. if operand, push to stack
|
||||
s.push(c - '0');
|
||||
}
|
||||
else
|
||||
{ // 2. if operator, pop two operands, evaluate and push result
|
||||
int op2 = s.top();
|
||||
s.pop();
|
||||
int op1 = s.top();
|
||||
s.pop();
|
||||
switch (c)
|
||||
{
|
||||
case '+':
|
||||
s.push(op1 + op2);
|
||||
break;
|
||||
case '-':
|
||||
s.push(op1 - op2);
|
||||
break;
|
||||
case '*':
|
||||
s.push(op1 * op2);
|
||||
break;
|
||||
case '/':
|
||||
s.push(op1 / op2);
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
return s.top(); // 3. result is the only element in stack
|
||||
}
|
||||
|
||||
int main()
|
||||
{
|
||||
std::string infix;
|
||||
// std::cout << "Enter an arithmetic expression: ";
|
||||
// std::getline(std::cin, infix);
|
||||
|
||||
// --- part 2 iteration:
|
||||
// 1,4,5,*,+
|
||||
// 1,20,+
|
||||
// 21
|
||||
std::cout << "e.g. 1 1+4*5" << std::endl;
|
||||
infix = "1+4*5";
|
||||
std::string postfix = infixToPostfix(infix);
|
||||
std::cout << "Postfix expression: " << postfix << std::endl;
|
||||
|
||||
int result = evaluatePostfix(postfix);
|
||||
std::cout << "Result: " << result << std::endl;
|
||||
|
||||
// --- part 2 iteration:
|
||||
// 1,4,+,5,*
|
||||
// 5,5,*
|
||||
// 25
|
||||
std::cout << "e.g. 2 (1+4)*5" << std::endl;
|
||||
infix = "(1+4)*5";
|
||||
postfix = infixToPostfix(infix);
|
||||
std::cout << "Postfix expression: " << postfix << std::endl;
|
||||
result = evaluatePostfix(postfix);
|
||||
std::cout << "Result: " << result << std::endl;
|
||||
|
||||
// --- part 2 iteration:
|
||||
// 1,4,+,5,-
|
||||
// 5,5,-
|
||||
// 0
|
||||
std::cout << "e.g. 3 1+4-5" << std::endl;
|
||||
infix = "1+4-5";
|
||||
postfix = infixToPostfix(infix);
|
||||
std::cout << "Postfix expression: " << postfix << std::endl;
|
||||
result = evaluatePostfix(postfix);
|
||||
std::cout << "Result: " << result << std::endl;
|
||||
|
||||
// --- part 2 iteration:
|
||||
// 1,4,5,*,+,5,*
|
||||
// 1,20,+,5,*
|
||||
// 21,5,*
|
||||
// 105
|
||||
std::cout << "e.g. 4 (1+4*5)*5" << std::endl;
|
||||
infix = "(1+4*5)*5";
|
||||
postfix = infixToPostfix(infix);
|
||||
std::cout << "Postfix expression: " << postfix << std::endl;
|
||||
|
||||
result = evaluatePostfix(postfix);
|
||||
std::cout << "Result: " << result << std::endl;
|
||||
|
||||
// --- part 2 iteration:
|
||||
// 1,4,5,*,+,2,5,*,-
|
||||
// =>1,20,+,2,5,*,-
|
||||
// =>21,2,5,*,-
|
||||
// =>21,10,-
|
||||
// =>11
|
||||
std::cout << "e.g. 5 (1+4*5)-2*5" << std::endl;
|
||||
infix = "(1+4*5)-2*5";
|
||||
postfix = infixToPostfix(infix);
|
||||
std::cout << "Postfix expression: " << postfix << std::endl;
|
||||
|
||||
result = evaluatePostfix(postfix);
|
||||
std::cout << "Result: " << result << std::endl;
|
||||
|
||||
return 0;
|
||||
}
|
BIN
task2/temp/output
Normal file
BIN
task2/temp/output
Normal file
Binary file not shown.
31
task2/temp/prompt.md
Normal file
31
task2/temp/prompt.md
Normal file
@@ -0,0 +1,31 @@
|
||||
# Task
|
||||
I want you to write a simple calculator to evaluate arithmetic expressions.
|
||||
Solve your problem step by step with C++.
|
||||
Please consider the precedence of operators as well.
|
||||
|
||||
## It includes two parts:
|
||||
|
||||
- In the first part, you need to convert the input to a postfix expression.
|
||||
- In the second part, you need to evaluate the postfix expression that you get in the first
|
||||
part.
|
||||
- You need to implement your algorithms using c++.
|
||||
- You also need to write comment inside the code explaining the following items.
|
||||
|
||||
## Part 1: Convert an expression to postfix expression using stack in STL
|
||||
|
||||
1. how to read and store the input?
|
||||
2. pseudo code of the algorithm
|
||||
3. data structures used in the algorithm
|
||||
4. time complexity
|
||||
5. space complexity
|
||||
6. how to store the postfix expression?
|
||||
|
||||
## Part 2: Use stack of STL to evaluate a postfix expression
|
||||
|
||||
1. how to read the postfix expression
|
||||
2. pseudo code of the algorithm
|
||||
3. data structures used in the algorithm
|
||||
4. time complexity
|
||||
5. space complexity
|
||||
6. how to output the final result?
|
||||
|
Reference in New Issue
Block a user