自己写的一个链表应用程序

简介:

 

  进入程序主界面,你有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;
  } 
  
 }
 

}


 

目录
相关文章
|
25天前
|
存储 编译器 C语言
【数据结构】深入浅出理解链表中二级指针的应用
【数据结构】深入浅出理解链表中二级指针的应用
27 0
|
1月前
|
存储 缓存 算法
C++链表解析:从基础原理到高级应用,全面掌握链表的使用
C++链表解析:从基础原理到高级应用,全面掌握链表的使用
51 0
|
2月前
|
算法
链表中快慢指针的应用
链表中快慢指针的应用
|
4月前
数据结构循环链表之介绍和应用 | 第一套
数据结构循环链表之介绍和应用 | 第一套
25 0
|
8月前
|
存储 算法 Java
Java LinkedList:探索双向链表的灵活应用
在Java编程中,LinkedList是一种重要的数据结构,它在内存中以双向链表的形式存储数据,为我们提供了一种动态而灵活的数据管理方式。本文将引导您深入了解Java中的LinkedList,包括其特点、用法、与ArrayList的比较,以及实际应用场景。
|
9月前
|
存储 C++
指针及其应用5——指针链表
指针及其应用5——指针链表
|
存储
【线性表】—带头哨兵卫单链表的应用
【线性表】—带头哨兵卫单链表的应用
100 0
|
机器学习/深度学习 存储 人工智能
【数组与链表算法】矩阵算法在程序中常见的简单应用 | C++
数组与链表都是相当重要的结构化数据类型,也都是典型线性表的应用。线性表用于计算机中的数据存储结构,按照内存存储的方式基本上可以分为以下两种:静态数据结构和动态数据结构。数组类型就是一种典型的静态数据结构,动态数据结构又称为链表。在我前面的算法系列文章都细致的对二者的使用方法做过讲解。
154 0
【数组与链表算法】矩阵算法在程序中常见的简单应用 | C++
|
存储 Java
Java单链表的应用实例
Java单链表的应用实例
166 0
Java单链表的应用实例