The BOTTOM LINE Quote Of The Day

The BOTTOM LINE Quote Of The Day

Don't Ever Tell GOD How BIG Your Problems are.
Just Tell Your Problems How BIG your GOD is ;)

Monday, January 31, 2011

Infix-Postfix Coversion


#include<stdio.h>
#include<conio.h>
#include<string.h>
#define max 50

char exprsn[max];
char pstfx[max];
char stack[max];
int set_prcdnce(char ch)
{
 switch(ch)
 {
 case '^' : return 5;
 case '*' : return 4;
 case '/' : return 4;
 case '+' : return 3;
 case '-' : return 3;
 default  : return 0;
 }
}

void main()
{
clrscr();
int flag,i,top=0,tb=0,j=0;
int cpd,ppd;
 do
 {
  flag = 0;
  printf("\n Enter the expression : ");
  gets(exprsn);
  if(strlen(exprsn) > max )
  {
   printf("\n\n WARNING : Exceeding the limits !! \n\n");
   flag = 1;
  }
  for(i=0;exprsn[i]!='\0';i++)
   {
    if(exprsn[i]=='(')
     tb++;
    if(exprsn[i]==')')
     tb--;
   }
  printf("\n\ntb = %d\n",tb);
  if(tb != 0)
  {
   printf("\n\n WARNING : Unequal Right n Left Brackets in the expression !! \n\n");
   flag = 1;
  }
 }while(flag == 1);
printf("\n\n The expression in normal form : ");
puts(exprsn);
int x = strlen(exprsn);
exprsn[x] = ')';
exprsn[x+1] = '\0';
//printf("\n%c\n",exprsn[x]);
x+=1;
stack[top] = '(';
for(i=0;i<=x;i++)
   {
    switch(exprsn[i])
    {
    case '(' :  top += 1;
stack[top] = exprsn[i];
break;
    case ')' :  while(stack[top] != '(')
{
pstfx[j] = stack[top];
top -= 1; j++;
}
top -= 1;
break;
    case '^' :
    case '/' :
    case '*' :
    case '+' :
    case '-' :  cpd = set_prcdnce(exprsn[i]);
ppd = set_prcdnce(stack[top]);
if(cpd < ppd)
{
while(cpd<=ppd)
{
if(top == -1) break;
pstfx[j] = stack[top];
top -= 1; j++;
ppd = set_prcdnce(stack[top]);
}
}
top += 1; stack[top] = exprsn[i];
break;
    default :   pstfx[j] = exprsn[i]; j++;
break;
    }
   }
pstfx[j] = '\0';
printf("\n\n The expression in postfix form : ");
puts(pstfx);
getch();
}

Wednesday, January 26, 2011

Infix-Postfix Conversion and Expression Evalution



#include<stdio.h>
#include<conio.h>
#include<string.h>
#include<math.h>
#include<ctype.h>
#define max 50

char infx[max];
char pstfx[max];
char stack[max];
int nstore[max];
int nstack[max];

int top;
int cnt = 0;

int set_prcdnce(char ch)
{
 switch(ch)
 {
 case '^' : return 3;
 case '*' : return 2;
 case '/' : return 2;
 case '+' : return 1;
 case '-' : return 1;
 default  : return 0;
 }
}

int evaluate(char opt)
{
 int a,b;
 b = nstack[top]; top-=1;
 a = nstack[top];
 switch(opt)
 {
 case '^' : return pow(a,b);
 case '*' : return a*b;
 case '/' : return a/b;
 case '+' : return a+b;
 case '-' : return a-b;
 }
}

void stck_stat(char ch,int cho)
{
 int i;
 if(isalpha(ch) && cho == 2)
  {
  printf("  %d \t",nstore[cnt]);  cnt++;
  }
 else
  printf("  %c \t",ch);
 for(i=0;i<=top;i++)
  {
  if(cho==1)
   printf(" %c",stack[i]);
  else
   printf(" %d",nstack[i]);
  }
}

