Click here to Skip to main content
15,400,388 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
I already know this code is inefficient, but how do I make it more efficient?



#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;
bool isnumber(const string& s)
{
    for (char const &ch : s) {
        if (isdigit(ch) == 0)
            return false;
    }
    return true;
 }
     stack<int> presedence;
     stack<string> oprators;
     queue<double> numbers;
     stack<char> test;
    string tests = "test string hhahah \n";
     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();}
double str2int(string test)
{
    int qtest;
    stringstream etest;
    etest << test;
    etest >> qtest;
    return qtest;
}
    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";
double token2;
queue<double> qans;
     while(again == "n"&&abrt == 0){
     // queue<double> numbers; stack<int> pres;
            queue<string> output;
            queue<double> ans;
       int test;
     string name;
     cout<<"input expected: "<<endl;
       getline(cin, name);
       istringstream iss(name);
     string token;
        while(iss >> token)
        {
          if(autoregex(token)){
            output.push(token);
          }
          if(autrege(token))//token area
          {


              namehere(token);
            if( !presedence.empty() && num2 == presedence.top())
            {
                output.push(oprators.top());
                oprators.pop();
                oprators.push(token);
                presedence.pop();
                presedence.push(num2);

            }
           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();
             oprators.push(token);
            presedence.push(num2);
           }
    //3-T 2-ME
          }

    }

    while(presedence.empty() != 1 && oprators.empty() != 1){  output.push(oprators.top());
    oprators.pop();presedence.pop();}
 while(!output.empty())
 {
  if(autoregex(output.front()))
  {
      ans.push(str2int(output.front()));
      output.pop();
  }
  if(autrege(output.front()))
  {
    double x   = ans.front();
    ans.pop();
    double y = ans.front();
    ans.pop();
    if(output.front() == "+")
    {
     ans.push(y + x);
     output.pop();
    }
        if(output.front() == "-")
    {
     ans.push(y - x);
     output.pop();
    }
        if(output.front() == "*")
    {
     ans.push(y * x);
     output.pop();
    }
        if(output.front() == "/")
    {
     ans.push(y / x);
     output.pop();
    }
        if(output.front() == "^")
    {
     ans.push(pow(y,x));
     output.pop();
    }

  }
 }
 if(output.empty())
 {
     cout<<ans.front()<<endl;
 }

    }


    while(again != "n"&&abrt == 0){
string testt = "4.93827061728e+12";
isnumber(testt);

    }


    }


What I have tried:

ya nothing, couldn't think of anything
Posted
Updated 29-Dec-21 19:13pm
Comments
Dave Kreskowiak 29-Dec-21 18:43pm
   
First, proper code formatting is everything if you want your code to be readable.

Second, if you're trying to make a parser, RegEx is not the way to go. Pretty much the only way to make this code "more efficient" is to scrap it and completely rethink how you're approaching the problem.
Abdulrahmon Tijani 29-Dec-21 19:46pm
   
why can't I use regex?
Rick York 29-Dec-21 19:58pm
   
It's not that you can't use them. You asked how to make your code more efficient and that is the answer - use something besides regular expressions. What you need is a non-destructive tokenizer. A function like strtok is destructive so it is not appropriate for this. At least, that's how I would approach it.
Abdulrahmon Tijani 29-Dec-21 20:17pm
   
Destructive?? Im confused,im a beginner so can you explian that?

The biggest thing you can do is stop creating the regex objects every time you call autoregex(), autorege() or namehere(), since that seems to be the meat of your program.

Consider these two programs, first a program that continually creates a regex object:
C++
#include <regex>
#include <iostream>
#include <fstream>
#include <cstdio>

bool foo(const std::string& test)
{
    std::regex e{"[-+]?([0-9]*\\.[0-9]+|[0-9]+)"};
    if( regex_match(test, e) )
        return true;
    return false;
}

int main(int argc, char **argv)
{   
    std::ifstream f(argv[1]);

    std::string str;
    while(getline(f, str))
    {
        foo(str);
    }

}
and the same program, only I instantiate the regex at startup:
C++
#include <regex>
#include <iostream>
#include <fstream>
#include <cstdio>

bool foo(const std::string& test, std::regex re)
{
    if( regex_match(test, re) )
        return true;
    return false;
}   

int main(int argc, char **argv)
{   
    std::ifstream f(argv[1]);

    std::regex e{"[-+]?([0-9]*\\.[0-9]+|[0-9]+)"};
    std::string str;
    while(getline(f, str))
    {
        foo(str, e);
    }

}

On my system, with an input file of 1000 lines, the first one runs in about 7 seconds. The second one runs in about 0.05 seconds. That's about 140 times faster, right there.
   
The answer you're looking for would take an entire book to explain. I'm working on a parser for an interpreter right now. The problem is for me to explain how to do this and make your code work would take more typing than the code I've written for my parser.

Pick up a book on the subject, like Crafting Interpreters[^].
   

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