【数据结构】邓玉欣的编程作业indexing

#include 
#include 
#include 
#include 
#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 word;
    string s;
    while(!fin.eof())
    {
        ch=fin.get();
        fout<='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

第一次的project的第一题。


大概意思就是自己实现一个数据结构,用于储存一串国家名。并且可以按照字母序排序。


上面是main函数部分,用于对文章进行输入。


其中wordlist这个是一个自己编写的类。如下

#ifndef WORDLIST_H_INCLUDED
#define WORDLIST_H_INCLUDED
#include 
#include 
#include 
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;is2;
    }
    public:
    wordOfSameLetter()
    {
        head = curr = new node();
        size = 0;
    }
    ~wordOfSameLetter()
    {
        delete head;
        delete curr;
    }
    //冒泡排序
    void merge()
    {
        for(int i=0;inext;
            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"<sword;
            cout<<"\t"<count<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<letter<<":"<print();
            cout<


以上。(以前的作业,贴的晚了..)





你可能感兴趣的:(数据结构,dengyuxin,链表,类)