Showing posts with label Convert. Show all posts
Showing posts with label Convert. Show all posts

Monday, March 28, 2016

C program to convert prefix expression into postfix notations

#include<stdio.h>
#include<conio.h>
#include<string.h>
#include<ctype.h>
#include<process.h>
char operator_stack[50];
int operator_top=-1;
int isoperator(char s)
 {
  if(s=='^'||s=='*'||s=='/'||s=='%'||s=='+'||s=='-')
   {
    return 1;
   }
  else
   {
    return 0;
   }
 }
int isparenthesis(char pa)
 {
  if(pa=='(')
   return (1);
  else if(pa==')')
   return(2);
  else
   return(0);
 }
void operator_push(char n)
{
 if(operator_top==49)
  {
   printf("\Overflow condition");
   exit(0);
  }
 operator_stack[++operator_top]=n;
}
int priority(char p)
 {
  if(p=='^')
   return 3;
  else if(p=='*'||p=='/'||p=='%')
   return 2;
  else if(p=='+'||p=='-')
   return 1;
  else
   return 0;
 }
char operator_pop()
 {
  if(operator_top==-1)
   {
    printf("\nUnderflow condition");
    exit(0);
   }
  return(operator_stack[operator_top--]);
 }
void main()
{
 int x;
 char *exp_infix;
 char postfix[50][50];
 int digit;
 char dig[50];
 int y;
 printf("\nEnter the infix notation");
 scanf("%[^\n]s",exp_infix);
 for(x=0,y=0;exp_infix[x]!='\0';)
  {
   if(isdigit(exp_infix[x]))
    {
     digit=0;
     while((exp_infix[x]>=48)&&(exp_infix[x]<=57))
      {
       dig[digit]=exp_infix[x];
       x++;
       digit=digit+1;
      }
     dig[digit]='\0';
     //puts(dig);
     strcpy(postfix[y],dig);
   //  puts(postfix[y]);
     //postfix[y]=exp_infix[x];
     y++;
    }
    else if(isoperator(exp_infix[x])||isparenthesis(exp_infix[x]))
    {
     if(operator_top==-1||isparenthesis(exp_infix[x])==1)
      {
       operator_push(exp_infix[x]);
       x++;
      }
     else
      {
       if(isparenthesis(exp_infix[x])==2)
{
while(operator_stack[operator_top]!='(')
 {
  digit=0;
  dig[digit]=operator_pop();
  strcpy(postfix[y],dig);
  y++;
 }
operator_pop();
x++;
continue;
}
       if(priority(exp_infix[x])<priority(operator_stack[operator_top]))
{
while((priority(exp_infix[x])<priority(operator_stack[operator_top])&&(operator_top>-1)))
 {
  digit=0;
  dig[digit]=operator_pop();
  digit=digit+1;
  dig[digit]='\0';
  strcpy(postfix[y],dig);
  y++;
 }
operator_push(exp_infix[x]);
x++;
}
else if(priority(exp_infix[x])==priority(operator_stack[operator_top]))
{
  digit=0;
  dig[digit]=operator_pop();
  digit=digit+1;
  dig[digit]='\0';
  strcpy(postfix[y],dig);
  y++;
  operator_push(exp_infix[x]);
  x++;
}
       else if(priority(exp_infix[x])>priority(operator_stack[operator_top]))
{
operator_push(exp_infix[x]);
x++;
}
      }
    }
  }
while(operator_top!=-1)
   {
    digit=0;
    dig[digit]=operator_pop();
    digit++;
    dig[digit]='\0';
    strcpy(postfix[y],dig);
    y++;
   }
  //postfix[y]='\0';
  for(x=0;x<y;x++)
   {
    printf("%s",postfix[x]);
   }
getch();
}

output:
Enter the infix notation 5*2+6
652*+

Tuesday, March 15, 2016

C program to convert a digit into words

#include<stdio.h>
#include<conio.h>
#include<string.h>
char *single_digit[]={"","one","two","three","four","five","six","seven","eight","nine"};
char *two_digit[]={"ten","eleven","twelve","thirteen","fourteen","fiveteen","sixteen","seventeen","eighteen","nineteen"};
char *tens_multiple[]={"","","twenty","thirty","fourty","fifty","sixty","seventy","eighty","ninty"};
char *tens_power[]={"hundred","thousand"};
void one(char *num1)
{
 printf("%s",single_digit[*num1-'0']);
}
void two(char *num2)
{
 if(*num2=='0')
 {
  num2=num2+1;
  one(num2);
 }
 else if(*(num2)=='1')
      {
       printf("%s",two_digit[*(num2+1)-'0']);
      }
     else
      {
       printf("%s ",tens_multiple[*num2-'0']);
       num2=num2+1;
       one(num2);
      }
}
void three(char *num3)
{
 if(*num3=='0')
  {
   num3=num3+1;
   two(num3);
  }
 else
  {
   one(num3);
   printf("%s "," hundred");
   num3=num3+1;
   two(num3);
  }
}
void four(char *num4)
{
 if(*num4=='0')
  {
   num4=num4+1;
   three(num4);
  }
 else
  {
   one(num4);
   printf("%s "," thousand");
   num4=num4+1;
   three(num4);
  }
}
void convert(char *number)
{
 //printf("\nNumber is %s length is %d",number,strlen(number));
 while(*number=='0')
  {
   number=number+1;
  }
  //printf("\nnumber is %s length is %d",number,strlen(number));
  int l=strlen(number);
  //printf("\n first number is %s ",single_digit[0]);
  switch(l)
   {
    case 1:
     one(number);
     break;
    case 2:
     two(number);
     break;
    case 3:
     three(number);
     break;
    case 4:
     four(number);
     break;
    default:
     printf("\nOnly four digit numbers are allowed");
    }
}
void main()
 {
  char *num;
  printf("\n Enter a number ");
  scanf("%s",num);
  //printf("String is %s",num);
  convert(num);
  getch();
 }

output
Enter a number 1230
one thousand two hundred thirty