Program To Convert Infix To Prefix Expression Using Stack
Program To Convert Infix To Prefix Expression Using Stack
STACK
#include«stdio.h»
#include«string.h»
/* MAIN PROGRAM */
void main()
{
init();
printf("Enter an infix expression :\n");
scanf("%s",infix);
infixtoprefix();
strrev(prefix);
printf("The resulting prefix expression is %s",prefix);
getch();
} // End of main
#include<iostream.h>
#include<conio.h>
#include<stdio.h>
#include<string.h>
void main()
{
clrscr();
char c[20];
int a[10][2];
int l,n=0,s=0,t;
cout<<"Enter the string:";
cin>>c;
l=strlen(c);
for(int f=0;f<l;f++)
{
if(c[f]=='+')
{
s++;
}
}
n=s+1;
cout<<"Thus states="<<n;
for(int i=0;i<n;i++)
{
t=1;
a[i][1]=t;
a[i][2]=(t-1);
t++;
if(i>=1)
{
a[i][2]=n-2;
if(i==2)
{
a[i][1]=n-1;
a[i][2]=n-1;
}
}
}
cout<<"\n\n\tNFA\n\n";
cout<<"state \t"<<"a\t"<<"b\t";
for(int j=0;j<n;j++)
{
cout<<"\nq"<<j<<"\t"<<"q"<<a[j][1];
if(j==1)
{
cout<<",q"<<j+1;
}
cout<<"\t"<<"q"<<a[j][2];
}
getch();
}
Program to convert NDFA to DFA.
#include«Iostream.h»
#include«conio.h»
#include«stdlib.h»
void main()
{ clrscr();
int n=10,A[10][2],init,final;
int temp[10][2];
cout««"Enter no of input";
cin»»n;
for(int i=0;i«n;i++)
{ cout««"Enter transition state for "««i+1««" node";
cout««"(Enter 11 for NULL)\n";
cout««"For a =";
cin»»A[i][1];
cout««"For b =";
cin»»A[i][2];
}
clrscr();
cout««"Entered NFA is\n";
cout««"STATE\t\t"««"a\t"««"b";
for(i=0;i«n;i++)
{ cout««"\nq"««i+1««"\t\t"««"q"««A[i][1]««"\t"««"q"««A[i][2];
}
cout««"\nEnter initial state: ";
cin»»init;
cout««"\nEnter final state: ";
cin»»final;
cout««"\n\n";
cout««" DFA is \n";
cout««"STATE\t\t"««"a\t"««"b\n";
cout««"q"««init««"\t\t"««"q"««A[init-1][1]««"\t"««"q"««A[init-1][2];
temp[init][1]=A[init][1];
temp[init][2]=A[init][2];
getch();
}
String accepted by grammer or not
#include<iostream.h>
#include<stdio.h>
#include<conio.h>
int main()
{ char ch='y';
char ss[10];
int i, size;
clrscr();
cout<<"\nThe grammar is a*ab";
do
{
cout<<"\nenter the size";
cin>>size ;
cout<<"\nenter the string";
for(i=0;i<size;i++)
{
cin>>ss[i];
}
}
else
cout<<"\nincorrect string REJECTED!!!";
cout<<"\npress y to continue...";
cin>>ch;
}while(ch=='y'|| ch=='Y');
getch();
return 0;
}