Ở câu bangMaBit[node].bits=new char[nMaBit]; lỗi syntax error before new
if (huffTree[node].nLeft == -1 && huffTree[node].nRight == -1) { //la nut khong co con
bangMaBit[node].soBit = nMaBit;
bangMaBit[node].bits =new char[nMaBit];
for (int i = 0; i < nMaBit; i++) {
bangMaBit[node].bits[i] = maBit[i];
}
return;
}
Có phải là do khai báo sai bangMaBit giống như huffTree không?
Đây là toàn bộ code.
#include <stdio.h>
#include <stdbool.h>
typedef struct {
unsigned char c; // ky tu
int freq; // so lan xuat hien
bool used; // danh dau node da xu ly chua
int nLeft; // chi so nut con nam ben trai
int nRight; // chi so nut con nam ben phai
}NODE;
NODE huffTree[2034];
typedef struct {
char* bits;
int soBit;
}MABIT;
MABIT bangMaBit[256];
//const int MAX_NODE = 256*9;//pow(2,8);
const int MAX_BIT_LEN = 10000;
//NODE huffTree[MAX_NODE];
//MABIT bangMaBit[256];
void KhoiTao() {
int i;
for (i = 0; i < 2304; i++) {
huffTree[i].c = i;
huffTree[i].freq = 0;
huffTree[i].used = false;
huffTree[i].nLeft = -1;
huffTree[i].nRight = -1;
}
}
void ThongKeTanSoXuatHien(char* tenFile) {
FILE* fi = fopen(tenFile, "rt");
unsigned char c;
while (1) {
fscanf(fi, "%c", &c);
if (feof(fi)) {
break;
}
huffTree[c].freq ++; // Tang so lan xuat hien cua c
}
fclose(fi);
}
void XuatBangThongKe() {
printf("Bang thong ke tan suat: \n");
int i;
for (i = 0; i < 256; i++) {
if (huffTree[i].freq > 0) {// Dam bao rang chu nay xuat hien
printf("%c: %d\n",i, huffTree[i].freq);
}
}
}
bool Tim2PhanTuMin(int i, int j, int nNode) {
i = -1;
j = -1;
// tim 2 phan tu co trong so nho nhat
int k;
for (k = 0; k < nNode; k++)
if (huffTree[k].used == false && huffTree[k].freq > 0) { // Ki tu nay chua duoc xu ly va tan so lon hon 0
if (i == -1) {
i = k;
}
else if (j == -1) {
j = k;
}
else {
if (huffTree[i].freq > huffTree[j].freq) {
if (huffTree[k].freq < huffTree[i].freq) {
i = k;
}
}
else {
if (huffTree[k].freq < huffTree[j].freq) {
j = k;
}
}
}
}
// sap xep lai 2 phan tu de co i: phan tu co trong so nho nhat, j: phan tu co trong so nho nhi
// co 2 truong hop can doi i,j:
// huffTree[i].freq > huffTree[j].freq
// huffTree[i].freq == huffTree[j].freq va (huffTree[i].c > huffTree[j].c)
if (i != -1 && j!= -1) {
if ( (huffTree[i].freq > huffTree[j].freq) || ((huffTree[i].freq > huffTree[j].freq) && (huffTree[i].c > huffTree[j].c))) {
int t = i;
i = j;
j = t;
}
return true;
}
else {
return false;
}
}
int TaoCayHuffman() {
int nNode = 256;
int i, j;
bool timThay = false;
while (true) {
// tim 2 phan tu nho nhat de dua vao 2 nhanh xa nhat cua cay
timThay = Tim2PhanTuMin(i, j, nNode);
if (!timThay) {
break;
}
// them 2 node va luu vi tri
huffTree[nNode].c = (huffTree[i].c < huffTree[j].c) ? huffTree[i].c : huffTree[j].c;
huffTree[nNode].freq = huffTree[i].freq + huffTree[j].freq;
huffTree[nNode].nLeft = i;
huffTree[nNode].nRight = j;
// danh dau 2 nhanh nay da duyet
huffTree[i].used = true;
huffTree[j].used = true;
// danh dau nut moi chua duyet
huffTree[nNode].used = false;
nNode++;
}
return nNode - 1; // tiep tuc thuc hien de qui voi cay moi
}
void XuatCayHuffman(int node, int tab) {
if (node == -1) {
return;
}
int i;
for (i = 0; i < tab; i++) {
printf("\t");
}
if (huffTree[node].nLeft == -1 && huffTree[node].nRight == -1) {
printf("%c\n", huffTree[node].c);
}
else {
printf("%c..\n", huffTree[node].c);
XuatCayHuffman(huffTree[node].nLeft, tab + 1);
XuatCayHuffman(huffTree[node].nRight, tab + 1);
}
}
void DuyetCayHuffman(int node, char maBit[], int nMaBit) {
if (node == -1) {
return;
}
if (huffTree[node].nLeft == -1 && huffTree[node].nRight == -1) { //la nut khong co con
bangMaBit[node].soBit = nMaBit;
bangMaBit[node].bits = new char[nMaBit];
int i;
for (i = 0; i < nMaBit; i++) {
bangMaBit[node].bits[i] = maBit[i];
}
return;
}
else {
// neu nam ben trai thi de qui voi nut ben trai
maBit[nMaBit] = 0;
DuyetCayHuffman(huffTree[node].nLeft, maBit, nMaBit + 1);
// ben phai thi xet node bang node right
maBit[nMaBit] = 1;
DuyetCayHuffman(huffTree[node].nRight, maBit, nMaBit + 1);
}
}
void PhatSinhMaBit(int nRoot) { // Phat sinh ma bit tinh tu nut goc
int i;
for (i = 0; i < 256; i++) {
bangMaBit[i].soBit = 0;
bangMaBit[i].bits = NULL;
};
char maBit[MAX_BIT_LEN/8];
int nMaBit = 0;
DuyetCayHuffman(nRoot, maBit, nMaBit);
}
void XuatBangMaBit() {
int i;
for (i = 0; i < 256; i++)
if (bangMaBit[i].soBit > 0) {
printf("%c: ", i);
int j;
for (j = 0; j < bangMaBit[i].soBit; j++)
printf("%d", bangMaBit[i].bits[j]);
printf("\n");
}
}
void XuatByte(unsigned char out, int soBitCoNghia) {
int i;
for (i = 7; i >= 7 - soBitCoNghia + 1; i--) {
if ((out >> i) & 1) {// Kiem tra dieu kien de in ra byte
printf("1");
}
else {
printf("0");
}
}
printf(" ");
}
void MaHoa1KyTu(unsigned char c, unsigned char out, unsigned char viTriBit) { // c:la ki tu ca ma hoa,out:ki tu da dc ma hoa,viTriBit :vi tri cua bit
int i;
for (i = 0; i < bangMaBit[c].soBit; i++) {
if (bangMaBit[c].bits[i] == 1) {
out = out | (1 << viTriBit); //bat bit i cua so nguyen out
}
if (viTriBit == 0) { // da du 1 byte, ghi byte do ra file
viTriBit = 7;
XuatByte(out, 8);
out = 0;
}
else {
viTriBit--;
}
}
}
void NenHuffman(char* inputFile) {
// BUOC 1: thong ke tan suat xuat hien cua cac ki tu
KhoiTao();
ThongKeTanSoXuatHien(inputFile);
// XuatBangThongKe();
// BUOC 2: tao cay Huffman
int nRoot = TaoCayHuffman();
// XuatCayHuffman(nRoot, 0);
// BUOC 3: phat sinh ma bit
PhatSinhMaBit(nRoot);
// XuatBangMaBit();
// BUOC 4: thay the cac ky tu bang ma bit tuong ung
unsigned char out = 0; // ky tu se xuat ra
unsigned char soBitCoNghia = 0; // byte cuoi co the ko su dung het cac bit nen can luu so bit co nghia cua byte cuoi
unsigned char c;
unsigned char viTriBit = 7; //viTriBit la so bit can de bieu dien
FILE* fi = fopen(inputFile, "rt");
while (true) {
fscanf(fi, "%c", &c);
if (feof(fi)) {
break;
}
MaHoa1KyTu(c, out, viTriBit);
}
soBitCoNghia = 7 - viTriBit;
if (soBitCoNghia == 0) {
soBitCoNghia = 8;
}
else {
XuatByte(out, soBitCoNghia);
}
fclose(fi);
}
void main() {
// nen
NenHuffman("input.txt");
}