-1

code just turns infix to infix but I can't figure out why?, my code is supposed to turn infix to rpn but this line here:

while( !presedence.empty() && presedence.top() >= num2 )
               {
                output.push(oprators.top());
                oprators.pop();
                presedence.pop();
               }

is the one that causes error, I think, because if I remove the '=' it just works, be it without allowing letters of the same precedence to join the output queue.

full code:

  #include <cmath>
        #include <stack>
        #include<queue>
        #include <iostream>
        #include <regex>
         #include <vector>
        #include <thread>
         #include <bitset>
    
     #include <ctime>
    
     #include <string.h>
    
     #include <math.h>
    #include <bits/stdc++.h>
    
     #define M_PI 3.14159265358979323846264338327950288419716939937510582097494
    
     #include<windows.h>
    #define gkey GetAsyncKeyState
     using namespace std;
    #define rxs regex_search
    #define loop while(true)
    double a;
    double num1;
     double c3 = 299792458;
     double c3sq = 89875517873681764;
     int mc;
         string again;
    
     stack<int> presedence;
     stack<string> oprators;
     queue<double> numbers;
     stack<char> test;
     double num5;
     double num6;
     int E;
     int abrt = 0;
     double num2;
    
     double ans = num1 + num2;
     int num;
     int numalt = 0;
     int numaltt = 0;
     //int nums [] = {srand(time(0));rand();}
    
    bool autoregex(string test){
        regex e ("[-+]?([0-9]*\.[0-9]+|[0-9]+)");
       if (regex_match (test,e))
            return true;
    
        return false;
    }
    bool autrege(string test){
        regex aret("SIN|LOG|sqrt|sin|log|tan|pi|e|ln|EE|[^0-9a-z$@#&\]");
                   if (regex_match (test,aret)){
                    return true;
                   }
    else{
        return false;}
                   }
                   void namehere(string test){
    if(autrege(test) == true){
    regex bret("[+-]");
    regex cret("[/*xX]");
    regex dret("SIN|LOG|sqrt|sin|log|tan|pi|!|e|ln|EE|\\^");
    regex omega("\\)");
    regex canmae("\\(");
    if (regex_match (test,bret)){num2 = 1;};
    if (regex_match (test,cret)){num2 = 2;};
    if (regex_match (test,dret)){num2 = 3;};
    if (regex_match (test,omega)){num2 = 4;numaltt = numaltt + 1;};
    if (regex_match (test,canmae)){num2 = 4;numalt = numalt + 1;};
    
    }
    
    
    
    
                   }
    
    int main()
    {
    vector<double> vec;
        again = "n";
    
     while(again == "n"&&abrt == 0){
     // queue<double> numbers; stack<int> pres;
            queue<string> output;
       int test;
     string name;
       getline(cin, name);
       istringstream iss(name);
     string token;
        while(iss >> token)
        {
          if(autoregex(token) == true){
                output.push(token);
          }
          if(autrege(token)== true)//token area
          {
    
    
              namehere(token);
              num6++;
              if(num2 == -1){cout<<"wrong move: ";again = "n";again = "y";cout<<num2<<endl;}
           while(presedence.empty() == 1 && oprators.empty() == 1)
            {
                presedence.push(num2);
                oprators.push(token);
           }
           while(presedence.top() < num2 && !presedence.empty())
            {
            oprators.push(token);
            presedence.push(num2);
    
           }
    
           while(presedence.top() >= num2 && !presedence.empty())
           {
            output.push(oprators.top());
            oprators.pop();
            presedence.pop();
           }
    //3-T 2-ME
          }
    
    }
    while(presedence.empty() != 1 && oprators.empty() != 1){  output.push(oprators.top());
    oprators.pop();presedence.pop();}
    while(!output.empty()){cout<<output.front()<<", ";output.pop();}
    
    }
    
    
    while(again != "n"&&abrt == 0){
    
    }
    
    
    }

it breaks when I try to see if the top of the operator stack is greater than the current operator,any reasons?

bigbroin
  • 1
  • 1
  • 1
  • 1
    add a bit of context about what your code is doing? – Aadil Hoda Dec 25 '21 at 22:38
  • Please note that questions should not be changed in such a way that they invalidate existing answers. If you want to add additional code to a question, which applies fixes mentioned in an existing answer, then you can add it as an "update" to the bottom of the question, while keeping the original question intact. – Andreas Wenzel Dec 25 '21 at 23:24
  • Regarding your followup-question: Have you tried running your code line by line in a debugger while monitoring the values of all variables, in order to determine at which point your program stops behaving as intended? If you did not try this, then you may want to read this: [What is a debugger and how can it help me diagnose problems?](https://stackoverflow.com/q/25385173/12149471) You may also want to read this: [How to debug small programs?](https://ericlippert.com/2014/03/05/how-to-debug-small-programs/). – Andreas Wenzel Dec 25 '21 at 23:37
  • @AndreasWenzel tried the debugging, doesn't show me watches, I added presedence.top() to the watches but it kept giving me errors on it – bigbroin Dec 26 '21 at 03:07

1 Answers1

0

NOTE: This answer refers to revision 2 of the question, which was about why OP's code was crashing. Meanwhile, OP has applied the fix mentioned in my answer (which seems to have fixed the crash), and has overwritten the question to address the next problem in the code. Therefore, this answer of mine no longer corresponds to the question in its current state.

The line

while(presedence.top() >= num2 && !presedence.empty())

is wrong. You should check whether the stack is empty before accessing the topmost element:

while( !presedence.empty() && presedence.top() >= num2 )

Otherwise, your program will be invoking undefined behavior if the stack is indeed empty.

Andreas Wenzel
  • 22,760
  • 4
  • 24
  • 39