Kontera

Thursday, December 1, 2011

Postfix Evaluation using Stack


/************************************************************
* Filename: postfix_evaluation.c
* Description: postfix evaluation using stack
* Author:Sarju S
* Date: 02-Dec-2011
*************************************************************/


#include
#include
#define MAX_EXPR_SIZE 100
#define MAX_STACK_SIZE 100 


typedef enum {eos,lparen, rparen, plus, minus, times, divide,mod,operand} precedence;
char postfixExpr[MAX_EXPR_SIZE];
int stack[MAX_STACK_SIZE];
int top=-1,postfixCount=0;


void stackFull()
{
fprintf(stderr, "Stack is full cannot add elements\n");
exit(EXIT_FAILURE);
}


void stackEmpty()
{
fprintf(stderr, "Stack is empty cannot delete elements\n");
exit(EXIT_FAILURE);
}


void push(int item)
{/* add an element to stack */
if(top>=MAX_STACK_SIZE-1)
stackFull();
top++;
stack[top] = item;
}


int pop()
{/*Delete top element from the stack*/
if(top==-1)
stackEmpty();
return stack[top--];
}




precedence getToken(char *symbol)
{ /* get the next token, symbol is the character representation,which is 
returned, the token is represented by its enumerated value, which is 
returned in the function name */
*symbol = postfixExpr[postfixCount];
postfixCount++;
switch(*symbol) {
case '\0': return eos;
case '(' : return lparen;
case ')' : return rparen;
case '+' : return plus;
case '-' : return minus;
case '/' : return divide;
case '*' : return times;
case '%' : return mod;
default  : return operand;
}
}




int postfixEvaluate(){
/* evaluate a postfix expression and returns the result */
precedence token;
char symbol;
int op1,op2;
token = getToken(&symbol);
while(token!=eos){
if(token==operand)
push(symbol-'0');

else{
/* pop two operands, perform operation and 
push result to stack */
op2 = pop();
op1 = pop();
switch(token){
case plus :push(op1+op2);
  break;
case minus :push(op1-op2);
  break;
case times :push(op1*op2);
  break;
case divide :push(op1/op2);
  break;
case mod :push(op1%op2);
 
}
}
token = getToken(&symbol);
}
return pop();/* return result */
}


void main(){
int result;
printf("\nEnter the POSTFIX expression:");
scanf("%s",postfixExpr);
result=postfixEvaluate(); /* Function Call*/
printf("\nThe Result is:%d",result);
}




OUTPUT

krishnakripa@krishnakripa-K8Upgrade-VM800:~$ gcc postfix_evaluation.c
krishnakripa@krishnakripa-K8Upgrade-VM800:~$ ./a.out


Enter the POSTFIX expression:62/3-42*+


The Result is:8
krishnakripa@krishnakripa-K8Upgrade-VM800:~$ 

Infix to Postfix conversion using stack


/************************************************************
* Filename: infix_to_postfix.c
* Description: infix to postfix conversion using stack
* Author:Sarju S
* Date: 01-Dec-2011
*************************************************************/
#include
#include
#define MAX_EXPR_SIZE 100
#define MAX_STACK_SIZE 100 

typedef enum {eos,lparen, rparen, plus, minus, times, divide,mod,operand} precedence;
char infixExpr[MAX_EXPR_SIZE],postfixExpr[MAX_EXPR_SIZE];
precedence stack[MAX_STACK_SIZE];
int top=-1,infixCount=0,postfixCount=0;

void stackFull()
{
fprintf(stderr, "Stack is full cannot add elements\n");
exit(EXIT_FAILURE);
}

void stackEmpty()
{
fprintf(stderr, "Stack is empty cannot delete elements\n");
exit(EXIT_FAILURE);
}

void push(precedence item)
{/* add an element to stack */
if(top>=MAX_STACK_SIZE-1)
stackFull();
top++;
stack[top] = item;
}

precedence pop()
{/*Delete top element from the stack*/
if(top==-1)
stackEmpty();
return stack[top--];
}

void printToken(precedence token){
/* to print the symbol corresponding to token*/
switch(token) {
case 3 :postfixExpr[postfixCount]='+';
postfixCount++;
break;
case 4 :postfixExpr[postfixCount]='-';
postfixCount++;
break;
case 5 :postfixExpr[postfixCount]='*';
postfixCount++;
break; 
case 6 :postfixExpr[postfixCount]='/';
postfixCount++;
break;
case 7 :postfixExpr[postfixCount]='%';
postfixCount++;
}
}

