Who Am I?

A.First Edition
This is the  first edition of another deduction example. It is really a boring routine to do this kind of 
repeating job. But for me, it is a kind of fun.
B.Idea of program
1¡£ Basic idea: 
Same as old silly game: a set of propositions, a set of rules and a set of facts. Then using the analysing
method to verify the truth value of each propositions according to rules.
2¡£ Program design: 
I found a need for "Exclusive Or" and try to overload operators for it. But soon give up as it will this 
new operator involves a lot of job of my "Logic Library" and I would rather not risk to change it since I
almost forgot all what it is doing after a couple of months. Also a noticeable events is that my Logic class 
fail to deduct "Exclusive Or" in form of ["A ==> not B" and "B==> not A"]. Similar form of ["A and B" or "not A 
and not B"] is also not working. The only form is ["A or B" and "not (A and B)"]. It is quite interesting and 
implies some omission in my class. 
 
4¡£ Further improvement£º
To add a function of "Exclusive Or" in LogicLib.lib in near future. And check out why equivalent proposition of
"Exclusive Or" does not work.	
C. What is the problem?
x is an animal and following is some facts and rules:
1. Any long-nosed mammal is either an ant-eater or elephant.
2. Elephant is huge.
3. Ant-eaters are small.
4. Indian elephants are small-eared.
5, Nairobi is in Africa.
6. Any non-Indian elephant is African.
7. X has a long nose.
8. x lives in Nairobi.
9. x is a mammal.
10. x is huge.
11. x has large ears.
Then Who am I?
There is some underlined proposition that we, human, assume without explicitly mention it. They are 
1. If it is in Africa, then it cannot be in India. verse visa.
2. If it is huge, it cannot be small. verse visa.
3. If it is ant-eater, it cannot be elephant. verse visa.
4. If it is large-eared, it cannot be small-eared. verse visa.
¡¡
	
#include <iostream>
#include "LogicLib.h"

using namespace std;


Logic logic[11];
enum Proposition
	{Long_nosed, Mammal, Ant_eater, Elephant, Huge, Small,
	Indian, African, Nairobi, Large_ear, Small_ear};

char* logicName[11] = {"Long_nosed", "Mammal", "Ant_Eater", "Elephant", "Huge", "Small",
	"Indian", "African", "Nairobi", "Large_ear", "Small_ear"};

Logic& rules1()
{
	return logic[Long_nosed] >>(logic[Ant_eater]||logic[Elephant]);
}

Logic& rules2()
{
	return logic[Elephant]>>logic[Huge];
}

Logic& rules3()
{
	return logic[Ant_eater]>>logic[Small];
}

Logic& rules4()
{
	return (logic[Indian]&&logic[Elephant])>>logic[Small_ear];
}

Logic& rules5()
{
	return logic[Nairobi]>>logic[African];
}

Logic& rules6()
{
	return (!(logic[Indian]&&logic[Elephant]))>>logic[African];
}

Logic& extraRules1()
{
	return (logic[Huge]||logic[Small])&&(!(logic[Huge]&&logic[Small]));
}

Logic& extraRules2()
{
	return (logic[Large_ear]||logic[Small_ear])&&(!(logic[Large_ear]&&logic[Small_ear]));
}

Logic& extraRules3()
{
	return (logic[Ant_eater]||logic[Elephant])&&((!(logic[Ant_eater])&&logic[Elephant]));
}

void initialize()
{
	for (int i=0; i< 11; i++)
	{
		logic[i].setExpress(logicName[i]);
	}
	logic[Long_nosed].setState(Positive);
	logic[Nairobi].setState(Positive);
	logic[Mammal].setState(Positive);
	logic[Huge].setState(Positive);
	logic[Large_ear].setState(Positive);
}

void display()
{
	for (int i =0; i< 11; i++)
	{
		cout<<logic[i].getExpress()<<" is ";
		switch (logic[i].getState())
		{
		case Positive:
			cout<<"True";
			break;
		case Negative:
			cout<<"False";
			break;
		default:
			cout<<"Unknown";
			break;
		}
		cout<<endl;
	}
}

Logic& (*ruleArray[9])() = {rules1, rules2, rules3, rules4, rules5, rules6,
		extraRules1, extraRules2, extraRules3};

int main()
{
	int j=0;
	initialize();
	do 
	{
		for (int i=0; i<9; i++)
		{
			ruleArray[i]().doAnalysing();
		}
		j++;
	}
	while (j==3);
	display();



	return 0;
}






¡¡

This is what my program found out:

Long_nosed is True
Mammal is True
Ant_Eater is False
Elephant is True
Huge is True
Small is False
Indian is False
African is True
Nairobi is True
Large_ear is True
Small_ear is False

Or in English it is said:" I am a long-nosed mammal and I live in Nairobi in Africa. I am a large-eared elephant. Obviously I am not an ant-eater, not small, not small-eared and not in India. Can you imagine what I am?"


¡¡

                                                        back.gif (341 bytes)       up.gif (335 bytes)         next.gif (337 bytes)

Hosted by www.Geocities.ws

1