Demonstration of a simple program for interpreting AI rules (C ++)

Source: Internet
Author: User

# Include "iostream. H"
# Include "stdio. H"
# Include "conio. H"
# Include "string. H"

Typedef struct rule // The first rule repository
{
Char condition [6] [20];
Char result [20];
Int cnum;
Bool used;
} Rule;

Typedef struct fact // defines the inventory Library
{
Char item [20] [20];
Int fnum;
} Fact;

Rule R [2, 100];
Fact F;

Void create_rules (); // initialize the rule repository
Void create_facts (); // initialize the producer Library
Int get_rulesnum ();
Void ratiocinate (int n );

Void main ()
{
Int rnum = 0;
Create_rules ();
Create_facts ();
Rnum = get_rulesnum ();
// Cout <rnum <Endl;
Ratiocinate (rnum );

}

Void create_rules ()
{
Strcpy (R [0]. condition [0], "milk ");
Strcpy (R [0]. result, "Mammal ");
R [0]. cnum = 1;
R [0]. Used = false;
 
Strcpy (R [1]. condition [0], "hairy ");
Strcpy (R [1]. result, "Mammal ");
R [1]. cnum = 1;
R [1]. Used = false;

Strcpy (R [2]. condition [0], "Feather ");
Strcpy (R [2]. result, "bird ");
R [2]. cnum = 1;
R [2]. Used = false;

Strcpy (R [3]. condition [0], "Flying ");
Strcpy (R [3]. condition [1], "egg ");
Strcpy (R [3]. result, "bird ");
R [3]. cnum = 2;
R [3]. Used = false;

Strcpy (R [4]. condition [0], "Mammal ");
Strcpy (R [4]. condition [1], "catch ");
Strcpy (R [4]. condition [2], "toothed ");
Strcpy (R [4]. condition [3], "eye forward view ");
Strcpy (R [4]. result, "carnivals ");
R [4]. cnum = 4;
R [4]. Used = false;

Strcpy (R [5]. condition [0], "Mammal ");
Strcpy (R [5]. condition [1], "meat ");
Strcpy (R [5]. result, "carnivals ");
R [5]. cnum = 2;
R [5]. Used = false;

Strcpy (R [6]. condition [0], "Mammal ");
Strcpy (R [6]. condition [1], "hoof ");
Strcpy (R [6]. result, "hoof animal ");
R [6]. cnum = 2;
R [6]. Used = false;

Strcpy (R [7]. condition [0], "hoof animals ");
Strcpy (R [7]. condition [1], "repeat ");
Strcpy (R [7]. result, "hoof animal ");
R [7]. cnum = 2;
R [7]. Used = false;

Strcpy (R [8]. condition [0], "carnivals ");
Strcpy (R [8]. condition [1], "Yellow Brown ");
Strcpy (R [8]. condition [2], "black striped ");
Strcpy (R [8]. result, "Tiger ");
R [8]. cnum = 3;
R [8]. Used = false;

Strcpy (R [9]. condition [0], "carnivals ");
Strcpy (R [9]. condition [1], "Yellow Brown ");
Strcpy (R [9]. condition [2], "Black Spot ");
Strcpy (R [9]. result, "leopard ");
R [9]. cnum = 3;
R [9]. Used = false;

Strcpy (R [10]. condition [0], "hoof animals ");
Strcpy (R [10]. condition [1], "long leg ");
Strcpy (R [10]. condition [2], "Long neck ");
Strcpy (R [10]. condition [3], "Dark Spot ");
Strcpy (R [10]. condition [4], "Yellow Brown ");
Strcpy (R [10]. result, "giraffe ");
R [10]. cnum = 5;
R [10]. Used = false;

Strcpy (R [11]. condition [0], "hoof animals ");
Strcpy (R [11]. condition [1], "White ");
Strcpy (R [11]. condition [2], "black striped ");
Strcpy (R [11]. result, "ZEBRA ");
R [11]. cnum = 3;
R [11]. Used = false;

Strcpy (R [12]. condition [0], "bird ");
Strcpy (R [12]. condition [1], "No Fly ");
Strcpy (R [12]. condition [2], "long leg ");
Strcpy (R [12]. condition [3], "Long neck ");
Strcpy (R [12]. condition [4], "Black and White ");
Strcpy (R [12]. result, "Ostrich ");
R [12]. cnum = 5;
R [12]. Used = false;

Strcpy (R [13]. condition [0], "bird ");
Strcpy (R [13]. condition [1], "No Fly ");
Strcpy (R [13]. condition [2], "swimming ");
Strcpy (R [13]. condition [3], "Black and White ");
Strcpy (R [13]. result, "Penguin ");
R [13]. cnum = 4;
R [13]. Used = false;

Strcpy (R [14]. condition [0], "bird ");
Strcpy (R [14]. condition [1], "Flying ");
Strcpy (R [14]. condition [2], "not afraid of wind and waves ");
Strcpy (R [14]. result, "Haiyan ");
R [14]. cnum = 3;
R [14]. Used = false;
}

Void create_facts ()
{
Strcpy (F. item [0], "with hair ");
Strcpy (F. item [1], "hoof ");
Strcpy (F. item [2], "long leg ");
Strcpy (F. item [3], "Long neck ");
Strcpy (F. item [4], "Dark Spot ");
Strcpy (F. item [5], "Yellow Brown ");
F. fnum = 6;
}

Int get_rulesnum ()
{
Int num = 0;
Int I = 0;
While (1)
{
If (R [I]. condition [0] [0] = '\ 0 ')
{
Break;
}
I ++;
Num ++;
}
Return num;
}

Void ratiocinate (int n)
{
Bool findfact (char * Str );
Void insertintofact (char * Str );

Int I = 0, J; // I is used to control the number of query rules, and J is used to control the number of query rules.
While (1)
{
If (R [I]. Used = false)
{
For (j = 0; j <R [I]. cnum; j ++)
{
If (findfact (R [I]. condition [J])
{
Continue;
}
Else
{
Cout <"rule" <I + 1 <"matching failed" <Endl;
Break;
}
}
}
If (j = R [I]. cnum) // if all the front parts meet
{
R [I]. Used = true;
Cout <"rule" <I + 1 <"matched successfully, and the conclusion is inserted to the rule library" <Endl;
If (! Findfact (R [I]. Result ))
{
Insertintofact (R [I]. Result); // insert facts
Cout <"the new fact is:" <F. item [F. Fnum-1] <Endl;
}
I = 0;
// Continue;
}
Else I ++; // view the next rule
If (I = N)
{
Cout <"you are not looking for a qualified animal. Please add a new rule" <Endl;
Break;
}
Else if (strcmp (F. item [F. fnum-1], "Tiger") = 0 | strcmp (F. item [F. fnum-1], "leopard") = 0 | strcmp (F. item [F. fnum-1], "giraffe") = 0 | strcmp (F. item [F. fnum-1], "ZEBRA") = 0 | strcmp (F. item [F. fnum-1], "Ostrich") = 0 | strcmp (F. item [F. fnum-1], "Penguin") = 0 | strcmp (F. item [F. fnum-1], "Haiyan") = 0)
{
Cout <"animals that match the facts you give:" <F. item [F. Fnum-1] <Endl;
Break;
}
}

}

// Check whether the fact in the inventory database is equal to or not to the front file. If this fact exists, the new conclusion will not be added to the inventory database.
Bool findfact (char * Str)
{
Int I;
For (I = 0; I <F. fnum; I ++)
{
If (strcmp (F. item [I], STR) continue;
Else return true;
}
Return false;

}

// Insert new facts
Void insertintofact (char * Str)
{
Strcpy (F. item [F. fnum], STR );
F. fnum ++;
}

It is a very simple function, mainly to reflectSimple demonstration andAlgorithm!

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.