// 追加スタック数2 MyStack sortStack(MyStack stack) { MyStack ans(10), tmp(10); // 最小値を見つける while (!stack.isEmpty()) { int min = stack.peek(); while (!stack.isEmpty()) { int t = stack.peek(); stack.pop(); if (t <= min) { min = t; } tmp.push(t); } // 最小値を入れる while (!tmp.isEmpty()) { if (tmp.peek() == min) { ans.push(tmp.peek()); } else { stack.push(tmp.peek()); } tmp.pop(); } } return ans; }
int pop() { while(!S1.isEmpty()) S2.push(S1.pop()); temp = S2.pop(); while(!S2.isEmpty()) S1.push(S2.pop()); return temp; }
void processChar( char item, MyStack<Token> &stack, string &operations, bool &OK ) { // Note: // I could check top of stack before adding tokens // and catch many errors "now" instead of later. // I'd rather be lazy and let collapse() catch the errors. Token token = toToken(item); switch (token) { case TOKEN_VALUE: if ( stack.isEmpty() ) { stack << token; return; } switch ( stack.top() ) { case TOKEN_LEFT_GROUP: stack << token; break; case TOKEN_ADDITION: stack << token; break; // don't collapse yet case TOKEN_MULTIPLICATION: collapse( stack << token, operations, OK ); break; default: OK = false; break; } break; case TOKEN_ADDITION: collapse( stack, operations, OK); stack << TOKEN_ADDITION; break; case TOKEN_MULTIPLICATION: case TOKEN_LEFT_GROUP: stack << token; break; case TOKEN_RIGHT_GROUP: // clear any pending addidion collapse( stack, operations, OK ); if ( !OK ) return; // convert ( value ) to value if ( !stack.isEmpty() && TOKEN_VALUE == stack.pop() && !stack.isEmpty() && TOKEN_LEFT_GROUP == stack.pop() ) stack << TOKEN_VALUE; else OK = false; break; case TOKEN_UNKNOWN: OK = false; break; } }
bool isExpression( string expressionCandidate, string &operations ) { MyStack<Token> stack; bool OK = true; for ( string::size_type index = 0 ; index < expressionCandidate.size() ; ++index ) //loop through each character in the string like an array //e.g. "(a+b)*c+b+c" will loop over one iteration per character: '(', 'a', '+', 'b', ')', etc. processChar( expressionCandidate[index], stack, operations, OK ); if ( !OK ) return false; // clean up any remaining addition operation collapse( stack, operations, OK ); // make sure only a single value remains return OK && !stack.isEmpty() && TOKEN_VALUE == stack.pop() && stack.isEmpty(); }
/* Function: sortTokenByStacks() * Usage: is called by formulaStringScanning() for single token; * -----------------------------------------------------------------------------------------// * Sort this token through the stacks. If token is number - push it to stackNumbers. * If token is valid operator token - process it due to Shunting-Yard conditions. * * @param token Current token in formula string * @param stackNumbers Stack of number values for current recursion * @param stackOperators Stack of operators for current recursion */ void sortTokenByStacks(string token, MyStack<double> &stackNumbers, MyStack<string> &stackOperators) { if(stringIsDouble(token)) { //Token is number double num = stringToDouble(token); stackNumbers.push(num);//Just save token to stack } else { // Token is operator /* Main operators process */ if(stackOperators.isEmpty()) { //Empty - push there without conditions stackOperators.push(token); } else { //If there are some operators in stack string topOper = stackOperators.peek();//Get top operator if(getOperPrecedence(topOper) < getOperPrecedence(token)) { /* Top operator precednce is * weaker then this token operator - just save this token */ stackOperators.push(token); } else { /* Top operator precednce is higher - evaluate two top numbers * with top operator, and sort current token again */ if(!failFlag) { //If there some fails - break this function /* Main calculation for top numbers and top operator */ twoNumsProcess(stackNumbers, stackOperators); /* Call sorting again to process current token operator */ sortTokenByStacks(token, stackNumbers, stackOperators); } } } } }
// 追加スタック数1 MyStack sortStack1(MyStack stack) { MyStack ans(10); while(!stack.isEmpty()) { int t = stack.peek(); stack.pop(); /// 要素がansのtopより小さい間,ansからstackに戻す while (!ans.isEmpty() && ans.peek() < t) { stack.push(ans.peek()); ans.pop(); } ans.push(t); } return ans; }
// remove one complete arithmetic operation, if it is there void collapse( MyStack<Token> &stack, string &operations, bool &OK ) { // It should end with a value if ( stack.isEmpty() || stack.top() != TOKEN_VALUE ) return; stack.pop(); // if that was the ONLY thing on the stack or it is preceeded by (, it is OK if ( stack.isEmpty() || TOKEN_LEFT_GROUP == stack.top() ) { stack << TOKEN_VALUE; return; } // The value should be preceeded with an operator Token operation; if ( stack.isEmpty() || !isOperator(operation = stack.pop()) ) { OK = false; return; } operations += ((operation==TOKEN_ADDITION) ? "+" : "*"); // The operator should be preceeded with a value if ( stack.isEmpty() || stack.pop() != TOKEN_VALUE ) { OK = false; return; } // sucessful collapse - to a single value stack << TOKEN_VALUE; }
/* Function: getFinalStacksResult() * Usage: is called by formulaStringScanning() at the end of current recursion. * -----------------------------------------------------------------------------------------// * Calculates main result, due to stacks, for current formulaStringScanning() recursion * stage. * Precondition: it's end of main formula string or brackets closed process. * * @param stackNumbers Stack of number values for current recursion * @param stackOperators Stack of operators for current recursion */ double getFinalStacksResult(MyStack<double> stackNumbers, MyStack<string> stackOperators) { /* Stacks elements validation checking for calculating process */ if((stackNumbers.size() - stackOperators.size()) != 1) { cout << " - CHECK YOUR INPUT, NOT ENOUGH NUMBERS IN FORMULA!" << endl; failFlag = true; } /* Lunches calculations for all remain values from stacks */ while(!stackOperators.isEmpty()) { if(failFlag) break;//Some fails appear during calculations /* Calculation for two top stack numbers and single top operator */ twoNumsProcess(stackNumbers, stackOperators); } /* If all operators are processed - end result value remains at top of numbers stack */ if(!failFlag) { return stackNumbers.pop(); } else { return 0; } }
int main() { MyStack S; Queue Q; Q.enQueue(5); Q.enQueue(6); Q.enQueue(1); Q.enQueue(2); Q.enQueue(3); Q.enQueue(4); Q.enQueue(7); Q.enQueue(9); Q.display(); while(!Q.isEmpty()) S.push(Q.deQueue()); while(!S.isEmpty()) Q.enQueue(S.pop()); Q.display(); }
bool PascalChecker::checkPair(char code[]) { char *check_point; check_point = code; int last_pair = -1; /* while(*check_point != 0) { if(compareChar(check_point, ch_begin, 5)) { pair_stack.push(BEGIN); ++check_point; break; } if(compareChar(check_point, ch_if, 2)) return false; if(compareChar(check_point, ch_end, 3)) return false; if(compareChar(check_point, ch_then, 4)) return false; if(compareChar(check_point, ch_else, 4)) return false; ++check_point; }*/ while(*check_point != 0) { if(compareChar(check_point, ch_begin, 6)) pair_stack.push(BEGIN); if(compareChar(check_point, ch_if, 3)) { //if(pair_stack.isEmpty()) //return false; pair_stack.push(IF); } if(compareChar(check_point, ch_end, 4)) { do { last_pair = pair_stack.pop(); } while(last_pair == THEN && !pair_stack.isEmpty()); if(last_pair != BEGIN) return false; last_pair = -1; } if(compareChar(check_point, ch_then, 5)) { if(pair_stack.pop() != IF) return false; else pair_stack.push(THEN); } if(compareChar(check_point, ch_else, 5)) { if(pair_stack.pop() != THEN) return false; } ++check_point; } if(pair_stack.isEmpty()) return true; else return false; }