Showing posts with label Stacks. Show all posts
Showing posts with label Stacks. Show all posts

Wednesday, April 20, 2011

Evaluation of prefix expression

/* program on Evaluation of prefix expression */

#include "stdio.h"
#include "stdlib.h"
#include "math.h"

#define OPT 0
#define OPND 1
#define LP 2
#define RP 3

char infix[100], prefix[100]; /* global variables declare here */
float val[26];

struct stack
{
float info;
struct stack *next;
} *stk;


/* main function here */

main()
{
int i, type;
char ch;
clrscr();
printf("\nEnter a infix expression : ");
scanf("%s",prefix);
for(i=0;prefix[i]!='\0';i++)
{
ch = prefix[i];
type = gettype(ch);
if(type == OPND)
{
printf("Enter value of %c : ",ch);
scanf("%f",&val[ch-65]);
}
}
printf("\nAfter evaluation: %f",(float)evalprefix());
getch();
}


/* push function here */

push(struct stack *s, float n)
{
struct stack *temp;
temp = (struct stack *)malloc(sizeof(struct stack));
temp->info = n;
temp->next = s;
stk = temp;
}

/* Pop function here */

float pop(struct stack *s)
{
float ch;
if(s!=NULL)
{
ch = s->info;
stk = s->next;
free(s);
}
return ch;
}
int gettype(char ch)
{
if(ch=='+'||ch=='-'||ch=='*'||ch=='/'||ch=='%')
return OPT;
else if(ch == '(')
return LP;
else if(ch == ')')
return RP;
else
return OPND;
}

prec(char op)
{
switch(op)
{
case ')':
case '(': return 0;
case '+':
case '-': return 1;
case '*':
case '/':
case '%': return 2;
case '^': return 3;
}
}



float operate(float op1, float op2, char ch)
{
switch(ch)
{
case '+': return op1+op2;
case '-': return op1-op2;
case '*': return op1*op2;
case '/': if(op2==0)
return 0;
else
return op1/op2;
case'^': return pow(op1,op2);
}
}

/* function to evaluate the prefix expression */

evalprefix()
{
int i, type;
float op1, op2, res;
char ch;
for(i=0;prefix[i]!='\0';i++);
for(i=i-1; i>=0; i--)
{
ch = prefix[i];
type = gettype(ch);
if(type == OPND)
push(stk, val[ch-65]);
else
{
op1 = pop(stk);
op2 = pop(stk);
res = operate(op1, op2, ch);
push(stk, res);
}
}
return (float)stk->info;
}

Evaluation of prefix expression

/* program on Evaluation of prefix expression */

#include "stdio.h"
#include "stdlib.h"
#include "math.h"

#define OPT 0
#define OPND 1
#define LP 2
#define RP 3

char infix[100], prefix[100]; /* global variables declare here */
float val[26];

struct stack
{
float info;
struct stack *next;
} *stk;


/* main function here */

main()
{
int i, type;
char ch;
clrscr();
printf("\nEnter a infix expression : ");
scanf("%s",prefix);
for(i=0;prefix[i]!='\0';i++)
{
ch = prefix[i];
type = gettype(ch);
if(type == OPND)
{
printf("Enter value of %c : ",ch);
scanf("%f",&val[ch-65]);
}
}
printf("\nAfter evaluation: %f",(float)evalprefix());
getch();
}


/* push function here */

push(struct stack *s, float n)
{
struct stack *temp;
temp = (struct stack *)malloc(sizeof(struct stack));
temp->info = n;
temp->next = s;
stk = temp;
}

/* Pop function here */

float pop(struct stack *s)
{
float ch;
if(s!=NULL)
{
ch = s->info;
stk = s->next;
free(s);
}
return ch;
}
int gettype(char ch)
{
if(ch=='+'||ch=='-'||ch=='*'||ch=='/'||ch=='%')
return OPT;
else if(ch == '(')
return LP;
else if(ch == ')')
return RP;
else
return OPND;
}

prec(char op)
{
switch(op)
{
case ')':
case '(': return 0;
case '+':
case '-': return 1;
case '*':
case '/':
case '%': return 2;
case '^': return 3;
}
}



