⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 模糊控制dlg.cpp

📁 设计算法求出模糊控制表
💻 CPP
字号:
// 模糊控制Dlg.cpp : implementation file
//

#include "stdafx.h"
#include "模糊控制.h"
#include "模糊控制Dlg.h"

#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif

/////////////////////////////////////////////////////////////////////////////
// CAboutDlg dialog used for App About
static double cc=0;
class CAboutDlg : public CDialog
{
public:
	CAboutDlg();

// Dialog Data
	//{{AFX_DATA(CAboutDlg)
	enum { IDD = IDD_ABOUTBOX };
	//}}AFX_DATA

	// ClassWizard generated virtual function overrides
	//{{AFX_VIRTUAL(CAboutDlg)
	protected:
	virtual void DoDataExchange(CDataExchange* pDX);    // DDX/DDV support
	//}}AFX_VIRTUAL

// Implementation
protected:
	//{{AFX_MSG(CAboutDlg)
	//}}AFX_MSG
	DECLARE_MESSAGE_MAP()
};

CAboutDlg::CAboutDlg() : CDialog(CAboutDlg::IDD)
{
	//{{AFX_DATA_INIT(CAboutDlg)
	//}}AFX_DATA_INIT
}

void CAboutDlg::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
	//{{AFX_DATA_MAP(CAboutDlg)
	//}}AFX_DATA_MAP
}

BEGIN_MESSAGE_MAP(CAboutDlg, CDialog)
	//{{AFX_MSG_MAP(CAboutDlg)
		// No message handlers
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CMyDlg dialog

CMyDlg::CMyDlg(CWnd* pParent /*=NULL*/)
	: CDialog(CMyDlg::IDD, pParent)
{
	//{{AFX_DATA_INIT(CMyDlg)
	m_z = _T("");
	//}}AFX_DATA_INIT
	// Note that LoadIcon does not require a subsequent DestroyIcon in Win32
	m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
}

void CMyDlg::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
	//{{AFX_DATA_MAP(CMyDlg)
	DDX_Control(pDX, IDC_EDIT2, m_y_Edit);
	DDX_Control(pDX, IDC_EDIT1, m_x_Edit);
	DDX_Text(pDX, IDC_EDIT3, m_z);
	//}}AFX_DATA_MAP
}

BEGIN_MESSAGE_MAP(CMyDlg, CDialog)
	//{{AFX_MSG_MAP(CMyDlg)
	ON_WM_SYSCOMMAND()
	ON_WM_PAINT()
	ON_WM_QUERYDRAGICON()
	ON_BN_CLICKED(IDC_BUTTON1, OnButton1)
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CMyDlg message handlers

BOOL CMyDlg::OnInitDialog()
{
	CDialog::OnInitDialog();

	// Add "About..." menu item to system menu.

	// IDM_ABOUTBOX must be in the system command range.
	ASSERT((IDM_ABOUTBOX & 0xFFF0) == IDM_ABOUTBOX);
	ASSERT(IDM_ABOUTBOX < 0xF000);

	CMenu* pSysMenu = GetSystemMenu(FALSE);
	if (pSysMenu != NULL)
	{
		CString strAboutMenu;
		strAboutMenu.LoadString(IDS_ABOUTBOX);
		if (!strAboutMenu.IsEmpty())
		{
			pSysMenu->AppendMenu(MF_SEPARATOR);
			pSysMenu->AppendMenu(MF_STRING, IDM_ABOUTBOX, strAboutMenu);
		}
	}

	// Set the icon for this dialog.  The framework does this automatically
	//  when the application's main window is not a dialog
	SetIcon(m_hIcon, TRUE);			// Set big icon
	SetIcon(m_hIcon, FALSE);		// Set small icon
	
	// TODO: Add extra initialization here
	
	return TRUE;  // return TRUE  unless you set the focus to a control
}

