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 in the SCR. However, with some input, there are problems with parentheses. For example, if you enter a + b c d + (ef) (g h + i), an error occurs

An exception was thrown at the address 0x010F3CD3 in OPZ.exe: 0xC0000005: access violation while reading at 0x00000000. And points to the string

 while (begin->info != '(') { 

I look at debugging, there the output line is abcd ** + ef-gh * (* + i + As I understand it, the error is because the opening bracket is removed from the stack, but I do not understand why this happens

  • This, of course, is not the answer, but C ++ is full of standard containers. Take better std::vector , and you will have much less problems. - HolyBlackCat
  • Yes, I would love to, but do the job manually through the stacks - Sergei Mikhailovskii
  • @SergeiMikhailovskii I would advise you to check begin on NULL before doing any operations with it - ThusMad
  • @ThusMad, fixed, but the problem remained the same - Sergei Mikhailovskii

2 answers 2

first mistake:

 stack *begin = NULL; 

since after reading the letter in the next cycle, begin remains zero, and the compiler will not be able to check:

 else { if (priority(begin->info) <= priority(input[i])) //... 

so:

 stack *begin = new stack; 
  • and not related to your question error: `

second mistake:

you never delete objects created in the free memory area with new , i.e. stack objects

third mistake:

you do not return anything from the int priority(char op) function if op is not one of the listed characters (it is better to return zero and use it in your conditions, that is, if priority(char op)== 0 , means ` op is not a symbol of a mathematical operation

and finally: if you were storing one string("*/()%+-"} , then using string::find could make the same code more compact and with less if/else s

  • @HolyBlackCat, an object is not a heap, and the objects created in the heap are on the stack - AR Hovsepyan
  • HolyBlackCat, in this case we are not talking about the standard stack, but the intrusive stack created by the author of the question - AR Hovsepyan

Forgot to check brackets in executeFromStackElements .

 void executeFromStackElements(string & output, stack *& begin) { while (begin != NULL and begin->info != '(') { output += begin->info; begin = begin->next; }} 
  • and delete begin memory inaccessible. - AlexGlebe
  • and this is also correct, but the problem with the fall of the code does not solve - AR Hovsepyan