Click here to Skip to main content
15,924,196 members
Please Sign up or sign in to vote.
1.00/5 (3 votes)
See more:
for some reason this 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.empty() && presedence.top() < num2 )
           {
           oprators.push(token);
           presedence.push(num2);

          }

       while( !presedence.empty() && presedence.top() > num2 )
              {
               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){

   }


   }


stops working(as in doesn't do what it is expected to do);
it is expected to convert infix to rpn, which it does, but when I try to see if the operator's precedence is = to the top operator it just spits out the expression in infix, where did i go wrong in the algorithm?

What I have tried:

I've tried checking the watch menu, didn't help that much, couldn't see the data for a stack on it, I've tried adding the == as a separate if/while statement doesn't work, I've tried changing the algorithm to be based of off numbers, didn't work, what should I do now?
Posted
Updated 25-Dec-21 23:10pm
Comments
Rick York 26-Dec-21 0:55am    
Why is M_PI defined? You do not use it in your code and it is already defined in math.h. It certainly does not need to be defined again.

1) Get rid of your global variables. 99.99 % of the time, a global variable is a sign of bad design.

2) Let's be kind and assume that your code is not indented like it is in the post, and that the layout style is the result of cut&paste weirdness. If it is indented as show here, then for goodness sake, take a look at some of the indent styles here[^], choose one you like, or some variation thereof and stick with it.

3) Add some meaningful comments to your code - particularly if you are posting it to a web site for assistance. In the long run this will help you later on if you need to come back to this and are trying to remember what's going on. In the short term, it will help those of us that are willing to look at the code a clue as to what it is the code is supposed to be doing.

4) provide a concrete example of what input is causing your issue. The information you've provided here doesn't give me any clue as to what your program is expecting and what's going wrong.

5) If at all possible, reduce the code to the bare minimum that produces the error or unwanted behavior. For the most part, 150 lines of code is more than most of us are willing to wade through to find what went wrong. Ideally, if you can narrow down the problem to maybe less than 50 lines, then there's a better chance someone will actually look at it.

6) In this case, maybe give some clue as to how to exit the program! The user and/or tester should not have to abort the program to get it to stop. Maybe add some prompt to let the user know that input is expected?

7) Research how to use the debugger for your system. There will probably be ways to examine the contents of STL containers, but you may have to do some work to find do so. Google is your friend. Probably someone here can help with how to examine the contents of a stack if they know what your programming environment is.

8) Ultimately, you can use normal output routines to help show what your code is doing. It might get ugly, but you could write a print_stack() function that will print the contents of the stack. Don't forget that you can't iterate over a stack, so you will need to create a copy before you print it. This can be accomplished most simply by not using a reference as the function's argument. You can then call your stack print routine at strategic places in your code to show you what the stack looks like. Since you have stack<int>, stack<char>, and stack<string> this might be a good time to look into template functions. You should be able to write a single template print_stack() function that will handle any stack type.
 
Share this answer
 
Quote:
Code doesn't convert infix to rpn when I check if the current precedence is == to the top precedence and I don't know why

Your code do not behave the way you expect, or you don't understand why !

There is an almost universal solution: Run your code on debugger step by step, inspect variables.
The debugger is here to show you what your code is doing and your task is to compare with what it should do.
There is no magic in the debugger, it don't know what your code is supposed to do, it don't find bugs, it just help you to by showing you what is going on. When the code don't do what is expected, you are close to a bug.
To see what your code is doing: Just set a breakpoint and see your code performing, the debugger allow you to execute lines 1 by 1 and to inspect variables as it execute.

Debugger - Wikipedia, the free encyclopedia[^]

Mastering Debugging in Visual Studio 2010 - A Beginner's Guide[^]
Basic Debugging with Visual Studio 2010 - YouTube[^]

1.11 — Debugging your program (stepping and breakpoints) | Learn C++[^]

The debugger is here to only show you what your code is doing and your task is to compare with what it should do.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900