float operate(float op1, float op2, char ch)
{
switch(ch)
{
case '+': return op1+op2;
case '-': return op1-op2;
case '*': return op1*op2;
case '/': if(op2==0)
return 0;
else
return op1/op2;
case'^': return pow(op1,op2);
}
}

/* function to evaluate the prefix expression */

evalprefix()
{
int i, type;
float op1, op2, res;
char ch;
for(i=0;prefix[i]!='\0';i++);
for(i=i-1; i>=0; i--)
{
ch = prefix[i];
type = gettype(ch);
if(type == OPND)
push(stk, val[ch-65]);
else
{
op1 = pop(stk);
op2 = pop(stk);
res = operate(op1, op2, ch);
push(stk, res);
}
}
return (float)stk->info;
}

Evaluation of postfix expression

/* program on Evaluation of postfix expression */

#include"stdio.h"
#include"stdlib.h"
#include"math.h"

#define OPT 0
#define OPND 1
#define LP 2
#define RP 3

char infix[100], postfix[100]; /* global variables declare here */
float val[26];

struct stack
{
float info;
struct stack *next;
} *stk;


/* main function here */

main()
{
int i, type;
char ch;
clrscr();
printf("\nEnter a infix expression : ");
scanf("%s",postfix);
for(i=0;postfix[i]!='\0';i++)
{
ch = postfix[i];
type = gettype(ch);
if(type == OPND)
{
printf("Enter value of %c : ",ch);
scanf("%f",&val[ch-65]);
}
}
printf("\nAfter evaluation: %f",(float)evalpostfix());
getch();
}


/* push function here */

push(struct stack *s, float n)
{
struct stack *temp;
temp = (struct stack *)malloc(sizeof(struct stack));
temp->info = n;
temp->next = s;
stk = temp;
}

/* Pop function here */

float pop(struct stack *s)
{
float ch;
if(s!=NULL)
{
ch = s->info;
stk = s->next;
free(s);
}
return ch;
}
int gettype(char ch)
{
if(ch=='+'||ch=='-'||ch=='*'||ch=='/'||ch=='%')
return OPT;
else if(ch == '(')
return LP;
else if(ch == ')')
return RP;
else
return OPND;
}

prec(char op)
{
switch(op)
{
case ')':
case '(': return 0;
case '+':
case '-': return 1;
case '*':
case '/':
case '%': return 2;
case '^': return 3;
}
}



float operate(float op1, float op2, char ch)
{
switch(ch)
{
case '+': return op1+op2;
case '-': return op1-op2;
case '*': return op1*op2;
case '/': if(op2==0)
return 0;
else
return op1/op2;
case'^': return pow(op1,op2);
}
}

/* function to evaluate the postfix expression */

evalpostfix()
{
int i, type;
float op1, op2, res;
char ch;
for(i=0; postfix[i]!='\0'; i++)
{
ch = postfix[i];
type = gettype(ch);
if(type == OPND)
push(stk, val[ch-65]);
else
{
op2 = pop(stk);
op1 = pop(stk);
res = operate(op1, op2, ch);
push(stk, res);
}
}
return (float)stk->info;
}

Evaluation of postfix expression

/* program on Evaluation of postfix expression */

#include"stdio.h"
#include"stdlib.h"
#include"math.h"

#define OPT 0
#define OPND 1
#define LP 2
#define RP 3

char infix[100], postfix[100]; /* global variables declare here */
float val[26];

struct stack
{
float info;
struct stack *next;
} *stk;


/* main function here */

main()
{
int i, type;
char ch;
clrscr();
printf("\nEnter a infix expression : ");
scanf("%s",postfix);
for(i=0;postfix[i]!='\0';i++)
{
ch = postfix[i];
type = gettype(ch);
if(type == OPND)
{
printf("Enter value of %c : ",ch);
scanf("%f",&val[ch-65]);
}
}
printf("\nAfter evaluation: %f",(float)evalpostfix());
getch();
}


/* push function here */

push(struct stack *s, float n)
{
struct stack *temp;
temp = (struct stack *)malloc(sizeof(struct stack));
temp->info = n;
temp->next = s;
stk = temp;
}

