Question:
There is a code:
#include <iostream>
#include <string>
using namespace std;
struct stack {
char info;
stack* next;
};
void addToStack(char sym, stack *&begin);
void showStack(stack *begin);
int priority(char op);
void showElementsInBrackets(string &output, stack *&begin);
void executeFromStackElements(string &output, stack *&begin);
int main() {
stack *begin = NULL;
string input, output = "";
cin >> input;
for (int i = 0; i < input.length(); i++) {
if (input[i] >= 'a' && input[i] <= 'z') {
output += input[i];
}
else if (input[i] == '+' || input[i] == '-' || input[i] == '*' || input[i] == '/' || input[i] == '%' || input[i] =='(') {
if (begin == NULL || input[i] == '(') {
addToStack(input[i], begin);
}
else {
if (priority(begin->info) <= priority(input[i])) {
addToStack(input[i], begin);
}
else {
executeFromStackElements(output, begin);
addToStack(input[i], begin);
}
}
}
else if (input[i] == ')') {
showElementsInBrackets(output, begin);
}
}
executeFromStackElements(output, begin);
cout << "Output:" << endl;
cout << output << endl;
system("pause");
return 0;
}
void addToStack(char sym, stack *& begin)
{
stack *t = new stack;
t->info = sym;
t->next = begin;
begin = t;
}
void showStack(stack * begin)
{
stack *t = begin;
while (t != NULL) {
cout << t->info << endl;
t = t->next;
}
}
int priority(char op)
{
if (op == '(') {
return 1;
}
else if (op == '+' || op == '-') {
return 2;
}
else if (op == '*' || op == '/' || op == '%') {
return 3;
}
}
void showElementsInBrackets(string & output, stack *& begin)
{
while (begin->info != '(' && begin != NULL) {
output += begin->info;
begin = begin->next;
}
begin = begin->next;
}
void executeFromStackElements(string & output, stack *& begin)
{
while (begin != NULL) {
output += begin->info;
begin = begin->next;
}
}
The task is to translate the expression into the HMO. However, with certain input, there are problems with parentheses. For example, if you enter a + b c d + (ef) (g h + i), the error occurs
An exception was thrown at 0x010F3CD3 in OPZ.exe: 0xC0000005: Read access violation at 0x00000000. And points to the line
while (begin->info != '(') {
I look at the debug, where the output line is abcd ** + ef-gh * (* + i + As I understand it, the error is due to the fact that the opening parenthesis is popped from the stack, but I do not understand why this is happening
Answer:
first error:
stack *begin = NULL;
because after reading the letter in the next loop, begin
remains zero, and the compiler will not be able to check:
else {
if (priority(begin->info) <= priority(input[i]))
//...
That's why:
stack *begin = new stack;
- and errors not related to your question: `
second error:
you never delete objects created in the free memory area via new
, i.e. stack objects.
third error:
you do not return anything from the function int priority(char op)
if op
is not one of the characters you listed (it is better to return zero and use this in your conditions, i.e. if priority(char op)== 0
, then ` op is not a math operation symbol
and finally: if you stored one string("*/()%+-"}
, then using string::find
could make the same code more compact and with fewer if/else
s