Example #1
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);
}
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);
}
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());
}
Example #4
0
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;
}
Example #5
0
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;
}