Vous êtes sur la page 1sur 63

/*WATER JUG PROBLEM*/

PROGRAM CODE:

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

class water
{
public:
int j1,j2,f1,f2,h,l,a;
void getdata();
void jug_fill(int,int,int,int,int,int);
};

void water::getdata()
{
water o;
cout<<"water jug problem \n";
cout<<"enter the jug size:";
cin>>j1;
cin>>j2;
cout<<"enter the final stage of the jug:\n";
cin>>f1;
cin>>f2;
cout<<"initial stage:0 0\n";
l=j2;
h=0;
a=0;
cout<<"next stage"<<h<<" "<<l<<endl;
o.jug_fill(j1,j2,f1,h,l,a);
}

void water::jug_fill(int j1,int j2,int f1,int h,int l,int a)


{
water o1;
if(j1>=h)
{
h=h+l;
l=0;
a=h;
if(a==f1)
{
cout<<"next state"<<h<<" "<<l<<endl;
cout<<"final state achieved!!"<<endl;
}
else if(h>j1)
{
l=h-j1;
h=j1;
cout<<"next state:"<<h<<" "<<l<<endl;
h=0;
a=h;
cout<<"next state:"<<h<<" "<<l<<endl;
o1.jug_fill(j1,j2,f1,h,l,a);
}
else
{
l=h;
cout<<"next state:"<<h<<" "<<l<<endl;
o1.jug_fill(j1,j2,f1,h,l,a);
}
}
}

void main()
{
clrscr();
water o;
o.getdata();
getch();
}
SAMPLE INPUT AND OUTPUT:

WATER JUG PROBLEM


Enter the jug size:
43
Enter the final state of the jugs:
20
Initial state:0 0
Next state:0 3
Next state:3 3
Next state:4 2
Next state:0 2
Next state:2 0
Final state is achieved!
/*SINGLE PLAYER GAME*/

PROGRAM CODE:

#include<iostream.h>
#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
int tmp,no=0;

class single
{
public:
int i,j;
int arr[10][10];
int h_no;
void h_fn();
void get();
void print();
void copy(int);
void find_sol();
int find_min();
void path();
}temp[100],start,goal,visited[30];

void single::get()
{
cout<<"Enter the starting array:\n";
for(i=1;i<=3;i++)
for(j=1;j<=3;j++)
cin>>start.arr[i][j];
start.h_fn();
cout<<"Enter the ending array:\n";
for(i=1;i<=3;i++)
for(j=1;j<=3;j++)
cin>>goal.arr[i][j];
goal.h_fn();
}
void single::h_fn()
{
int temp=0;
for(i=1;i<=3;i++)
for(j=1;j<=3;j++)
if(arr[i][j]!=goal.arr[i][j])
++temp;
h_no=temp;
}

void single::copy(int t)
{
for(i=1;i<=3;i++)
for(j=1;j<=3;j++)
temp[t].arr[i][j]=arr[i][j];
}

void single::find_sol()
{
tmp=0;
int si,sj;
for(i=1;i<=3;i++)
for(j=1;j<=3;j++)

if(arr[i][j]==0)
{
si=i;
sj=j;
break;
}

if(sj<3)
{
++tmp;
copy(tmp);
temp[tmp].arr[si][sj]=arr[si][sj+1];
temp[tmp].arr[si][sj+1]=0;
temp[tmp].h_fn();
}
if(sj>1)
{
++tmp;
copy(tmp);
temp[tmp].arr[si][sj]=arr[si][sj-1];
temp[tmp].arr[si][sj-1]=0;
temp[tmp].h_fn();
}
if(si>1)
{
++tmp;
copy(tmp);
temp[tmp].arr[si][sj]=arr[si-1][sj];
temp[tmp].arr[si-1][sj]=0;
temp[tmp].h_fn();
}
if(si<3)
{
++tmp;
copy(tmp);
temp[tmp].arr[si][sj]=arr[si+1][sj];
temp[tmp].arr[si+1][sj]=0;
temp[tmp].h_fn();
}
}

void single::print()
{
for(i=1;i<=3;i++)
{
for(j=1;j<=3;j++)
{
if(arr[i][j]==0)
cout<<"0"<<" ";
else
cout<<arr[i][j]<<" ";
}
cout<<"\n";
}
h_fn();
getch();
cout<<endl<<endl;
}
int single::find_min()
{
int min=1;
for(i=1;i<tmp;i++)
if(temp[i].h_no>temp[i+1].h_no)
min=i+1;
return min;
}
void single::path()
{
++no;
for(i=1;i<=3;i++)
for(j=1;j<=3;j++)
visited[no].arr[i][j]=arr[i][j];
visited[no].print();
if(visited[no].h_no==0)
{
printf("SOLUTION FOUND!");
getch();
exit(0);
}
visited[no].find_sol();
next_min:int min_h=0;
min_h=find_min();
int flag=0;
for(i=1;i<=no;i++)
{
flag=0;
if(visited[i].h_no==temp[min_h].h_no)
{
for(j=1;j<=3;j++)
for(int k=1;k<=3;k++)
if(visited[i].arr[j][k]!=temp[min_h].arr[j][k])
{
flag=1;
}
if(flag==0)
{
flag=2;
break;
}
}
}
if(flag==0||flag==1)
{
temp[min_h].path();
}

else if(flag==2)
{
temp[min_h].h_no=99;
goto next_min;
}
}