void CMyDlg::OnSysCommand(UINT nID, LPARAM lParam)
{
	if ((nID & 0xFFF0) == IDM_ABOUTBOX)
	{
		CAboutDlg dlgAbout;
		dlgAbout.DoModal();
	}
	else
	{
		CDialog::OnSysCommand(nID, lParam);
	}
}

// If you add a minimize button to your dialog, you will need the code below
//  to draw the icon.  For MFC applications using the document/view model,
//  this is automatically done for you by the framework.

void CMyDlg::OnPaint() 
{
	if (IsIconic())
	{
		CPaintDC dc(this); // device context for painting

		SendMessage(WM_ICONERASEBKGND, (WPARAM) dc.GetSafeHdc(), 0);

		// Center icon in client rectangle
		int cxIcon = GetSystemMetrics(SM_CXICON);
		int cyIcon = GetSystemMetrics(SM_CYICON);
		CRect rect;
		GetClientRect(&rect);
		int x = (rect.Width() - cxIcon + 1) / 2;
		int y = (rect.Height() - cyIcon + 1) / 2;

		// Draw the icon
		dc.DrawIcon(x, y, m_hIcon);
	}
	else
	{
		CDialog::OnPaint();
	}
}

// The system calls this to obtain the cursor to display while the user drags
//  the minimized window.
HCURSOR CMyDlg::OnQueryDragIcon()
{
	return (HCURSOR) m_hIcon;
}