precedence getToken(char *symbol)
{ /* get the next token, symbol is the character representation,which is 
returned, the token is represented by its enumerated value, which is 
returned in the function name */

*symbol = infixExpr[infixCount];
infixCount++;
switch(*symbol) {
case '\0': return eos;
case '(' : return lparen;
case ')' : return rparen;
case '+' : return plus;
case '-' : return minus;
case '/' : return divide;
case '*' : return times;
case '%' : return mod;
default  : return operand;
}
}

int checkPriority(precedence token){
switch(token) {
case plus   : 
case minus  : return 4;
case times  : 
case divide : return 6;
}

}

void infixToPostfix()
{/* output postfix of expression */
char symbol;
precedence token;
top = 0;
stack[0]= eos;
token = getToken(&symbol);
while(token!=eos){
if(token == operand){
postfixExpr[postfixCount]=symbol;
postfixCount++;
}

else if(token == rparen){
/*unstack tokens until left parenthesis */
while(stack[top]!= lparen)
printToken(pop());
pop(); /*remove left parenthesis */
}
else {
/*remove and print symbols whose in-stack precedence is greater than or 
 equal to the current tokens incoming precedence */
if(token!=1) /*Avoid Comparison of Left Paranthesis */
while(checkPriority(stack[top])>= checkPriority(token))
printToken(pop());
push(token);
}
token =getToken(&symbol);

while((token = pop())!=eos)
printToken(token);
postfixExpr[postfixCount]='\0';
}

void main(){
printf("\nEnter the INFIX expression:");
scanf("%s",infixExpr);
infixToPostfix(); /* Function Call*/
printf("\nThe POSTFIX expression:");
printf("%s\n",postfixExpr);

}

OUTPUT

krishnakripa@krishnakripa-K8Upgrade-VM800:~$ gcc infix_to_postfix.c
krishnakripa@krishnakripa-K8Upgrade-VM800:~$ ./a.out

Enter the INFIX expression:((a/(b-c+d)))*(e-a)*c

The POSTFIX expression:abc-d+/ea-*c*
krishnakripa@krishnakripa-K8Upgrade-VM800:~$ ./a.out

Enter the INFIX expression:a/b-c+d*e-a*c        

The POSTFIX expression:ab/c-de*+ac*-
krishnakripa@krishnakripa-K8Upgrade-VM800:~$ 

Wednesday, November 30, 2011

Stack operations using array


/************************************************************
* Filename: Stack_Using_Array.c
* Description: To do stack operations using array 
* Author: Sarju S
* Date: 01-Dec-2011
*************************************************************/
#include
#include

#define MAX_STACK_SIZE 100 /*maximum stack size*/

int stack[MAX_STACK_SIZE],top=-1;/* Global Declarations */

void stackFull()
{
fprintf(stderr, "Stack is full cannot add elements\n");
exit(EXIT_FAILURE);
}

void stackEmpty()
{
fprintf(stderr, "Stack is empty cannot delete elements\n");
exit(EXIT_FAILURE);
}

void push(int item)
{/* add an element to stack */
if(top>=MAX_STACK_SIZE-1)
stackFull();
stack[++top] = item;
}

int pop()
{/*Delete top element from the stack*/
if(top==-1)
stackEmpty();
return stack[top--];

}

void display()
{
int i;
for(i=0;i<=top;i++)
printf("%d\t",stack[i]);
}

void main()
{
int ch,element;
do /* Loop for repeating the menu*/
{
printf("\nMENU\n1.PUSH \n2.POP\n3.EXIT");
printf("\nEnter Your Choice:");
scanf("%d",&ch);
switch(ch)
{
case 1: printf("\nEnter the element to add:");
scanf("%d",&element);
push(element);
printf("\nElement added is:%d",element);
printf("\nThe Current Stack is:\n");
display();
break;
case 2: element = pop();
printf("\nElement deleted is:%d",element);
printf("\nThe Current Stack is:\n");
display();

}
}while(ch<3);
}

OUTPUT

krishnakripa@krishnakripa-K8Upgrade-VM800:~$ gcc Stack_Using_Array.c
krishnakripa@krishnakripa-K8Upgrade-VM800:~$ ./a.out

MENU
1.PUSH 
2.POP
3.EXIT
Enter Your Choice:2
Stack is empty cannot delete elements
krishnakripa@krishnakripa-K8Upgrade-VM800:~$ ./a.out

MENU
1.PUSH 
2.POP
3.EXIT
Enter Your Choice:1

Enter the element to add:11

Element added is:11
The Current Stack is:
11
MENU
1.PUSH 
2.POP
3.EXIT
Enter Your Choice:1

Enter the element to add:22

Element added is:22
The Current Stack is:
11 22
MENU
1.PUSH 
2.POP
3.EXIT
Enter Your Choice:1

Enter the element to add:33

Element added is:33
The Current Stack is:
11 22 33
MENU
1.PUSH 
2.POP
3.EXIT
Enter Your Choice:2

Element deleted is:33
The Current Stack is:
11 22
MENU
1.PUSH 
2.POP
3.EXIT
Enter Your Choice:2

Element deleted is:22
The Current Stack is:
11
MENU
1.PUSH 
2.POP
3.EXIT
Enter Your Choice:2

Element deleted is:11
The Current Stack is:

MENU
1.PUSH 
2.POP
3.EXIT
Enter Your Choice:2
Stack is empty cannot delete elements


Transpose of a Sparse Matrix using array


/************************************************************
* Filename: sparse_matrix.c
* Description: To find transpose of a sparse matrix using array
* Author: Sarju S
* Date: 25-Nov-2011
*************************************************************/

#define MAX_TERMS 100
#include
typedef struct{
int col;
int row;
int value;
}sparse_matrix;
sparse_matrix a[MAX_TERMS],b[MAX_TERMS];
int input_matrix[10][10];

void create_sparse(int row, int col){
int i,j,k=1,count=0;
a[0].row=row;
a[0].col=col;
for(i=0;i
for(j=0;j
if(input_matrix[i][j]!=0)
{
count++;
a[k].row=i;
a[k].col=j;
a[k].value = input_matrix[i][j];
k++;
}
a[0].value = count;
}

void print_sparse(sparse_matrix matrix[]){
int i;
for(i=0;i<=matrix[0].value;i++)
printf("%d\t%d\t%d\n",matrix[i].row,matrix[i].col,matrix[i].value);
}

void transpose_sparse(){
int currentb=1,n,i,j;
n=a[0].value; /* total number of elements*/
b[0].row = a[0].col;
b[0].col = a[0].row;
b[0].value = a[0].value;
for(i=0;i
for(j=1;j<=n;j++)
if(a[j].col==i) 
{
/* Element is in the current column add it to b*/
b[currentb].row = a[j].col;
b[currentb].col = a[j].row;
b[currentb].value = a[j].value;
currentb++;

}
}


void main(){
int row,col,i,j;
printf("\nEnter the order of the matrix:");
scanf("%d%d",&row,&col);
printf("\nEnter the elements\n");

/*Read the matrix*/
for(i=0;i
for(j=0;j
scanf("%d",&input_matrix[i][j]);

/*Create sparse matrix*/
create_sparse(row,col);
printf("The Given Matrix is\n");
for(i=0;i
{
for(j=0;j
printf("%d\t",input_matrix[i][j]);
printf("\n");
}

/*Print sparse matrix*/
printf("\nThe Sparse Matrix is\n");
print_sparse(a);

/* Call transpose function*/
transpose_sparse();

/*Print transpose of sparse matrix*/
printf("\nThe transpose of Sparse Matrix is\n");
print_sparse(b);


}


OUTPUT


sjcet@sjcet-laptop:~$ gcc sparse_matrix.c
sjcet@sjcet-laptop:~$ ./a.out

Enter the order of the matrix:6 6

Enter the elements
15 0 0 22 0 -15
0 11 3 0 0 0
0 0 0 -6 0 0
0 0 0 0 0 0
91 0 0 0 0 0
0 0 28 0 0 0
The Given Matrix is
15 0 0 22 0 -15
0 11 3 0 0 0
0 0 0 -6 0 0
0 0 0 0 0 0
91 0 0 0 0 0
0 0 28 0 0 0

The Sparse Matrix is
6 6 8
0 0 15
0 3 22
0 5 -15
1 1 11
1 2 3
2 3 -6
4 0 91
5 2 28

The transpose of Sparse Matrix is
6 6 8
0 0 15
0 4 91
1 1 11
2 1 3
2 5 28
3 0 22
3 2 -6
5 0 -15