void main()
{
single p[20];
clrscr();
start.get();
cout<<"\n Solution:\n";
start.path();
}
SAMPLE INPUT AND OUTPUT:

SINGLE PLAYER GAME


Enter the starting array:
283
164
705
Enter the ending array:
123
804
765

Solution:
283
164
705

283
104
765

283
014
765

083
214
765

803
214
765

813
204
765

813
023
765
013
824
765

123
804
765

SOLUTION FOUND!
//TWO PLAYER GAME//

PROGRAM CODE:

#include<iostream.h>
#include<stdio.h>
#include<conio.h>
void printstate(int s[3][3])
{
cout<<"\n\n\t\t\t";
for(int i=0;i<3;i++)
{
for(int j=0;j<3;j++)
{
cout<<s[i][j]<<"\t";
}
cout<<endl<<"\t\t\t";
}
}

void makemove(int arr[][3],int i)


{
int j,k;
switch(i)
{
case 0:
for(int j=0;j<3;j++)
{
if(arr[0][j]==0)
{
arr[0][j]=5;
break;
}
}
break;
case 1:
for(j=0;j<3;j++)
{
if(arr[1][j]==0)
{
arr[1][j]=5;
break;
}
}
break;

case 2:
for(j=0;j<3;j++)
{
if(arr[2][j]==0)
{
arr[2][j]=5;
break;
}
}
break;

case 3:
for(j=0;j<3;j++)
{
if(arr[j][0]==0)
{
arr[j][0]=5;
break;
}
}
break;

case 4:
for(j=0;j<3;j++)
{
if(arr[j][1]==0)
{
arr[j][1]=5;
break;
}
}
break;

case 5:
for(j=0;j<3;j++)
{
if(arr[j][2]==0)
{
arr[j][2]=5;
break;
}
}
break;

case 6:
for(j=0;j<3;j++)
{
if(arr[j][j]==0)
{
arr[j][j]=5;
break;
}
}
break;

case 7:
for(j=0,k=2;j<3&&k>0;j++,k--)
{
if(arr[j][k]==0)
{
arr[j][k]=5;
break;
}
}
break;
}
}

void main()
{
clrscr();
int arr[3][3],a[8],x,y,over=0,movemade=0;
for(int i=0;i<3;i++)
for(int j=0;j<3;j++)
arr[i][j]=0;
cout<<"Instructions:"<<endl;
cout<<"---------------:"<<endl;
cout<<"\t 0 indicates that the cell is free"<<endl;
cout<<"\t 3 indicates the move of the user"<<endl;
cout<<"\t 5 indicates the move of computer"<<endl;
while(1)
{
movemade=0;
printstate(arr);
cout<<"make your move"<<endl;
cout<<"-----------"<<endl;
cout<<"Enter the row number:";
cin>>x;
cout<<"Enter the column number:";
cin>>y;
if((arr[x-1][y-1]==3)||arr[x-1][y-1]==5)
{
cout<<"\n\n\t\t\t The cell is not free\a\n";
cout<<"\n\t\t Make another choice\n";
continue;
}
else
arr[x-1][y-1]=3;
a[0]=arr[0][0]+arr[0][1]+arr[0][2];
a[1]=arr[1][0]+arr[1][1]+arr[1][2];
a[2]=arr[2][0]+arr[2][1]+arr[2][2];
a[3]=arr[0][0]+arr[1][0]+arr[2][0];
a[4]=arr[0][1]+arr[1][1]+arr[2][1];
a[5]=arr[0][2]+arr[1][2]+arr[2][2];
a[6]=arr[0][0]+arr[1][1]+arr[2][2];
a[7]=arr[0][2]+arr[1][1]+arr[2][0];
for(int i=0;i<8;i++)
{
if(a[i]==15)
{
printstate(arr);
cout<<"Computer won.Better luck next time";
over=1;
break;
}
if(a[i]==9)
{
printstate(arr);
cout<<"You won.Well done";
over=1;
break;
}
}
if(over==1)
break;
for(i=0;i<8;i++)
{
if(a[i]==10)
{
makemove(arr,i);
printstate(arr);
cout<<"\n\nComputer won.Better luck next time";
movemade=1;
break;
}
}
if(movemade==1)
break;
for(i=0;i<8;i++)
{
if(a[i]==6)
{
makemove(arr,i);
movemade=1;
break;
}
}
if(movemade==1)
continue;
for(i=0;i<8;i++)
{
if(a[i]==8)
{
makemove(arr,i);
movemade=1;
break;
}
}
if(movemade==1)
continue;
for(i=0;i<8;i++)
{
if(a[i]==3)
{
makemove(arr,i);
movemade=1;
break;
}
}

if(movemade==1)
continue;
printstate(arr);
cout<<"\n\n Game is draw.Better luck next time";
break;
}
getch();
}
SAMPLE INPUT AND OUTPUT:

TWO PLAYER GAME

