www.pudn.com > BPLetterRecognation.rar > BPNetworkView.cpp
// BPNetworkView.cpp : implementation of the CBPNetworkView class
//
#include "stdafx.h"
#include "BPNetwork.h"
#include "BPNetworkDoc.h"
#include "BPNetworkView.h"
extern void init();
extern double go_one_step();
extern int recognize( double* to_recog );
extern double X[10][64];
extern double T[10][10];
#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif
/////////////////////////////////////////////////////////////////////////////
// CBPNetworkView
IMPLEMENT_DYNCREATE(CBPNetworkView, CView)
BEGIN_MESSAGE_MAP(CBPNetworkView, CView)
//{{AFX_MSG_MAP(CBPNetworkView)
ON_WM_LBUTTONUP()
ON_COMMAND(ID_MENU_CLEAR, OnMenuClear)
ON_COMMAND(ID_MENU_LEARN, OnMenuLearn)
ON_COMMAND(ID_MENU_RECOGNIZE, OnMenuRecognize)
ON_COMMAND(ID_MENU_SAMPLE, OnMenuSample)
//}}AFX_MSG_MAP
END_MESSAGE_MAP()
/////////////////////////////////////////////////////////////////////////////
// CBPNetworkView construction/destruction
CBPNetworkView::CBPNetworkView()
{
// TODO: add construction code here
inputstate = 0;
displayinput = 0;
displayoutput = 0;
displaylearn = 0;
displaysample = 0;
int x2,y2;
int step=25;
int x=130;
int y=100;
for(int i=0;i<64;i++){
x2=x+step;
y2=y+step;
Rect_input[i].SetRect(x,y,x2,y2);
if(i%8==7)
{
y+=step+2;
x-=step*7+14;
}
else
x+=step+2;
}
x=480;
y=100;
for(i=0;i<64;i++){
x2=x+step;
y2=y+step;
Rect_output[i].SetRect(x,y,x2,y2);
if(i%8==7)
{
y+=step+2;
x-=step*7+14;
}
else
x+=step+2;
}
}
CBPNetworkView::~CBPNetworkView()
{
}
BOOL CBPNetworkView::PreCreateWindow(CREATESTRUCT& cs)
{
// TODO: Modify the Window class or styles here by modifying
// the CREATESTRUCT cs
return CView::PreCreateWindow(cs);
}
/////////////////////////////////////////////////////////////////////////////
// CBPNetworkView drawing
void CBPNetworkView::OnDraw(CDC* pDC)
{
CBPNetworkDoc* pDoc = GetDocument();
ASSERT_VALID(pDoc);
// TODO: add draw code for native data here
if( displaysample )
DrawSample(pDC);
if( displayinput )
{
for(int i=0;i<64;i++)
{
if( inputarray[i]<0.0001 )
pDC->FillSolidRect(Rect_input[i],RGB(0,0,0) );
else
pDC->FillSolidRect(Rect_input[i],RGB(0,0,255) );
}
}
if( displayoutput )
{
for(int i=0;i<64;i++)
{
if( outputarray[i]<0.0001 )
pDC->FillSolidRect(Rect_output[i],RGB(0,0,0) );
else
pDC->FillSolidRect(Rect_output[i],RGB(0,0,255) );
}
}
}
/////////////////////////////////////////////////////////////////////////////
// CBPNetworkView diagnostics
#ifdef _DEBUG
void CBPNetworkView::AssertValid() const
{
CView::AssertValid();
}
void CBPNetworkView::Dump(CDumpContext& dc) const
{
CView::Dump(dc);
}
CBPNetworkDoc* CBPNetworkView::GetDocument() // non-debug version is inline
{
ASSERT(m_pDocument->IsKindOf(RUNTIME_CLASS(CBPNetworkDoc)));
return (CBPNetworkDoc*)m_pDocument;
}
#endif //_DEBUG
/////////////////////////////////////////////////////////////////////////////
// CBPNetworkView message handlers
void CBPNetworkView::OnLButtonUp(UINT nFlags, CPoint point)
{
// TODO: Add your message handler code here and/or call default
if( inputstate!=0 )
{
for(int i=0;i<64;i++)
{
if (Rect_input[i].PtInRect(point)) {
if(inputarray[i]<0.001)
inputarray[i]=1;
else
inputarray[i]=0;
InvalidateRect(Rect_input[i]);
}
}
}
CView::OnLButtonUp(nFlags, point);
}
void CBPNetworkView::OnMenuClear()
{
// TODO: Add your command handler code here
inputstate = 1;
displayinput = 1;
displayoutput = 0;
displaylearn = 0;
displaysample = 0;
for(int count=0;count<64;count++)
{
inputarray[count]=0;
outputarray[count]=0;
}
Invalidate();
}
void CBPNetworkView::OnMenuLearn()
{
// TODO: Add your command handler code here
inputstate = 0;
displayinput = 0;
displayoutput = 0;
displaylearn = 1;
displaysample = 0;
double myerror=1;
init();
int num=0;
CDC* pDC = GetDC();
pDC->FillSolidRect( 0,0,1000,800,RGB(255,255,255) );
pDC->MoveTo(10,500);
pDC->LineTo(790,500);
pDC->MoveTo(10,500);
pDC->LineTo(10,10);
while (myerror>0.001)
{
myerror = go_one_step();
num++;
if( num%5==0 )
pDC->SetPixel( 10+num/5,500-myerror*100,RGB(255,0,0) );
}
::AfxMessageBox("学习完毕,现在可以输入让我识别");
OnMenuClear();
}
void CBPNetworkView::OnMenuRecognize()
{
// TODO: Add your command handler code here
inputstate = 1;
displayinput = 1;
displayoutput = 1;
displaylearn = 0;
displaysample = 0;
int result;
result = recognize( inputarray );
if(result==-1)
{
AfxMessageBox("识别失败!失败原因:没有找到匹配的字符");
displayoutput = 0;
}
else if(result==-2)
{
AfxMessageBox("识别失败!失败原因:匹配的字符过多(>=2)");
displayoutput = 0;
}
else
AfxMessageBox("识别成功!");
for(int i=0;i<64;i++)
outputarray[i] = X[result][i];
Invalidate();
}
void CBPNetworkView::OnMenuSample()
{
// TODO: Add your command handler code here
inputstate =0;
displayinput = 0;
displayoutput = 0;
displaylearn = 0;
displaysample = 1;
Invalidate();
}
void CBPNetworkView::DrawSample(CDC *pDC)
{
CRect Rect_temp[64];
int x,y,x2,y2,startx,starty;
int step=14;
int whichinput = 0;
pDC->TextOut(100,60,"本例共有十个样本,其中输入为8×8的英文字符,教师信号是长度为十的行向量。");
for(starty=100;starty<500;starty+=220)
for(startx=30;startx<700;startx+=150)
{
x = startx;
y = starty;
for(int i=0;i<64;i++)
{
x2=x+step;
y2=y+step;
Rect_temp[i].SetRect(x,y,x2,y2);
if(i%8==7)
{
y+=step+2;
x-=step*7+14;
}
else
x+=step+2;
}
for(i=0;i<64;i++)
{
if( X[whichinput][i]<0.0001 )
pDC->FillSolidRect(Rect_temp[i],RGB(0,0,0) );
else
pDC->FillSolidRect(Rect_temp[i],RGB(0,0,255) );
}
for(i=0;i<10;i++)
{
if( T[whichinput][i]<0.0001 )
pDC->FillSolidRect(startx+12*i,starty+150,11,11,RGB(0,0,0) );
else
pDC->FillSolidRect(startx+12*i,starty+150,11,11,RGB(0,0,255) );
}
whichinput++;
}
}