România
Roportal.ro

Am nevoie de ajutor cu acest cod

Început de Nosferatus91, Vin 22 iun 2012 · 1 răspunsuri · 3.830 vizualizări

Nosferatus911 mesaje#1

#include<stdio.h>

#include<string.h>

#include<stdlib.h>

#include<conio.h>

struct nod

{

char chr;

int val;

nod *st,*dr;

nod(){val=0;chr=NULL;st=NULL;dr=NULL;}

};

struct Huffman

{

char* mesaj;

char *text_lung;

int dim;

char *mesaj_codat;

char* mesaj_decodat;

nod chr[200];

};

//Hufman struct functions

int caracter_nou(char c, struct Huffman *huffman);

void frecventa(struct Huffman *huffman);

void sortare(struct Huffman *huffman);

void constructie_arbore(int dim, struct Huffman *huffman);

void vsd_codare(nod *c,char *cod,char chr, struct Huffman *huffman);

void InitHuffman(struct Huffman *huffman);

void codHuffman(struct Huffman *huffman);

void decodHuffman(struct Huffman *huffman);

struct Huffman *huffman;

int caracter_nou(char c,struct Huffman *huffman)

{

huffman->dim=1;

huffman->text_lung=new char[1000];

strcpy(huffman->text_lung,"");

FILE *f=fopen("text_lung","r");

char *s;

s=new char[300];

if(!f)

{

printf("EROARE LA DESCHIDEREA FISIERULUI!!!");

exit(1);

}

while(!feof(f))

{

fgets(s,300,f);

strcat(huffman->text_lung,s);

}

huffman->mesaj=new char[200];

printf("\n Da-ti mesajul care urmeaza a fi codat:");

gets(huffman->mesaj);

huffman->mesaj_codat=new char[300];

huffman->mesaj_decodat=new char[300];

strcpy(huffman->mesaj_codat,"");

strcpy(huffman->mesaj_decodat,"");

}

int caracter_nou_1(char c,struct Huffman *huffman)

{

for(int i=0; i<huffman->dim; i++)

if(c==huffman->chr.chr) return 0;

return 1;

}

void frecventa(struct Huffman *huffman)

{

int i,j;

for(i=0; i<strlen(huffman->text_lung); i++)

if(caracter_nou(huffman->text_lung,huffman))

{

huffman->chr[huffman->dim].chr=huffman->text_lung;

huffman->chr[huffman->dim].val++;

huffman->dim++;

}

else

{

for(j=1; j<=huffman->dim; j++)

if(huffman->text_lung==huffman->chr[j].chr)

{

huffman->chr[j].val++;

j=huffman->dim;

}

}

}

void sortare(struct Huffman *huffman)

{

int i,j;

nod aux;

for(i=1; i<huffman->dim;i++)

for(j=i+1; j<=huffman->dim; j++)

if(huffman->chr.val<huffman->chr[j].val)

{

aux=huffman->chr;

huffman->chr=huffman->chr[j];

huffman->chr[j]=aux;

}

}

void constructie_arbore(int dim,struct Huffman *huffman)

{

nod q,*p,*p_st,*p_dr;

p_st=new nod();

p_dr=new nod();

if(dim>0)

{

p_st->val=huffman->chr[dim].val;

p_st->chr=huffman->chr[dim].chr;

p_st->st=huffman->chr[dim].st;

p_st->dr=huffman->chr[dim].dr;

p_dr->val=huffman->chr[dim].val;

p_dr->chr=huffman->chr[dim].chr;

p_dr->st=huffman->chr[dim-1].st;

p_dr->dr=huffman->chr[dim-1].dr;

q.chr=NULL;

q.val=p_st->val+p_dr->val;

q.st=p_st;

q.dr=p_dr;

huffman->chr[dim-1]=q;

dim--;

sortare(huffman);

constructie_arbore(huffman->dim,huffman);

}

else

return;

}

void vsd_codare(nod *c,char *cod, char chr,struct Huffman *huffman)//parcurgere in adancime;

{

char *cod1,*cod2;

if©

{

cod1=new char[strlen(cod)+2];

cod2=new char[strlen(cod)+2];

strcpy(cod1,cod);

strcpy(cod2,cod);

strcpy(cod1,"0");

strcat(cod2,"1");

if(c->chr==chr)

{

//printf("%s %s",c->chr,cod);

strcat(huffman->mesaj_codat,cod);

}

vsd_codare(c->st,cod1,chr,huffman);

vsd_codare(c->dr,cod2,chr,huffman);

}

}

void codHuffman(struct Huffman *huffman)

{

frecventa(huffman);

sortare(huffman);

constructie_arbore(huffman->dim,huffman);

for(int i=0; i<strlen(huffman->mesaj); i++)

vsd_codare(&huffman->chr[1],"",huffman->mesaj,huffman);

printf("\nMesajul codat este urmatorul\n");

printf("%s",huffman->mesaj_codat);

}

void decodHuffman(struct Huffman *huffman)

{

printf("Mesajul decodat este:\n");

nod *p=new nod;

p=&huffman->chr[1];

for(int i=0; i<strlen(huffman->mesaj_codat)+1; i++)

{

if(p->chr)

{

printf("%s",p->chr);

p=&huffman->chr[1];

i--;

}

else

if(huffman->mesaj_codat=='0')

p=p->st;

else

p=p->dr;

}

}

void main()

{

Huffman *msg;

msg=new Huffman;

codHuffman(msg);

decodHuffman(msg);

getch();

}

nu mi se aloca memorie pt huffman->text_lung..... si am intrat si cu debug si prea inteleg eroarea....va rog...am nevoie de ajutor

Redondo690 mesaje#2

nu e de la huffman->text_lung

e gresita implementarea. ai citire din fisier si dupa ai citire si din linia de comanda care cere mesajul de mai multe ori...

trebuie sa o iei pas cu pas, sa vezi cum se muta datele prin structuri, cum se construieste arborele..

si da-i si delete dupa ce ai dat new..

si corect e "Dati mesajul care urmeaza a fi codat:"

ti-am atasat un pdf(cu teorie) sa vezi cum se construieste arborele

Codificare Huffman Folosind Arbori Binari.pdf

Editat Sâm 23 iun 2012 11:05

← Înapoi la Programare