Friday, July 27, 2012

Infix to Postfix Conversion


#include<stdio.h>
#include<conio.h>

#define SIZE 50

char stack[SIZE];
int top=-1;


void main()
{
char ch,x;

//infix expression and postfix expression(output expression)
char *ifxexpr,pfxexpr[50];

//functions to calculate in-stack priority and in-coming priority
int ISP(char);
int ICP(char);


void push(char);
char pop();
int i,size=0;

clrscr();

printf("\nEnter Valid Infix Expression : ");
gets(ifxexpr);


//push '(' to the stack
push('(');

//Append ')' to mark end of the expression
strcat(ifxexpr,")");

i=0;



while(top>-1)
{
x=pop();
ch=ifxexpr[i];

if(isalpha(ch)) //check if the character is operand
{

//append to the output postfix expression
pfxexpr[size]=ch;
pfxexpr[size+1]='\0';
size++;
push(x);

}
else if(ch==')')
{
//pop the items and add it to output untill '(' appears
while(x!='(')
{

pfxexpr[size]=x;
pfxexpr[size+1]='\0';
size++;
x=pop();
}
}
else if(ISP(x)>=ICP(ch))
{

//pop the items and add it to the output untill we get lower priority symbol in stack
while(ISP(x)>ICP(ch))
{

pfxexpr[size]=x;
pfxexpr[size+1]='\0';
size++;
x=pop();
}
push(x);
push(ch);
}
else if(ISP(x)<ICP(ch))
{
push(x);
push(ch);
}
else
{
printf("\nInvalid Expression");
exit(0);
}

i++;

}

printf("\nPostfix Expression : ");
puts(pfxexpr);
getch();

}


void push(char item)
{
if(top>=SIZE)
{
printf("\nStack is Full");
getch();
}
else
{
top++;
stack[top]=item;
}
}

char pop()
{
char item;
if(top<0)
{

item='\0';
}
else
{
item=stack[top];
top--;
}
return(item);
}


int ISP(char ch)
{
int p;
switch(ch)
{
case '+':
case '-':
p=2;
break;
case '*':
case '/':
p=4;
break;
case '^':
p=5;
break;
case '(':
p=0;
break;

}
return(p);
}

int ICP(char ch)
{
int p;
switch(ch)
{
case '+':
case '-':
p=1;
break;
case '*':
case '/':
p=3;
break;
case '^':
p=6;
break;
case '(':
p=9;
break;
case ')':
p=0;
break;

}
return(p);
}

Thursday, July 19, 2012

Array Implementation of Stack


#include<stdio.h>
#include<conio.h>

#define SIZE 10

int stack[SIZE];
int top=-1;


void main()
{
void push(int);
int pop();
void show();

int choice,item;

while(1)
{

clrscr();
printf("\n1. Push an Item");
printf("\n2. Pop an Item");
printf("\n3. Display Stack");
printf("\n4. Exit");
printf("\nEnter your choice : ");
scanf("%d",&choice);

switch(choice)
{
case 1:
printf("\nEnter number : ");
scanf("%d",&item);
push(item);
break;
case 2:
item=pop();
break;
case 3:
show();
break;
case 4:
exit(0);
default:
printf("\nWron Input.....");
getch();
}

}

}

void push(int item)
{
if(top>=SIZE)
{
printf("\nStack is Full");
getch();
}
else
{
top++;
stack[top]=item;
printf("\nItem is pushed");
getch();
}
}

int pop()
{
int item;
if(top<0)
{
printf("\nStack is Empty");
getch();
}
else
{
item=stack[top];
top--;
printf("\nItem %d popped",item);
getch();
}
return(item);
}

void show()
{
int i;
if(top<0)
{
printf("\nStack is Empty");
getch();
}
else
{
printf("\nStack Elements : ");
for(i=0;i<=top;i++)
{
printf("%d ",stack[i]);
}
getch();

}
}

Tuesday, July 17, 2012

A Function That Converts Digits into Words - Visual Basic Code


Public Function numtowords(no As Long) As String

no_str = CStr(no)


str_len = Len(no_str)

temp_str = ""

