我的程式碼如下 這是一個讀任意檔案近來
計算出每一個 char出現幾次
並將次數由大到小用link list排序的程式
連結如下 http://140.117.189.47/hw7.rar #include
#include
#include
//---------------------------------------------------------------------------
#pragma package(smart_init)
#pragma resource "*.dfm" //---------------------------------------------------------------------------
__fastcall TForm1::TForm1(TComponent* Owner)
: TForm(Owner)
{
}
//---------------------------------------------------------------------------
TForm1 *Form1;
struct sorting_list
{
int count;
AnsiString st;
struct sorting_list *before,*after;
};
FILE *f; struct sorting_list *head,*rear,*find; struct node
{
AnsiString string;
struct node *father,*leftson,*rightson;
}; AnsiString filename,st;
struct Ascii
{
int count;
char st;
};
struct Ascii ascii[255]; void __fastcall TForm1::N2Click(TObject *Sender)
{ f=fopen("222.txt","a "); int i,j,k; for(k=0;k<=255;k )
{
ascii[k].count=0; } if(OpenDialog1->Execute())
{
filename=OpenDialog1->FileName;
}
else return; //**********計算每一個字元有多少個
TFileStream* fs = new TFileStream(filename, fmOpenRead);
這ㄧ行有時候會出錯
char AByte; while(fs->Read(&AByte, 1))
{
k=AByte;
ascii[k].count ;
ascii[k].st=AByte; //array[k].count ;
} for(k=0;k<=255;k ) fprintf(f,"%d次數=%d\n",k,ascii[k].count); fclose(f);
/*
//----------------------------建 link list
head->count=ascii[0].count; // <<<<<<<<<<<<<<<< 指向這ㄧ行說無法開啟檔案
head->st=ascii[0].st;
rear=head;
head->before=head->after=NULL; //----------------------------------- 排序
for(i=1;i<255;i )
{
struct sorting_list *ps;
ps=(sorting_list*)malloc(sizeof(sorting_list));
ps->count=ascii[i].count;
ps->st=ascii[i].st;
if(ps->count>=head->count)
{
ps->before=NULL;
ps->after=head;
head->before=ps;
head=ps;
} //if
else if(ps->count<=rear->count)
{
ps->after=NULL;
ps->before=rear;
rear->after=ps;
rear=ps;
} //else if
else
{
for(find=head;find->count>=ps->count&&find->after->countcount;find=find->after);
find->after=ps;
ps->before=find;
ps->after=find->after;
find->after->before=ps;
} //else }//for */