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

📄 testview.cpp

📁 用C++写的函数逼近的例子!可以设置步长
💻 CPP
字号:
// testView.cpp : implementation of the CTestView class
//

#include "stdafx.h"
#include "test.h"

#include "testDoc.h"
#include "testView.h"

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

/////////////////////////////////////////////////////////////////////////////
// CTestView

IMPLEMENT_DYNCREATE(CTestView, CView)

BEGIN_MESSAGE_MAP(CTestView, CView)
	//{{AFX_MSG_MAP(CTestView)
	ON_COMMAND(ID_MENU_DISP, OnMenuDisp)
	ON_WM_KEYDOWN()
	ON_UPDATE_COMMAND_UI(ID_MENU_DISP, OnUpdateMenuDisp)
	ON_UPDATE_COMMAND_UI(ID_MENU_LEARN, OnUpdateMenuLearn)
	ON_UPDATE_COMMAND_UI(ID_MENU_TEST, OnUpdateMenuTest)
	//}}AFX_MSG_MAP
	// Standard printing commands
	ON_COMMAND(ID_FILE_PRINT, CView::OnFilePrint)
	ON_COMMAND(ID_FILE_PRINT_DIRECT, CView::OnFilePrint)
	ON_COMMAND(ID_FILE_PRINT_PREVIEW, CView::OnFilePrintPreview)
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CTestView construction/destruction

CTestView::CTestView()
{
	// TODO: add construction code here
	e_value = NULL;
	m_strWait = _T("Learning... (Press a key will stop it!)");
	num = 0;
	method = 0;
	max = 0.0;
	min = 0.0;
}

CTestView::~CTestView()
{
	if (e_value != NULL)
	{
		for (int x=0; x<method; x++)
		{
			delete [] e_value[x];
		}
		delete [] e_value;
	}
}

BOOL CTestView::PreCreateWindow(CREATESTRUCT& cs)
{
	// TODO: Modify the Window class or styles here by modifying
	//  the CREATESTRUCT cs

	return CView::PreCreateWindow(cs);
}

/////////////////////////////////////////////////////////////////////////////
// CTestView drawing

void CTestView::OnDraw(CDC* pDC)
{
	CTestDoc* pDoc = GetDocument();
	ASSERT_VALID(pDoc);
	// TODO: add draw code for native data here
	CRect rect;
	GetClientRect(&rect);

	int dm = pDoc->dispmode;
	if (dm ==1)
	{
		TEXTMETRIC tm;
		pDC->GetTextMetrics(&tm);
		double error = pDoc->max_error_tollerance;
		int ptn = pDoc->selpattern, bin;
		TRACE("\nPTN=%d\n",ptn);
		int outnum = pDoc->output_array_size;
		int innum = pDoc->input_array_size;
		TRACE("\ninnum=%d\n",innum);
		int nLineHeight = tm.tmHeight + tm.tmExternalLeading;
		CString str1,str2;
		CPoint pText(0,0);

		str1.Format("输入:");
		for (int y=0; y<innum; y++)
		{
			str2.Format("%.0f ",pDoc->input[ptn][y]);
			TRACE("pDoc->input[%d][%d]=%.0f\n",ptn,y,pDoc->input[ptn][y]);
			str1 += str2;
		}
		pDC->TextOut(pText.x,pText.y,str1);
		pText.y += nLineHeight;

		for (int i=0; i<outnum; i++)
		{
			if ((1-pDoc->output[ptn][i])<error)
			{
				bin = 1;
			}
			else
			{
				if (pDoc->output[ptn][i]<error)
				{
					bin = 0;
				}
				else
				{
					bin = -1;
				}
			}
			str2.Format("输出:%d  实际值:%f",bin,pDoc->output[ptn][i]);
			pDC->TextOut(pText.x, pText.y+i*nLineHeight,str2);
		}
	}
	if (dm == 2)
	{
		pDC->DrawText(m_strWait,-1,&rect,DT_SINGLELINE|DT_CENTER|DT_VCENTER);
	}
	if (dm == 3)
	{
		int nWidth = rect.Width();
		int nHeight = rect.Height();
		int max_num = num[0]>num[1]?num[0]:num[1];
	
		rect.DeflateRect(nWidth/6, nHeight/6);
		nWidth = rect.Width();
		nHeight = rect.Height();

		int intvl_h = rect.Height()/6;
		int intvl_w = rect.Width()/50;
		int intvl_m = rect.Width()%50;
		intvl_m /= intvl_w;//计算剩余空间的线条数
	
		
		CPen pen1(PS_DOT,1,RGB(128,128,128)),
			 pen2(PS_SOLID,1,RGB(255,0,0)),
			 pen3(PS_SOLID,1,RGB(0,0,255));
		CPen *pOldPen = pDC->SelectObject(&pen1);

		pDC->Rectangle(rect.left,rect.top,rect.right,rect.bottom);

		for (int i=0; i<(50+intvl_m); i++)
		{
		    pDC->MoveTo(rect.left+i*intvl_w, rect.top);
			pDC->LineTo(rect.left+i*intvl_w, rect.bottom);
		}
		for (i=0; i<6; i++)
		{
			pDC->MoveTo(rect.left, rect.top+i*intvl_h);
			pDC->LineTo(rect.right, rect.top+i*intvl_h);
		}

	    pDC->TextOut(rect.left, rect.bottom+5,"学习次数:"); 
		CString str1,str2;
		str1.Format("红色---%d次", num[0]);
		str2.Format("蓝色---%d次", num[1]);
		pDC->TextOut(rect.left+70, rect.bottom+5, str1);
		pDC->TextOut(rect.left+70, rect.bottom+23, str2);
	
		pDC->SetViewportOrg(rect.left, rect.bottom);

		CPoint *aPoint = new CPoint [ num[0] ];
		if(!aPoint)::AfxMessageBox("Memory error");
		pDC->SelectObject(&pen2);
		for (i=0;i<num[0];i++)
		{
			aPoint[i].x = (i*nWidth)/max_num;
			aPoint[i].y = (int)(-e_value[0][i]*nHeight);
		}
		pDC->Polyline(aPoint, num[0]);
        delete [] aPoint;

		pDC->SelectObject(&pen3);
	    aPoint = new CPoint [ num[1] ];
		if(!aPoint)::AfxMessageBox("Memory error");
		for (i=0;i<num[1];i++)
		{
			aPoint[i].x = (i*nWidth)/max_num;
			aPoint[i].y = (int)(-e_value[1][i]*nHeight);
		}
		pDC->Polyline(aPoint, num[1]);
	    delete [] aPoint;

		pDC->SelectObject(pOldPen);
	}

    return;
}

