//阿D的连表#include <vector>
#include <string>
#include <algorithm>
#include <iterator>
#include <iomanip>
#include <iostream>
#include <ctype.h> #include <fstream>
#include <string>#include<stdio.h>using namespace std;
class Data
{
public:
Data(char *name):next(0),back(0)
{
strcpy(Name,name);
strcpy(Homeplace,"");//出生地
}
public:
Data *back;//上1节点;
Data *next;//下1节点public:
char Name[100];
char Homeplace[100];//出生地
};class Role
{
public:
Role():count(0)
{
head=NULL;//头节点
front=NULL;//当前节点 }
public:
Data *head;//头节点(遍历的起始节点);
Data *front;//当前节点; int count;
public: //修改值
void SetData(int dataname,char *result,Data *temp)
{
int count=0;//计算第几个节点 if(dataname==1)//名
{
strcpy(temp->Name,result);
}
else if(dataname==2)//出生地
{
strcpy(temp->Homeplace,result);
}
else
{
cout<<"没有这个项"<<endl;
}
} public:
bool is_empty() const
{
return head == 0;
} void add(char *name)
{
count++;
//产生新的节点
// allocate a new QueueItem object
//类模板格式
Data *pt =new Data(name);
pt->next=head; if ( is_empty())//当连表为空时
{
head = front= pt;//当前节点及上一个节点,都指向同一个位置
//当前节点
front->back=pt;
front->next=pt;
cout<<"is null list;"<<endl;
}
else
{
cout<<"pt->Name:"<<pt->Name<<endl;
//当前节点
front->next = pt;//新节点为原节点的下一个节点
Data *temp1=front;//原节点
front=pt;//新节点为当前节点
front->back=temp1;//原节点为当前节点(新节点)的上一个节点
cout<<"is not null<back>:Name:"<<endl;
}
}
public: //打印当前节点DATA
void PrintfData(Data *temp)
{
cout<<"name:"<<temp->Name<<endl;
cout<<"出生地:"<<temp->Homeplace<<endl;
}
//打印当前节点DATA/next/back;
void PrintfData1(Data *temp1)
{
Data *temp=temp1;
cout<<"temp(当前节点)"<<endl;
cout<<"name:"<<temp->Name<<endl;
cout<<"出生地:"<<temp->Homeplace<<endl<<endl; temp=temp1->back;
cout<<"temp->back(当前节点的上一个节点)"<<endl;
cout<<"name:"<<temp->Name<<endl;
cout<<"出生地:"<<temp->Homeplace<<endl<<endl; temp=temp1->next;
cout<<"temp->next(当前节点的下一个节点)"<<endl;
cout<<"name:"<<temp->Name<<endl;
cout<<"出生地:"<<temp->Homeplace<<endl<<endl;
} //打印所有节点值
void PrintfData2()
{
Data *temp=head;
while(temp!=NULL)
{
cout<<"name:"<<temp->Name<<endl;
cout<<"出生地:"<<temp->Homeplace<<endl<<endl;
temp=temp->next;
if(temp==head || temp==NULL)
{
cout<<"printf end\n";
break;
}
}
if(temp==NULL)
{
cout<<"list is null\n";
}
} //打印当前节点DATA/next/back;
void PrintfData3()
{
Data *temp=head;
Data *temp1=head;
while(temp1!=NULL)
{
temp=temp1;
cout<<"temp(当前节点)"<<endl;
cout<<"name:"<<temp->Name<<endl;
cout<<"出生地:"<<temp->Homeplace<<endl<<endl; temp=temp1->back;
cout<<"temp->back(当前节点的上一个节点)"<<endl;
cout<<"name:"<<temp->Name<<endl;
cout<<"出生地:"<<temp->Homeplace<<endl<<endl; temp=temp1->next;
cout<<"temp->next(当前节点的下一个节点)"<<endl;
cout<<"name:"<<temp->Name<<endl;
cout<<"出生地:"<<temp->Homeplace<<endl<<endl;
if(temp1->next==NULL || temp1->next==head)
{
cout<<"end printf\n";
break;
}
temp1=temp1->next;
}
} public: void Remove(Data *temp)
{
Data *temp1=temp->back;
Data *temp2=temp->next; //要delete节点与头节点相同
if(temp==head)//节点名相同
{
if(temp->next==NULL)//连表只有一个值
{
cout<<"(delete)temp->next==NULL\n";
temp->next=NULL;
temp->back=NULL;
delete temp;
temp=NULL;
head=NULL;
return ;
}
else if(temp->next==head)//连表只有一个值
{
cout<<"(delete)temp->next==head\n";
temp->next=NULL;
temp->back=NULL;
delete temp;
temp=NULL;
head=NULL;
return ;
}
else//连表多于一个值
{
cout<<"(delete)(temp==head)连表多于一个值\n";
//当连表删完值,不为空时,设置head下移一位
head=temp->next;
temp->back=head;
temp->next->back=temp->back;
front->next=head;
delete temp;
temp=NULL;
}
}
else if(temp!=head && temp!=NULL)//不为头也不为空
{
cout<<"delete 与temp!=head && temp!=NULL\n";
temp->back->next=temp->next;
temp->next->back=temp->back; delete temp;
temp=NULL;
}
else if(temp==NULL)
{
cout<<"delete 与temp==NULL\n";
}
}
public:
//输入名,返回节点指针
Data *getnode()
{
char name[100]="";
cout<<"请输入节点名\n";
cin>>name; int count=0;
Data *temp=head; while(temp!=NULL)
{
if(strcmp(temp->Name,name)==0)//节点名相同
{
cout<<"count:"<<count<<endl;
return temp;
}
count++;
temp=temp->next;
if(temp==head)
{
return NULL;
}
}
return NULL;
}
public:
bool couterror(char *s)
{
cout<<s<<endl;
return false;
} bool checkEnter(Data *temp,char *s)
{
if(strcmp("名",s)==0)//节点名相同
{
return true;
}
if(strcmp("出生地",s)==0)//节点名相同
{
return true;
}
cout<<"没有这一项\n";
return false;
}
};
void main()
{
Role Ro1; char Name[100]="";//名
char Homeplace[100]="";//出生地
int count=0;//计算输入了几个节点 char dataname[100];//要修改的数据
char result[100];//修改的值 while(1)
{
char cha[10]="";
int num=0; printf("Menu(1):输入:1加节点 2:打印当前,next及back数据 3:修改节点值\n ");
printf("4:打印节点数据 5:delete node 6:所有node 7:打印all node 10:EXIT\n");
cin>>cha;//输入功能
num=atoi(cha);//将字符数字转换成整型数字,字母为0
if(num==1)//加节点
{
count++;
cout<<"Menu(1-1)请输入加入的名字\n";
cin>>Name;
Ro1.add(Name);
}
else if(num==2)//打印当前节点DATA/next/back;
{
while(1)
{
cout<<"Menu(1-2)打印当前节点DATA/next/back\n";
Data *temp=Ro1.getnode();
if(temp==NULL)
{
Ro1.couterror("输入节点节点出错");
continue;
}
//打印当前节点DATA/next/back;
Ro1.PrintfData1(temp);
break;
}
}
else if(num==3)//修改节点值
{
while(1)
{
printf("Menu(1-3):1:修改节点值 10:EXIT:\n");
cin>>cha;//输入功能
num=atoi(cha);//将字符数字转换成整型数字,字母为0
if(num==1)//修改值
{
cout<<"请输入要修改的信息\n";
Data *temp=Ro1.getnode();
if(temp==NULL)
{
Ro1.couterror("输入节点节点出错");
continue;
}
while(1)
{
num=0;
cout<<"1:名 2:出生地"<<endl;
cin>>cha;//输入功能
num=atoi(cha);//将字符数字转换成整型数字,字母为0
if(num!=0 && num<=2)
{
cout<<"请输入要修改的值\n";
cin>>result;
Ro1.SetData(num,result,temp);
break;
}
else
{
continue;
}
}
}
else if(num==10)
{
break;
}
}
}
else if(num==4)//打印节点数据
{
while(1)
{
printf("Menu(1-4):1:打印节点数据 10:EXIT:\n");
cin>>cha;//输入功能
num=atoi(cha);//将字符数字转换成整型数字,字母为0
if(num==1)
{
while(1)
{
cout<<"请输入打印节点数据(人名)\n";
Data *temp=Ro1.getnode();
if(temp==NULL)
{
Ro1.couterror("输入节点节点出错");
continue;
}
Ro1.PrintfData(temp);
break;
}
}
else if(num==10)
{
break;
}
}
}
else if(num==5)
{
while(1)
{
cout<<"Menu(1-5):请输入delete node(人名)\n";
Data *temp=Ro1.getnode();
if(temp==NULL)
{
Ro1.couterror("输入节点节点出错");
continue;
}
Ro1.Remove(temp);
break;
} }
else if(num==6)
{
//打印所有节点值
Ro1.PrintfData2();
}
else if(num==7)
{
//打印所有node DATA/next/back
Ro1.PrintfData3();
}
else if(num==10)//跳出
{
break;
}
}
cout<<endl;
system("pause");
}你用连表试试,这里设置的是,只能输入小keyboard数字.
所以要10个隐层node,4个输出层node输入为n个。思路如下1 隐层算法
将输入x与(0,1,2,....9) 比大小,10个节点 输出结果
如x=7 x-1 = 6
x-2 = 5
....
x-7 = 0
x-8 =-1
...
大于0置1,小于等于0置0
2 输出层算法
输入(如上例): (1,1,1,1,1,0,0,0)
输入累加,作为节点的输入。
节点d的函数是
sigma(x)-1 = 5
节点c的函数是
sigma(x)-3 = 3
节点b的函数是
sigma(x)-5 = 1
节点a的函数是
sigma(x)-7 = -1
大于等于0置1,小于0置0
所以输出就是0111
对训练后,
1。一个节点的情况:是/否——思路比较清晰,即s函数输出大于域值为1,小于或者等于为0;
2。多个节点的情况:怎么处理呢?是对各种0-9的字体一起训练,还是每个数字都训练一遍?
第二隐层节点可以有8个,用于判断校正后的8方向链码
输出么就随便了,放4个可以得到2进制编码这样总共节点28个。不多训练主要是针对第一隐层的各个节点的输入权重,单独训练好各个节点后,然后16个放一起,作为第一隐层的训练初值。
再进行一遍即可。和感知器学习算法很接近。第一层和第二层可以手动计算得到。第二层和输出也是可以手动得到