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

📄 testview.cpp

📁 Handwritten digital recognition is made by matlab nerve network toolbox in visual c++.net
💻 CPP
字号:
// testView.cpp : implementation of the CTestView class
//

#include "stdafx.h"
#include "test.h"
#include "Glob.h"
#include "testDoc.h"
#include "testView.h"
#include <math.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)
	//}}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
}

CTestView::~CTestView()
{
}

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
}

/////////////////////////////////////////////////////////////////////////////
// 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::OnUpdate(CView* pSender, LPARAM lHint, CObject* pHint) 
{
	// TODO: Add your specialized code here and/or call the base class
}

void CTestView::OnRun() 
{
	// TODO: Add your command handler code here
	if(!chuli)
	{
		MessageBox("请先点击'处理'按钮!");
		return;
	}
	CTestDoc *pDoc = (CTestDoc*)GetDocument();
	Engine         *ep;
	array_Thirteen TNumber;
	array_four     FNumber;
	CThirteen      parray1;
	CThirty        parray2;
	CFour          tarray;
	CString        str;	
	mxArray        *x=NULL;
	double         *unm;
	double         Test[13];
	int            number[4];
	int            output;
	int            i;
	int            j;
	FILE           *fp;
	fp=fopen("FILE\\Train_P.txt","r");
	if(!fp)
	{
		MessageBox("样本值不存在,请重新输入!");
		return;
	}
    while(!feof(fp))
	{
		for(i=0;i<13;i++)
			TNumber.NUM[i]=(fgetc(fp)-48);
        parray1.Add(TNumber);
		
	}
	parray1.RemoveAt(parray1.GetSize()-1);
	fclose(fp);
    fp=fopen("FILE\\Train_T.txt","r");
	if(!fp)
	{
		MessageBox("样本值不存在,请重新输入!");
		return;
	}
    while(!feof(fp))
	{
		for(i=0;i<4;i++)
				FNumber.NUM[i]=(fgetc(fp)-48);
        tarray.Add(FNumber);
		
	}
	tarray.RemoveAt(tarray.GetSize()-1);
	fclose(fp);
	int pm=parray1.GetSize();
	int tm=tarray.GetSize();
	double (*p)[13]=new double[pm][13];
	double (*t)[4]=new double[tm][4];
	
	for(i=0;i<pm;i++)
		for(j=0;j<13;j++)
			p[i][j]=parray1.GetAt(i).NUM[j];	
	for(i=0;i<tm;i++)
		for(j=0;j<4;j++)
			t[i][j]=tarray.GetAt(i).NUM[j];
	if(!check)
	{
		MessageBox("请先点击'训练样本'!");
		return;
	}
	if(0==option)
	{
		mxArray *P=mxCreateDoubleMatrix(pm,13,mxREAL);
	    mxArray *T=mxCreateDoubleMatrix(pm,4,mxREAL);
	    mxArray *targe=mxCreateDoubleMatrix(1,13,mxREAL);
	    for(i=0;i<13;i++)
			Test[i]=test1[i];
		if(!(ep=engOpen(NULL)))
		{
			MessageBox("Can not open the matlab engine!");
		    exit(-1);
		}	
    	engSetVisible(ep,false);
    	memcpy((char *)mxGetPr(P),(char *)p,pm*13*sizeof(double));
    	memcpy((char *)mxGetPr(T),(char *)t,pm*4*sizeof(double));
	    memcpy((char *)mxGetPr(targe),(char *)Test,13*sizeof(double));
	    engPutVariable(ep,"P",P);
	    engPutVariable(ep,"T",T);
	    engPutVariable(ep,"targe",targe);
		engEvalString(ep,"P=P';");
		engEvalString(ep,"T=T';");
		engEvalString(ep,"targe=targe';");
        engEvalString(ep,"net=newff(minmax(P),[27,4],{'purelin','logsig'},'trainlm');");
		engEvalString(ep,"net.trainParam.epochs=50;");
        engEvalString(ep,"net.trainParam.goal=0.001;");
		engEvalString(ep,"LP.lr=0.05;");
		engEvalString(ep,"net=train(net,P,T);");
		engEvalString(ep,"y=sim(net,targe);");
        x=engGetVariable(ep,"y");	
	    if(x==NULL)
		MessageBox("Can't Get Array From Matlab!","Shape Recognition",MB_OK);
    	else
		{
			unm=mxGetPr(x);	
	 /*       for(i=0;i<4;i++)	       
				if(abs(unm[i])>=0.5)
					number[i]=1;
				else
					number[i]=0;
				output=number[0]*8+number[1]*4+number[2]*2+number[3];*/
				output=(int)(unm[0]*8+unm[1]*4+unm[2]*2+unm[3]);
			if(output>9)
				output=9;
			data=output;
			str.Format("%d",data);
			MessageBox(str);
		}
	    engClose(ep);
	    mxDestroyArray(P);
	    mxDestroyArray(T);
	    mxDestroyArray(targe);
	}
	else if(2==option)	
	{
		mxArray *P=mxCreateDoubleMatrix(pm,13,mxREAL);
	    mxArray *T=mxCreateDoubleMatrix(pm,4,mxREAL);
	    mxArray *targe=mxCreateDoubleMatrix(1,13,mxREAL);
		for(i=0;i<13;i++)
			Test[i]=test1[i];
	    if(!(ep=engOpen(NULL)))
		{
			MessageBox("Can not open the matlab engine!");
		    exit(-1);
		}	
	    engSetVisible(ep,false);
	    memcpy((char *)mxGetPr(P),(char *)p,pm*13*sizeof(double));
	    memcpy((char *)mxGetPr(T),(char *)t,pm*4*sizeof(double));
	    memcpy((char *)mxGetPr(targe),(char *)Test,13*sizeof(double));
	    engPutVariable(ep,"P",P);
	    engPutVariable(ep,"T",T);
	    engPutVariable(ep,"targe",targe);
		engEvalString(ep,"P=P';");
		engEvalString(ep,"T=T';");
		engEvalString(ep,"targe=targe';");
		engEvalString(ep,"net=newrbe(P,T,5);");
		engEvalString(ep,"y=sim(net,targe);");
        x=engGetVariable(ep,"y");	
	    if(x==NULL)
			MessageBox("Can't Get Array From Matlab!","Shape Recognition",MB_OK);
	    else
		{
			unm=mxGetPr(x);
			for( i=0;i<4;i++)
				if(abs(unm[i])>=0.5)
					number[i]=1;
				else
					number[i]=0;
			output=number[0]*8+number[1]*4+number[2]*2+number[3];
			if(output<0)
				output=0;
			if(output>9)
				output=9;
			data=output;
		}
	    engClose(ep);
	    mxDestroyArray(P);
	    mxDestroyArray(T);
	    mxDestroyArray(targe);
	
	}
	else
	{
	}
	delete [] p;
	delete [] t;
	mxDestroyArray(x);
	parray1.RemoveAll();
	parray2.RemoveAll();
	tarray.RemoveAll();
	int send=1;
	GetDocument()->UpdateAllViews(NULL,4,(CObject*)&send);
}

⌨️ 快捷键说明

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