自己写的一个链表应用程序
进入程序主界面,你有6个选项,下面一一介绍:
*********************************************************
(0)退出
(1)创建新单词
(2)保存已有单词
(3)载入单词表
(4)听写单词开始
(5)浏览单词表
(6)查找单词或释义
*********************************************************
0.退出;就是退出(如果你感兴趣的话一般不会选择这个的);
1.创建新单词:用户自己编写的单词,输入单词、释义即可,然后回主界面;
2.保存已有单词:你新建了很多单词,结果忘记了选择这个,一般会后悔的;
(我忘记保存了)
3.载入单词表:保存过的单词表会在你的 VC 6.0 工程目录下生成你命名的文件,这个文件就是你新建的单词表。载入时记得把文件名写对!
4.听写单词开始:写了这么多,为的就是检测一下自己,选择这个吧,让你对单词不再陌生!
你可以不保存单词表,直接创建单词后就可以开始测试了。
5.浏览单词表:当你不是很有把握时,先浏览一下吧,长个记性。
6.查找单词或释义:这是一个词典的功能,只是有局限性:你的输入释义必须和已有单词的释义完全一致(连空格都不能多!),否则会显示“查无此单词”或出错;
好了,就说这么多,剩下的就靠各位自己琢磨了,顺便多提宝贵意见!
/////////////////////////////////////start:
#include <iostream.h>
#include <string.h>
#include <stdio.h>
////////////////////////////////////
class words
{
public:
words()
{
cout<<"输入单词:";
cin>>word;
cout<<"输入单词释义:";
cin>>mean;
low=strlen(word);
lom=strlen(mean);
}
words(int i)
{
for(int j=0;j<20;j++)
{
word[j]='/0';
mean[j]='/0';
}
low=lom=0;
}
~words(){}
char * getword()
{
return word;
}
char * getmean()
{
return mean;
}
int getlengthofword(){return low;}
int getlengthofmean(){return lom;}
void show()
{
cout<<"*****"<<endl;
cout<<"单词:"<<word<<endl;
cout<<"意思:"<<mean<<endl;
cout<<"*****"<<endl;
}
////////////////////////////
private:
char word[20];
char mean[20];
int low;
int lom;
};
//////////////////////////////////////////////////////
class node
{
public:
node():head(0),next(0){}
node(words * p):head(p),next(0){}
~node()
{
if(head)
{
delete head;
head=0;
}
if(next)
{
delete next;
next=0;
}
}
words * gethead()
{
return head;
}
node * getnext()
{
return next;
}
void setnext(node * n)
{
next=n;
}
void sethead(words * t)
{
head=t;
}
private:
words * head;
node * next;
};
///////////////////////////////////////////////////
class List
{
public:
List():first(0),Count(1){}
List(node * p):first(p),Count(1){}
~List()
{
Count=0;
if(first){delete first;first=0;}
cout<<"链表被删除!"<<endl;
}
char * Find()
{
char p[30];
cout<<"输入单词或释义:";
cin>>p;
node *t=first;
for(t;strcmp(t->gethead()->getword(),p) && strcmp(t->gethead()->getmean(),p);t=t->getnext());
if(strcmp(t->gethead()->getword(),p))
return t->gethead()->getword();
else return t->gethead()->getmean();
}
void Insert(node * p)
{
if(first)
{
node *t=first;
while(t->getnext()){t=t->getnext();}
t->setnext(p);
}
else
{
first=p;
}
Count++;
}
void Delete(char * x)
{
node * t,*q;
for(t=first;t;q=t,t=t->getnext())
{
if(strcmp(t->gethead()->getword(),x)==0)
break;
}
if(t==0)
{
cout<<"查无此单词!";
return;
}
q->setnext(t->getnext());
t->~node();
}
void Iterate()
{
node * t=first;
if(!t)
{
cout<<"链表为空!";
return ;
}
while(t)
{
if(!t->gethead())
{
cout<<"无单词!"<<endl;
return;
}
t->gethead()->show();
t=t->getnext();
for(int i=0;i<10000;i++);//延时
}
}
void Test()
{
int i,j;
node *t;
char s[20];
for(i=0,j=0,t=first;t;t=t->getnext(),j++)
{
if(!t->gethead())break;
cout<<"单词释义:"<<t->gethead()->getmean()<<endl;
cout<<"单词:";
cin>>s;
if(strcmp(t->gethead()->getword(),s))
{
cout<<"电脑:错了吧!哈哈哈哈!"<<endl;i++;
cout<<"*****************************/n";
}
else
{
cout<<"电脑:太牛了,答对了!再来!"<<endl;
cout<<"*****************************/n";
}
}
cout<<"*****************************/n";
cout<<"总计:"<<j<<endl;
cout<<"正确:"<<j-i<<endl;
cout<<"错误:"<<i<<endl;
cout<<"*****************************/n";
if(i==0)
cout<<"电脑:你是一个天才!"<<endl;
else if(i<=2)
cout<<"电脑:你可以做的更好! "<<endl;
else
cout<<"电脑:再来一次!相信自己!"<<endl;
}
void Save()
{
char x[20];
cout<<"文件名为:";
cin>>x;
node * t=first;
FILE * fp=fopen(x,"w");
if(!fp)
{
cout<<"打开文件失败!"<<endl;
return ;
}
for(t;t;t=t->getnext())
{
fwrite(t->gethead(),sizeof(words),1,fp);
}
fclose(fp);
cout<<"保存成功!"<<endl;
}
void Load()
{
if(first)
{
cout<<"请先保存当前链表!";
return;
}
char x[20];
cout<<"文件名为:";
cin>>x;
FILE * fp=fopen(x,"r");
if(!fp)
{
cout<<"打开失败!"<<endl;
return ;
}
first=new node;
node *t=first;
node * y;
for(t;!feof(fp);)
{
words * w=new words(1);
fread(w,sizeof(words),1,fp);
t->sethead(w);
y=new node;
t->setnext(y);
t=t->getnext();
}
fclose(fp);
cout<<"成功载入!"<<endl;
}
node * GetFirst()
{
return first;
}
int GetCount()
{
return Count;
}
private:
node * first;
int Count;
};
void main()
{
int choice =99;
node * p;
List l;
words * t;
while (choice)
{
cout<<"*********************************************************/n";
cout<<"/t(0)退出/n/t(1)创建新单词/n/t(2)保存已有单词/n/t(3)载入单词表/n/t(4)听写单词开始/n/t(5)浏览单词表/n";
cout<<"/t(6)查找单词或释义/n";
cout<<"*********************************************************/n";
cin>>choice;
switch(choice)
{
case 1:
t=new words;
p=new node(t);
l.Insert(p);
break;
case 2:l.Save();break;
case 3:l.Load();break;
case 4:l.Test();break;
case 5:l.Iterate();break;
case 6:
if(l.GetFirst()==0)
{
cout<<"链表为空!"<<endl;
break;
}
else
{
cout<<"你要找的是:"<<l.Find()<<" 对吗?"<<endl;
break;
}
default:break;
}
}
}
最后更新:2017-04-02 00:06:30