如何把它改一下结构,输出结果不变
#include <stdio.h>#include <string.h>
#include <math.h>
#define MAX(x, y) ((x)>(y)?(x):(y))
#define MIN(x, y) ((x)<(y)?(x):(y))
class FuzzySet_Guass
{
public:
double m_sigma;
double m_aver;
double m_border_left;
double m_border_right;
double m_points;
char m_name[300];
double *m_data;
void ApplyAggregationMethod(FuzzySet_Guass &set1)
{
for (int i = 0; i < m_points;i++ )
{
m_data[i] = MAX(m_data[i], set1.m_data[i]);
}
}
FuzzySet_Guass(double sigma, double aver, double border_left, double border_right, int points,char *name)
{
m_sigma = sigma;
m_aver = aver;
m_border_left = border_left;
m_border_right = border_right;
m_points = points;
//m_name = name;
strcpy(m_name, name);
m_data=new double[points];
double x = m_border_left;
for(int i=0;i <m_points;i++)
{
x = m_border_left + i * (m_border_right - m_border_left) / (m_points-1);
m_data[i]=Gauss(x, m_sigma, m_aver);
}
}
double FuzzifyInput(double accurate_input)
{
return Gauss(accurate_input, m_sigma, m_aver);
}
void ApplyImplicationMethod(double a)
{
for(int i=0;i <m_points;i++)
{
m_data[i] = MIN(a, m_data[i]);
}
}
double Defuzzify()
{
double numerator=0;
double denominator = 0;
double x=0;
for (int i = 0; i < m_points; i++ )
{
x = m_border_left + i * (m_border_right - m_border_left) / (m_points-1);
numerator += x * m_data[i];
denominator += m_data[i];
}
return numerator / denominator;
}
void PrintFuzzySetElements()
{
printf("Fuzzy Set --%s", m_name);
int i = 0;
for(i =0; i<m_points; i++)
{
printf("%f", m_data[i]);
}
printf("\n") ;
for (i = 0; i < m_points; i++)
{
double x = m_border_left + i * (m_border_right - m_border_left) / (m_points-1);
printf("i=%d, data=%f, x=%f" , i, m_data[i],x);
}
printf("\n") ;
}
double Gauss(double x, double sigma, double aver)
{
return pow(2.7182818284590451, -pow((x - aver) / sigma, 2)/2);
}
};
class Program
{
public:
double ApplyFuzzyOperation(double a,double b)
{
return MIN(a, b);
}
void Run()
{
while (true)
{
//模糊规则
//if e is E_NB and ec is EC_NB, then u is U_NB
//if e is E_PB and ec is EC_PB, then u is U_PB
//定义模糊集合
FuzzySet_Guass U_NB(3, -10, -10, 10, 1001, "U_NB");
FuzzySet_Guass U_PB(3, 10, -10, 10, 1001, "U_PB");
FuzzySet_Guass E_NB(3, -10, -10, 10, 1001, "E_NB");
FuzzySet_Guass E_PB(3, 10, -10, 10, 1001, "E_PB");
FuzzySet_Guass EC_NB(3, -10, -10, 10, 1001, "EC_NB");
FuzzySet_Guass EC_PB(3, 10, -10, 10, 1001, "EC_PB");
//输入误差e和误差变化率ec
printf("e=");
float input1 = 0.0;
scanf("%f", &input1);
float input2 = 0.0;
printf("ec=");
scanf("%f", &input2);
//step1
double e1 = E_NB.FuzzifyInput(input1);
double ec1 = EC_NB.FuzzifyInput(input2);
printf("e1=%e, ec1=%e\n", e1, ec1);
//step2
double a1=ApplyFuzzyOperation(e1, ec1);
printf("a1=%e\n", a1);
//step3
U_NB.ApplyImplicationMethod(a1);//被改变的U_NB
//step1
double e2 = E_PB.FuzzifyInput(input1);
double ec2 = EC_PB.FuzzifyInput(input2);
//step2
double a2 = ApplyFuzzyOperation(e2, ec2);
//step3
U_PB.ApplyImplicationMethod(a2);//被改变的U_PB
//step4
U_PB.ApplyAggregationMethod(U_NB);//被改变的U_PB
//step5
double u = U_PB.Defuzzify();
printf("u=%e\n", u);
}
}
};
void main()
{
Program pp;
pp.Run();
}