/////////////////////////////////////////////////////////////////////////////
// CTestView printing

BOOL CTestView::OnPreparePrinting(CPrintInfo* pInfo)
{
	// default preparation
	return DoPreparePrinting(pInfo);
}

void CTestView::OnBeginPrinting(CDC* /*pDC*/, CPrintInfo* /*pInfo*/)
{
	// TODO: add extra initialization before printing
}

void CTestView::OnEndPrinting(CDC* /*pDC*/, CPrintInfo* /*pInfo*/)
{
	// TODO: add cleanup after printing
}

/////////////////////////////////////////////////////////////////////////////
// CTestView diagnostics

#ifdef _DEBUG
void CTestView::AssertValid() const
{
	CView::AssertValid();
}

void CTestView::Dump(CDumpContext& dc) const
{
	CView::Dump(dc);
}

CTestDoc* CTestView::GetDocument() // non-debug version is inline
{
	ASSERT(m_pDocument->IsKindOf(RUNTIME_CLASS(CTestDoc)));
	return (CTestDoc*)m_pDocument;
}
#endif //_DEBUG

/////////////////////////////////////////////////////////////////////////////
// CTestView message handlers

void CTestView::OnMenuDisp() 
{
	// TODO: Add your command handler code here
	double delta, *temp_min;

	ifstream in(BIASNUM_FILE);
	if (!in){::AfxMessageBox("Fail to load file");exit(1);}
	ifstream in_num(BIAS_FILE);
	if (!in_num){::AfxMessageBox("Fail to load file"); exit(1);}

	method = LEARN_METHOD;
	min = 1.0;
	max = 0.0;

	num = new int [method];
	if (!num){::AfxMessageBox("memory error"); exit(1);}
 	for (int x=0; x< method; x++)
	{
		in >> num[x];
	}
		
	e_value = new double* [method];
	if (!e_value){::AfxMessageBox("memory error"); exit(1);}
    for (x=0; x<method; x++)
	{
		e_value[x] = new double [ num[x] ];
		if (!e_value[x]){::AfxMessageBox("memory error"); exit(1);}
	}
	for (x=0; x<method; x++)
	{
		for (int y=0; y<num[x]; y++)
		{
			in_num >> e_value[x][y];
		}
	}
	in.close();
	in_num.close();

	max = e_value[0][0];
	temp_min = new double [method];
	for (x=0; x<method; x++)
	{
		temp_min[x] = e_value[x][ num[x]-1 ];
		if(min > temp_min[x])
		{
			min = temp_min[x];
		}
	}
	delta = max-min;
	
	for (x=0; x<method; x++)
	{
		for (int y=0; y<num[x]; y++)
		{
			//e_value[x][y] = (e_value[x][y] - temp_min[x])/delta;
			e_value[x][y] = (e_value[x][y] - min)/delta;
		}
	}

	CTestDoc* pDoc = GetDocument();
	ASSERT_VALID(pDoc);
	pDoc->dispmode = 3;	//显示曲线;
	
    CRect rect;
    GetClientRect(&rect);
	InvalidateRect(&rect, TRUE);
	return;
}

void CTestView::OnKeyDown(UINT nChar, UINT nRepCnt, UINT nFlags) 
{
	// TODO: Add your message handler code here and/or call default
	CTestDoc *pDoc = GetDocument();
	pDoc->notkeyhit = FALSE;
	CView::OnKeyDown(nChar, nRepCnt, nFlags);
}

void CTestView::OnUpdateMenuDisp(CCmdUI* pCmdUI) 
{
	// TODO: Add your command update UI handler code here
	CTestDoc *pDoc = GetDocument();
	pCmdUI->Enable(pDoc->data_learned);
}

void CTestView::OnUpdateMenuLearn(CCmdUI* pCmdUI) 
{
	// TODO: Add your command update UI handler code here
	CTestDoc *pDoc = GetDocument();
	pCmdUI->Enable(pDoc->can_learn);
}

void CTestView::OnUpdateMenuTest(CCmdUI* pCmdUI) 
{
	// TODO: Add your command update UI handler code here
	CTestDoc *pDoc = GetDocument();
	pCmdUI->Enable(pDoc->data_learned);
}

⌨️ 快捷键说明

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