【資料結構】鄧玉欣的程式設計作業indexing
阿新 • • 發佈:2019-02-10
#include <iostream> #include <fstream> #include <string> #include <vector> #include "wordlist.h" using namespace std; int count = 0; int main() { ifstream fin("shenbingyu.txt"); ofstream fout("sby.txt"); if(!fin){cerr<<"ERROR";} char ch; vector<string> word; string s; while(!fin.eof()) { ch=fin.get(); fout<<ch; if(!(((ch>='A')&&(ch<='Z'))||((ch>='a')&&(ch<='z')))) { count++; if(s!="")word.push_back(s); s=""; continue; } s.push_back(ch); } fin.close(); fout.close(); passage shenbingyu; for(size_t i=0;i<word.size();i++) { shenbingyu.insert(word[i]); } cout<<"There are "<<count<<" words in this passage."<<endl; cout<<"=================================================="<<endl; shenbingyu.print(); return 0; }
第一次的project的第一題。
大概意思就是自己實現一個數據結構,用於儲存一串國家名。並且可以按照字母序排序。
上面是main函式部分,用於對文章進行輸入。
其中wordlist這個是一個自己編寫的類。如下
#ifndef WORDLIST_H_INCLUDED #define WORDLIST_H_INCLUDED #include <string> #include <iostream> #include <iomanip> using namespace std; //node 用於記錄一個單詞 class node { public: string sword; node *next; int count;//這個單詞記錄的次數 node(string w =""){next=NULL;count=0;sword=w;} ~node(){delete next;} }; //wordOfSameLetter 用於儲存單詞連結串列 class wordOfSameLetter { private: node *head; node *curr; int size;//包含的單詞數 //swap交換兩個node裡面內容 void swap(node*n1,node*n2) { string w = n1->sword;n1->sword=n2->sword;n2->sword=w; int t = n1->count; n1->count=n2->count; n2->count= t; } //按照字典序比較 int compare(string s1,string s2) { char c1=s1[0];char c2=s2[0]; int i1=s1.size();int i2 = s2.size(); for(int i=0;i<i1;i++) s1[i]=tolower(s1[i]); for(int i=0;i<i2;i++) s2[i]=tolower(s2[i]); if(s1==s2) return c1<c2; return s1>s2; } public: wordOfSameLetter() { head = curr = new node(); size = 0; } ~wordOfSameLetter() { delete head; delete curr; } //氣泡排序 void merge() { for(int i=0;i<size;i++) { curr = head; node *tmp; curr = curr->next; tmp = curr; while(tmp->next!=NULL) { //if(tmp->sword > tmp->next->sword) if(compare(tmp->sword,tmp->next->sword)==1) swap(tmp,tmp->next); tmp = tmp->next; } } } void insertl(string w)//如果wordOfSameLetter中有w這個單詞就count++;沒有的話就插在最後 { bool f = false; curr =head; while(curr->next!=NULL) { curr=curr->next; if(w==curr->sword) { curr->count++; f = true; } } if(!f){ curr = curr->next =new node(w); curr->count++; size++;} } void print() { merge();//如果輸出的話就把列表merge一下 curr = head; while(curr->next!=NULL) { curr = curr->next; cout<<"\t"<<left<<setw(15)<<curr->sword; cout<<"\t"<<curr->count<<endl; } } }; //用於儲存首字母相同的單詞連結串列 class letterlist { public: char letter; letterlist* next; wordOfSameLetter words; int nol; letterlist(char a='^',letterlist* n =NULL) { letter = a; next = n; nol = 0; } ~letterlist(){delete next;} void insertw(string w)//在letterlist中插入一個單詞 { words.insertl(w); ++nol; } void print()//列印letterlist中的單詞 { words.print(); } }; //包括26個letterlist分別用來儲存單詞 class passage { private: letterlist *head; letterlist *curr; public: passage() { head=curr=new letterlist(); for(char ch='A';ch<'Z'+1;ch++) { curr->next=new letterlist(ch,NULL); curr=curr->next; } } ~passage() { delete head; delete curr; } void insert(string w)//在passage中插入一個單詞 { char ch = w[0]; for(curr=head->next;curr!=NULL;curr=curr->next) { if((ch==curr->letter)||(ch==(curr->letter+32))) { curr->insertw(w); break; } } } void print()//輸出所有不為空的letterlist { for(curr=head->next;curr!=NULL;curr=curr->next) { if(curr->nol==0) continue; cout<<curr->letter<<":"<<endl; curr->print(); cout<<endl; } } }; #endif // WORDLIST_H_INCLUDED
以上。(以前的作業,貼的晚了..)