node.cpp

来自「数字识别」· C++ 代码 · 共 80 行

CPP
80
字号
// Node.cpp: implementation of the CNode class.
//
//////////////////////////////////////////////////////////////////////

#include "stdafx.h"
#include "数字识别.h"
#include "Node.h"
#include <fstream.h>

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

//////////////////////////////////////////////////////////////////////
// Construction/Destruction
//////////////////////////////////////////////////////////////////////

CNode::CNode(char fileName[]){
	valve = 1;
	file = NULL;
	file = new char[strlen(fileName)];
	if(NULL != file){
		length = 140; // 长度可变
		int i;
		strcpy(file, fileName);
		ifstream inf(fileName);
		weight = new int[length];
		for(i = 0; i < length; i++){
			weight[i] = 0;
			inf>>weight[i];
		}
	}
}

CNode::~CNode(){
	Save();
	if(NULL != file){
		delete[] file;
		delete[] weight;
	}
}

int CNode::GetResult(int * input){ // 计算输出
	int i = 0;
	int sum = 0;
	for(i = 0;i < length; i++){
		sum += input[i] * weight[i];
	}
	return sum > valve?1:0; // 函数比较简单,大于1(初始化时value为1)则输入1,否则输出0
}

void CNode::Save(){
	if(NULL != file){
		int i;
		ofstream ofs(file, ios::out);
		for(i = 0; i < length; i++){
			ofs<<weight[i]<<" ";
		}
	}
}

void CNode::Study(int * input, int result){ // 学习函数
	int tr = GetResult(input);
	if(tr > result){
		//-
		int i = 0;
		for(i = 0;i < length; i++){
			weight[i] -= input[i];
		}
	}
	else if(tr < result){
		//+
		int i = 0;
		for(i = 0;i < length; i++){
			weight[i] += input[i];
		}
	}
}

⌨️ 快捷键说明

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