void main()
{
clrscr();
int flag,i,tb=0,j=0;
int cpd,ppd;
 do
 {
  flag = 0;
  printf("\n Enter the infx expression : ");
  gets(infx);
  if(strlen(infx) > max )
  {
   printf("\n\n WARNING : Exceeding the limits !! \n\n");
   flag = 1;
  }
  for(i=0;infx[i]!='\0';i++)
   {
    if(infx[i]=='(')
     tb++;
    if(infx[i]==')')
     tb--;
   }
  if(tb != 0)
  {
   printf("\n\n WARNING : Unequal Right n Left Brackets in the expression !! \n\n");
   flag = 1;
  }
 }while(flag == 1);
printf("\n\n The expression in its infix form : ");
puts(infx);
int x = strlen(infx);
infx[x] = ')';   infx[x+1] = '\0';
x+=1;
top = 0;
stack[top] = '(';
printf("\n\n     Element\t  Stack Status\t  Postfix Expression");
printf("\n     -------- \t ---------------  -------------------- \n\n");
for(i=0;i<strlen(infx);i++)
   {
    switch(infx[i])
    {
    case '(' :  top += 1;
stack[top] = infx[i];
break;
    case ')' :  while(stack[top] != '(')
{
pstfx[j] = stack[top];
top -= 1; j++;
}
top -= 1;
break;
    case '^' :
    case '/' :
    case '*' :
    case '+' :
    case '-' :  cpd = set_prcdnce(infx[i]);
ppd = set_prcdnce(stack[top]);
if(cpd < ppd)
{
while(cpd<=ppd)
{
if(top == -1) break;
pstfx[j] = stack[top];
top -= 1; j++;
ppd = set_prcdnce(stack[top]);
}
}
top += 1; stack[top] = infx[i];
break;
    default :   pstfx[j] = infx[i]; j++;
break;
    }
   printf("  %-2d. ",i+1);
   stck_stat(infx[i],1);
   gotoxy(40,11+i);
   puts(pstfx);
   }
pstfx[j] = '\0';
printf("\n\n The expression in postfix form : ");
puts(pstfx);
j=0;
for(i=0;i<strlen(pstfx);i++)
 {
  if(isalpha(pstfx[i]))
   {
     printf("\n Enter value for %c :",pstfx[i]);
     scanf("%d",&nstore[j]);  j++;
   }
 }

j=0;
printf("\n\n Now, The postfix expression becomes as : ");
for(i=0;i<strlen(pstfx);i++)
 {
  if(isalpha(pstfx[i]))
   {
     printf("%d,",nstore[j]);  j++;
   }
  else
   printf("%c,",pstfx[i]);
 }
printf("\n\n Evaluating the stack as follows : ");
printf("\n\n Element Check \t Stack Status");
printf("\n ----------- \t ------------ \n");
j = 0;   top = -1;
for(i=0;i<strlen(pstfx);i++)
 {
 printf("\n %-2d ",i+1);
 switch(pstfx[i])
    {
    case '^' :
    case '/' :
    case '*' :
    case '+' :
    case '-' :  nstack[top] = evaluate(pstfx[i]);
break;
    default :   top +=1; nstack[top] = nstore[j]; j+=1;
break;
    }
 stck_stat(pstfx[i],2);
 }
printf("\n\n After, Evaluating the expression the result is : %d",nstack[top]);
getch();
}

Saturday, January 15, 2011

Quadratic Equation

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


 void main()
   {
     clrscr();
       int a,b,c,d;
       float det,x,y;
       printf("\n\n For the following quadratic eqation ,\n\n\t\t a*x*x + b*x + c = 0 \n\n Enter the variables a , b, and c : ");
       scanf("%d%d%d",&a,&b,&c);
       printf("\n\n & Now, the quadratic eqation is : \n\n\t\t %d*x*x + %d*x + %d = 0 ",a,b,c);
       d = b*b - 4*a*c;
       if(d==0)
 {
    x= (float) -1*b/2*a;
    printf("\n\n The roots of the abv. equations are real and equal .");
    printf("\n\n The root is : %f",x );
 }
else if(d>0)
 {
    det = sqrt(d);
    x= (float) (-1*b-det)/2*a;
    y= (float) (-1*b+det)/2*a;
    printf("\n\n The roots of the abv. equations are real and distinct .");
    printf("\n\n The root are : %f and %f",x,y);
 }
else
 {
    det=sqrt(-1*d);
    x= (float) (-1*b)/2*a;
    printf("\n\n The roots of the abv. equations are imaginary .");
    printf("\n\n The root are : %f + %fi and %f - %fi",x,det,x,det);
 }




       getch();
   }