/* Pop function here */

float pop(struct stack *s)
{
float ch;
if(s!=NULL)
{
ch = s->info;
stk = s->next;
free(s);
}
return ch;
}
int gettype(char ch)
{
if(ch=='+'||ch=='-'||ch=='*'||ch=='/'||ch=='%')
return OPT;
else if(ch == '(')
return LP;
else if(ch == ')')
return RP;
else
return OPND;
}

prec(char op)
{
switch(op)
{
case ')':
case '(': return 0;
case '+':
case '-': return 1;
case '*':
case '/':
case '%': return 2;
case '^': return 3;
}
}



float operate(float op1, float op2, char ch)
{
switch(ch)
{
case '+': return op1+op2;
case '-': return op1-op2;
case '*': return op1*op2;
case '/': if(op2==0)
return 0;
else
return op1/op2;
case'^': return pow(op1,op2);
}
}

/* function to evaluate the postfix expression */

evalpostfix()
{
int i, type;
float op1, op2, res;
char ch;
for(i=0; postfix[i]!='\0'; i++)
{
ch = postfix[i];
type = gettype(ch);
if(type == OPND)
push(stk, val[ch-65]);
else
{
op2 = pop(stk);
op1 = pop(stk);
res = operate(op1, op2, ch);
push(stk, res);
}
}
return (float)stk->info;
}

Infix expression to prefix expression conversion

/* program on conversion of infix to prefix expression */

#include "stdio.h"
#include "stdlib.h"
#include "string.h"

#define OPT 0
#define OPND 1
#define LP 2
#define RP 3

char infix[100], prefix[100]; /* global variables declare here */

struct stack
{
char info;
struct stack *next;
} *stk;


/* main function here */

int main(void)
{
clrscr();
printf("\nEnter a infix expression : ");
fflush(stdin);
gets(infix);
intopre();
printf("\nPrefix expression is : %s", prefix);
getch();
return 0;
}


/* push function here */

push(struct stack *s, char n)
{
struct stack *temp;
temp = (struct stack *)malloc(sizeof(struct stack));
temp->info = n;
temp->next = s;
stk = temp;
}

/* Pop function here */

char pop(struct stack *s)
{
char ch;
if(s!=NULL)
{
ch = s->info;
stk = s->next;
free(s);
}
return ch;
}
/* ---- get character type here wheather operator or operand ----- */
int gettype(char ch)
{
if(ch=='+'|| ch == '-' || ch == '*' || ch == '/' || ch == '%')
return OPT;
else if(ch == '(')
return LP;
else if(ch == ')')
return RP;
else
return OPND;
}

/* --- get precedence of operator ------ */
prec(char op)
{
switch(op)
{
case ')':
case '(': return 0;
case '+':
case '-': return 1;
case '*':
case '/':
case '%': return 2;
case '^': return 3;
}
}


/* convert infix to prefix expression ---- */
intopre()
{
int i,p, type, pr;
char ch, op;
for(i=0; infix[i]!='\0';i++);

for(i=i-1, p=0; i>=0;i--)
{
ch = infix[i];
type = gettype(ch);
switch(type)
{
case OPND: prefix[p++] = ch;
break;
case RP: push(stk, ch); break;
case OPT: pr = prec(ch);
while(stk!=NULL&& prinfo))
prefix[p++] = pop(stk);
push(stk, ch);
break;
case LP: while(stk!=NULL && (op = pop(stk))!=')')
prefix[p++] = op;
break;
}
}

while(stk!=NULL)
prefix[p++] = pop(stk);
prefix[p] = '\0';
strrev(prefix);
}

Infix expression to prefix expression conversion

/* program on conversion of infix to prefix expression */

#include "stdio.h"
#include "stdlib.h"
#include "string.h"

#define OPT 0
#define OPND 1
#define LP 2
#define RP 3

char infix[100], prefix[100]; /* global variables declare here */

struct stack
{
char info;
struct stack *next;
} *stk;


/* main function here */

int main(void)
{
clrscr();
printf("\nEnter a infix expression : ");
fflush(stdin);
gets(infix);
intopre();
printf("\nPrefix expression is : %s", prefix);
getch();
return 0;
}


