647
技術社區[雲棲]
自己寫的一個鏈表應用程序
進入程序主界麵,你有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