Skip to content

Commit

Permalink
TheBestLabsInTheWorld
Browse files Browse the repository at this point in the history
  • Loading branch information
Korobokkk committed Feb 9, 2025
1 parent d834084 commit f5a198f
Show file tree
Hide file tree
Showing 6 changed files with 37 additions and 23 deletions.
2 changes: 0 additions & 2 deletions KiselevIV/03_lab/include/array_stack.h
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,6 @@ class array_stack : public stack<TElem>
elems = new TElem[maxSize];
top = -1;
}


array_stack(const array_stack<TElem>& s)
{
maxSize = s.maxSize;
Expand Down
1 change: 0 additions & 1 deletion KiselevIV/03_lab/include/list_stack.h
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ class list_stack : public stack<TElem>

public:
list_stack() {};

list_stack(const list_stack<TElem>& s)
{
elems = s.elems;
Expand Down
4 changes: 2 additions & 2 deletions KiselevIV/03_lab/include/postfix_form.h
Original file line number Diff line number Diff line change
Expand Up @@ -27,12 +27,12 @@ class PostfixForm
public:
PostfixForm(const string& s, char stype) : infix(s)
{
priorts = { {"+", 1}, {"-", 1}, {"*", 2}, {"/", 2} };
priorts = { {"+", 1}, {"-", 1}, {"*", 2}, {"/", 3} };
stackType = stype;
to_postfix();
}
void setOperands(map<string, double> opernds);
void setOperands();
void setOperands(map<string, double> opernds);
string getInfix() const;
string getPostfix() const;
double calculate();
Expand Down
8 changes: 4 additions & 4 deletions KiselevIV/03_lab/samples/postfix_sample.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@ using namespace std;
int main()
{
string infix = setInfix();
PostfixForm tmp(infix, setStackType());
tmp.setOperands();
cout << tmp.getInfix() << "\n" << tmp.getPostfix() << "\n" << tmp.calculate();
PostfixForm bob(infix, setStackType());
bob.setOperands();
cout << bob.getInfix() << "\n" << bob.getPostfix() << "\n" << bob.calculate();
return 0;
}
}
24 changes: 13 additions & 11 deletions KiselevIV/03_lab/src/postfix_form.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -91,27 +91,27 @@ void PostfixForm::parse()
{
lexems.push_back(token);
}
return;
}

void PostfixForm::to_postfix() {
void PostfixForm::to_postfix()
{
stack<string>* operations, * operands;
allocateStack(operands, type_of_stack);
allocateStack(operations, type_of_stack);
parsing();
allocStack(operands, stackType);
allocStack(operations, stackType);
parse();

string currentToken, tempToken;

for (size_t i = 0; i < lex.size(); i++) {
currentToken = lex[i];

for (size_t i = 0; i < lexems.size(); i++) {
currentToken = lexems[i];
if (currentToken == "+" || currentToken == "-" || currentToken == "*" || currentToken == "/") {

if (currentToken.length() > 1) {
operands->push(currentToken);
continue;
}

while (!operations->is_empty() && priority[currentToken] < priority[operations->show_top()]) {
while (!operations->is_empty() && priorts[currentToken] < priorts[operations->show_top()]) {
operands->push(operations->show_top());
operations->pop();
}
Expand Down Expand Up @@ -148,8 +148,6 @@ void PostfixForm::to_postfix() {
}

return;
}void PostfixForm::setOperands(map <string, double> op) {
operands = op;
}
void PostfixForm::setOperands()
{
Expand All @@ -176,6 +174,10 @@ void PostfixForm::setOperands()
}
return;
}
void PostfixForm::setOperands(map<string, double> opernds) {
// Êîïèðóåì ïåðåäàííûå îïåðàíäû â ïðèâàòíîå ïîëå êëàññà
this->operands = opernds;
}
double PostfixForm::calculate()
{
stack<double>* s;
Expand Down
21 changes: 18 additions & 3 deletions KiselevIV/03_lab/test/test_postfix_form.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -76,8 +76,8 @@ TEST(Postfix_form, can_turn_infix_to_postfix_on_Lstack1)

TEST(Postfix_form, can_turn_infix_to_postfix_on_Astack2)
{
PostfixForm tmp("A-B*(A+C)", 'A');
EXPECT_EQ(tmp.getPostfix(), "A B A C + * - ");
PostfixForm let("A-B*(A+C)", 'A');
EXPECT_EQ(let.getPostfix(), "A B A C + * - ");
}

TEST(Postfix_form, can_turn_infix_to_postfix_on_Lstack2)
Expand Down Expand Up @@ -136,4 +136,19 @@ TEST(Postfix_form, Lstack2)
map<string, double> operands{ {"A", 1}, {"B", 2}, {"C", 3} };
tmp.setOperands(operands);
EXPECT_EQ(tmp.calculate(), -7);
}
}
TEST(PostfixForm, Astack3)
{
PostfixForm tmp("(A-B)/(C-A)*(K+B)+A", 'A');
map<string, double>operands{ {"A", 1}, {"B", 3}, {"C", 2}, {"K", -2} };
tmp.setOperands(operands);
EXPECT_EQ(tmp.calculate(), -1);
}

TEST(PostfixForm, Lstack3)
{
PostfixForm tmp("(A-B)/(C-A)*(K+B)+A", 'L');
map<string, double>operands{ {"A", 1}, {"B", 3}, {"C", 2}, {"K", -2} };
tmp.setOperands(operands);
EXPECT_EQ(tmp.calculate(), -1);
}

0 comments on commit f5a198f

Please sign in to comment.