Wednesday, January 12, 2011

Pyramid Of Number - 2

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


 void main()
   {
       int num,i,j,k,l,x=1;
       X: clrscr();
       printf("\n\n Enter the number of level u wanna print in this pyramind (Max. 33):");
       scanf("%d",&num);
if(num>33)
{ goto X; }
       printf("\n\n The Following Pyramind is : \n\n");
for(i=1;i<=num;i++)
   {
    k = num-i;
    for(j=1;j<=k;j++)
{ printf(" "); }
    for(l=1;l<=i;l++)
{
printf("%d ",x%2);
x++;
}
    for(j=1;j<=k+2;j++)
{ printf(" "); }
    printf(" LEVEL:%d \n",k+1);
   }
     getch();
   }

Tuesday, January 11, 2011

Quicksort

#include<stdio.h>
#include<conio.h>
#define max 50
struct ele
{
 int info;
}ar[max],stup[max],stlw[max],tmp;


int noe,i,beg,end,top,loc,left,right;


void inp_dat()
{
 do
 {
 printf("\n\n Enter the no. of elements : ");
 scanf("%d",&noe);
 }while(noe<=0 || noe>=max);
 printf("\n\n Enter the '%d' elements of array in unsorted order : ",noe);
 for(i=0;i<noe;i++)
  scanf("%d",&ar[i].info);
 printf("\n\n The '%d' elements of array in unsorted order :",noe);
 for(i=0;i<noe;i++)
  printf(" %d",ar[i].info);
}


void quick(int &beg,int &end,int &loc)
{
 left = beg;
 right = end;
 loc = beg;
 while(loc != right || loc !=left)
 {
  while(ar[loc].info <= ar[right].info && right != loc)
    right -= 1;
  if(ar[loc].info > ar[right].info)
  {
   tmp = ar[loc];
   ar[loc] = ar[right];
   ar[right] = tmp;
   loc = right;
  }
  while(ar[loc].info >= ar[left].info && left != loc)
    left += 1;
  if(ar[loc].info < ar[left].info)
  {
   tmp = ar[loc];
   ar[loc] = ar[left];
   ar[left] = tmp;
   loc = left;
  }
 }
}


void qk_srt()
{
top = 0;
stlw[top].info = 0;   stup[top].info = noe-1;
while(top != -1)
{
 beg = stlw[top].info;
 end = stup[top].info;
 top = top-1;
 quick(beg,end,loc);
 if(beg < loc-1)
 {
 top = top+1;
 stlw[top].info = beg;   stup[top].info = loc-1;
 }
 if(loc+1 < end)
 {
 top = top+1;
 stlw[top].info = loc+1;   stup[top].info = end;
 }
}
 printf("\n\n The '%d' elements of array in sorted order :",noe);
 for(i=0;i<noe;i++)
  printf(" %d",ar[i].info);
}


void main()
{
clrscr();
inp_dat();
qk_srt();
getch();
}

Monday, January 10, 2011

Multiplication of Matrices

