Untitled

mail@pastecode.io avatar
unknown
plain_text
a year ago
1.9 kB
2
Indexable
#include <iostream>
#include <string>
#include <fstream>
#include <iomanip>
#include<vector>
using namespace std;
struct link_point{
    unsigned char value;
    int time=0;
    int Rank;
    vector<int> code;
    struct link_point *leftptr=NULL;
    struct link_point *rightptr=NULL;
};
typedef struct link_point* Linkpoint;
int main()
{
    int Size=256; string filename; string wordlist; ifstream outfile; Linkpoint chars[Size];// a lot of link_point *

    int counter=0;Linkpoint temp=new link_point;//thanks for GPT

    for(int i=0;i<Size;i++)//assign value
    {
        chars[i] = new link_point;  // allocate memory for each element      thanks for GPT
        chars[i]->value=static_cast<unsigned char>(i);
    }
    cout<<"start to input..."<<endl;


    while(getline(cin,wordlist))//count appear time
    {

            for(int i=0;i<wordlist.length();i++)
            {
                chars[static_cast<int>(wordlist[i])]->time++;
            }

    }


    for(int i=0;i<Size;i++)//sorting by times, have runtime error
    {
        for(int x=i+1;x<Size;x++)
        {
            if((chars[i]->time)>(chars[x]->time))//change struct content not pointer position
            {
                *temp=*chars[i];
                *chars[i]=*chars[x];
                *chars[x]=*temp;
            }
        }
    }

    for(int i=0,x=0;i<Size;i++,x++)//assign rank...  after Ex:chars[1]->value=91 chars[1]->rank=2 chars[1]->time=15
    {
        chars[i]->Rank=x+1;

        do
        {
            if(i<Size-1&&chars[i]->Rank==chars[i+1]->Rank)
            {
                chars[i+1]->Rank=x+1;
                counter++;i++;
            }
        }while(i<Size-1&&chars[i]->Rank==chars[i+1]->Rank);

        x+=counter;counter=0;
    }
    for(int i=0;i<Size;i++)
    {
        cout<<chars[i]->value<<"\t"<<chars[i]->time<<endl;
    }
    return 0;
}
Leave a Comment