// verifies equality of two STACK objects friend int operator==(STACK a, STACK b) { // Take care of empty cases. int status = 0; // set status to false if (a.empty() && b.empty()) status = 1; // both STACK objects are empty // Handle non-empty cases else if (a.N == b.N) { // number of items on each STACK are equal // check equality of each item for (int i = 0; i < N; i++) { if (a.s[i] == b.s[i]) continue; else break; } if (i == N) // all items in both STACK's are equal status = 1; } return status; } // end friend function operator==
int main() { //declare objects of stack STACK<char, 100> P; char SENT; P.MakeStack(); //collect the sentence cout << "Please, type a sentence: "; cin.get(SENT); //read first character //collect the sentence a character at a time while( SENT!='\n' ) { if( isalpha(SENT) ) P.PushStack(SENT); cin.get(SENT); //get next character } //display sentence in reverse (*reverse is normal in stack*) while( !P.EmptyStack()!='\0' ) cout<<P.PopStack(); cout<<endl; //end program system("PAUSE"); return 0; }
void update(VVI & inp,STACK &sti,STACK &stj,int i, int j,int &val){ if(inp[i][j]>0){ val++; sti.push(i); stj.push(j); } else { emptyStacks(inp,sti,stj,val); } }
void emptyStacks(VVI &inp,STACK &sti,STACK &stj,int &val){ while(!sti.empty()){ int i = sti.top(); sti.pop(); int j = stj.top(); stj.pop(); if(inp[i][j]<val){ inp[i][j] = val; } } val = 0; }
void show_impl(STACK stack) { std::cout << "["; while (!stack.empty()) { std::cout << stack.back(); stack.pop_back(); if (!stack.empty()) {std::cout << ", "; } } std::cout << "]" << std::endl; return; }
void Path() { int i, j, k; //cout << mazepath.Length() << endl; //mazepath.Display(); //cout << endl; for(k = 1; k <= mazepath.Length(); k ++) { cout << "µÚ" << k << "²½" << endl; for(i = 0; i < row; i ++) { for(j = 0; j < column; j ++) { if(MYCOORD<int>(j, i) == mazepath[k]) { cout << (char)3 << ' '; } else if(layout[i][j]) { cout << '.' << ' '; } else { cout << '#' << ' '; } } cout << endl; } cout << endl; Sleep(1000); } }
int main() { while(getline(cin, line)) { istringstream parsing(line); STACK matrioshkas; SETFALSE(flag1); SETTRUE(flag2); while(parsing >> m && flag2) { SETTRUE(flag1); if(m < 0) { m = labs(m); if(!matrioshkas.empty()) { (matrioshkas.top().second <= m) ? SETFALSE(flag2) : matrioshkas.top().second -= m; } PUSH(matrioshkas, make_pair(m, m)); } else { if(matrioshkas.empty() || matrioshkas.top().first != m) SETFALSE(flag2); else POP(matrioshkas); } } if(!flag1 || !matrioshkas.empty()) SETFALSE(flag2); cout << ((flag2) ? ":-) Matrioshka!\n" : ":-( Try again.\n"); } return 0; }
float POSTFIX_EVALUATION(char* postfix_arr, int lengthofpostfix) { STACK<float> STACK; //Initializes evaluation float stack float answer = 0, temp = 0, operandx = 0, operandy = 0; //Final evaluated answer variable int i = 0, c = 0; //Counters and operand variables char op, check; //Operator variable for (c = 0; c < lengthofpostfix; c++) //For postfix equation { (char)postfix_arr[i]; if (postfix_arr[i] >= 'a' && postfix_arr[i] <= 'z'){ //If operand STACK.PUSH2((float)postfix_arr[i]); //PUSHes operand to stack i++; //Increments postfix array } else if(postfix_arr[i] == '+' || postfix_arr[i] == '-' || postfix_arr[i] == '/' || postfix_arr[i] == '*'|| postfix_arr[i] == '^'){ //If Operator op = postfix_arr[i]; //Sets operator to postfix array slot operandx = STACK.POP2(); //Retrieves operand 1 from stack operandy = STACK.POP2(); //Retrieves operand 2 from stack answer = solve(op,operandy,operandx); //Solves the postfix STACK.PUSH2(answer); //Pushes the answer onto the stack i++; //Moves to next value in postfix array } } answer = STACK.Return_Top_of_Stack2(); //Returns final answer from stack STACK.POP2(); return answer; }
int main() { STACK<int> test; STACK<int> test2; for (int i=1; i<=5; i++){ test.Push(i); test2.Push(i+5); } test.Print(); test2.Print(); test.Last->setData(777); STACK<int> test3; test3 = test+test2; test3.Print(); test.~STACK(); test2.~STACK(); return 0; }
void Maze() { MYCOORD<int> present, next(start); int direction; do { if(!IsDeadCorner(next) && IsValid(next) && !mazepath.IsExist(next)) { present = next; mazepath.Push(present); if(present == end) break; direction = 1; next = present.Right_X(); } else { direction ++; if(direction == 2) { next = present.Down_Y(); } else if(direction == 3) { next = present.Left_X(); } else if(direction == 4) { next = present.Up_Y(); } else { dead_corner[p] = mazepath.Pop(); if(mazepath.IsEmpty()) break; present = mazepath.Top(); next = present; p ++; direction = 1; } } } while(!mazepath.IsEmpty()); }
inline void push2(STACK<int>& s, int A, int B) { if (A < B) { s.push(B); s.push(A); } }
int main() { // Declaration of variabless string sentence, cont; char charsent[50], c1, c2; // Creation of STACK and QUEUE STACK<char> S; QUEUE<char> Q; S.CreateStack(); Q.CreateQueue(); // While-loop for continue while(true) { cout << endl; cout << "Enter a sentence: "; getline(cin, sentence); strcpy(charsent, sentence.c_str()); // Pushing user input into STACK and QUEUE for (int i = 0; i < sentence.size(); i++) { c1 = charsent[i]; // Checking for alphanumeric if (isalnum(c1)) { S.Push(c1); Q.Push(c1); } else { // do nothing } } // STACK and QUEUE comparision while (!S.EmptyStack()) { c1 = S.Pop(); c2 = Q.Pop(); c1 = toupper(c1); c2 = toupper(c2); if (c1 != c2) { break; } } // If STACK and QUEUE was emptied with no break, Palindrome found if (S.EmptyStack() && Q.EmptyQueue()) { cout << "[" << sentence << "] is a Palindrome!" << endl; } else { cout << "[" << sentence << "] is NOT Palindrome." << endl; } // While-loop continue check cout << endl << "CONTINUE (y/n)? "; cin >> cont; if (cont != "y") { break; } else { // Clearing the STACK and QUEUE for new user input while(!S.EmptyStack() || !Q.EmptyQueue()) { S.Pop(); Q.Pop(); } } cin.ignore(); } return 0; }
REGION Acquisition::seedFill( REG_COLOR colorID, unsigned int u, unsigned int v) { #define canBePainted( colorID, u, v) (!analisedPixel(u,v) && calibracaoParam.getHardColor(ImBruta[v][u]) == colorID) static STACK s; REGION region; double su=0, sv=0, suu=0, svv=0, suv=0; int nPixel=0; unsigned int v1; unsigned int vMax=0,vMin=ImBruta.nlin(); unsigned int uMax=0,uMin=ImBruta.ncol(); bool expanLeft, expanRight; region.nPixel=0; region.colorID = colorID; if ( !canBePainted(colorID,u,v) ) return region; s.empty(); if (!s.push(u,v)) { cerr << "Buffer estourou 1!\n"; return region; } while(s.pop(u,v)) { v1 = v; while(v1>0 && canBePainted(colorID,u,v1-1)) v1--; expanLeft = expanRight = false; while(v1<ImBruta.nlin() && canBePainted(colorID,u,v1) ) { analisedPixel.setValue(u,v1,true); if(v1 < vMin) vMin = v1; if(v1 > vMax) vMax = v1; if(u < uMin) uMin = u; if(u > uMax) uMax = u; su += u; sv += v1; suu += u*u; svv += v1*v1; suv += u*v1; nPixel++; if(!expanLeft && u>0 && canBePainted(colorID,u-1,v1)) { if(!s.push(u-1,v1)) { cerr << "Buffer estourou 2!\n"; return region; } expanLeft = true; } else if(expanLeft && u>0 && !canBePainted(colorID,u-1,v1)) { expanLeft = false; } if(!expanRight && u<(ImBruta.ncol()-1) && canBePainted(colorID,u+1,v1)) { if(!s.push(u+1,v1)) { cerr << "Buffer estourou 3!\n"; return region; } expanRight = true; } else if(expanRight && u<(ImBruta.ncol()-1) && !canBePainted(colorID,u+1,v1)) { expanRight = false; } v1++; } } //testa se a regiao é uma linha vertical ou horizontal if((vMax-vMin) > LINE_THRESHOLD || (uMax-uMin) > LINE_THRESHOLD) return region; region.center.u() = su/nPixel; region.center.v() = sv/nPixel; region.nPixel = nPixel; double varu, varv, varuv; varu = suu/nPixel - (su/nPixel)*(su/nPixel); //a varv = svv/nPixel - (sv/nPixel)*(sv/nPixel); //c varuv = suv/nPixel - (su/nPixel)*(sv/nPixel); //b //testa se a regiao eh simetrica, ou seja, nao tem como calcular o angulo do segundo momento. double lim_zero = 0.001; if(fabs(varuv) < lim_zero && fabs(varu-varv) < lim_zero){ //a figura é simetrica region.symetric = true; region.orientation = 0.0; }else{ region.symetric = false; region.orientation = -atan2(varuv,varu-varv)/2.0; } return region; }
int main() { int x,y,n; STACK <char, 10> A; char item[10]; // clear stack A.Clear(); // get expression from user cout << "Enter a postfix expression with $ at the end: "; cin.getline(item,10); // process data while(strcmp(item, "$") != 0) { if(strcmp(item, "+") == 0) { x = A.Pop(); y = A.Pop(); A.Push(y+x); } else if(strcmp(item, "*") == 0) { x = A.Pop(); y = A.Pop(); A.Push(y*x); } else if(strcmp(item, "/") == 0) { x = A.Pop(); y = A.Pop(); A.Push(y/x); } else if(strcmp(item, "-") == 0) { x = A.Pop(); y = A.Pop(); A.Push(y-x); } // if its a number else { n = atoi(item); A.Push(n); } } int r = A.Pop(); // output result cout << "Result is " << r << endl; return(0); }
// a simple client to drive the stack // one improvement may be to have the client maintain a free list of unused nodes int main() { STACK s; STACK_NODE *p; bool done = false; int option, value; while( !done ) { cout << "1) push\n2) pop\n3) peek\n4) pop all\n5) init\n" << "6) check if empty\n7) quit\n\nEnter option : " << flush; cin >> option; switch( option ) { case 1: cout << "Enter value to push : " << flush; cin >> value; p = new STACK_NODE; p->key = value; s.push( p ); break; case 2: if( !s.IsEmpty() ) { p = s.pop(); value = p->key; delete p; cout << "Popped " << value << endl; } else { cout << "Nothing to pop!" << endl; } break; case 3: if( !s.IsEmpty() ) { // cast off const-ness (but DON't mess with the data!) p = const_cast<STACK_NODE *>(s.peek()); value = p->key; cout << "Peeked " << value << endl; } else { cout << "Nothing to peek!" << endl; } break; case 4: while( !s.IsEmpty() ) { p = s.pop(); value = p->key; delete p; cout << "Popped " << value << endl; } cout << "Stack is now empty" << endl; break; case 5: cout << "Initializing..." << endl; s.init(); break; case 6: cout << "IsEmpty returns " << ( s.IsEmpty() ? "true" : "false" ) << endl; break; case 7: done = true; break; default: cout << "What?" << endl; } } return 0; }
void main() { ifstream in("Primer.txt"); //Открываем файл для считывания информации string s1, s2; //Переменная будет считываться в строку while (in >> s2) s1 += s2; //Считываем пока можем in.close(); // Закрываем файл LEXER lexer; vector <string> tok = lexer.Tokens(s1); vector <int> tok_id = lexer.Tokens_ID(s1); bool tok_err = lexer.Error(s1); cout << "Tokens:\n" << "ID | Lexems\n-----------\n"; for (int n = 1; n < tok_id.size(); n++) //Вывод лексем в консоль { cout << tok_id[n]; if (tok_id[n] < 10) cout << " | "; else cout << " | "; cout << tok[n] << '\n'; } if (tok_err == true) cout << "Lexical error!" << '\n'; PARSER Parser; Parser.Set(tok, tok_id); vector <string> Pars_Test = Parser.Output(); vector <int> RAM_INT = Parser.Init_INT(); vector <string> RAM_ID = Parser.Init_ID(); cout << "\nInitializacia:\n"; for (int n = 1; n < RAM_INT.size(); n++) //Вывод значений в консоль { cout << RAM_ID[n] << " | " << RAM_INT[n] << '\n'; } cout <<"\nObratnaya Polskaya Zapis:\n"; for (int n = 1; n < Pars_Test.size(); n++) //Вывод лексем в консоль { cout << Pars_Test[n] << ' '; } STACK Output; vector <int> Mashine_RAM = Output.Output_RAM(Pars_Test,RAM_ID,RAM_INT); cout <<"\n\nOutput:\n"; for (int n = 1; n < Mashine_RAM.size(); n++) //Вывод значений в консоль после расчетов { cout << RAM_ID[n] << " | " << Mashine_RAM[n] << '\n'; } cout << "\nBiletova IVBO-08-14\n\n"; system("pause"); return; }
int INFIX_TO_POSTFIX(string infix, char* postfix_return) //Conversion function { STACK<char> STACK; int i = 0, z = 0; char postfix_array[25]; string postfix; cout << "Postfix Equation: "; for (int pos = 0; pos < infix.size() ; pos++) //For the length of the infix string until end of string: { if (infix[pos] == '+' || infix[pos] == '-' || infix[pos] == '/' || infix[pos] == '*' || infix[pos] == '^') //If is operator: { //Checks if stack is empty, if top of stack is parentheses, and the priority of each operator on the stack while(!STACK.isEmpty() && operatorPriority(STACK.Return_Top_of_Stack()) <= operatorPriority(infix[pos]) && STACK.Return_Top_of_Stack() != '(' && STACK.Return_Top_of_Stack() != ')') { postfix_array[i] = STACK.Return_Top_of_Stack(); i++; cout << STACK.POP(); //POPs stack } STACK.PUSH(infix[pos]); //PUSHes operator to stack } else if (infix[pos] == '('){ //If char is '(' STACK.PUSH(infix[pos]); //PUSHes to operator stack } else if (infix[pos] == ')'){ //If char is ')' while (STACK.Return_Top_of_Stack() != '(') { //Return top of stack until '(' postfix_array[i] = STACK.Return_Top_of_Stack(); //Stores top of stack into postfix string i++; cout << STACK.POP(); //Print POP'd operator } STACK.POP(); //POPs the '(' } else { postfix_array[i] = infix[pos]; i++; cout << infix[pos]; } } while (!STACK.isEmpty()) { //Dumps the stack postfix_array[i] = STACK.Return_Top_of_Stack(); //Stores contents of stack into postfix array i++; cout << STACK.POP(); } for (z = 0; z < i; z++) { postfix_return[z] = postfix_array[z]; //Stores postfix array into Main } cout << endl; int length = i; //Length of postfix array return length; }
int main() { char con; do { char num[20]; int count = 0; string out; int OperatorCount = 0; int ParenNumCount = 0; STACK <char, 20> loadNum; STACK <char, 20> loadOperator; STACK <char, 20> flipStack; STACK <char, 20> FlipOperator; cout << "Enter an infix expression:"; cin.getline(num,20); loadNum.ClearStack(); loadOperator.ClearStack(); flipStack.ClearStack(); FlipOperator.ClearStack(); NumCheck: while (num[count] != '$') { if (num[count] == '(') { count++; while (num[count] != ')') { if (isalpha(num[count])) { loadNum.PushStack(num[count]); count++; ParenNumCount++; } else if (num[count] == '*' || num[count] == '+' || num[count] == '-' || num[count] == '/') { loadOperator.PushStack(num[count]); count++; OperatorCount++; } } count++; goto NumCheck; } if (isalpha(num[count])) { loadNum.PushStack(num[count]); count++; goto NumCheck; } if (num[count] == '*' || num[count] == '+' || num[count] == '-' || num[count] == '/') { loadOperator.PushStack(num[count]); count++; OperatorCount++; } if (num[count] == '$') { goto NumCheck; } } //flip while (!loadNum.EmptyStack()) { flipStack.PushStack(loadNum.PopStack()); } while (!loadOperator.EmptyStack()) { FlipOperator.PushStack(loadOperator.PopStack()); } count = 0; //output if (ParenNumCount % 2 == 0) { while (!flipStack.EmptyStack()) { if (count % 2 == 0) { out += flipStack.PopStack(); out += flipStack.PopStack(); if (OperatorCount >= 0) { out += FlipOperator.PopStack(); OperatorCount--; } count++; } else { out += flipStack.PopStack(); if (OperatorCount >= 0) { out += FlipOperator.PopStack(); OperatorCount--; } } } } else if (ParenNumCount % 2 != 0) { while (!flipStack.EmptyStack()) { out += flipStack.PopStack(); } while (!FlipOperator.EmptyStack()) { out += FlipOperator.PopStack(); } } cout << "Postfix form of the expression is:" << out << endl; cout << "Continue y/n:"; cin >> con; cin.ignore(); } while (con != 'n'); system("pause"); return 0; }
int main() { int x,y,n,result,size; STACK <char, 10> A; string expression; // clear stack A.Clear(); // get expression from user cout << "Enter a postfix expression:"; cin >> expression; size = expression.length(); char item[size]; for(int i=0; i<size; ++i){ item[i] = expression[i]; } for(int j=0; j<size; ++j){ if(item[j] == '+'){ x = A.Pop(); y = A.Pop(); A.Push(y+x); } else if( item[j] == '*'){ x = A.Pop(); y = A.Pop(); A.Push(y*x); } else if( item[j] == '/'){ x = A.Pop(); y = A.Pop(); A.Push(y/x); } else if( item[j] == '-'){ x = A.Pop(); y = A.Pop(); A.Push(y-x); } else{ n = (item[j]) - (48); A.Push(n); } } // output result result = A.Pop(); cout << "Result is " << result << endl; return(0); }