int main() { printf("\nTree:%d\nBurning:%d\n\n",TREE, BURNING); // Seed the random number. Always!!!! srand(time(NULL)); int i,j; int **forest=(int**)calloc(NROWS+2,sizeof(int*)); for(i=0;i<NROWS+2;i++) forest[i] = (int*)calloc(NCOLS+2,sizeof(int)); double per_forest_burned = 0; for(i=0;i<num_experiments;i++) { initForest(forest); //print_forest(forest); for(j=0;j<num_steps;j++) { fillBoundary(forest); spread(forest,forest); //print_forest(forest); } per_forest_burned += per_of_forest_burned(forest); } printf("\n%lf\n",(per_forest_burned/num_experiments)); }
void generateMatrix(int ***forest, int rows, int cols, int iterations, int simulations, float pTree, float pBurning, float pGrow, float pImmune, float pLightning, int spread_type, int neighbourhood_type, int boundary_type){ int i,j,k,l,count; FILE* fptr; fptr=fopen("bell_please.tr","w"); if(fptr==NULL){ printf("Error! File not opened\n"); } for(i = 0; i<simulations; i++) { printf("%d\n", i); initForest(forest[0], rows, cols, pTree, pBurning); count = 0; /* for(k = 1; k<=rows; k++) { for(l = 1; l<=cols; l++) { if((forest[0][k][l]/10<=2) && (forest[0][k][l]!=EMPTY_BURNING)) { count++; } } } fprintf(fptr,"%Lf ",(long double)(count/(rows*cols))); */ for(j = 1; j<iterations; j++) { fillBoundary(forest[j-1],rows, cols, boundary_type); spread(forest[j-1],forest[j],rows, cols, pImmune, pLightning, pGrow, spread_type, neighbourhood_type); count = 0; for(k = 1; k<=rows; k++) { for(l = 1; l<=cols; l++) { if((forest[j][k][l]/10==2) && (forest[j][k][l]!=EMPTY_BURNING)) { count++; } } } fprintf(fptr,"%f ",((float)count/(rows*cols))); } fprintf(fptr, "\n"); } fclose(fptr); }
int main ( int argc, char* argv[] ) { //PFC编码、解码算法统一测试入口 /*DSA*/if ( 2 > argc ) { printf ( "Usage: %s <message#1> [message#2] ...\a\n", argv[0] ); return -1; } PFCForest* forest = initForest(); //初始化PFC森林 PFCTree* tree = generateTree ( forest ); release ( forest ); //生成PFC编码树 /*DSA*/print ( tree ); PFCTable* table = generateTable ( tree ); //将PFC编码树转换为编码表 /*DSA*/for ( int i = 0; i < N_CHAR; i++ ) printf ( " %c: %s\n", i + 0x20, * ( table->get ( i + 0x20 ) ) ); //输出编码表 for ( int i = 1; i < argc; i++ ) { //对于命令行传入的每一明文串 /*DSA*/printf ( "\nEncoding: %s\n", argv[i] ); //开始编码 Bitmap codeString; //二进制编码串 int n = encode ( table, codeString, argv[i] ); //将根据编码表生成(长度为n) /*DSA*/printf ( "%s\n", codeString.bits2string ( n ) ); //输出当前文本的编码串 /*DSA*/printf ( "Decoding: " ); //开始解码 decode ( tree, codeString, n ); //利用编码树,对长度为n的二进制编码串解码(直接输出) } release ( table ); release ( tree ); return 0; //释放编码表、编码树 }
/****************************************************************************************** * 无论编码森林由列表、完全堆还是左式堆实现,本测试过程都可适用 * 编码森林的实现方式采用优先级队列时,编译前对应的工程只需设置相应标志: * DSA_PQ_List、DSA_PQ_ComplHeap或DSA_PQ_LeftHeap ******************************************************************************************/ int main(int argc, char* argv[]) { //Huffman编码算法统一测试 /*DSA*/if (3 > argc) { printf("Usage: %s <sample-text-file> <message#1> [message#2] ...\a\n", argv[0]); return -1; } int* freq = statistics(argv[1]); //根据样本文件,统计各字符的出现频率 HuffForest* forest = initForest(freq); release(freq); //创建Huffman森林 HuffTree* tree = generateTree(forest); release(forest); //生成Huffman编码树 /*DSA*/print(tree); //输出编码树 HuffTable* table = generateTable(tree); //将Huffman编码树转换为编码表 /*DSA*/for (int i = 0; i < N_CHAR; i++) //输出编码表 /*DSA*/printf(" %c: %s\n", i+0x20, *(table->get(i+0x20))); for (int i = 2; i < argc; i++) { //对于命令行传入的每一明文串 /*DSA*/printf("\nEncoding: %s\n", argv[i]); Bitmap* codeString = new Bitmap; //二进制编码串将通过 int n = encode(table, codeString, argv[i]); //调用编码算法生成(总长为n) /*DSA*/printf("%s\n", codeString->bits2string(n)); //输出编码串 /*DSA*/printf("Decoding: "); decode(tree, codeString, n); //利用Huffman编码树,对长度为n的二进制编码串解码 release(codeString); } release(table); release(tree); return 0; //释放编码表、编码树 }