?? fp-growth.cpp
字號:
#include"stdio.h"
#include"stdlib.h"
#include "string.h"
//#define N 5
#include <vector>
#include <iostream.h>
#include <algorithm>
using namespace std; //總共字符個數
typedef struct node
{
char ch;
int count;
struct node *left,*right;
}node,*bitree;
typedef struct tnode
{
char name;
int count;
}Tnode;
vector <Tnode> T1;
//vector <Tnode> T2;
//int sort[2][N],counter=0; //排序使用
int search(bitree root,char ch,bitree *p)//在二叉排序數中(*root指向樹的根節點),若找到令p指向該節點并返回0
{//否則令p指向該節點并返回-1
int compress=0;
bitree ptr;
*p=NULL;
ptr=root;
while(ptr)
{
compress=(ptr->ch)-ch;
if(compress==0)
{
*p=ptr;
return 0;
}//查找成功,令p指向該節點,返回0
else
{
*p=ptr;
ptr=compress>0?ptr->left:ptr->right;
}
}//while
return -1;//查找失敗
}//search
int insert(bitree *t,char ch)
{
bitree p,s;
if(search(*t,ch,&p)==-1)
{//找不到ch
s=(node *)malloc(sizeof(node));
if(!s)
{
printf("儲存分配失敗!\n");
return -1;
}
s->left=NULL;
s->right=NULL;
s->ch=ch;
s->count=1;
if(p==NULL)
*t=s;//ch是第一個出現的字符,令s作為根節點
else if(p->ch<ch)
p->right=s;
else p->left=s;
}//end if
else p->count++;
return 0;
}
void inorder(bitree &root)//中序遍歷root指向根的二叉樹
{
Tnode temp;
if(root)
{
inorder(root->left);
{
printf(" %c (%d)\n",root->ch,root->count);
temp.name=root->ch;
temp.count=root->count;
T1.push_back(temp);
}
inorder(root->right);
}
}
bool comp(const Tnode &p1,const Tnode &p2)
{
return p1.count>p2.count;
}
sort1()//將A文件的按count的將序排序并寫入B文件
{
int temp,k=0;
char c,bb[5];
// Tnode pre;
/* for(int i=0;i<N;i++) //將其按count排序
for(int j=i+1;j<N;j++)
if(sort[1][i]<sort[1][j]){temp=sort[1][i];sort[1][i]=sort[1][j];sort[1][j]=temp;temp=sort[0][i];sort[0][i]=sort[0][j];sort[0][j]=temp;}
printf("The order is:\n");//將排序好的輸出(隱含著編碼過程即count最大的編碼為1次大的為2依類推...)
for(i=0;i<N;i++) printf(" %c (%d) \n",sort[0][i],sort[1][i]);*/
FILE *fp,*fp1; //定義文件指針(包括讀與寫)
fp=fopen("a.txt","r");
if(!fp)
{printf("open file error\n");return -1;}
fp1=fopen("b.txt","w"); //打開B文件,準備將結果寫入
if(!fp1)
{printf("open file error\n");return -1;}
while(!feof(fp))
{
c=fgetc(fp);
if(c>='a'&&c<='z')
{
for(int j=0;j<T1.size();j++)
if(c==T1[j].name)
bb[k++]=j;
}//編碼
if(c=='\n') {
for(int i=0;i<k;i++)//將編碼后的數據排序
for(int j=i+1;j<k;j++)
if(bb[i]>bb[j]){temp=bb[i];bb[i]=bb[j];bb[j]=temp;}
for(i=0;i<k;i++) fputc(T1[bb[i]].name,fp1);//反編碼,并寫入文件
fputc('\n',fp1);
k=0;
}
}
fclose(fp);
fclose(fp1);
return 0;
}
int main()
{
FILE* fp;
char c;
int row=0;
bitree root=NULL;
int N=T1.max_size();
// int bb[N];
// char s[T1.size]=NULL;
fp=fopen("a.txt","r");
if(!fp)
{printf("open file error\n");return -1;}
while(!feof(fp))
{
c=fgetc(fp);
if(c=='\n') row++;
printf("%c",c);
if(c>='a'&&c<='z') insert(&root,c);
}
row=row+1;
fclose(fp);
printf("\nrow = %d\n",row);
inorder(root);
sort (T1.begin(),T1.end(),comp);
printf("按出現的次數排列為:\n");
for(int i=0;i<T1.size();i++)
{
printf(" %c <%d>\n",T1[i].name,T1[i].count);
}
sort1(); //調用文件正序函數
FILE *fp2;
fp2=fopen("b.txt","r");
while(!feof(fp2))
{
c=fgetc(fp2);
printf("%c",c);
}
fclose(fp2);
return 0;
}
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -