Click here to Skip to main content
15,886,840 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
#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)
            {
            oprators.push(token);
            presedence.push(num2);
    
           }
    
           while(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){
    
    }
    
    
    }


What I have tried:

i've got a rough idea of which line is causing the error:
while(presedence.top() >= num2)
        {
         output.push(oprators.top());
         oprators.pop();
         presedence.pop();
        }

because when I remove the = it works just fine, what I need to know is why adding the = causes a stack overflow?
Posted
Updated 24-Dec-21 2:39am
Comments
Richard MacCutchan 24-Dec-21 4:46am    
Most likely because you have an out of control loop in that while statement. Use the debugger to trace what is happening.
Rick York 24-Dec-21 11:38am    
You are including math.h so there is no need to define M_PI because it is already done in math.h. You can add the definition, #define _USE_MATH_DEFINES before including math.h and you will get all of the math definitions enabled like M_PI, M_E, etc.

Quote:
I keep getting process returned -1073741819 (0xc0000005) whenever I run this code, what's the reason?

You have already been told that:
Greg Utas said:
0xc0000005 is the Windows error code for an invalid memory access (SIGSEGV)

This means that for some reasons, your code messes with memory it don't own.
Since you didn't gave a sample input that cause the problem, and since we don't know what is supposed to do the code, it is difficult to do a realistic run.

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
 
As Solution 1 mentioned, you've already been told what the problem is.

Please do not repost questions. It is considered inappropriate to such a degree that a repost is subject to being closed.

You need to debug your code to find out why it overflows the stack. No one is going to debug it for you. They wouldn't be doing you a favor anyway, because debugging is an indispensable skill that you need to learn. Trying to debug code by studying it, or executing it with pencil and paper, is error prone and extremely inefficient for all but the simplest programs.

Since you're writing C++, I have some suggestions.

1. Get rid of the #define statements, which are rarely appropriate in C++. The first one can be replaced with
C++
const double M_PI = 3.14159265358979323846;
The others are just abbreviations, so spell them out in full. In a large program that uses external interfaces, no one will want to have to remember your custom abbreviations to read your code.

2. Don't check a bool by comparing it to 0, 1, false, or true. It makes the code look like it was written by a newbie. Curiously, the code only does this in some places but not others.
C++
// Existing

if(autrege(test) == true)...
while(presedence.empty() == 1 && oprators.empty() == 1)...

// Cleaned up

if(autrege(test))...
while(!presedence.empty() && !oprators.empty())...
3. Format your code by indenting code within braces, putting each statement on its own line, adding a space before and after binary operators, and using a consistent style. This will make your code much easier to read. The first thing I do, when I need to study poorly formatted code, is to reformat it.
C++
// Existing

while(presedence.empty() != 1 && oprators.empty() != 1){output.push(oprators.top());
oprators.pop();presedence.pop();}
while(!output.empty()){cout<<output.front()<<", ";output.pop();}

// Cleaned up

while(!presedence.empty() && !oprators.empty()) {
    output.push(oprators.top());
    oprators.pop();
    presedence.pop();
}
while(!output.empty()) {
    cout << output.front() << ", ";
    output.pop();
}
Being consistent means things like
- Does a { go at the end of the line or on its own line?
- Is code indented a multiple of 3, 4, or 8 spaces?
- Is there a space between a keyword and a ( that follows?
 
Share this answer
 
v2
Comments
0x01AA 26-Dec-21 9:09am    
Small question: Why define M_PI again? It is allready in math.h or not?
Greg Utas 26-Dec-21 9:14am    
I didn't look to see if a constant for pi was already defined in math.h.

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