📄 ganzhiqi.cpp
字号:
// GanZhiQi.cpp : Defines the entry point for the console application.
//感知器算法程序
#include "stdafx.h"
#include <iostream.h>
#include<math.h>
//样本点结构
typedef struct sample
{
int x;
int y;
int z;
int k;
} SAMPLE;
//样本点点积
int multiply(SAMPLE a,SAMPLE b)
{
int mul;
mul=a.x*b.x+a.y*b.y+a.z*b.z+a.k*b.k;
return mul;
}
//输出样本点
void output(SAMPLE a)
{
cout<<a.x<<" "<<a.y<<" "<<a.z<<" "<<a.k<<endl;
}
//样本点和
SAMPLE add(SAMPLE a,SAMPLE b)
{
SAMPLE ad;
ad.x=a.x+b.x;
ad.y=a.y+b.y;
ad.z=a.z+b.z;
ad.k=a.k+b.k;
return ad;
}
void main()
{
SAMPLE point[8]={{0,0,0,1},{1,0,0,1},{1,0,1,1},{1,1,0,1},{0,0,-1,-1},{0,-1,-1,-1},{0,-1,0,-1},{-1,-1,-1,-1}};
SAMPLE weight={-1,-2,-2,0};//初始权向量
SAMPLE p;
int c=1;
int m,f;
int i;//第i个样本点
int count = 0;//迭代计数器
output(weight);
while(1)
{
f=0;
count++;
cout<<"第"<<count<<"次"<<"新的迭代开始:"<<endl;
for(i=0;i<8;i++)
{
m=multiply(weight,point[i]);
cout<<"m="<<m<<endl;
if(m<=0)
{
p.x=c*point[i].x;
p.y=c*point[i].y;
p.z=c*point[i].z;
p.k=c*point[i].k;
weight=add(weight,p);
f++;
output(weight);
}
else
output(weight);
}
if(f==0)
break;
}
//printf("weight=(%d,%d,%d,%d)\n",weight.x,weight.y,weight.z,weight.k);
output(weight);//输出解向量
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -