-
Notifications
You must be signed in to change notification settings - Fork 0
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Program calculates postfix expressions #12
base: master
Are you sure you want to change the base?
Conversation
{ | ||
this->data = 0; | ||
this->next = nullptr; | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Деструктор не нужен :)
|
||
struct Element | ||
{ | ||
Element(int data); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
explicit
struct Element | ||
{ | ||
Element(int data); | ||
~Element(); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Деструктор не нужен :)
int fromPostfixToResult(string const & str) | ||
{ | ||
Stack s; | ||
int sizeOfStr = (int)str.size(); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Зачем каст к инту, она ж возвращает size_t небось, можно было for (size_t i = 0; i < sizeOfStr; ++i)
} | ||
} | ||
int result = s.peek(); | ||
return result; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Можно просто return s.peek();, хотя как у Вас отлаживаться удобнее.
// ���������� | ||
~Stack(); | ||
// ����������� ����������� | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Невидимый конструктор :)
// ���������� �������� � ������ | ||
void push(int data); | ||
// �������� �������� �� ������ | ||
int pop(); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Может, лучше bool возвращать, раз оно только 0 или -1?
// ���������� �� �������� ������� | ||
int peek(); | ||
// ��������� �������� �� ���� ������ | ||
bool isEmpty(); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
bool isEmpty() const;, оно не меняет состояние объекта
// �������� �������� �� ������ | ||
int pop(); | ||
// ���������� �� �������� ������� | ||
int peek(); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
int peek() const;, оно не меняет состояние объекта
cout << "result is " << fromPostfixToResult(str) << endl; | ||
|
||
|
||
system("pause"); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Нельзя :)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Надо ещё чуть-чуть доисправлять
int fromPostfixToResult(string const & str) | ||
{ | ||
Stack s; | ||
size_t sizeOfStr = str.size(); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
const? Тут вообще много где можно const написать, но тут это было бы особенно полезно, потому что область видимости у sizeOfStr очень большая
} | ||
else if (!isOperator(str[i])) | ||
{ | ||
if (s.peek() == -1) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ой-ой. Ввожу 23-, мне говорят, что я неправ :) Вообще, для сигнализации об ошибке никогда-никогда нельзя использовать допустимые значения. Если множество допустимых значений покрывает весь тип, возвращаемый методом, то тип приходится расширять (например, возвращать пару), либо, что, как правило, более идеологически верно, использовать исключения.
I used stack for implementation