/* push function here */

push(struct stack *s, char n)
{
struct stack *temp;
temp = (struct stack *)malloc(sizeof(struct stack));
temp->info = n;
temp->next = s;
stk = temp;
}

/* Pop function here */

char pop(struct stack *s)
{
char ch;
if(s!=NULL)
{
ch = s->info;
stk = s->next;
free(s);
}
return ch;
}
/* ---- get character type here wheather operator or operand ----- */
int gettype(char ch)
{
if(ch=='+'|| ch == '-' || ch == '*' || ch == '/' || ch == '%')
return OPT;
else if(ch == '(')
return LP;
else if(ch == ')')
return RP;
else
return OPND;
}

/* --- get precedence of operator ------ */
prec(char op)
{
switch(op)
{
case ')':
case '(': return 0;
case '+':
case '-': return 1;
case '*':
case '/':
case '%': return 2;
case '^': return 3;
}
}


/* convert infix to prefix expression ---- */
intopre()
{
int i,p, type, pr;
char ch, op;
for(i=0; infix[i]!='\0';i++);

for(i=i-1, p=0; i>=0;i--)
{
ch = infix[i];
type = gettype(ch);
switch(type)
{
case OPND: prefix[p++] = ch;
break;
case RP: push(stk, ch); break;
case OPT: pr = prec(ch);
while(stk!=NULL&& prinfo))
prefix[p++] = pop(stk);
push(stk, ch);
break;
case LP: while(stk!=NULL && (op = pop(stk))!=')')
prefix[p++] = op;
break;
}
}

while(stk!=NULL)
prefix[p++] = pop(stk);
prefix[p] = '\0';
strrev(prefix);
}

Infix expression to postfix expression conversion

/* program on conversion of infix to postfix expression */

#include"stdio.h"
#include"stdlib.h"

#define OPT 0
#define OPND 1
#define LP 2
#define RP 3

char infix[100], postfix[100]; /* global variables declare here */

struct stack
{
char info;
struct stack *next;
} *stk;


/* main function here */

int main(void)
{
clrscr();
printf("\nEnter a infix expression : ");
fflush(stdin);
gets(infix);
intopost();
printf("\nPostfix expression is : %s", postfix);
getch();
return 0;
}


/* push function here */

push(struct stack *s, char n)
{
struct stack *temp;
temp = (struct stack *)malloc(sizeof(struct stack));
temp->info = n;
temp->next = s;
stk = temp;
}

/* Pop function here */

char pop(struct stack *s)
{
char ch;
if(s!=NULL)
{
ch = s->info;
stk = s->next;
free(s);
}
return ch;
}
/* get character type form infix expression */
int gettype(char ch)
{
if(ch=='+'|| ch == '-' || ch == '*' || ch == '/' || ch == '%')
return OPT;
else if(ch == '(')
return LP;
else if(ch == ')')
return RP;
else
return OPND;
}

/* get precedence of operator */
prec(char op)
{
switch(op)
{
case ')':
case '(': return 0;
case '+':
case '-': return 1;
case '*':
case '/':
case '%': return 2;
case '^': return 3;
}
}

/* convert infix to postfix expression */
intopost()
{
int i,p, type, pr;
char ch, op;

for(i=0, p=0; infix[i]!='\0';i++)
{
ch = infix[i];
type = gettype(ch);
switch(type)
{
case OPND: postfix[p++] = ch;
break;
case LP: push(stk, ch); break;
case OPT: pr = prec(ch);
while(stk!=NULL&& pr<=prec(stk->info))
postfix[p++] = pop(stk);
push(stk, ch);
break;
case RP: while(stk!=NULL && (op = pop(stk))!='(')
postfix[p++] = op;
break;
}
}

while(stk!=NULL)
postfix[p++] = pop(stk);
postfix[p] = '\0';
}

Infix expression to postfix expression conversion

/* program on conversion of infix to postfix expression */

#include"stdio.h"
#include"stdlib.h"

#define OPT 0
#define OPND 1
#define LP 2
#define RP 3