Instructions:
-------------
0 indicates that the cell is free
3 indicates the move of the user
5 indicates the move of the computer

000
000
000

Make your move


--------------------
Enter the row number:2
Enter the column number:2

000
530
000

Make your move


--------------------
Enter the row number:3
Enter the column number:3

500
530
003

Make your move


--------------------
Enter the row number:3
Enter the column number:1

500
530
353
Make your move
--------------------
Enter the row number:1
Enter the column number:3

503
530
353

You won. Well done!


/* A* ALGORITHM */

PROGRAM CODE:

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

struct node
{
char name;
int g;
int h;
int f;
char parent;
}open[20],closed[20],temp[10];

static int open_front,open_rear;


static int closed_front,closed_rear;
int current;
int n,pos;
int check(char);
void astar();

void next()
{
int i,j;
printf("\n\n\t Enter the number of childs:");
scanf("%d",&n);
pos=0;
for(i=0;i<n;i++)
{
printf("\n\n\t Enter the next node of %c:",closed[current]);
temp[pos].name=getche();
printf("\n\n\t Enter its heuristic value:");
scanf("%d",&temp[pos].h);
temp[pos].g=closed[current].g+1;
temp[pos].f=temp[pos].g+temp[pos].h;
temp[pos].parent=closed[current].name;
pos++;
}
}

int check(char temp)


{
int i;
for(i=0;i<open_rear;i++)
{
if(open[open_rear].name==temp)
{
return 0;
}
}
for(i=0;i<closed_rear;i++)
{
if(closed[closed_rear].name==temp)
{
return 0;
}
}
return 1;
}

void add_open()
{
int i;
char temp1;
for(i=0;i<n;i++)
{
temp1=temp[i].name;
if(check(temp1))
{
open[open_rear].name=temp[i].name;
open[open_rear].g=temp[i].g;
open[open_rear].h=temp[i].h;
open[open_rear].f=temp[i].f;
open[open_rear].parent=temp[i].parent;
open_rear++;
}
}
}
void swap(int i,int j)
{
node t;
t.name=open[i].name;
t.h=open[i].h;
t.g=open[j].g;
t.f=open[i].f;
t.parent=open[i].parent;
open[i].name=open[j].name;
open[i].h=open[j].h;
open[i].g=open[j].g;
open[i].f=open[j].f;
open[i].parent=open[j].parent;
open[j].name=t.name;
open[j].h=t.h;
open[j].g=t.g;
open[j].f=t.f;
open[j].parent=t.parent;
}

void find_min()
{
int i,j,min;
for(i=open_front;i<open_rear-1;i++)
{
for(j=i+1;j<open_rear;j++)
{
if(open[i].f>open[i].f)
swap(i,j);
}
}
}

void goal()
{
int i,dist=0;
clrscr();
printf("\n\n\tthe optimal path is :");
for(i=closed_front;i<closed_rear;i++)
{
printf("\t%c->",closed[i].name);
dist+=closed[i].h;
}
printf("\n\n\tThe optimal path is %d",dist);
printf("\t%c",closed[i].name);
getch();
exit(0);
}

void add_closed()
{
char temp;
closed[closed_rear].name=open[open_front].name;
closed[closed_rear].h=open[open_front].h;
closed[closed_rear].g=open[open_front].g;
closed[closed_rear].f=open[open_front].f;
closed[closed_rear].parent=open[open_front].parent;
open_front++;
current=closed_front;
closed_rear++;
s1:printf("\n\n\t do you want to continue(y/n)?");
temp=getche();
if(temp=='y'||temp=='Y')
{
astar();
}
else if(temp=='n'||temp=='N')
goal();
else
goto s1;
}

void astar()
{
next();
add_open();
find_min();
add_closed();
}
void main()
{
int i,j,temp;
char name;
clrscr();
printf("\n\n\t A* algorithm");
printf("\n\n\t enter the root node");
scanf("%c",&name);
printf("\n\n\tenter its heuristic value:");
scanf("%d",&temp);
getch();
closed[closed_rear].name=name;
closed[closed_rear].h=temp;
closed[closed_rear].parent='0';
closed[closed_rear].f=temp;
closed_rear++;
current=closed_front;
astar();
}
SAMPLE INPUT AND OUTPUT:

A* ALGORITHM
Enter the root node: A
Enter its heuristic value: 10

Enter the number of childs: 3

Enter the next node of A: B


Enter its heuristic value: 8

Enter the next node of A: C


Enter its heuristic value: 6

Enter the next node of A: D


Enter its heuristic value: 4

Do you want to continue(y/n)?: y

Enter the number of childs: 2

Enter the next node of A: E


Enter its heuristic value: 1

Enter the next node of A: F


Enter its heuristic value: 2

Do you want to continue(y/n)?: n

The optimal path is: A -> D -> E ->


Distance of the optimal path is: 15
/*UNIFICATION ALGORITHM*/

PROGRAM CODE:

#include<conio.h>
#include<string.h>
int s1=0,s2=0;
char new1[50],new2[50],arg1[25],arg2[25];

main()
{
char l1[50],l2[15];
clrscr();
printf("\nUNIFICATION ALGORITHM");
printf("\n____________________\n");
printf("\nEnter the first literal:");
scanf("%s",l1);
printf("Enter the second literal:");
scanf("%s",l2);
unify(l1,l2);
getch();
}

