i'm doing similar user did: error: deque iterator not dereferenceable
i've been looking age cannot see i'm going wrong. solution other poster finding place tried pop or top deque 0 elements. can't find i'm doing in code.
edit: suspect issue within syalg or osprocess, if helps.
// testcalculator.cpp : main project file. #include <string> #include <iostream> #include <locale> #include <ctype.h> #include <vector> #include <deque> using namespace system; using namespace std; //using std::string; bool lastchardigit = true; string rawstring; //contains raw equation user types in. deque<string> tokenequation(1); //contains equation in tokenised infix form. deque<string> rpnequation; //contains equation in tokenised rpn form. deque<string> operatorstack; //used part of shunting yard algorithm deque<string> solverstack; //used solve rpn equation. locale loc; //used verify digits. //start function declaration int main(); void tokeniser(string rawequation); void syalg(); void osprocess(string newoperator); void solver(); //end function declaration int main() { cout << "please enter valid infix notation equation, without parenthesis.\n"; cin >> rawstring; tokeniser(rawstring); cout << "\n"; system("pause"); return 0; } void tokeniser(string rawequation) { int testcharpos = -1; // initialise index of raw string int tokenvectorpos = 0; // initialise token array position int tokenvectorprintpos = 0; // initialise print position (int elength = rawequation.length(); elength != 0; elength--) // each character in raw string... { testcharpos++; // increment char we're testing char testchar = rawequation.at(testcharpos); // establish current test char if (isdigit(testchar, loc)) //if testchar digit { if (lastchardigit) //if last character digit { tokenequation[tokenvectorpos] += testchar; //append tested char current token array pos } if (!lastchardigit) //if last character not digit { tokenequation.push_back(string(1, testchar)); //establish new element testchar in it. tokenvectorpos++; } lastchardigit = true; } if (!isdigit(testchar, loc))//if testchar not digit { tokenequation.push_back(string(1, testchar)); //establish new element testchar in it. tokenvectorpos++; lastchardigit = false; } } cout << "the tokens of equation are:\n\n"; //outputs tokens testing purposes. (int tokenlength = tokenequation.size(); tokenlength != 0; tokenlength--) { cout << " " << tokenequation[tokenvectorprintpos]; cout << "\n"; tokenvectorprintpos++; } syalg(); //call syalg. } void syalg() //this function uses shunting yard algorithm convert infix tokens rpn. { cout << tokenequation.size(); (int testtokenlength = tokenequation.size(); testtokenlength != 0; testtokenlength--) //for each token in tokenised deque { if (isdigit(tokenequation.front().at(0), loc)) //check if it's number { rpnequation.push_back(tokenequation.front()); //add first raw token rpn equation tokenequation.pop_front(); //pop token deque } if (!isdigit(tokenequation.front().at(0), loc)) //if it's operator { osprocess(tokenequation.front()); //run syalg operator stack procedure. nb pop front of tokenequation you. } } cout << "the tokens of equation are:\n\n"; //outputs tokens testing purposes. int rpnprintpos = 0; (int tokenlength = rpnequation.size(); tokenlength != 0; tokenlength--) { cout << " " << rpnequation[rpnprintpos]; cout << "\n"; rpnprintpos++; } } void osprocess(string newoperator) //this function processes operator stack { bool pushednewoperator = false; std::string newopstd = newoperator; //creates std::string version of argument easier comparison. while (pushednewoperator == false){ //as long new operator still waiting go stack if (!operatorstack.empty()) //if there's operator on stack { if (newopstd == "/" || "*") { std::string osbackstd = operatorstack.back(); //create std version of of opstack comparison. if (osbackstd == "+" || "-") { operatorstack.push_back(newoperator); //add tested operator stack tokenequation.pop_front(); //and pop token equation pushednewoperator = true; //set flag variable true stop looping } else { rpnequation.push_back(operatorstack.back()); //add top of operator stack equation operatorstack.pop_back(); //pop } } else { rpnequation.push_back(operatorstack.back()); //add top of operator stack equation operatorstack.pop_back(); //pop } } if (operatorstack.empty()) { operatorstack.push_back(newoperator); //add tested operator stack tokenequation.pop_front(); //and pop token equation pushednewoperator = true; //set flag variable true stop looping } } //for each operator on stack, until following statement returns false... //check if precedence of newoperator less or equal top operator. } void solver() //this function solves rpnequation { //push each token solver stack //if push operator, solve against stack //when rpn equation empty , solver stack has 1 token in it, have solution }
one major issue multitude of lines if (newopstd == "/" || "*")
, or effect. these need changed if (newopstd.compare("/") == 0 || newopstd.compare("*") == 0)
.
i think these checks failing means while
loop they're in turns while(true)
.
Comments
Post a Comment