Showing posts with label stack. Show all posts
Showing posts with label stack. Show all posts

Saturday, October 1, 2016

Postfix Expression Evaluation For Multidigit :)

//Program to Evaluate Postfix Expression

#include<iostream>
#include<ctype.h>  //for isdigit function
using namespace std;
int stack[100];
int top=-1;
int value=0;    //Global Declarations
int pop()
{

    return (stack[top--]);
}
void push(int x)
{
    if(value==1)
    {
        int y;
        y=pop();
        stack[++top]=x+10*y;   //for more than one digit
    }
    else if(value==0)
    {
    stack[++top]=x;
    value=1;
}
}
main()
{
char c,postfix[100];
int i=0,y,x;
cout<<"\nEnter The Postfix Expression:";
cin.getline(postfix,100);
while((c=postfix[i++])!='\0')
{
    if(isdigit(c))
    push(c-'0');  //for converting datatype
    else if(c==',')
    {
        value=0;
    }
    else
    {
    x=pop();  //top element
    y=pop();  //next top element
    switch(c)
    {
        case'+':
            push(x+y);
         break;
         case '-':
             push(y-x);
             break;
        case '*':
        push(y*x);
        break;
        case '/':
        push(y/x);
        break;
        default:
        cout<<"\nInvalid Operator";    
    }
    }
    }
    cout<<"\n\nEntered Expression:"<<postfix;
cout<<"\n\nResult Of Evaluation:"<<stack[top];
}

Output:

 

Tuesday, September 27, 2016

Postfix Evaluation

Program To evaluate Postfix Expression:

#include<iostream>
#include<ctype.h>  //for isalnum function
using namespace std;
int stack[100];
int top=-1;
void push(int x)
{
stack[++top]=x;
}
int pop()
{

return (stack[top--]);
}
main()
{
char c,postfix[100];
int i=0,y,x;
cout<<"\nEnter The Postfix Expression:";
cin.getline(postfix,100);
while((c=postfix[i++])!='\0')
{
if(isdigit(c))
push(c-'0');  //for converting datatype
else
{
x=pop();
y=pop();
switch(c)
{
case'+':
push(x+y);
    break;
    case '-':
    push(y-x);
    break;
   case '*':
push(y*x);
break;
case '/':
push(y/x);
break;
}
}
}
cout<<"\n\nEntered Expression:"<<postfix;
cout<<"\n\nResult Of Evaluation:"<<stack[top];
}

Sunday, September 25, 2016

Transforming INFIX to POSTFIX Expression

Program to Convert An Infix Expression To Postfix Expression:


#include<stdio.h>
#include<ctype.h> //for isalnum function
char stack[50];
int top=-1;
void push(char x)
{
if(top==49)
{printf("Overflow");
}
else{

stack[++top]=x;
}
}
char pop()
{ if(top==-1)
{
return -1;
}
else{

return stack[top--];
}
}
int priority(char x)
{
if(x=='(')
return 0;
else if(x=='+'||x=='-')
 return 1;
 else if(x=='*'||x=='/')
 return 2;
}
main()
{
char infix[50];
char x,*e;  //pointer to access each character
printf("Enter The Expression  ");
scanf("%s",infix);
e=infix;
while(*e!='\0')
{
if(*e=='(')
{
push(*e);
}
else if(isalnum(*e))
{
printf("%c",*e);
}
else if(*e==')')
{
while((x=pop())!='(')
printf("%c",x);
}
else
{
while(priority(stack[top])>=priority(*e))
printf("%c",pop());
push(*e);
}
e++;}
while(top!=-1)
printf("%c",pop());
}

Output:


Monday, September 5, 2016

PROGRAM TO PERFORM PUSH AND POP OPERATIONS ON STACK:

PROGRAM TO PERFORM PUSH AND POP OPERATIONS ON STACK IMPLEMENTED AS AN ARRAY.

#include <iostream>
using namespace std;
void push(int stack[],int max,int i,int top);
void pop(int stack[],int max,int i,int top);
int main()
{
    int choice,stack[30],max,i;
start:
    int top=-1;
    cout<<"\n\n***Operations on STACK***\n\n";
    cout<<"Enter Size of Stack: ";
    cin>>max;
    cout<<"\nEnter "<<max<<" Elements of Stack: ";

    for(i=0; i<max; i++)
    {
        cin>>stack[i];
        top++;
    }
    cout<<"\n1: Push\n2: Pop\n";
    cout<<"Enter your Choice(Press F6 to Exit): ";

    while(cin>>choice)
    {

        switch(choice)
        {
        case 1:
            push(stack,max,i,top);
            cout<<"\nEnd of Program";
            break;
        case 2:
            pop(stack,max,i,top);
            cout<<"\nEnd of Program";
            break;
        default:
            cout<<"\nInvalid Input";
        }
        goto start;
    }
    return 0;
}

void push(int stack[],int max,int i,int top)
{
    int item;
    if(top==29)
        cout<<"\nStack Overflow!!";
    else
    {
        cout<<"\nEnter Element to be Inserted(Pushed): ";
        cin>>item;
        top++;
        stack[top]=item;
        cout<<"\nNew Stack is:\n";
        for(i=0; i<=top; i++)
            cout<<stack[i]<<" ";
    }
}


void pop(int stack[],int max,int i,int top)
{
    if(top==-1)
        cout<<"\nStack UnderFlow!!";
    else
    {
        top--;
        cout<<"\nNew Stack is:\n";
        for(i=0; i<=top; i++)
            cout<<stack[i]<<" ";
    }
}