unify(char l1[50],char l2[50])


{
int a,b;
a=chkfun(l1,l2);
if(a==0)
{
printf("\n The function name does not match!”);
printf(“\n Unification cannot be done!");
return;
}
else
{
a=chkarg(l1,l2);
if(a==0)
{
printf("\nThe no.of arg do not match!”);
printf(“\n Unification cannot be done!");
return;
}

else
{
if(strcmp(arg1,arg2)==0)
printf("\nUnification not needed!”);
printf(“\n Both the predicate and literals are same!");
else
{
printf("Unification possible");
chksub(l1,l2);
}
return;
}
}
}

chkfun(char l1[50],char l2[50])


{
int i,c=0;
char x[1];
for(i=0;i<strlen(l1);i++)
{
if(l1[i]!='(')
{
c++;
x[0]=l1[s1];
x[1]='\0';
strcat(new1,x);
s1++;
}
if(l1[i]=='(')
break;
}

for(i=0;i<strlen(l2);i++)
{
if(l2[i]!='(')
{
c++;
x[0]=l2[s2];
x[1]='\0';
strcat(new2,x);
s2++;
}
if(l2[i]=='(')
break;
}
if(strcmp(new1,new2)==0)
return 1;
else
return 0;
}

chkarg(char l1[50],char l2[50])


{
int l;
int i;
char x[1];
for(i=0;i<strlen(l1);i++)
{
if(l1[i]=='(')
{
i++;
while(l1[i]!=')')
{
if(l1[i]!=')')
{
x[0]=l1[i];
x[1]='\0';
}
strcat(arg1,x);
i++;
}
break;
}
else
continue;
}
for(i=0;i<strlen(l2);i++)
{
if(l2[i]=='(')
{
i++;
while(l2[i]!=')')
{
if(l2[i]!=')')
{
x[0]=l2[i];
x[1]='\0';
strcat(arg2,x);
}
i++;
}
break;
}
else
continue;
}
if(strlen(arg1)==strlen(arg2))
return 1;
else
return 0;
}

chksub(char l1[50],char l2[50])


{
int i;
for(i=0;i<strlen(l1);i++)
{
if(l1[i]!=l2[i])
printf("\nSubstitution:%c/%c",l2[i],l1[i]);
}
return;
}
SAMPLE INPUT AND OUTPUT:

UNIFICATION ALGORITHM
--------------------------------------
Enter the first literal: abc(p,q)
Enter the second literal: abc(x,y)
Unification possible!
Substitution: x / p
Substitution: y / q

UNIFICATION ALGORITHM
--------------------------------------
Enter the first literal: abc(p,q)
Enter the second literal: abcd(x,y)
The function name does not match. Unification cannot be done!

UNIFICATION ALGORITHM
--------------------------------------
Enter the first literal: abc(p,q)
Enter the second literal: abc(x)
The number of arguments does not match. Unification cannot be done!

UNIFICATION ALGORITHM
--------------------------------------
Enter the first literal: abc(p,q)
Enter the second literal: abc(p,q)
Unification not needed. Both the predicate and literals are same!
//N-QUEENS PROBLEM //

PROGRAM CODE:

#include<iostream.h>
#include<stdio.h>
#include<conio.h>
#include<math.h>
int x[10],n,count=0;
unsigned char a[10][10];
void nqueens(int,int);

void main()
{
int i,j,k,n;
clrscr();
cout<<"\n\n N-QUEENS PROBLEM";
cout<<"\n\t Enter the number of queens:";
cin>>n;
nqueens(1,n);
cout<<"\t The number of solutions are:"<<count;
getch();
}

int place(int k,int i)


{
int j;
for(j=1;j<=k-1;j++)
{
if((x[j]==i)||abs(x[j]-i)==abs(j-k))
{
return(0);
}
}
return(1);
}
void nqueens(int k,int n)
{
int i,flag,j;
for(i=1;i<=n;i++)
for(j=1;j<=n;j++)
a[i][j]='-';
for(i=1;i<=n;i++)
{
flag=place(k,i);
if(flag==1)
{
x[k]=i;
if(k==n)
{
count++;
cout<<"\n\n";
cout<<"Solution"<<count<<":\n";
for(i=1;i<=n;i++)
{
for(j=1;j<=n;j++)
{
a[i][x[i]]='Q';
cout<<"\t"<<a[i][j];
}
cout<<"\n";
}
cout<<"\n";
}
else
nqueens(k+1,n);
}
}
}
SAMPLE INPUT AND OUTPUT:

N-QUEENS PROBLEM
Enter the number of queens:4
Solution1:
- Q - -
- - - Q
Q - - -
- - Q -
Solution2:
- - Q -
Q - - -
- - - Q
- Q - -

The number of solutions are:2


//EXPERT SYSTEM FOR MEDICAL DIAGNOSIS//

PROGRAM CODE:

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

struct expert
{
char *name;
int bit[17];
}p[17];

void comp();
void check(int);
int ques,ans[17];
char *rep;

void setrules()
{
p[1].name="Measles";
p[1].bit[1]=1;
p[1].bit[2]=1;
p[1].bit[3]=0;
p[1].bit[4]=1;
p[1].bit[5]=1;
p[1].bit[6]=1;
p[1].bit[7]=0;

p[2].name="German Measles";
p[2].bit[1]=1;
p[2].bit[2]=1;
p[2].bit[3]=1;
p[2].bit[4]=1;
p[2].bit[5]=0;
p[2].bit[6]=0;
p[2].bit[7]=0;

p[3].name="Flu";
p[3].bit[1]=1;
p[3].bit[2]=0;
p[3].bit[3]=1;
p[3].bit[4]=1;
p[3].bit[5]=1;
p[3].bit[6]=1;
p[3].bit[7]=1;

p[4].name="Mumps";
p[4].bit[1]=1;
p[4].bit[2]=0;
p[4].bit[3]=0;
p[4].bit[4]=0;
p[4].bit[5]=0;
p[4].bit[6]=0;
p[4].bit[7]=0;

p[5].name="Chicken pox";
p[5].bit[1]=1;
p[5].bit[2]=1;
p[5].bit[3]=0;
p[5].bit[4]=0;
p[5].bit[5]=0;
p[5].bit[6]=0;
p[5].bit[7]=1;

p[6].name="Typhoid";
p[6].bit[1]=1;
p[6].bit[2]=0;
p[6].bit[3]=0;
p[6].bit[4]=0;
p[6].bit[5]=0;
p[6].bit[6]=1;
p[6].bit[7]=0;

p[7].name="Cholera";
p[7].bit[1]=1;
p[7].bit[2]=0;
p[7].bit[3]=0;
p[7].bit[4]=0;
p[7].bit[5]=1;
p[7].bit[6]=1;
p[7].bit[7]=0;
}

void getinput()
{
cout<<"\nGuess the Disease!!!\n";
ques=1;

cout<<"Does the person have fever?";


cin>>rep;
check(ques);
ques++;

cout<<"Does the person have rash?";


cin>>rep;
check(ques);
ques++;

cout<<"Does the person have headache?";


cin>>rep;
check(ques);
ques++;

cout<<"Does the person have running nose?";


cin>>rep;
check(ques);
ques++;
cout<<"Does the person have conjunctivitis?";
cin>>rep;
check(ques);
ques++;

cout<<"Does the person have cough?";


cin>>rep;
check(ques);
ques++;

cout<<"Does the person have bodyache?";


cin>>rep;
check(ques);
ques++;
comp();
}

void check(int temp)


{
if(strcmp(rep,"y")==0)
ans[temp]=1;
else
ans[temp]=0;
return;
}

void comp()
{
int j,k,flag=0,flag1=0;
for(j=1;j<=7;j++)
{
for(k=1;k<=7;k++)
{
if(p[j].bit[k]==ans[k])
flag=1;
else
{
flag=0;
break;
}
}

if(flag==1)
{
flag1=1;
cout<<"The person is suffering from "<<p[j].name<< " disease!";
continue;
}
}
if(flag1==0)
cout<<"None of the symptoms matches the given condition";
}

void display()
{
cout<<"\n Expert sstem for medical diagnosis";
cout<<"\n The diseases are:";
cout<<"\nMeasles \n German measles \n Flu \n Mumps \n Chicken pox \n
Typhoid \n Cholera \n";
}

void main()
{
clrscr();
setrules();
display();
getinput();
getch();
}
SAMPLE INPUT AND OUTPUT:

EXPERT SYSTEM FOR MEDICAL DIAGNOSIS


The diseases are:
Measles
Germen measles
Flu
Mumps
Chicken Pox
Typhoid
Cholera

Guess the Disease!!!


Does the person have fever? y
Does the person have rash? y
Does the person have headache? n
Does the person have running nose? n
Does the person have conjunctivitis? n
Does the person have cough? n
Does the person have body ache? y

The person is suffering from Chicken Pox disease!


//EXPERT SYSTEM FOR FINDING A PERSONALITY//

PROGRAM CODE:

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

struct expert
{
char *name;
int bit[17];
}p[17];

void comp();
void check(int);
int ques,ans[17];
char *rep;

void setrules()
{
p[1].name="Kapil dev";
p[1].bit[1]=1;
p[1].bit[2]=1;
p[1].bit[3]=1;
p[1].bit[4]=0;
p[1].bit[5]=1;
p[1].bit[6]=0;
p[1].bit[7]=0;

p[2].name="Latha Mangeshkar";
p[2].bit[1]=1;
p[2].bit[2]=0;
p[2].bit[3]=1;
p[2].bit[4]=0;
p[2].bit[5]=0;
p[2].bit[6]=0;
p[2].bit[7]=1;

p[3].name="Amitab bachan";
p[3].bit[1]=1;
p[3].bit[2]=1;
p[3].bit[3]=0;
p[3].bit[4]=0;
p[3].bit[5]=0;
p[3].bit[6]=0;
p[3].bit[7]=1;
p[4].name="Sachin Tendulkar";
p[4].bit[1]=1;
p[4].bit[2]=1;
p[4].bit[3]=0;
p[4].bit[4]=0;
p[4].bit[5]=0;
p[4].bit[6]=1;
p[4].bit[7]=0;
p[5].name="Abdul Kalam";
p[5].bit[1]=1;
p[5].bit[2]=1;
p[5].bit[3]=1;
p[5].bit[4]=0;
p[5].bit[5]=1;
p[5].bit[6]=0;
p[5].bit[7]=0;
p[6].name="Shabana Azmi";
p[6].bit[1]=1;
p[6].bit[2]=0;
p[6].bit[3]=0;
p[6].bit[4]=0;
p[6].bit[5]=0;
p[6].bit[6]=0;
p[6].bit[7]=1;
p[7].name="Rahul Dravid";
p[7].bit[1]=1;
p[7].bit[2]=1;
p[7].bit[3]=0;
p[7].bit[4]=0;
p[7].bit[5]=0;
p[7].bit[6]=1;
p[7].bit[7]=1;
}

void getinput()
{
cout<<"\nGuess the Personality!!!\n";
ques=1;

cout<<"Is the personality alive?";


cin>>rep;
check(ques);
ques++;

cout<<"Is the person male?";


cin>>rep;
check(ques);
ques++;

cout<<"Is the person retired?";


cin>>rep;
check(ques);
ques++;

cout<<"Is the person politician?";


cin>>rep;
check(ques);
ques++;

cout<<"Is the person science?";


cin>>rep;
check(ques);
ques++;

cout<<"Is the person sports?";


cin>>rep;
check(ques);
ques++;

cout<<"Is the person film?";


cin>>rep;
check(ques);
ques++;
comp();
}

void check(int temp)


{
if(strcmp(rep,"y")==0)
ans[temp]=1;
else
ans[temp]=0;
return;
}

void comp()
{
int j,k,flag=0,flag1=0;
for(j=1;j<=7;j++)
{
for(k=1;k<=7;k++)
{
if(p[j].bit[k]==ans[k])
flag=1;
else
{
flag=0;
break;
}
}
if(flag==1)
{
flag1=1;
cout<<"The personality name is "<<p[j].name<<"";
continue;
}
}
if(flag1==0)
cout<<"None of the personality matches the given condition";
}
void display()
{
cout<<"\n The personalities are:";
cout<<"\n Kapil dev \n Latha Mangeshkar \n Amitab bachan \n sachin tendulkar
\n Abdul kalam \n Shabana Azmi \n Rahul dravid \n";
}

void main()
{
clrscr();
setrules();
display();
getinput();
getch();
}
SAMPLE INPUT AND OUTPUT:

EXPERT SYSTEM TO FIND A PERSONALITY


The personalities are:
Kapil Dev
Latha Mangeshkar
Amithab Bachan
Sachin Tendulkar
Abdul Kalam
Shabana Azmi
Rahul Dravid

Guess the personality!!!


Is the person alive? y
Is the person a male? y
Is the person retired? n
Is the person a politician? n
Is the person in sports? y
Is the person in science? n
Is the person in films? n

The personality is Sachin Tendulkar!


/*PREDICATE TO PROPOSITIONAL LOGIC*/

PROGRAM CODE:

#include<graphics.h>
#include<stdio.h>
#include<conio.h>
#include<string.h>
int conj(char *);
int noun(char *);
int article(char *);
int prep(char *);
int aux(char *);
void process(char *);
int length=0;
char ans[100];
void main()
{
char a[90],s[30];
int i,j;
clrscr();
printf("\nEnter the fact:");
gets(a);
i=0;
while((a[i]!='\n')&&(a[i]!='\0'))
{
j=0;
while((a[i]!=' ')&&(a[i]!='\0')&&(a[i]!='\n'))
{
s[j]=a[i];
i++;
j++;
}
while(a[i]==' ')
i++;
s[j]='\0';
printf("\n %s",s);
process(s);
}
printf("\n prepositional logic:%s",ans);
getch();
}

void process(char *s)


{
int i;
if(noun(s)||aux(s)||article(s)||prep(s)||(!strcmpi("if",s))||(!strcmpi("all",s))||
(!strcmpi("every",s)))
return;

else if(!strcmpi(s,"then"))
{
ans[length]='-';
length++;
}
else if(conj(s))
{
i=conj(s);
if(i==1)ans[length]='&';
if(i==2)ans[length]='|';
if(i==3)ans[length]='~';
length++;
}
else
{
i=0;
while(s[i]!='\0')
{
ans[length]=s[i];
length++;
i++;
}
ans[length]='\0';
}
}

int noun(char *s)


{
char pron[10][20]={"he","she","it","they"};
int i=0;
for(i=0;i<4;i++)
if(!strcmpi(pron[i],s))
return(1);
return(0);
}

int article(char *s)


{
char article[10][10]={"the","a","an"};
int i=0;
for(i=0;i<3;i++)
if(!strcmpi(article[i],s))
return(1);
return(0);
}

int prep(char *s)


{
char prep[10][10]={"to","on","in","at","by","from"};
int i=0;
for(i=0;i<6;i++)
if(!strcmpi(prep[i],s))
return(1);
return(0);
}

int aux(char *s)


{
char aux[20][20] = {"is","was","has","have","had","am","were","are",
"will","would","should","may","might"};
int i=0;
for(i=0;i<14;i++)
if(!strcmpi(aux[i],s))
return(1);
return(0);
}
int conj(char *s)
{
char log[4][20]={"the","a","an"};
int i=0;
for(i=0;i<4;i++)
if(!strcmpi(log[i],s))
return(1);
return(0);
}

SAMPLE INPUT AND OUTPUT:

PREDICATE TO PROPOSITION LOGIC


Enter the input in predicate form:
if it is raining then it is not sunny

if
it
not
raining
then
it
is
not
sunny

The prepositional logic is: raining->not sunny


//SYNTAX CHECKING OF ENGLISH SENTENCES-
ENGLISH GRAMMAR//

PROGRAM CODE:

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

int np(), vp(), np1(), n(), pn(), pro(), adj(), v();

void adjs();
static char sent[116];
char* st = sent;

int s()
{
if(np())
return vp();
return 0;
}

int np()
{
char the[10];
sscanf (st , "%s" , the);
if(!strcmp(the, "the"))
{
st+=strlen("the")+1;
return np1();
}

else if(pro())
return 1;

else if(pn())
return 1;

else if(np1())
return 1;
return 0;
}

int np1()
{
adjs();
return n();
}

void adjs()
{
if(adj())
{
adjs();
}
}

int vp()
{
if(v())
return 1;
if(n())
return np();
return 0;
}

int n()
{
char t[10];
sscanf(st , "%s" , t);
if(!(strcmp(t,"file")) || !(strcmp(t,"printer")))
{
st+=strlen(t)+1;
return 1;
}
return 0;
}

int pn()
{
char t[10];
sscanf(st, "%s" , t);
if( !(strcmp(t,"bill") ))
{
st+=strlen(t)+1;
return 1;
}
return 0;
}

int pro()
{
char t[10];
sscanf(st, "%s" , t);
if( !(strcmp(t,"I") ))
{
st+=strlen(t)+1;
return 1;
}
return 0;
}

int adj()
{
char t[10];
sscanf(st , "%s" , t);
if( !(strcmp(t,"short")) || !(strcmp(t,"fast")) || !(strcmp(t,"long")))
{
st+=strlen(t)+1;
return 1;
}
return 0;
}

int v()
{
char t[10];
sscanf(st , "%s" , t);
if( !(strcmp(t,"printed")) || !(strcmp(t,"created")) || !(strcmp(t,"want")))
{
st+=strlen(t)+1;
return 1;
}
return 0;
}

void main()
{
int i=0;

for(i=1 ; i<116 ; i++)


sent[i] = '\0';
clrscr();

cout<<"ENTER THE SENTENCE\n";


scanf ("%[^\n]" , sent);
getchar();
if(!s())
cout<<"\n GO AND LEARN GRAMMAR!!!";

else if(!st[0])
cout<<"\n YOUR SENTENCE IS CORRECT";
else
cout<<"THERE ARE EXTRA TOKENS IN YOUR SENTENCE";

getch();
}
SAMPLE INPUT AND OUTPUT:

SYNTAX CHECKING OF ENGLISH GRAMMAR


---------------------------------------------------------------
Enter the sentence: the file printed

Your sentence is correct!

SYNTAX CHECKING OF ENGLISH GRAMMAR


---------------------------------------------------------------
Enter the sentence: file created

Your sentence is correct!

SYNTAX CHECKING OF ENGLISH GRAMMAR


---------------------------------------------------------------
Enter the sentence: the fast printer

Go and learn grammar!


/*PREDICATE TO CLAUSE CONVERSION*/

PROGRAM CODE:

#include<stdio.h>
#include<conio.h>
#include<string.h>
char clause[15][50];
char token[15][15];
int N;

void main()
{
int i;
clrscr();
printf("\nPREDICATE TO CLAUSE CONVERSION");
printf("\nEnter the input in predicate conversion:");
split();
printf("\n");
printf("\n The clause form for the given predicate is:");
for(i=0;i<N;i++)
{
printf("\n %d: %s\n",i+1,clause[i]);
}
getch();
}

split()
{
FILE*fp;
char str[100];
int n;
fp=fopen("input.txt","r");
if(!fp)
printf("\nCannot open the input file!");
n=-1;
do
{
++n;
str[n]=fgetc(fp);
if(str[n]=='\n')
{
str[n]='\0';
printf("\n%s",str);
scan(str);
n=-1;
}
}while(str[n]!=EOF);
str[n]='\0';
printf("\n%s",str);
scan(str);
fclose(fp);
return 0;
}

void scan(char *ch)


{
int i=0,j=0,k=0;
while(ch[i])
{
token[j][k]=ch[i];
if(token[j][k]==' ')
{
token[j][k]='\0';
k=-1;
j++;
}
i++;
k++;
token[j][k]='\0';
}
for(k=0;k<=j;k++)
if(match(">",token[k]))
{
for(k=0;k<=j;)
{
if(!match(":",token[k]))
{
while(!match(">",token[k]))
{
replace(k);
k++;
}
token[k][0]='|';
token[k][1]='\0';
break;
}
k++;
}
break;
}
for(k=0;k<=j;k++)
{
if(match("&",token[k]))
{
N++;
k++;
}
if(!match(":",token[k]))
{
strcat(clause[N],token[k]);
strcat(clause[N]," ");
}
}
N++;
}

replace(int k)
{
if(match("&",token[k]))
{
token[k][0]='|';
token[k][1]='\0';
}
else if(match("|",token[k]))
{
token[k][0]='&';
token[k][1]='\0';
}
else
if(!remove_not(token[k]))
insert(token[k]);
return 5
}

int match(char *m1,char *m2)


{
int i=0,j=0,k=0;
int l=strlen(m1);
int l1=strlen(m1);
while(k<11)
{
if(i==j&&m2[k]==m1[i])
{
i++;
j++;
}
else
{
i=0;
j=0;
}
if(i==1)
return 1;
k++;
}
return 0;
}

int remove_not(char *m)


{
int i=0,j=0,flag=0;
int l=strlen(m);
for(i=0;i<1;i++)
{
m[j]=m[i];
if(m[i]!='7')
j++;
else
flag=1;
}
if(flag)
return 1;
return 0;
}

insert(char *m)
{
char *temp;
int i=0,j=1,flag=0;
int l=strlen(m);
temp=(char*)malloc(l);
strcpy(temp,m);
m[0]='7';
for(i=0;i<1;i++)
if(temp[i]!=' ')
{
m[j]=temp[i];
j++;
}
}

trim(char *s)
{
int i=0,j=0;
int l=strlen(s);
for(i=0;i<1;i++)
{
s[j]=s[i];
if(s[i]!=' ')
j++;
}
}
SAMPLE INPUT AND OUTPUT:

PREDICATE TO CLAUSE CONVERSION


Enter the input in predicate form:
#x: %y: person(x) | ruler(y) -> 7loyalto(x,y)
#x: pompean(x) -> roman(x)
man(marcus)
#x: %y: loyalto(x,y) & lucky(x)

The clause form for the given predicate is:


1: 7person(x)

2: 7ruler(y) | 7loyalto(x,y)

3: 7pompean(x) | roman(x)

4: man(marcus)

5: loyalto(x,y)

6: lucky(x)
STUDY OF PROLOG

AIM:

To study the general format of Prolog.

DOMAIN:

The notation of the domain corresponds exactly to that of type in typed programs.

PREDICATES:

A predicate is nothing but a procedure in PROLOG which is in a group of class


about the same relation.

Eg. : is-a-parent(X) : father-of (X,-1)


is-a-parent(X) : mother-of(X,-1)

Here the two classes together define the condition same one to be the parent together
they form a procedure.

GOALS:

A query to the PROLOG interpreter consists of one or more goals.

PRIMITIVE DATATYPES:

Variable and constants. Prolog includes integers(1,2,3,……), real numbers(1.2,2.3,


….) and characters(a,b,c…).
Names beginning with a lower case letter represent a character fact. Upper case
represents variables that may be unified with a fact during prolog execution. The scope
of a variable is the rule within which it is used.

STRUCTURED DATATYPES:

Objects may be atoms(strings, constants, and variable) or bits. A list is represented as


[A,B,C,…]. The notation [A/B] is used to indicate ‘A’ as the head of the list and ‘B’ is
used to indicate the tail of the list.

SEQUENCE CONTROL:

Prolog uses unification with backtracking. There is no need of real concept of local
or global environment. All the ways have local context. Because of the way unification
has been implemented a query such as q1,q2,q3….qn. First evaluate q1 and q2 and so on.
Rules are stored in the order they are being entered in the database.

EXPRESSIONS:

Arithmetic expressions +,-,*,/ are defined. Relations such as =-,<.>,<=,>= are also
defined. The equality operator = means ‘same as’. The operate ‘is’ means ‘evaluate’.

STATEMENTS:

Facts: Facts are relationships that are stated during a consult operation, which adds
new facts and rules to the database. There are n tuples f(a1,a2,a3,..an) and such states
the relationship among n arguments according to relationship f.

Rules: Rules are implications that are stored/started during a consult operation.

Syntax: Employed (X):- Employee(X,-)


Queries: Queries consists of a sequence of terms ending with a period, q1,q2,….qn.
Execution proceeds by unifyingq1, possibly by refining to its definition in the database
and if true, unifying q2 with its definition. If unification fact at any point lacks up to the
previous correct choice and tries another alternative, rules may be combined to make up
complex queries.

PROBLEM WITH NEGATION:

Negation is defined as:

not(X):= -X ! fail
not (-)

This is not the same as return true if X is false. If X is true, then not(X) will process X
as true. If X is false, then the first rule fails, but not(-) succeeds.

INPUT AND OUTPUT:

The default output of the defined variables is sufficient for writing out answers.
However a ‘write’ function also exists to write out any results.

Eg. : write(‘abc’)
- - - ->abc

The function (n1) writes out a new line.

STANDARD FUNCTION:

Most Prolog systems include built-in functions to aid program generation,


consult(filename) reads the file, filename and appends new facts and rules into the
database.
RESULT:

Thus the basic concepts of PROLOG were studied in detail.

Vous aimerez peut-être aussi