char infix[100], postfix[100]; /* global variables declare here */

struct stack
{
char info;
struct stack *next;
} *stk;


/* main function here */

int main(void)
{
clrscr();
printf("\nEnter a infix expression : ");
fflush(stdin);
gets(infix);
intopost();
printf("\nPostfix expression is : %s", postfix);
getch();
return 0;
}


/* push function here */

push(struct stack *s, char n)
{
struct stack *temp;
temp = (struct stack *)malloc(sizeof(struct stack));
temp->info = n;
temp->next = s;
stk = temp;
}

/* Pop function here */

char pop(struct stack *s)
{
char ch;
if(s!=NULL)
{
ch = s->info;
stk = s->next;
free(s);
}
return ch;
}
/* get character type form infix expression */
int gettype(char ch)
{
if(ch=='+'|| ch == '-' || ch == '*' || ch == '/' || ch == '%')
return OPT;
else if(ch == '(')
return LP;
else if(ch == ')')
return RP;
else
return OPND;
}

/* get precedence of operator */
prec(char op)
{
switch(op)
{
case ')':
case '(': return 0;
case '+':
case '-': return 1;
case '*':
case '/':
case '%': return 2;
case '^': return 3;
}
}

/* convert infix to postfix expression */
intopost()
{
int i,p, type, pr;
char ch, op;

for(i=0, p=0; infix[i]!='\0';i++)
{
ch = infix[i];
type = gettype(ch);
switch(type)
{
case OPND: postfix[p++] = ch;
break;
case LP: push(stk, ch); break;
case OPT: pr = prec(ch);
while(stk!=NULL&& pr<=prec(stk->info))
postfix[p++] = pop(stk);
push(stk, ch);
break;
case RP: while(stk!=NULL && (op = pop(stk))!='(')
postfix[p++] = op;
break;
}
}

while(stk!=NULL)
postfix[p++] = pop(stk);
postfix[p] = '\0';
}

Monday, April 18, 2011

Stack Dynamic implemetation by Sweta Mam

Stack (Dynamic implemetation) by Sweta Mam

/* Stack Dynamic implemetation by Sweta Mam */

#include"stdio.h"
#include"stdlib.h"
#include"conio.h"

struct stack
{
int info;
struct stack *next;
}*front;


int main(void)
{
int n, ch;
while(1)
{
clrscr();
puts("1. Push\n2. Pop\n3. Display\n4. Exit");
puts("Enter your choice : ");
scanf("%d",&ch);
switch(ch)
{
case 1: printf("Enter info : ");
scanf("%d",&n);
push(front, n);
break;
case 2: n = pop(front);
if(n == -1)
{
printf("\nPopped %d",n);
getch();
}
break;
case 3: disp(front);
break;
case 4: exit(0);

default: puts("Invalid choice.");
getch();
}
}
return 0;
}


/* push function here */

push(struct stack *s, int n)
{
struct stack *temp;
temp = (struct stack *)malloc(sizeof(struct stack));
temp->info = n;
temp->next = s;
front = temp;
}

/* pop function here */

pop(struct stack *s)
{
int n;
if(s==NULL)
{
printf("Stack underflow.");
return -1;
}
n = s->info;
front = s->next;
free(s);
return n;
}

/* display function here */

disp(struct stack *s)
{
clrscr();
if(s!=NULL)
{
printf("\nStack are : \n");

while(s!=NULL)
{
printf("\n\t %d",s->info);
s = s->next;
}
getch();
}
else
{
printf("Stack is empty.");
getch();
}
}

Stack Dynamic implemetation by Sweta Mam

Stack (Dynamic implemetation) by Sweta Mam

/* Stack Dynamic implemetation by Sweta Mam */

#include"stdio.h"
#include"stdlib.h"
#include"conio.h"

struct stack
{
int info;
struct stack *next;
}*front;


int main(void)
{
int n, ch;
while(1)
{
clrscr();
puts("1. Push\n2. Pop\n3. Display\n4. Exit");
puts("Enter your choice : ");
scanf("%d",&ch);
switch(ch)
{
case 1: printf("Enter info : ");
scanf("%d",&n);
push(front, n);
break;
case 2: n = pop(front);
if(n == -1)
{
printf("\nPopped %d",n);
getch();
}
break;
case 3: disp(front);
break;
case 4: exit(0);

default: puts("Invalid choice.");
getch();
}
}
return 0;
}


