0% found this document useful (0 votes)
10 views

TOC File

The document is a practical file submitted by Aashutosh Tiwari, a student of CSE studying in the 3rd year at Madhav Institute of Technology & Science Gwalior for the subject Theory of Computation. It contains 10 programs designed to create machines that accept various strings based on different conditions. For each program, the code is provided along with an output section.

Uploaded by

Aashutosh Tiwari
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
10 views

TOC File

The document is a practical file submitted by Aashutosh Tiwari, a student of CSE studying in the 3rd year at Madhav Institute of Technology & Science Gwalior for the subject Theory of Computation. It contains 10 programs designed to create machines that accept various strings based on different conditions. For each program, the code is provided along with an output section.

Uploaded by

Aashutosh Tiwari
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 24

MADHAV INSTITUTE OF TECHNOLOGY &

SCIENCE GWALIOR, M.P. - 474005


(A Govt. Aided UGC Autonomous Institute, Affiliated to RGPV Bhopal)

Practical File
on
THEORY OF COMPUTATION(150503)
July-December, 2019

Submitted to: Submitted by:

Prof. Sneha Garg Aashutosh Tiwari (0901CS171002)

Prof. Mahesh Parmar CSE, 3rd Year


INDEX

S.No. Name of Program Date of Submission Faculty’s Signature


PROGRAM 1

Design a Program for creating machine that accepts three consecutive 1’s.

#include<bits/stdc++.h>
using namespace std;
int main()
{
string s;
cout<<"Enter a string in binary form: ";
cin>>s;
map<int,char> state;
state[0]='A';state[1]='B';state[2]='C';state[3]='D';
int i=0;
for(int c=0;c<s.length();c++)
{
switch(s[c])
{
case '0':
if(i%2)
i=2;
else
i=0;
break;

case '1':
if(i%2==0)
i+=1;
else if(i==3)
i=1;
}
}
cout<<"Current state is "<<state[i];
if(i==3)
cout<<" that means string is accepted. \n";
else
cout<<" that means string is not accepted. \n";
return 0;
}
OUTPUT:
PROGRAM 2

Design a Program for creating machine that accepts the string always
ending with 101.

#include<bits/stdc++.h>
using namespace std;
int main()
{
string s;
cout<<"Enter a string in binary form:";
cin>>s;
map<int,char>state;
state[0]='A';state[1]='B';state[2]='C';state[3]='D';
int i=0;
for(int c=0;c<s.length();c++)
{
if(s[c]=='0')
i=0;
else
{
i+=1;
i%=4;
}
if(i==3)
break;
}
cout<<"Current state is "<<state[i];
}
OUTPUT:
PROGRAM 3

Design a Program for Mod 3 Machine


#include<stdio.h>
#define s1 1
#define s2 2
#define s3 3
int main()
{
char c;
int state=s1;
printf("Enter the string: ");
scanf("%c",&c);
while(c!='\n')
{
if((c=='1')&&(state==s1))
state=s2;
else if((c=='1')&&(state==s2))
state=s1;
else if((c=='0')&&(state==s2))
state=s3;
else if((c=='0')&&(state==s3))
state=s2;
scanf("%c",&c);
}
if(state==s1)
printf("Machine is in the final state \n");
else
printf("Machine is not in the final state \n");
}
OUTPUT:
PROGRAM 4

Design a Program for accepting decimal number divisible by 2

#include<iostream>
using namespace std;
int main()
{
string str;
char state='A';
cout<<"Enter your string: ";
cin>>str;
for(int i=0;i<str.length();i++)
{
switch(str[i])
{
case '0':
state='A';
break;
case '1':
state='B';
}
}
if(state=='A')
cout<<"Machine is in final state \n";
else
cout<<"Machine is not in final state \n";
}
OUTPUT:
PROGRAM 5

Design a Program for creating a machine which accepts strings having


equal no. of 1’s and 0’s

#include<iostream>
using namespace std;
int main()
{
int count0=0,count1=0;
string str;
cout<<"Enter your string: ";
cin>>str;
for(int i=0;i<str.length();i++)
{
if(str[i]=='0')
count1++;
if(str[i]=='0')
count0++;
}
if(count0==count1)
cout<<"String is accepted \n";
else
cout<<"String is not accepted \n";
}
OUTPUT:
PROGRAM 6

Design a Program for creating a machine which count number of 1’s and
0’s in a given string.

#include<iostream>
using namespace std;
int main()
{
int count0=0,count1=0;
string str;
cout<<"Enter your string: ";
cin>>str;
for(int i=0;i<str.lengt h();i++)
{
if(str[i]=='1')
count1++;
if(str[i]=='0')
count0++;
}
cout<<"Number of 1's: "<<count1;
cout<<"\nNumber of 0's: "<<count0;
}
OUTPUT:
PROGRAM 7

Design a program for creating machine that aceepts even string.

#include<bits/stdc++.h>
using namespace std;
int main()
{
stack<char> stake;
cout<<"Enter a string: ";
char c;
scanf("%c",&c);
while(c!='\n')
{
stake.push(c);
scanf("%c",&c);
}
if(stake.size()%2==1)
cout<<"String is not an even length string and it is not accepted";
else
cout<<"String is an even length string and it is accepted";
return 0;
}
OUTPUT:
PROGRAM 8

Design a program that accepts the string having second last element 0.

#include<iostream>
using namespace std;
int main()
{
string str;
char state='A';
cout<<"Enter your string:";
cin>>str;
for(int i=0; i<str.length();i++)
{
switch(state)
{
case'A':
if(str[i]=='1')
state='A';
else
state='B';
break;
case 'B':
state='C';
break;
case'C':
if(str[i]=='0')
state='B';
else
state='A';
}
}
if(state=='C')
cout<<"String is accepted";
else
cout<<"String is not accepted";
}
OUTPUT:
PROGRAM 9

Design a Program to create a machine that accepts all the strings having
two consecutive 0’s.

#include<iostream>
using namespace std;
int main()
{
string str;
char state='A';
cout<<"Enter your string:";
cin>>str;
for(int i=0; i<str.length();i++)
{
switch(state)
{
case'A':
if(str[i]=='1')
state='A';
else
state='B';
break;
case 'B':
if(str[i]=='1')
state='A';
else
state='C';
break;
case'C':
state='C';
}
}
if(state=='C')
cout<<"String is accepted";
else
cout<<"String is not accepted";
}
OUTPUT:
PROGRAM 10

Design a PDA that accepts an bm cn using stack.

#include<bits/stdc++.h>
using namespace std;
int main()
{
stack<char>stake;
stake.push('$');
string ins;
cout<<"Enter a string: ";
cin>>ins;
int i=0,bad=0;
switch(ins[i])
{
case'a':
stake.push('x');
while(ins[++i]=='a')
{
stake.push('x');
}
if(ins[i-1]!='a')
{
bad=1;break;
}
while(ins[++i]=='b')
{
continue;
}
if(ins[i-1]!='b')
{
bad=1;break;
}
while(ins[i++]=='c')
{
stake.pop();
if(stake.size()==1)
break;
}
break;
default:
bad=1;
}
if(bad || i!=ins.length())
cout<<"String is not accepted \n";
else
cout<<"String is accepted \n";
return 0;
}
OUTPUT:

You might also like