If str_len < 9 Then
For i = 1 To 9 - str_len
temp_str = temp_str + "0"
Next
no_str = temp_str + no_str
End If


'For last 2 digits

If Val(Right(no_str, 2)) < 20 Then
If Val(Right(no_str, 2)) < 10 Then
unit = unitnum(Right(no_str, 1))
Else
unit = unitnum(Right(no_str, 2))
End If
Else
unit = numty(Left(Right(no_str, 2), 1)) + " " + unitnum(Right(no_str, 1))
End If


'For 3rd digit from last

If Val(Left(Right(no_str, 3), 1)) <> 0 Then
temp_unit = unitnum(Left(Right(no_str, 3), 1)) + " Hundred "
unit = temp_unit + unit
End If

'For 4th & 5th digits from last
If Left(Right(no_str, 5), 2) <> "00" Then

If Val(Left(Right(no_str, 5), 2)) < 20 Then

If Val(Left(Right(no_str, 5), 2)) < 10 Then
temp_unit = unitnum(Left(Right(no_str, 4), 1))
Else
temp_unit = unitnum(Left(Right(no_str, 5), 2))
End If

unit = temp_unit + " Thousand " + unit

Else

temp_unit = numty(Left(Right(no_str, 5), 1)) + " " + unitnum(Left(Right(no_str, 4), 1))

unit = temp_unit + " Thousand " + unit

End If

End If
'For 6th & 7th digits from last


If Left(Right(no_str, 7), 2) <> "00" Then
If Val(Left(Right(no_str, 7), 2)) < 20 Then

If Val(Left(Right(no_str, 7), 2)) < 10 Then
temp_unit = unitnum(Left(Right(no_str, 6), 1))
Else
temp_unit = unitnum(Left(Right(no_str, 7), 2))
End If

unit = temp_unit + " Lakhs " + unit

Else

temp_unit = numty(Left(Right(no_str, 7), 1)) + " " + unitnum(Left(Right(no_str, 6), 1))

unit = temp_unit + " Lakhs " + unit

End If

End If
'For 8th & 9th digits from last

If Left(Right(no_str, 9), 2) <> "00" Then

If Val(Left(Right(no_str, 9), 2)) < 20 Then

If Val(Left(Right(no_str, 9), 2)) < 10 Then
temp_unit = unitnum(Left(Right(no_str, 8), 1))
Else
temp_unit = unitnum(Left(Right(no_str, 9), 2))
End If

unit = temp_unit + " Crores " + unit

Else

temp_unit = numty(Left(Right(no_str, 9), 1)) + " " + unitnum(Left(Right(no_str, 8), 1))

unit = temp_unit + " Crores " + unit

End If

End If

word_str = unit
numtowords = word_str

End Function

Public Function numty(num As Integer) As String
Dim tstr As String
Select Case num

Case 2:
tstr = "Twenty"
Case 3:
tstr = "Thirty"
Case 4:
tstr = "Forty"
Case 5:
tstr = "Fifty"
Case 6:
tstr = "Sixty"
Case 7:
tstr = "Seventy"
Case 8:
tstr = "Eighty"
Case 9:
tstr = "Ninty"
End Select

numty = tstr

End Function

Public Function unitnum(num As String) As String
Dim tstr As String
Select Case num

Case "0":
tstr = ""
Case "1":
tstr = "One"
Case "2":
tstr = "Two"
Case "3":
tstr = "Three"
Case "4":
tstr = "Four"
Case "5":
tstr = "Five"
Case "6":
tstr = "Six"
Case "7":
tstr = "Seven"
Case "8":
tstr = "Eight"
Case "9":
tstr = "Nine"
Case "10":
tstr = "Ten"
Case "11":
tstr = "Eleven"
Case "12":
tstr = "Twelve"
Case "13":
tstr = "Thirteen"
Case "14":
tstr = "Fourteen"
Case "15":
tstr = "Fifteen"
Case "16":
tstr = "Sixteen"
Case "17":
tstr = "Seventeen"
Case "18":
tstr = "Eighteen"
Case "19":
tstr = "Nineteen"
End Select

unitnum = tstr

End Function