//形成控制表
int CMyDlg::contorol(double X, double Y)//X和Y分别对e和差值e的模糊化表
{ int x,y;
	if (X>4)
	{
		x=4;
	} 
	else if (X<-4)
	{
		x=-4;
	} 
	else
	{
		x=int(X);
	}
	if (Y>2)
	{
		y=2;
	} 
	else if (Y<-2)
	{
		y=-2;
	} 
	else
	{
		y=int(Y);
	}
	
//e的模糊化
double PBe[9],PSe[9],Ze[9],NSe[9],NBe[9];
int i=0;
for ( i=0;i<9;i++)
{
	PBe[i]=0;PSe[i]=0;Ze[i]=0;NSe[i]=0;NBe[i]=0;
}
PBe[7]=0.5;PBe[8]=1;
PSe[5]=0.5;PSe[6]=1;PSe[7]=0.5;
Ze[3]=0.5;Ze[4]=1;Ze[5]=0.5;
NSe[1]=0.5;NSe[2]=1;NSe[3]=0.5;
NBe[0]=1;NBe[1]=0.5;NBe[2]=0.5;

//差值 e 的模糊化
int PBde[5],PSde[5],Zde[5],NSde[5],NBde[5];
for ( i=0;i<=4;i++)
{
	PBde[i]=0;PSde[i]=0;Zde[i]=0;NSde[i]=0;NBde[i]=0;
}
PBde[4]=1;PSde[3]=1;Zde[2]=1;NSde[1]=1;NBde[0]=1;

//u 的模糊化
double PBu[7],PSu[7],Zu[7],NSu[7],NBu[7];
for ( i=0;i<7;i++)
{
	PBu[i]=0;PSu[i]=0;Zu[i]=0;NSu[i]=0;NBu[i]=0;
}
PBu[5]=0.5;PBu[6]=1;
PSu[4]=1;PSu[5]=0.5;
Zu[2]=0.5;Zu[3]=1;Zu[4]=0.5;
NSu[1]=1;NSu[2]=0.5;
NBu[0]=1;NBu[1]=0.5; 



	int de=0,flag=-1;//de不为0的项的模糊数 flag为项的标志,  对差值e 即规则B
	//令NB,NS,Z,PS,PB分别为0,1,2,3,4
	if(PBde[y+2]) {de=0;flag=4;}
	else if(PSde[y+2]) {de=1;flag=3;}
	else if(Zde[y+2])  {de=1;flag=2;}
	else if(NSde[y+2]) {de=1;flag=1;}
	else if(NBde[y+2]) {de=1;flag=0;}

	double f=0,e=0,dai[3][3]={0};//不为0的项的模糊数, 对e 即规则 A
    int q=0; 
	   if(PBe[x+4])
	   { 
		e=PBe[x+4];
		   if(flag>-1)
		{ 
			f=Fetch(4,flag);dai[q][0]=e;dai[q][1]=de;dai[q][2]=f;q++;
		}
	   }

	   if(PSe[x+4])
	   {
		   e=PSe[x+4];
           if(flag>-1)
		   { 
			   f=Fetch(3,flag);dai[q][0]=e;dai[q][1]=de;dai[q][2]=f;q++;
		   }
	   }
	if(Ze[x+4])  
	{
		e=Ze[x+4];
	    if(flag>-1)
		{f=Fetch(2,flag);dai[q][0]=e;dai[q][1]=de;dai[q][2]=f;q++;}
	}
	if(NSe[x+4]) 
	{
		e=NSe[x+4];
		if(flag>-1) 
		{f=Fetch(1,flag);dai[q][0]=e;dai[q][1]=de;dai[q][2]=f;q++;}
	}
	if(NBe[x+4])
	{
		e=NBe[x+4];
		if(flag>-1){f=Fetch(0,flag);dai[q][0]=e;dai[q][1]=de;dai[q][2]=f;q++;}
	}
	//求出C撇  至多为3个
	double c[7],c1[7],c2[7],c3[7];
	for (i=0;i<7;i++)
	{
      c[i]=0;c1[i]=0;c2[i]=0,c3[i]=0;
	}

	for (i=0;i<7;i++)
	{
    	if(dai[0][2]==0)  cc=NBu[i];
		if(dai[0][2]==1)  cc=NSu[i]; 
		if(dai[0][2]==2)  cc=Zu[i];
		if(dai[0][2]==3)  cc=PSu[i];
		if(dai[0][2]==4)  cc=PBu[i];
        c1[i]=minimum(dai[0][0],dai[0][1],cc);
		c2[i]=minimum(dai[1][0],dai[1][1],cc);
		c3[i]=minimum(dai[2][0],dai[2][1],cc);
	}

	for (i=0;i<7;i++)
	{
		c[i]=maximum(c1[i],c2[i],c3[i]);
	}
//重心法算出Z
	double g=0,p=0;
	for (i=0;i<7;i++)
	{
		if(c[i])
		{
			g+=c[i]*(i-3);
			p+=c[i];
		} 
	}
	int z=0; double z1=0,z2=0;//计算权值
	z=int(g/p); z1=g/p; z2=z1-z;
	if(z2>0.5&&z2<1) 
		z=z+1;
	

return z;
}



	//查找有效规则表

int CMyDlg::Fetch(int a, int b)  //规则 C
{
	
//规则表确定
//令NB,NS,Z,PS,PB分别为0,1,2,3,4
int G[5][5]={4,4,3,2,0,
             4,4,3,1,0,
			 4,3,2,1,0,
			 4,3,1,0,0,
			 4,2,1,0,0};
   return G[a][b];
}



void CMyDlg::OnButton1() 
{
	// TODO: Add your control notification handler code here
	UpdateData(TRUE);
	char cstrItem1[10], cstrItem2[10];
	double dfItem1,dfItem2;
	int dfResult;
	char cBuffer[50];
	m_x_Edit.GetWindowText(cstrItem1,10);
	m_y_Edit.GetWindowText(cstrItem2,10);
	dfItem1=atof((LPCTSTR)cstrItem1);
	dfItem2=atof((LPCTSTR)cstrItem2);
	dfResult=contorol(dfItem1, dfItem2);
	_gcvt(dfResult,10,cBuffer);
	m_z=(LPCTSTR)cBuffer;
	UpdateData(FALSE);

}



double CMyDlg::minimum(double a, double b, double c)
{
  return min(min(a,b),c);
}



double CMyDlg::maximum(double a, double b, double c)
{
   return max(max(a,b),c); 
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -