Click here to Skip to main content
15,888,205 members
Please Sign up or sign in to vote.
1.00/5 (2 votes)
C++
// FuzzyLogic_TempCompare.cpp : Defines the entry point for the console application.
//

#include "stdafx.h"
#include <iostream>
#include <cmath>
#include <cstring>

const double cdMinimumTemp = -20.0f;
const double cdMaximumTemp = 150.0f;

using namespace std;

class CFuzzyFunction
{
protected:
	double dLeft, dRight;
	char   cType;
	char*  sName;

public: 
	CFuzzyFunction(){};
	virtual ~CFuzzyFunction(){ delete[] sName; sName = NULL; }

	virtual void
		setInterval(double l, double r)
		{
			dLeft = l; dRight = r;
		}
	
	virtual void setMiddle(double dL = 0, double dR = 0) = 0;

	virtual void setType(char c)
		{
			cType = c;
		}

	virtual void setName(const char* s)
		{
			sName = new char[strlen(s) + 1];
			strcpy(sName, s);
		}

	bool isDotInInterval(double t)
		{
			if ((t >= dLeft) && (t <= dRight)) return true; else return false;
		}

	char getType(void)const{ return cType; }

	void getName() const
		{
			cout << sName << endl;
		}

	virtual double getValue(double t) = 0;
};

class CTriangle : public CFuzzyFunction
{
private:
	double dMiddle;

public:
	void setMiddle(double dL, double dR)
		{
			dMiddle = dL;
		}

	double getValue(double t)
		{
			if (t <= dLeft)
				return 0;
			else if (t<dmiddle)
				return (t - dLeft) / (dMiddle - dLeft);
			else if (t == dMiddle)
				return 1.0;
			else if (t<dright)
				return (dRight - t) / (dRight - dMiddle);
			else
				return 0;
		}
};

class CTrapezoid : public CFuzzyFunction
{
private:
	double dLeftMiddle, dRightMiddle;

public:
	void setMiddle(double dL, double dR)
		{
			dLeftMiddle = dL; dRightMiddle = dR;
		}

	double getValue(double t)
		{
			if (t <= dLeft)
				return 0;
			else if (t<dleftmiddle)
				return (t - dLeft) / (dLeftMiddle - dLeft);
			else if (t <= dRightMiddle)
				return 1.0;
			else if (t<dright)
				return (dRight - t) / (dRight - dRightMiddle);
			else
				return 0;
		}
};

int
main(void)
{
	CFuzzyFunction *FuzzySet[4];

	FuzzySet[0] = new CTrapezoid;
	FuzzySet[1] = new CTriangle;
	FuzzySet[2] = new CTriangle;
	FuzzySet[3] = new CTrapezoid;

	FuzzySet[0]->setInterval(-10, 40);
	FuzzySet[0]->setMiddle(0, 30);
	FuzzySet[0]->setType('r');
	FuzzySet[0]->setName("Cold");

	FuzzySet[1]->setInterval(20, 70);
	FuzzySet[1]->setMiddle(30, 60);
	FuzzySet[1]->setType('t');
	FuzzySet[1]->setName("Cool");

	FuzzySet[2]->setInterval(50, 100);
	FuzzySet[2]->setMiddle(60, 90);
	FuzzySet[2]->setType('t');
	FuzzySet[2]->setName("Warm");

	FuzzySet[3]->setInterval(80, 120);
	FuzzySet[3]->setMiddle(90, 120);
	FuzzySet[3]->setType('r');
	FuzzySet[3]->setName("Hot");

	double dValue;
	do
	{
		cout << "\nInput temperature to fuzzify->"; cin >> dValue; cout << endl;

		if (dValue<cdminimumtemp) continue;
		if (dValue>cdMaximumTemp) continue;

		for (int i = 0; i<4; i++)
		{

			cout << "For "; FuzzySet[i]->getName();

			cout << "The membership = ";

			cout << FuzzySet[i]->getValue(dValue); cout << endl; cout << endl;

		}

	} while (true);

	return EXIT_SUCCESS;
}


What I have tried:

Now, to be clear, this code is from Intro to Fuzzy Logic with C++ Fuzzy Set Calculator Example Program[^] and it works. Everything from the "int main()" down I understand/modified for my own class project. What I want to understand or for someone to breakdown for me is how the code under "class CTrapezoid" and "class CTriangle" work?

Thanks.
Posted
Updated 1-Mar-16 15:10pm
v3
Comments
Matt T Heffron 1-Mar-16 21:12pm    
Maybe you should ask where you got the code!
Sergey Alexandrovich Kryukov 1-Mar-16 22:14pm    
This main point is not even that. Paul (OriginalGriff) has his usual way of explaining why "explaining the code" is just counter-productive; see also my comment below, where I suggest the inquirer what will be better to do.
—SA
Arthur V. Ratz 21-Mar-16 2:17am    
5.
Sergey Alexandrovich Kryukov 1-Mar-16 22:12pm    
Explanation of code is simply counter-productive. Write your own code, ask a question if you have a problem, and so on. In this case, you will be explaining what are you trying to do with some part of this code, and this could be really productive, because you are the one who knows what you want to achieve.
—SA
Richard MacCutchan 2-Mar-16 4:45am    
You can step through the code, either visually or in the debugger to see how they work.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900