Documente Academic
Documente Profesional
Documente Cultură
OUTPUT:
ENTER THE EXPRESSION : (((())))
VALID
#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
#define TRUE 1
#define FALSE 0
struct Node;
typedef struct Node *PtrToNode;
typedef struct Node *Stack;
void push(Stack S,char);
void pop(Stack S);
int empty(Stack S);
struct Node
{
char Element;
PtrToNode Next;
};
Stack S;
void main()
{
char instr[20];
int valid=TRUE,i=0;
S=(Stack)malloc(sizeof(struct Node));
S->Next=NULL;
printf("enter the arithmetic expression");
scanf("%s",instr);
while(instr[i]!='\0')
{
if(instr[i]=='(')
push(S,instr[i]);
if(instr[i]==')')
{
if(empty(S))
{
valid=FALSE;
break;
}
else
{
pop(S);
}
}
i++;
}
if(!empty(S))
valid=FALSE;
if(valid)
printf("\n The given expression is valid");
else
printf("\n The given expression is invalid");
getch();
}
void pop(Stack S)
{
PtrToNode FirstCell;
FirstCell=S->Next;
S->Next=S->Next->Next;
free(FirstCell);
}
int empty(Stack S)
{
if(S->Next==NULL)
return TRUE;
else
return FALSE;
}
OUTPUT:
$./a.out
$./a.out
#include<stdio.h>
#include<ctype.h>
#include<conio.h>
#include<stdlib.h>
struct stack
{
int top;
int operand[40];
};
void push(int);
void pop();
struct stack s;
void main()
{
char instr[40];
int i=0,result,op1,op2;
int value;
s.top=-1;
clrscr();
printf("Enter an expression in postfix form\n");
while((instr[i]=getchar())!='\n')
{
if(isdigit(instr[i]))
{
printf("Pushing...%d\n",instr[i]-'0');
push(instr[i]-'0');
}
else
{
op2=s.operand[s.top];
pop();
printf("Popping....%d\n",op2);
op1=s.operand[s.top];
pop();
printf("Popping ....%d\n",op1);
printf("Applying operator %c\n",instr[i]);
switch(instr[i])
{
case '+':
result=op1+op2;
break;
case '-':
result=op1-op2;
break;
case '*':
result=op1*op2;
break;
case '/':
result=op1/op2;
break;
}
printf("\n Pushing intermediate Result ....%d\n",result);
push(result);
}
i++;
}
printf("\n Value of postfix expression is %d\n",result);
getch();
void push(int n)
{
s.top++;
s.operand[s.top]=n;
}
void pop()
{
s.top--;
}
OUTPUT:
$
Enter an expression in postfix form42-23+*
Pushing...4
Pushing...2
Popping....2
Popping ....4
Applying operator -
#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
#include<ctype.h>
struct Node;
typedef struct Node *PtrToNode;
typedef struct Node *Stack;
void push(Stack S,char);
void pop(Stack S);
struct Node
{
char Element;
PtrToNode Next;
};
Stack S;
void main()
{
char instr[40];
int i=0,result,op1,op2;
int value;
clrscr();
S=(Stack)malloc(sizeof(struct Node));
S->Next=NULL;
printf("Enter an expression in postfix form\n");
scanf("%s",instr);
while(instr[i]!='\0')
{
if(isdigit(instr[i]))
{
printf("Pushing...%d\n",instr[i]-'0');
push(S,instr[i]-'0');
}
else
{
op2=S->Next->Element;
pop(S);
printf("Popping....%d\n",op2);
op1=S->Next->Element;
pop(S);
printf("Popping ....%d\n",op1);
printf("Applying operator %c\n",instr[i]);
switch(instr[i])
{
case '+':
result=op1+op2;
break;
case '-':
result=op1-op2;
break;
case '*':
result=op1*op2;
break;
case '/':
result=op1/op2;
break;
}
printf("\n Pushing intermediate result.......%d\n",result);
push(S,result);
}
i++;
}
printf("\n Value of postfix expression is %d\n",result);
getch();
}
void pop(Stack S)
{
PtrToNode FirstCell;
FirstCell=S->Next;
S->Next=S->Next->Next;
free(FirstCell);
}
OUTPUT:
$ ./a.out
Enter an expression in postfix form23*23*+
Pushing...2
Pushing...3
Popping....3
Popping ....2
Applying operator *