今天看C++primer的时候看到String读写的时候,突然想到可以将姓名常用汉字代码按一定格式读入到链表节点当中,然后编程实现所有汉字代码的添加和查询,下面附上代码,正在学习中,技术有限哈。
#include <iostream>
#include <fstream>
#include <string>
using namespace std;
class node
{
public:
string name;
string number;
node *next;
node(){name="";number="";next=NULL;}
};
class list
{
node *head;
node *last;
public:
list();
void add(string &Name,string &Number);
void select(string &Name);
};
list::list()
{
head=NULL;
last=NULL;
}
void list::add(string &Name,string &Number)
{
if(head==NULL)
{
head=new node;
head->name=Name;
head->number=Number;
head->next=NULL;
last=head;
last->next=NULL;
}
else
{
node *P=new node;
P->name=Name;
P->number=Number;
P->next=NULL;
last->next=P;
last=P;
}
}
void list::select(string &Name)
{
if(head==NULL)
{
cout<<"NULL ~!"<<endl;
return;
}
node *P=head;
while(P!=NULL)
{
if(P->name==Name)
{
cout<<Name<<"的编码为:"<<P->number<<endl;
return;
}
P=P->next;
}
cout<<"系统没有"<<Name<<"这个字,请查证!"<<endl;
//select(Name);
system("cls");
}
void main()
{
string str,str2;
list LIST;
ifstream in("姓名常用汉字代码.txt");
ofstream out("姓名汉字代码.txt");
if(!in.is_open())
{
cout<<"打开数据文件失败~!"<<endl;
exit(0);
}
cout<<"字库加载中。。。"<<endl;
while(!in.eof())
{
in>>str>>str2;
// out<<str+str2<<endl;
// cout<<str+str2<<endl;
LIST.add(str,str2);
}
cout<<"字库加载完毕"<<endl;
while(1)
{
cout<<"请输入你想查询的汉字:";
cin>>str;
LIST.select(str);
}
}