Programming Tutorial for C
Saturday, 2 April 2016
Zen Philosophy, Zen Stories, Zen Quotes, Zen Inspirations, Zip Zap Zing: Zen Stories, Zen Philosophy, The Real Miracle, rea...
Zen Philosophy, Zen Stories, Zen Quotes, Zen Inspirations, Zip Zap Zing: Zen Stories, Zen Philosophy, The Real Miracle, rea...: Learning to be Silent The pupils of the Tendai School used to study meditation before Zen entered Japan. Four of them who were intim...
Thursday, 31 March 2016
Sorting & Searching Quick Sort, With Recursion, With Out Recursion , Using Goto Statement
Write a program to perform Quick Sort
/* Quick sort.
How meny numbers you want to enter :- 5
Please enter the numbers : - 12
56
5
64
48
Array before sorting:
12 56 5 64 48
Quick sort.
Array after sorting:
5 12 48 56 64
*/
#include <stdio.h>
void quick ( int *, int, int ) ;
int split ( int *, int, int ) ;
int main( )
{
int arr[100];
int i,a ;
printf("How meny numbers you want to enter :- ");
scanf("%d",&a);
printf("Please enter the numbers : - ");
for(i=0;i<a;i++)
scanf("%d",&arr[i]);
printf ( "\nArray before sorting:\n") ;
for ( i = 0 ; i < a ; i++ )
printf ( "%d\t", arr[i] ) ;
printf ( "\nQuick sort.\n" ) ;
quick( arr, 0, a ) ;
printf ( "\nArray after sorting:\n") ;
for ( i = 0 ; i < a ; i++ )
printf ( "%d\t", arr[i] ) ;
}
void quick(int a[],int lower,int upper)
{
int i,beg,end,top=-1,n=upper,lower1[10],upper1[10],loc;
beg=lower;
end=n-1;
top++;
lower1[top]=beg;
upper1[top]=end;
while(top>=0)
{
beg=lower1[top];
end=upper1[top];
top--;
loc=split(a,beg,end);
if(beg<loc-1)
{
top++;
lower1[top]=beg;
upper1[top]=loc-1;
}
if(loc+1<end)
{
top++;
lower1[top]=loc+1;
upper1[top]=end;
}
}
}
int split ( int a[], int lower, int upper )
{
int i, p, q, t ;
p = lower + 1 ;
q = upper ;
i = a[lower] ;
while ( q >= p )
{
while ( a[p] < i )
p++ ;
while ( a[q] > i )
q-- ;
if ( q > p )
{
t = a[p] ;
a[p] = a[q] ;
a[q] = t ;
}
}
t = a[lower] ;
a[lower] = a[q] ;
a[q] = t ;
return q ;
}
/* Quick sort.
How meny numbers you want to enter :- 5
Please enter the numbers : - 12
56
5
64
48
Array before sorting:
12 56 5 64 48
Quick sort.
Array after sorting:
5 12 48 56 64
*/
#include <stdio.h>
void quick ( int *, int, int ) ;
int split ( int *, int, int ) ;
int main( )
{
int arr[100];
int i,a ;
printf("How meny numbers you want to enter :- ");
scanf("%d",&a);
printf("Please enter the numbers : - ");
for(i=0;i<a;i++)
scanf("%d",&arr[i]);
printf ( "\nArray before sorting:\n") ;
for ( i = 0 ; i < a ; i++ )
printf ( "%d\t", arr[i] ) ;
printf ( "\nQuick sort.\n" ) ;
quick( arr, 0, a ) ;
printf ( "\nArray after sorting:\n") ;
for ( i = 0 ; i < a ; i++ )
printf ( "%d\t", arr[i] ) ;
}
void quick(int a[],int lower,int upper)
{
int i,beg,end,top=-1,n=upper,lower1[10],upper1[10],loc;
beg=lower;
end=n-1;
top++;
lower1[top]=beg;
upper1[top]=end;
while(top>=0)
{
beg=lower1[top];
end=upper1[top];
top--;
loc=split(a,beg,end);
if(beg<loc-1)
{
top++;
lower1[top]=beg;
upper1[top]=loc-1;
}
if(loc+1<end)
{
top++;
lower1[top]=loc+1;
upper1[top]=end;
}
}
}
int split ( int a[], int lower, int upper )
{
int i, p, q, t ;
p = lower + 1 ;
q = upper ;
i = a[lower] ;
while ( q >= p )
{
while ( a[p] < i )
p++ ;
while ( a[q] > i )
q-- ;
if ( q > p )
{
t = a[p] ;
a[p] = a[q] ;
a[q] = t ;
}
}
t = a[lower] ;
a[lower] = a[q] ;
a[q] = t ;
return q ;
}
Quick Sort Another approach with 'goto' statement and without recursion
/*
enter the number of elements in the array
5
enter the element no.1:60
enter the element no.2:48
enter the element no.3:12
enter the element no.4:5
enter the element no.5:46
the sorted array is
5 12 46 48 60
*/
# include <stdio.h>
# include <conio.h>
int quick(int [],int,int);
int main(void)
{
int i,beg,end,top=-1,n,lower[10],upper[10],a[10],loc;
printf("enter the number of elements in the array\n");
scanf("%d",&n);
for(i=0;i<n;i++)
{
printf("enter the element no.%d:",i+1);
scanf("%d",&a[i]);
}
beg=0;
end=n-1;
top++;
lower[top]=beg;
upper[top]=end;
while(top>=0)
{
beg=lower[top];
end=upper[top];
top--;
loc=quick(a,beg,end);
if(beg<loc-1)
{
top++;
lower[top]=beg;
upper[top]=loc-1;
}
if(loc+1<end)
{
top++;
lower[top]=loc+1;
upper[top]=end;
}
}
printf("the sorted array is\n");
for(i=0;i<n;i++)
printf("%d\t",a[i]);
getch();
}
int quick(int a[10],int beg,int end)
{
int loc,temp;
loc=beg;
rscan: while((a[loc]<=a[end]) && (loc!=end))
end-=1;
if(loc==end)
return loc;
if(a[loc]>a[end])
{
temp=a[loc];
a[loc]=a[end];
a[end]=temp;
loc=end;
goto lscan;
}
lscan: while((a[beg]<=a[loc])&&(loc!=beg))
beg++;
if(loc==beg)
return loc;
if(a[beg]>a[loc])
{
temp=a[beg];
a[beg]=a[loc];
a[loc]=temp;
loc=beg;
goto rscan;
}
}
Write a program for the Quick sort purely recursion
/* Quick sort Purely recursion.
How meny numbers you want to enter :- 5
Please enter the numbers : - 12
56
5
64
48
Array before sorting:
12 56 5 64 48
Quick sort.
Array after sorting:
5 12 48 56 64
*/
#include <stdio.h>
void quicksort ( int *, int, int ) ;
int split ( int*, int, int ) ;
void main( )
{
int arr[100];
int i,a ;
clrscr() ;
printf("How meny numbers you want to enter :- ");
scanf("%d",&a);
printf("Please enter the numbers : - ");
for(i=0;i<a;i++)
scanf("%d",&arr[i]);
printf ( "\nArray before sorting:\n") ;
for ( i = 0 ; i < a ; i++ )
printf ( "%d\t", arr[i] ) ;
printf ( "\nQuick sort.\n" ) ;
quicksort ( arr, 0, a ) ;
printf ( "\nArray after sorting:\n") ;
for ( i = 0 ; i < a ; i++ )
printf ( "%d\t", arr[i] ) ;
getch( ) ;
}
void quicksort ( int a[ ], int lower, int upper )
{
int i ;
if ( upper > lower )
{
i = split ( a, lower, upper ) ;
quicksort ( a, lower, i - 1 ) ;
quicksort ( a, i + 1, upper ) ;
}
}
int split ( int a[ ], int lower, int upper )
{
int i, p, q, t ;
p = lower + 1 ;
q = upper ;
i = a[lower] ;
while ( q >= p )
{
while ( a[p] < i )
p++ ;
while ( a[q] > i )
q-- ;
if ( q > p )
{
t = a[p] ;
a[p] = a[q] ;
a[q] = t ;
}
}
t = a[lower] ;
a[lower] = a[q] ;
a[q] = t ;
return q ;
}
Sorting and Searching; String Sort
Write a program to sort strings using C
/*
Enter successive lines, pressing Enter at the end of each line.
Just press Enter to end.
this
is
a
c
program
to
sort
strings
Your input sorted in order is:
a
c
is
program
sort
strings
this
to
*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define TRUE 1
#define FALSE 0
#define BUFFER_LEN 240
#define NUM_P 50
int str_in(char **); /* Function prototype */
void str_sort(char *[], int); /* Function prototype */
void str_out(char *[], int); /* Function prototype */
/*execution starts here */
int main()
{
char *pS[NUM_P]; /* Array of string pointers */
int count = 0; /* Number of strings read */
printf("\nEnter successive lines, pressing Enter at the end of each line.\nJust press Enter to end.\n");
for(count = 0; count < NUM_P ; count++) /* Max of NUM_P strings */
if(!str_in(&pS[count]) ) /* Read a string */
break; /* Stop input on 0 return */
str_sort( pS, count ); /* Sort strings */
str_out( pS, count ); /* Output strings */
}
/*****************************************************
* String input routine *
* Argument is a pointer to a string pointer *
* which is char** *
* Returns FALSE for empty string or if memory not *
* obtained, and returns TRUE otherwise. *
*****************************************************/
int str_in(char **pString)
{
char buffer[BUFFER_LEN]; /* Space to store input string */
if(gets(buffer) == NULL ) /* NULL returned from gets()? */
{
printf("\nError reading string.\n");
return FALSE; /* Read error */
}
if(buffer[0] == '\0') /* Empty string read? */
return FALSE;
*pString = (char*)malloc(strlen(buffer) + 1);
if(*pString == NULL) /* Check memory allocation */
{
printf("\nOut of memory.");
return FALSE; /* No memory allocated */
}
strcpy(*pString, buffer); /* Copy string read to argument */
return TRUE;
}
/****************************************************
* String sort routine *
* First argument is array of pointers to strings *
* which is of type char**. *
* Second argument is the number of elements in the *
* pointer array ?i.e. the number of strings *
****************************************************/
void str_sort(char *p[], int n)
{
char *pTemp = NULL; /* Temporary pointer */
int i = 0; /* Loop counter */
int sorted = FALSE; /* Strings sorted indicator */
while(!sorted) /* Loop until there are no swaps */
{
sorted = TRUE; /* Initialize to indicate no swaps */
for( i = 0 ; i<n-1 ; i++ )
if(strcmp(p[i], p[i + 1]) > 0)
{
sorted = FALSE; /* indicate we are out of order */
pTemp= p[i]; /* Swap pointers p[i] */
p[i] = p[i + 1]; /* and */
p[i + 1] = pTemp; /* p[i + 1] */
}
}
}
/****************************************************
* String output routine *
* First argument is an array of pointers to *
* strings which is the same as char** *
* The second argument is a count of the number of *
* pointers in the array i.e. the number of strings *
****************************************************/
void str_out(char *p[] , int n)
{
int i = 0; /* Loop counter */
printf("\nYour input sorted in order is:\n\n");
for (i = 0 ; i<n ; i++)
{
printf("%s\n", p[i]); /* Display a string */
free(p[i]); /* Free memory for the string */
p[i] = NULL;
}
return;
}
Another way to sort Strings in C language
/*
Enter successive lines, pressing Enter at the end of each line.
Just press Enter to end.
this
is
a
program
written
in
c
to sort
strings
Your input sorted in order is:
a
c
in
is
program
strings
this
to sort
written
*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define BUFFER_LEN 100 /* Length of input buffer */
#define NUM_P 100 /* maximum number of strings */
#define TRUE 1
#define FALSE 0
void main()
{
char buffer[BUFFER_LEN]; /* space to store an input string */
char *pS[NUM_P] = { NULL }; /* Array of string pointers */
char *pTemp = NULL; /* Temporary pointer */
int i = 0; /* Loop counter */
int sorted = FALSE; /* Indicated when strings are sorted */
int last_string = 0; /* Index of last string entered */
printf("\nEnter successive lines, pressing Enter at the"
" end of each line.\nJust press Enter to end.\n\n");
while((*gets(buffer) != '\0') && (i < NUM_P))
{
pS[i] = (char*)malloc(strlen(buffer) + 1);
if(pS[i]==NULL) /* Check for no memory allocated */
{
printf(" Memory allocation failed. Program terminated.\n");
return;
}
strcpy(pS[i++], buffer);
}
last_string = i; /* Save last string index */
/* Sort the strings in ascending order */
while(!sorted)
{
sorted = TRUE;
for (i = 0 ; i < last_string - 1 ; i++)
if(strcmp(pS[i], pS[i + 1]) > 0)
{
sorted = FALSE; /* We were out of order */
pTemp= pS[i]; /* Swap pointers pS[i] */
pS[i] = pS[i + 1]; /* and */
pS[i + 1] = pTemp; /* pS[i + 1] */
}
}
/* Displayed the sorted strings */
printf("\nYour input sorted in order is:\n\n");
for (i = 0 ; i < last_string ; i++)
{
printf("%s\n", pS[i] );
free( pS[i] );
pS[i] = NULL;
}
}
/*
Enter successive lines, pressing Enter at the end of each line.
Just press Enter to end.
this
is
a
c
program
to
sort
strings
Your input sorted in order is:
a
c
is
program
sort
strings
this
to
*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define TRUE 1
#define FALSE 0
#define BUFFER_LEN 240
#define NUM_P 50
int str_in(char **); /* Function prototype */
void str_sort(char *[], int); /* Function prototype */
void str_out(char *[], int); /* Function prototype */
/*execution starts here */
int main()
{
char *pS[NUM_P]; /* Array of string pointers */
int count = 0; /* Number of strings read */
printf("\nEnter successive lines, pressing Enter at the end of each line.\nJust press Enter to end.\n");
for(count = 0; count < NUM_P ; count++) /* Max of NUM_P strings */
if(!str_in(&pS[count]) ) /* Read a string */
break; /* Stop input on 0 return */
str_sort( pS, count ); /* Sort strings */
str_out( pS, count ); /* Output strings */
}
/*****************************************************
* String input routine *
* Argument is a pointer to a string pointer *
* which is char** *
* Returns FALSE for empty string or if memory not *
* obtained, and returns TRUE otherwise. *
*****************************************************/
int str_in(char **pString)
{
char buffer[BUFFER_LEN]; /* Space to store input string */
if(gets(buffer) == NULL ) /* NULL returned from gets()? */
{
printf("\nError reading string.\n");
return FALSE; /* Read error */
}
if(buffer[0] == '\0') /* Empty string read? */
return FALSE;
*pString = (char*)malloc(strlen(buffer) + 1);
if(*pString == NULL) /* Check memory allocation */
{
printf("\nOut of memory.");
return FALSE; /* No memory allocated */
}
strcpy(*pString, buffer); /* Copy string read to argument */
return TRUE;
}
/****************************************************
* String sort routine *
* First argument is array of pointers to strings *
* which is of type char**. *
* Second argument is the number of elements in the *
* pointer array ?i.e. the number of strings *
****************************************************/
void str_sort(char *p[], int n)
{
char *pTemp = NULL; /* Temporary pointer */
int i = 0; /* Loop counter */
int sorted = FALSE; /* Strings sorted indicator */
while(!sorted) /* Loop until there are no swaps */
{
sorted = TRUE; /* Initialize to indicate no swaps */
for( i = 0 ; i<n-1 ; i++ )
if(strcmp(p[i], p[i + 1]) > 0)
{
sorted = FALSE; /* indicate we are out of order */
pTemp= p[i]; /* Swap pointers p[i] */
p[i] = p[i + 1]; /* and */
p[i + 1] = pTemp; /* p[i + 1] */
}
}
}
/****************************************************
* String output routine *
* First argument is an array of pointers to *
* strings which is the same as char** *
* The second argument is a count of the number of *
* pointers in the array i.e. the number of strings *
****************************************************/
void str_out(char *p[] , int n)
{
int i = 0; /* Loop counter */
printf("\nYour input sorted in order is:\n\n");
for (i = 0 ; i<n ; i++)
{
printf("%s\n", p[i]); /* Display a string */
free(p[i]); /* Free memory for the string */
p[i] = NULL;
}
return;
}
Another way to sort Strings in C language
/*
Enter successive lines, pressing Enter at the end of each line.
Just press Enter to end.
this
is
a
program
written
in
c
to sort
strings
Your input sorted in order is:
a
c
in
is
program
strings
this
to sort
written
*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define BUFFER_LEN 100 /* Length of input buffer */
#define NUM_P 100 /* maximum number of strings */
#define TRUE 1
#define FALSE 0
void main()
{
char buffer[BUFFER_LEN]; /* space to store an input string */
char *pS[NUM_P] = { NULL }; /* Array of string pointers */
char *pTemp = NULL; /* Temporary pointer */
int i = 0; /* Loop counter */
int sorted = FALSE; /* Indicated when strings are sorted */
int last_string = 0; /* Index of last string entered */
printf("\nEnter successive lines, pressing Enter at the"
" end of each line.\nJust press Enter to end.\n\n");
while((*gets(buffer) != '\0') && (i < NUM_P))
{
pS[i] = (char*)malloc(strlen(buffer) + 1);
if(pS[i]==NULL) /* Check for no memory allocated */
{
printf(" Memory allocation failed. Program terminated.\n");
return;
}
strcpy(pS[i++], buffer);
}
last_string = i; /* Save last string index */
/* Sort the strings in ascending order */
while(!sorted)
{
sorted = TRUE;
for (i = 0 ; i < last_string - 1 ; i++)
if(strcmp(pS[i], pS[i + 1]) > 0)
{
sorted = FALSE; /* We were out of order */
pTemp= pS[i]; /* Swap pointers pS[i] */
pS[i] = pS[i + 1]; /* and */
pS[i + 1] = pTemp; /* pS[i + 1] */
}
}
/* Displayed the sorted strings */
printf("\nYour input sorted in order is:\n\n");
for (i = 0 ; i < last_string ; i++)
{
printf("%s\n", pS[i] );
free( pS[i] );
pS[i] = NULL;
}
}
Friday, 11 March 2016
Expression Conversion, Number Conversion, like Any Base to Any Base, Infix to Post Fix etc.
Convert a Infix Expression to Its Prefix Form
/* Program to convert an Infix expression to Prefix form.
(A+B)*C
*+A B C
*/
#include <stdio.h>
#include <conio.h>
#include <string.h>
#include <ctype.h>
#define MAX 50
struct infix
{
char target[MAX] ;
char stack[MAX] ;
char *s, *t ;
int top, l ;
} ;
void initinfix ( struct infix * ) ;
void setexpr ( struct infix *, char * ) ;
void push ( struct infix *, char ) ;
char pop ( struct infix * ) ;
void convert ( struct infix * ) ;
int priority ( char c ) ;
void show ( struct infix ) ;
int main( )
{
struct infix q ;
char expr[MAX] ;
initinfix ( &q ) ;
printf ( "\nEnter an expression in infix form: " ) ;
gets ( expr ) ;
setexpr ( &q, expr ) ;
convert ( &q ) ;
printf ( "The Prefix expression is: " ) ;
show ( q ) ;
getch( ) ;
}
/* initializes elements of structure variable */
void initinfix ( struct infix *pq )
{
pq -> top = -1 ;
strcpy ( pq -> target, "" ) ;
strcpy ( pq -> stack, "" ) ;
pq -> l = 0 ;
}
/* reverses the given expression */
void setexpr ( struct infix *pq, char *str )
{
pq -> s = str ;
strrev ( pq -> s ) ;
pq -> l = strlen ( pq -> s ) ;
*( pq -> target + pq -> l ) = '\0' ;
pq -> t = pq -> target + ( pq -> l - 1 ) ;
}
/* adds operator to the stack */
void push ( struct infix *pq, char c )
{
if ( pq -> top == MAX - 1 )
printf ( "\nStack is full.\n" ) ;
else
{
pq -> top++ ;
pq -> stack[pq -> top] = c ;
}
}
/* pops an operator from the stack */
char pop ( struct infix *pq )
{
if ( pq -> top == -1 )
{
printf ( "Stack is empty\n" ) ;
return -1 ;
}
else
{
char item = pq -> stack[pq -> top] ;
pq -> top-- ;
return item ;
}
}
/* converts the infix expr. to prefix form */
void convert ( struct infix *pq )
{
char opr ;
while ( *( pq -> s ) )
{
if ( *( pq -> s ) == ' ' || *( pq -> s ) == '\t' )
{
pq -> s++ ;
continue ;
}
if ( isdigit ( *( pq -> s ) ) || isalpha ( *( pq -> s ) ) )
{
while ( isdigit ( *( pq -> s ) ) || isalpha ( *( pq -> s ) ) )
{
*( pq -> t ) = *( pq -> s ) ;
pq -> s++ ;
pq -> t-- ;
}
}
if ( *( pq -> s ) == ')' )
{
push ( pq, *( pq -> s ) ) ;
pq -> s++ ;
}
if ( *( pq -> s ) == '*' || *( pq -> s ) == '+' || *( pq -> s ) == '/' ||
*( pq -> s ) == '%' || *( pq -> s ) == '-' || *( pq -> s ) == '$' )
{
if ( pq -> top != -1 )
{
opr = pop ( pq ) ;
while ( priority ( opr ) > priority ( *( pq -> s ) ) )
{
*( pq -> t ) = opr ;
pq -> t-- ;
opr = pop ( pq ) ;
}
push ( pq, opr ) ;
push ( pq, *( pq -> s ) ) ;
}
else
push ( pq, *( pq -> s ) ) ;
pq -> s++ ;
}
if ( *( pq -> s ) == '(' )
{
opr = pop ( pq ) ;
while ( opr != ')' )
{
*( pq -> t ) = opr ;
pq -> t-- ;
opr = pop ( pq ) ;
}
pq -> s++ ;
}
}
while ( pq -> top != -1 )
{
opr = pop ( pq ) ;
*( pq -> t ) = opr ;
pq -> t-- ;
}
pq -> t++ ;
}
/* returns the priotity of the operator */
int priority ( char c )
{
if ( c == '$' )
return 3 ;
if ( c == '*' || c == '/' || c == '%' )
return 2 ;
else
{
if ( c == '+' || c == '-' )
return 1 ;
else
return 0 ;
}
}
/* displays the prefix form of given expr. */
void show ( struct infix pq )
{
while ( *( pq.t ) )
{
printf ( "%c", *( pq.t ) ) ;
pq.t++ ;
}
}
Program to Convert a Number from Decimal to Binary using recursion
/* Program to convert an Infix expression to Prefix form.
(A+B)*C
*+A B C
*/
#include <stdio.h>
#include <conio.h>
#include <string.h>
#include <ctype.h>
#define MAX 50
struct infix
{
char target[MAX] ;
char stack[MAX] ;
char *s, *t ;
int top, l ;
} ;
void initinfix ( struct infix * ) ;
void setexpr ( struct infix *, char * ) ;
void push ( struct infix *, char ) ;
char pop ( struct infix * ) ;
void convert ( struct infix * ) ;
int priority ( char c ) ;
void show ( struct infix ) ;
int main( )
{
struct infix q ;
char expr[MAX] ;
initinfix ( &q ) ;
printf ( "\nEnter an expression in infix form: " ) ;
gets ( expr ) ;
setexpr ( &q, expr ) ;
convert ( &q ) ;
printf ( "The Prefix expression is: " ) ;
show ( q ) ;
getch( ) ;
}
/* initializes elements of structure variable */
void initinfix ( struct infix *pq )
{
pq -> top = -1 ;
strcpy ( pq -> target, "" ) ;
strcpy ( pq -> stack, "" ) ;
pq -> l = 0 ;
}
/* reverses the given expression */
void setexpr ( struct infix *pq, char *str )
{
pq -> s = str ;
strrev ( pq -> s ) ;
pq -> l = strlen ( pq -> s ) ;
*( pq -> target + pq -> l ) = '\0' ;
pq -> t = pq -> target + ( pq -> l - 1 ) ;
}
/* adds operator to the stack */
void push ( struct infix *pq, char c )
{
if ( pq -> top == MAX - 1 )
printf ( "\nStack is full.\n" ) ;
else
{
pq -> top++ ;
pq -> stack[pq -> top] = c ;
}
}
/* pops an operator from the stack */
char pop ( struct infix *pq )
{
if ( pq -> top == -1 )
{
printf ( "Stack is empty\n" ) ;
return -1 ;
}
else
{
char item = pq -> stack[pq -> top] ;
pq -> top-- ;
return item ;
}
}
/* converts the infix expr. to prefix form */
void convert ( struct infix *pq )
{
char opr ;
while ( *( pq -> s ) )
{
if ( *( pq -> s ) == ' ' || *( pq -> s ) == '\t' )
{
pq -> s++ ;
continue ;
}
if ( isdigit ( *( pq -> s ) ) || isalpha ( *( pq -> s ) ) )
{
while ( isdigit ( *( pq -> s ) ) || isalpha ( *( pq -> s ) ) )
{
*( pq -> t ) = *( pq -> s ) ;
pq -> s++ ;
pq -> t-- ;
}
}
if ( *( pq -> s ) == ')' )
{
push ( pq, *( pq -> s ) ) ;
pq -> s++ ;
}
if ( *( pq -> s ) == '*' || *( pq -> s ) == '+' || *( pq -> s ) == '/' ||
*( pq -> s ) == '%' || *( pq -> s ) == '-' || *( pq -> s ) == '$' )
{
if ( pq -> top != -1 )
{
opr = pop ( pq ) ;
while ( priority ( opr ) > priority ( *( pq -> s ) ) )
{
*( pq -> t ) = opr ;
pq -> t-- ;
opr = pop ( pq ) ;
}
push ( pq, opr ) ;
push ( pq, *( pq -> s ) ) ;
}
else
push ( pq, *( pq -> s ) ) ;
pq -> s++ ;
}
if ( *( pq -> s ) == '(' )
{
opr = pop ( pq ) ;
while ( opr != ')' )
{
*( pq -> t ) = opr ;
pq -> t-- ;
opr = pop ( pq ) ;
}
pq -> s++ ;
}
}
while ( pq -> top != -1 )
{
opr = pop ( pq ) ;
*( pq -> t ) = opr ;
pq -> t-- ;
}
pq -> t++ ;
}
/* returns the priotity of the operator */
int priority ( char c )
{
if ( c == '$' )
return 3 ;
if ( c == '*' || c == '/' || c == '%' )
return 2 ;
else
{
if ( c == '+' || c == '-' )
return 1 ;
else
return 0 ;
}
}
/* displays the prefix form of given expr. */
void show ( struct infix pq )
{
while ( *( pq.t ) )
{
printf ( "%c", *( pq.t ) ) ;
pq.t++ ;
}
}
Convert an expression from Infix to Postfix
/* Program to convert an Infix form to Postfix form */
#include <stdio.h>
#include <conio.h>
#include <string.h>
#include <ctype.h>
#define MAX 50
struct infix
{
char target[MAX] ;
char stack[MAX] ;
char *s, *t ;
int top ;
} ;
void initinfix ( struct infix * ) ;
void setexpr ( struct infix *, char * ) ;
void push ( struct infix *, char ) ;
char pop ( struct infix * ) ;
void convert ( struct infix * ) ;
int priority ( char ) ;
void show ( struct infix ) ;
void main( )
{
struct infix p ;
char expr[MAX] ;
initinfix ( &p ) ;
printf ( "\nEnter an expression in infix form: " ) ;
gets ( expr ) ;
setexpr ( &p, expr ) ;
convert ( &p ) ;
printf ( "\nThe postfix expression is: " ) ;
show ( p ) ;
getch( ) ;
}
/* initializes structure elements */
void initinfix ( struct infix *p )
{
p -> top = -1 ;
strcpy ( p -> target, "" ) ;
strcpy ( p -> stack, "" ) ;
p -> t = p -> target ;
p -> s = "" ;
}
/* sets s to point to given expr. */
void setexpr ( struct infix *p, char *str )
{
p -> s = str ;
}
/* adds an operator to the stack */
void push ( struct infix *p, char c )
{
if ( p -> top == MAX )
printf ( "\nStack is full.\n" ) ;
else
{
p -> top++ ;
p -> stack[p -> top] = c ;
}
}
/* pops an operator from the stack */
char pop ( struct infix *p )
{
if ( p -> top == -1 )
{
printf ( "\nStack is empty.\n" ) ;
return -1 ;
}
else
{
char item = p -> stack[p -> top] ;
p -> top-- ;
return item ;
}
}
/* converts the given expr. from infix to postfix form */
void convert ( struct infix *p )
{
char opr ;
while ( *( p -> s ) )
{
if ( *( p -> s ) == ' ' || *( p -> s ) == '\t' )
{
p -> s++ ;
continue ;
}
if ( isdigit ( *( p -> s ) ) || isalpha ( *( p -> s ) ) )
{
while ( isdigit ( *( p -> s ) ) || isalpha ( *( p -> s ) ) )
{
*( p -> t ) = *( p -> s ) ;
p -> s++ ;
p -> t++ ;
}
}
if ( *( p -> s ) == '(' )
{
push ( p, *( p -> s ) ) ;
p -> s++ ;
}
if ( *( p -> s ) == '*' || *( p -> s ) == '+' || *( p -> s ) == '/' || *( p -> s ) == '%' || *( p -> s ) == '-' || *( p -> s ) == '$' )
{
if ( p -> top != -1 )
{
opr = pop ( p ) ;
while ( priority ( opr ) >= priority ( *( p -> s ) ) )
{
*( p -> t ) = opr ;
p -> t++ ;
opr = pop ( p ) ;
}
push ( p, opr ) ;
push ( p, *( p -> s ) ) ;
}
else
push ( p, *( p -> s ) ) ;
p -> s++ ;
}
if ( *( p -> s ) == ')' )
{
opr = pop ( p ) ;
while ( ( opr ) != '(' )
{
*( p -> t ) = opr ;
p -> t++ ;
opr = pop ( p ) ;
}
p -> s++ ;
}
}
while ( p -> top != -1 )
{
char opr = pop ( p ) ;
*( p -> t ) = opr ;
p -> t++ ;
}
*( p -> t ) = '\0' ;
}
/* returns the priority of an operator */
int priority ( char c )
{
if ( c == '$' )
return 3 ;
if ( c == '*' || c == '/' || c == '%' )
return 2 ;
else
{
if ( c == '+' || c == '-' )
return 1 ;
else
return 0 ;
}
}
/* displays the postfix form of given expr. */
void show ( struct infix p )
{
printf ( " %s", p.target ) ;
}
Program to evaluate an expression entered in postfix form using C
#include <stdio.h>
#include <conio.h>
#include <stdlib.h>
#include <math.h>
#include <ctype.h>
#define MAX 50
struct postfix
{
int stack[MAX] ;
int top, nn ;
char *s ;
} ;
void initpostfix ( struct postfix * ) ;
void setexpr ( struct postfix *, char * ) ;
void push ( struct postfix *, int ) ;
int pop ( struct postfix * ) ;
void calculate ( struct postfix * ) ;
void show ( struct postfix ) ;
void main( )
{
struct postfix q ;
char expr[MAX] ;
clrscr( ) ;
initpostfix ( &q ) ;
printf ( "\nEnter postfix expression to be evaluated: " ) ;
gets ( expr ) ;
setexpr ( &q, expr ) ;
calculate ( &q ) ;
show ( q ) ;
getch( ) ;
}
/* initializes data members */
void initpostfix ( struct postfix *p )
{
p -> top = -1 ;
}
/* sets s to point to the given expr. */
void setexpr ( struct postfix *p, char *str )
{
p -> s = str ;
}
/* adds digit to the stack */
void push ( struct postfix *p, int item )
{
if ( p -> top == MAX - 1 )
printf ( "\nStack is full." ) ;
else
{
p -> top++ ;
p -> stack[p -> top] = item ;
}
}
/* pops digit from the stack */
int pop ( struct postfix *p )
{
int data ;
if ( p -> top == -1 )
{
printf ( "\nStack is empty." ) ;
return NULL ;
}
data = p -> stack[p -> top] ;
p -> top-- ;
return data ;
}
/* evaluates the postfix expression */
void calculate( struct postfix *p )
{
int n1, n2, n3 ;
while ( *( p -> s ) )
{
/* skip whitespace, if any */
if ( *( p -> s ) == ' ' || *( p -> s ) == '\t' )
{
p -> s++ ;
continue ;
}
/* if digit is encountered */
if ( isdigit ( *( p -> s ) ) )
{
p -> nn = *( p -> s ) - '0' ;
push ( p, p -> nn ) ;
}
else
{
/* if operator is encountered */
n1 = pop ( p ) ;
n2 = pop ( p ) ;
switch ( *( p -> s ) )
{
case '+' :
n3 = n2 + n1 ;
break ;
case '-' :
n3 = n2 - n1 ;
break ;
case '/' :
n3 = n2 / n1 ;
break ;
case '*' :
n3 = n2 * n1 ;
break ;
case '%' :
n3 = n2 % n1 ;
break ;
case '$' :
n3 = pow ( n2 , n1 ) ;
break ;
default :
printf ( "Unknown operator" ) ;
exit ( 1 ) ;
}
push ( p, n3 ) ;
}
p -> s++ ;
}
}
/* displays the result */
void show ( struct postfix p )
{
p.nn = pop ( &p ) ;
printf ( "\nResult is: %d", p.nn ) ;
}
Program to Convert a Number from Decimal to Any Base
/*
Enter the number in decimal :
50
enter the base to convert
2
the converted number is
110010
*/
#include <stdio.h>
#include <math.h>
struct st{
int num;
char cho;
};
struct st stack[10];
int main(void){
int top,m,n,b1,b2;
printf("\nEnter the number in decimal :");
scanf("%d",&n);
printf("\nEnter the base to convert : ");
scanf("%d",&b2);
b1=10;
if(b1==10)
{
top=-1;
while(n>0)
{
top+=1;
m=n%b2;
if(m==10)
stack[top].cho='A';
if(m==11)
stack[top].cho='B';
if(m==12)
stack[top].cho='C';
if(m==13)
stack[top].cho='D';
if(m==14)
stack[top].cho='E';
if(m==15)
stack[top].cho='F';
stack[top].num=n%b2;
n=n/b2;
}
printf("\nthe converted number is\n");
printf("\n");
while(top>=0)
{
if(stack[top].num>=10)
printf("%c",stack[top].cho);
else
printf("%d",stack[top].num);
top-=1;
}
}
}
Program to Convert a Number from Decimal to Binary using loop or using Array
/*
Enter the number : 20
The Binary Equivalent is : 10100
*/
#include<stdio.h>
#include<conio.h>
int a[10],c,n,i,j,ans;
int main(void)
{
ans=1;
while(ans==1)
{
c=0;
i=0;
printf("\nEnter the number : ");
scanf("%d",&n);
if(n==0)
printf("\nThe Binary Equivalent is :0");
else
{
while(n>0)
{
a[i]=n%2;
i=i+1;
c=c+1;
n=n/2;
}
printf("\nThe Binary Equivalent is : ");
for(i=c-1;i>=0;i--)
{
printf("%d",a[i]);
}
printf("\n\nPress 1 to continue:");
scanf("%d",&ans);
}
}
getch();
}
Program to Convert a Number from Decimal to Binary using recursion
#include<stdio.h>
void bin(int);
int num1[10],i=0;
int main()
{
int num,j;
printf("Enter the number : - ");
scanf("%d",&num);
printf("The equivalent binary number is : - ");
bin(num);
for(j=i-1;j>=0;j--)
printf("%d",num1[j]);
}
void bin(int dec)
{
if(dec<=0)
return;
else
{ num1[i]=dec%2;
dec=dec/2;
i++;
bin(dec);
}
}
Program to find any base to any base
#include<stdio.h>
#include<conio.h>
#include<string.h>
#include<ctype.h>
void bd(void);
void db(void);
void doc(void);
void dh(long);
void od(void);
void ob(void);
void bo(void);
void bh(void);
void hb(void);
void hd(void);
void oh(long);
void ho(void);
int main()
{
int n;
char c;
begin:
printf(" ****MAIN MENU****\n");
printf("1. Binary to Decimal.\n");
printf("2. Decimal to Binary.\n");
printf("3. Decimal to Octal.\n");
printf("4. Decimal to Hexadecimal.\n");
printf("5. Octal to Decimal.\n");
printf("6. Octal to Binary.\n");
printf("7. Binary to Octal.\n");
printf("8. Binary to Hexadecimal.\n");
printf("9. Hexadecimal to Binary.\n");
printf("10. Hexadecimal to Decimal.\n");
printf("11. Octal to Hexadecimal.\n");
printf("12. Hexadecimal to Octal.\n\n");
printf("Enter your choise.(1-12) :- ");
scanf("%d",&n);
if(n<1 || n>12)
printf("Invalid Choise");
if(n==1)
bd();
else if(n==2)
db();
else if(n==3)
doc();
else if(n==4)
{
long a;
clrscr();
printf("Conversion from Decimal to Hexadecimal");
printf("Enter the decimal number.");
scanf("%ld",&a);
dh(a);
}
else if(n==5)
od();
else if(n==6)
ob();
else if(n==7)
bo();
else if(n==8)
bh();
else if(n==9)
hb();
else if(n==10)
hd();
else if(n==11)
{
unsigned long n,i=0,a,p=1,t=0;
clrscr();
printf("Conversion from Octal to Hexadecimal.");
printf("Enter a Octal number");
scanf("%ld",&n);
i=0;
while(n!=0)
{
a=n%10;
if(a>7)
t=1;
n=n/10;
i=i+a*p;
p=p*8;
}
if(t==0)
{
printf("Hexadecimal eq=");
oh(i);
}
else if(t==1)
printf("Numbert entered is not octal.");
}
else if(n==12)
ho();
printf("Do you Wish to continue(Y/N)");
scanf("%s",&c);
c=toupper(c);
if(c=='Y')
goto begin;
getch();
}
void bd()
{
int n,b=0,a[6],i=0,t=0;
clrscr();
printf("Conversion from Binary to Decimal");
printf("Enter Binary Number");
scanf("%d",&n);
while(n!=0)
{
a[i]=n%10;
n=n/10;
if(a[i]!=1 && a[i]!=0)
t=1;
i++;
}
a[i]=2;
n=1;
for(i=0;a[i]!=2;i++)
{
b=b+a[i]*n;
n=n*2;
}
if(t==0)
printf("Decimal Equivalent=%d",b);
else if(t==1)
printf("Entered number is not binary.");
}
void db()
{
int dec,bin,n,i=0,a[10];
clrscr();
printf("Conversion from Decimal to Binary");
printf("Input decimal no.");
scanf("%d",&dec);
do
{
a[i]=dec%2;
dec=dec/2;
i++;
}while(dec!=0);
for(n=i-1;n>=0;n--)
printf("%d",a[n]);
}
void doc()
{
int n,i,a[10];
clrscr();
printf("Conversion from Decimal to Octal");
printf("Enter a Decimal number");
scanf("%d",&n);
i=0;
printf("Octal equavalent of %d is ",n);
while(n!=0)
{
a[i]=n%8;
n=n/8;
i++;
}
i--;
for(;i>=0;i--)
printf("%d",a[i]);
}
void dh(long n)
{
long i;
if(n>0)
{
i=n%16;
n=n/16;
dh(n);
if(i>=10)
{
switch(i)
{
case 10:
printf("A");
break;
case 11:
printf("B");
break;
case 12:
printf("C");
break;
case 13:
printf("D");
break;
case 14:
printf("E");
break;
case 15:
printf("F");
break;
}
}
else
printf("%ld",i);
}
}
void od()
{
unsigned long n,i=0,a,p=1,t=0;
clrscr();
printf("Conversion from Octal to Decimal");
printf("Enter a Octal number");
scanf("%ld",&n);
i=0;
printf("Decimal equavalent of %ld",n);
while(n!=0)
{
a=n%10;
if(a>7)
t=1;
n=n/10;
i=i+a*p;
p=p*8;
}
if(t==0)
printf("= %ld",i);
else if(t==1)
printf(" can't be calculated because it is not an Octal Number.");
}
void ob()
{
int n,a[6],i=0,t=0;
clrscr();
printf("Convertion from Octal to Binary.");
printf("Enter an Octal Number.");
scanf("%d",&n);
while(n!=0)
{
a[i]=n%10;
n=n/10;
if(a[i]>7)
t=1;
i++;
}
i--;
if(t==0)
for(;i>=0;i--)
{
switch(a[i])
{
case 0:
printf("000");
break;
case 1:
printf("001");
break;
case 2:
printf("010");
break;
case 3:
printf("011");
break;
case 4:
printf("100");
break;
case 5:
printf("101");
break;
case 6:
printf("110");
break;
case 7:
printf("111");
break;
}
}
if(t==1)
printf("Not a Octal number");
}
void bo()
{
int i=0,a[5],t=0;
long int n;
clrscr();
printf("Convertion From Binary to Octal");
printf("Enter a Binary number");
scanf("%ld",&n);
while(n!=0)
{
a[i]=n%1000;
n=n/1000;
if(a[i]>111)
t=1;
i++;
}
i--;
if(t==0)
for(;i>=0;i--)
{
switch(a[i])
{
case 0:
printf("0");
break;
case 1:
printf("1");
break;
case 10:
printf("2");
break;
case 11:
printf("3");
break;
case 100:
printf("4");
break;
case 101:
printf("5");
break;
case 110:
printf("6");
break;
case 111:
printf("7");
break;
default:
printf("Entered number is not binary.Printed value is not correct.");
break;
}
}
if(t==1)
printf("Number is not Binary");
}
void bh()
{
int i=0,a[5],t=0;
long int n;
clrscr();
printf("Convertion from Binary to Hexadecimal");
printf("Enter a Binary number");
scanf("%ld",&n);
while(n!=0)
{
a[i]=n%10000;
n=n/10000;
if(a[i]>1111)
t=1;
i++;
}
i--;
if(t==0)
for(;i>=0;i--)
{
switch(a[i])
{
case 0:
printf("0");
break;
case 1:
printf("1");
break;
case 10:
printf("2");
break;
case 11:
printf("3");
break;
case 100:
printf("4");
break;
case 101:
printf("5");
break;
case 110:
printf("6");
break;
case 111:
printf("7");
break;
case 1000:
printf("8");
break;
case 1001:
printf("9");
break;
case 1010:
printf("A");
break;
case 1011:
printf("B");
break;
case 1100:
printf("C");
break;
case 1101:
printf("D");
break;
case 1110:
printf("E");
break;
case 1111:
printf("F");
break;
default:
printf("Entered number is not binary.Printed value is notcorrect.");
break;
}
}
if(t==1)
printf("Number is not Binary");
}
void hb()
{
int i;
char s[20];
clrscr();
printf("Convertion from Hexadecimal to Binary");
printf("Enter a Hexadecimal number");
scanf("%s",s);
//gets(s);
printf("Binary Equivalent=");
for(i=0;s[i]!=NULL;i++)
{
switch(s[i])
{
case '0':
printf("0000");
break;
case '1':
printf("0001");
break;
case '2':
printf("0010");
break;
case '3':
printf("0011");
break;
case '4':
printf("0100");
break;
case '5':
printf("0101");
break;
case '6':
printf("0110");
break;
case '7':
printf("0111");
break;
case '8':
printf("1000");
break;
case '9':
printf("1001");
break;
case 'a':
case 'A':
printf("1010");
break;
case 'b':
case 'B':
printf("1011");
break;
case 'c':
case 'C':
printf("1100");
break;
case 'd':
case 'D':
printf("1101");
break;
case 'e':
case 'E':
printf("1110");
break;
case 'f':
case 'F':
printf("1111");
break;
default:
printf("Entered number is not Hexadecimal.Printed value is notcorrect.");
break;
}
}
}
void hd()
{
int i,a[20];
unsigned long int h=0,m=1;
char s[20];
clrscr();
printf("Convertion from Hexadecimal to Decimal");
printf("Enter a Hexadecimal number");
scanf("%s",s);
printf("Decimal Equivalent=");
for(i=0;s[i]!=NULL;i++)
{
switch(s[i])
{
case '0':
a[i]=0;
break;
case '1':
a[i]=1;
break;
case '2':
a[i]=2;
break;
case '3':
a[i]=3;
break;
case '4':
a[i]=4;
break;
case '5':
a[i]=5;
break;
case '6':
a[i]=6;
break;
case '7':
a[i]=7;
break;
case '8':
a[i]=8;
break;
case '9':
a[i]=9;
break;
case 'a':
case 'A':
a[i]=10;
break;
case 'b':
case 'B':
a[i]=11;
break;
case 'c':
case 'C':
a[i]=12;
break;
case 'd':
case 'D':
a[i]=13;
break;
case 'e':
case 'E':
a[i]=14;
break;
case 'f':
case 'F':
a[i]=15;
break;
default:
printf("Entered number is not Hexadecimal.Printed value is notcorrect.");
break;
}
}
i--;
for(;i>=0;i--)
{
h=h+a[i]*m;
m=m*16;
}
printf("%ld ",h);
}
void oh(long n)
{
long i;
if(n>0)
{
i=n%16;
n=n/16;
oh(n);
if(i>=10)
{
switch(i)
{
case 10:
printf("A");
break;
case 11:
printf("B");
break;
case 12:
printf("C");
break;
case 13:
printf("D");
break;
case 14:
printf("E");
break;
case 15:
printf("F");
break;
}
}
else
printf("%ld",i);
}
}
void ho()
{
int i,a[20];
unsigned long int h=0,m=1;
char s[20];
clrscr();
printf("Convertion from Hexadecimal to Octal");
printf("Enter a Hexadecimal number");
scanf("%s",s);
// Converting hex to dec first
for(i=0;s[i]!=NULL;i++)
{
switch(s[i])
{
case '0':
a[i]=0;
break;
case '1':
a[i]=1;
break;
case '2':
a[i]=2;
break;
case '3':
a[i]=3;
break;
case '4':
a[i]=4;
break;
case '5':
a[i]=5;
break;
case '6':
a[i]=6;
break;
case '7':
a[i]=7;
break;
case '8':
a[i]=8;
break;
case '9':
a[i]=9;
break;
case 'a':
case 'A':
a[i]=10;
break;
case 'b':
case 'B':
a[i]=11;
break;
case 'c':
case 'C':
a[i]=12;
break;
case 'd':
case 'D':
a[i]=13;
break;
case 'e':
case 'E':
a[i]=14;
break;
case 'f':
case 'F':
a[i]=15;
break;
default:
printf("Entered number is not Hexadecimal.Printed value is not correct.");
break;
}
}
i--;
for(;i>=0;i--)
{
h=h+a[i]*m;
m=m*16;
}
// Now convering from decimal to octal (h)
i=0;
printf("Octal equavalent=");
while(h!=0)
{
a[i]=h%8;
h=h/8;
i++;
}
i--;
for(;i>=0;i--)
printf("%d",a[i]);
}
Subscribe to:
Posts (Atom)