/* push function here */

push(struct stack *s, int n)
{
struct stack *temp;
temp = (struct stack *)malloc(sizeof(struct stack));
temp->info = n;
temp->next = s;
front = temp;
}

/* pop function here */

pop(struct stack *s)
{
int n;
if(s==NULL)
{
printf("Stack underflow.");
return -1;
}
n = s->info;
front = s->next;
free(s);
return n;
}

/* display function here */

disp(struct stack *s)
{
clrscr();
if(s!=NULL)
{
printf("\nStack are : \n");

while(s!=NULL)
{
printf("\n\t %d",s->info);
s = s->next;
}
getch();
}
else
{
printf("Stack is empty.");
getch();
}
}

Stack static implementation program by Sweta Mam

/* Stack static implementation program by Sweta Mam */

#include"conio.h"
#include"stdio.h"
#include"stdlib.h"

#define MAX 5

struct stack
{
int info[MAX];
int top;
} stk;

int main(void)
{
int n, ch;
stk.top = -1;
clrscr();
while(1)
{
clrscr();
puts("\n\n1. Push\n2. Pop\n3. Display\n4. Exit.");
printf("Enter your choice : ");
scanf("%d",&ch);
switch(ch)
{
case 1: printf("Enter info : ");
scanf("%d",&n);
push(&stk, n);
break;
case 2: n = pop(&stk);
if(n!=-1)
{
printf("Popped : %d",n);
getch();
}
break;
case 3: disp(&stk);
break;
case 4: exit(0);
default: puts("\nInvalid Input.\nPlease enter 1-4.");
getch();
}
}
}
push(struct stack *s, int n)
{
if(s->top == MAX-1)
{
puts("Stack overflow.");
getch();
return;
}
s->info[++s->top] = n;
}

pop(struct stack *s)
{
int n;
if(s->top == -1)
{
printf("Stack underflow.");
getch();
return;
}
n = s->info[s->top--];
return n;
}

disp(struct stack *s)
{
int i;
clrscr();
printf("\nStacks are : \n");
if(s->top!= -1)
{
for(i=s->top; i>=0; i--)
printf("\n\t%d",s->info[i]);
}
else
printf("No stacks created yet.");
getch();
}

Stack static implementation program by Sweta Mam

/* Stack static implementation program by Sweta Mam */

#include"conio.h"
#include"stdio.h"
#include"stdlib.h"

#define MAX 5

struct stack
{
int info[MAX];
int top;
} stk;

int main(void)
{
int n, ch;
stk.top = -1;
clrscr();
while(1)
{
clrscr();
puts("\n\n1. Push\n2. Pop\n3. Display\n4. Exit.");
printf("Enter your choice : ");
scanf("%d",&ch);
switch(ch)
{
case 1: printf("Enter info : ");
scanf("%d",&n);
push(&stk, n);
break;
case 2: n = pop(&stk);
if(n!=-1)
{
printf("Popped : %d",n);
getch();
}
break;
case 3: disp(&stk);
break;
case 4: exit(0);
default: puts("\nInvalid Input.\nPlease enter 1-4.");
getch();
}
}
}
push(struct stack *s, int n)
{
if(s->top == MAX-1)
{
puts("Stack overflow.");
getch();
return;
}
s->info[++s->top] = n;
}

pop(struct stack *s)
{
int n;
if(s->top == -1)
{
printf("Stack underflow.");
getch();
return;
}
n = s->info[s->top--];
return n;
}

disp(struct stack *s)
{
int i;
clrscr();
printf("\nStacks are : \n");
if(s->top!= -1)
{
for(i=s->top; i>=0; i--)
printf("\n\t%d",s->info[i]);
}
else
printf("No stacks created yet.");
getch();
}

Related Posts Plugin for WordPress, Blogger...

Share

Twitter Delicious Facebook Digg Stumbleupon Favorites