#include<iostream.h>
#include<conio.h>
#define max 10


 void in_mat(int[max][max],int,int);
 void disp_mat(int[max][max],int,int);
 void mul_mat(int[max][max],int[max][max],int[max][max],int,int,int);


 void main()
  {
   int x[max][max],y[max][max],z[max][max],a,b,c,d;
   clrscr();
   do
   {
   cout<<"\n\n Enter the numbers of rows n columns of matrix #1 : ";
   cin>>a>>b;
   cout<<"\n\n Enter the numbers of rows n columns of matrix #2 : ";
   cin>>c>>d;
   if ( b != c )
    cout<<"\n\n Matrices can't be multiplied .. !! ";
   }while( b != c );
   cout<<"\n\n Now, The Matrices can be multiplied .. !! ";
   getch();


   cout<<"\n\n\n\n Enter the elements for the following matrices \n";
   cout<<"\n\n Enter for the Matrix #1 \n";
   in_mat(x,a,b);
   cout<<"\n\n Enter for the Matrix #2 \n";
   in_mat(y,c,d);
   clrscr();
   cout<<"\n\n The following matrices are : ";
   cout<<"\n\n\t\t Matrix #1 :- ";
   disp_mat(x,a,b);
   cout<<"\n\n\t\t Matrix #2 :- ";
   disp_mat(y,c,d);
   mul_mat(x,y,z,a,d,b);
   cout<<"\n\n\t The Product of the above two matrices are \n";
   disp_mat(z,a,d);
   getch();
  }


 void in_mat(int mat[max][max],int row,int col)
  {
   int i,j;
   for(i=0;i<row;i++)
    {
     cout<<"\n Enter its elements for Row #"<<i+1<<" : ";
     for(j=0;j<col;j++)
       cin>>mat[i][j];
    }
  }


 void disp_mat(int mat[max][max],int row,int col)
  {
   int i,j;
   for(i=0;i<row;i++)
    {
     cout<<"\n\t Row #"<<i+1<<" :";
     for(j=0;j<col;j++)
      cout<<"  "<<mat[i][j];
    }
  }


 void mul_mat(int mat1[max][max],int mat2[max][max],int mat3[max][max],int r1,int c2,int r2c1)
  {
   int i,j,k;
   for(i=0;i<r1;i++)
    {
     for(j=0;j<c2;j++)
      {
       mat3[i][j] = 0;
       for(k=0;k<r2c1;k++)
mat3[i][j] += mat1[i][k] * mat2[k][j];
      }
    }
  }

Saturday, January 8, 2011

Nasty Number

#include<stdio.h>
#include<conio.h>
#define SIZE 100


 struct nmbr
  {
    int fp[2];
    int fpas[2];
  };
 void main()
  {
    struct nmbr n[SIZE],*n1,*n2;
    clrscr();
    int flag = 0,i,j,num,x=0;
      printf("\n Enter any number : ");
      scanf("%d",&num);
      for(i=1;i<=num/2;i++)
       {
if(num%i==0)
 {
   n[x].fp[0] = num/i ;
   n[x].fp[1] = i ;
   n[x].fpas[0] = n[x].fp[0] + n[x].fp[1] ;
   n[x].fpas[1] = n[x].fp[0] - n[x].fp[1] ;
   x++;
 }
       }


      for(i=0;i<x;i++)
       {
n1 = n;
n2 = n+i;
for(j=0;j<x;j++)
{
  if( n2->fpas[1]==(n1+j)->fpas[0] || n2->fpas[0]==(n1+j)->fpas[1] )
   {
     flag = 1;
     break;
   }
   n1 ++;
}
       }
    if(flag == 1)
     printf("\n\n %d is a nasty number !! ",num);
    else
     printf("\n\n %d isn't a nasty number !! ",num);


    getch();
  }

Friday, January 7, 2011

Pyramid Of Number

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


 void main()
   {
       int num,i,j,k,l;
       X: clrscr();
       printf("\n\n Enter the number of level u wanna print in this pyramind (Max. 9):");
       scanf("%d",&num);
if(num>9)
{ goto X; }
       printf("\n\n The Following Pyramid is : \n\n");
for(i=1;i<=num;i++)
   {
    k = num-i;
    for(j=1;j<=k;j++)
{ printf(" "); }
    for(l=1;l<=i;l++)
{ printf("%d ",i); }
    for(j=1;j<=k+2;j++)
{ printf(" "); }
    printf(" LEVEL:%d \n",k+1);
   }
     getch();
   }

Thursday, January 6, 2011

Pascal Triangle

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


 void main()
   {
       int num,i,j,k,l,m,a[9][9];
       X: clrscr();
       printf("\n\n Enter the number of level u wanna print in a Pascal Triangle (Max. 9):");
       scanf("%d",&num);
if(num>9)
{ goto X; }
       printf("\n\n The Following Pascal Triangle is : \n\n");
for(i=0;i<=num;i++)
   {
    k = num-i;
    for(j=1;j<=k;j++)
{ printf(" "); }
    for(l=0;l<=i;l++)
{
 if( l==0 || l==i )
   a[i][l]=1;
 else
  {
   a[i][l] = a[i-1][l] + a[i-1][l-1];
  }
  printf("%d ",a[i][l]);
}
    for(j=1;j<=k+2;j++)
{ printf(" "); }
    printf(" n=%d \n",i);
   }
     getch();
   }

Wednesday, January 5, 2011

Linear And Binary Search

#include<stdio.h>
#include<conio.h>
#define max 20


 int data[max];


 void input_data();
 void disp_data(int [],int);
 void Sort(int [],int);
 void Lsearch(int [],int,int);
 void Bsearch(int [],int,int);


void main()
 {
  clrscr();
  int i,num,cho,sbch,item,ssbch;
  do
  {
  printf("Enter the no. of students :- ");
  scanf("%d",&num);
  }while( num <= 0 ||  num >= max );
  printf("\n\n Enter the data of the %d elements : ",num);
  for(i=0;i<num;i++)
   scanf("%d",&data[i]);
  disp_data(data,num);
  getch();
  do
  {
  clrscr();
  printf("\n\n\t\t\t  Menu Selection\n");
  printf("\n 1. Linear Seaching of an Element in an Array ");
  printf("\n 2. Binay Sorting of an Element in an Array ");
  printf("\n Enter your choice :- ");
  scanf("%d",&cho);
  if(cho==1 || cho==2)
  {
   printf("\n\nEnter any number to be searched : ");
   scanf("%d",&item);
  }
  switch(cho)
   {
    case 1: Lsearch(data,num,item);
   break;
    case 2: Bsearch(data,num,item);
   break;
    default: break;
   }
  if( cho == 1 || cho == 2 )
   disp_data(data,num);
  }while( cho == 1 || cho == 2 );
  getch();
 }


void disp_data(int s[],int noe)
 {
  int i;
  printf("\n\n\t\t The current data is :");
  for(i=0;i<noe;i++)
   printf(" %d",s[i]);
  getch();
 }


 void Sort(int x[],int noe)
  {
   int i,j,flag,small,posn,temp;


   for(i=0;i<noe;i++)
   {
    flag = 0;
    small = x[i];
    for(j=i+1;j<noe;j++)
    {
     if( x[j] < small )
      { small = x[j];  flag = 1; posn = j; }
    }
    if(flag==1)
     {
     temp = x[i];
     x[i] = x[posn];
     x[posn] = temp;
     }
   }
   printf("\n\n\t\t Sorting has been done ... \n\n");
  }


 void Lsearch(int x[],int noe,int ele)
  {
    int flag=0,i,noc=0,posn;
    for(i=0;i<noe;i++)
     {
      noc++;
      if(ele == x[i])
       {  flag = 1;  posn = i;  break;  }
     }
    if(flag==0)
     printf("\n\nThe data not found ");
    else
     {
      printf("\n\nThe data has been found at position #%d",posn+1);
      printf("\n& The no. of comparisions were made : %d",noc);
      }
    getch();
  }


void Bsearch(int x[],int noe,int ele)
   {
     int flag=0,i,noc=0,posn,beg,last,mid;
     beg = 0; last = noe - 1;
     Sort(x,noe);
     while(beg<=last)
      {
       mid = (beg+last)/2;
       noc++;
       if(ele == x[mid])
{
flag = 1;
posn = mid;
break;
}
       else if(ele > x[mid])
beg = mid + 1;
       else
last = mid - 1;
      }
     if(flag==0)
      printf("\n\nThe data not found ");
     else
      {
      printf("\n\nThe data has been found at position #%d",posn+1);
      printf("\n& The no. of comparisions were made : %d",noc);
      }
      getch();
   }

Tuesday, January 4, 2011

Introduction

Hi Guys,

Myself Ashish, a young Engg. Student and this is "Ash Bee Code". ;)
This is my BRAND NEW Blog especially useful for Young Programmers.
I'm gonna share you some of nice Programming Codes (whether C, C++, JAVA etc. )
Hope Y'all have a sneak peek of mine work so far.
Feel free to express and Post comment if you're having NE doubts and suggestions !!

With Regards,
Ashish Bardhan
(a.k.a Ash Bee Code)