#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的第一题。
大概意思就是自己实现一个数据结构,用于储存一串国家名。并且可以按照字母序排序。
其中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
以上。(以前的作业,贴的晚了..)
原文链接:https://www.f2er.com/datastructure/382875.html