[C++] Need help!!!

Intrebari despre limbajul C++, standardul C++, STL, OOP in C++ sau alte subiecte nelegate de VisualC++
Post Reply
Nosferatusoctavian
Junior
Junior
Posts: 23
Joined: 16 Jun 2012, 17:33
Judet: Bucureşti

[C++] Need help!!!

Post by Nosferatusoctavian » 22 Jun 2012, 22:32

Code: Select all

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);



#include<stdio.h>
#include<string.h>
#include<stdlib.h>
#include<conio.h>

#include"huff.h"
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[i].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[i],huffman))
		{
			huffman->chr[huffman->dim].chr=huffman->text_lung[i];
			huffman->chr[huffman->dim].val++;
			huffman->dim++;
		}
		else
		{
			for(j=1; j<=huffman->dim; j++)
				if(huffman->text_lung[i]==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[i].val<huffman->chr[j].val)
			{
				aux=huffman->chr[i];
				huffman->chr[i]=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(c)
	{
		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[i],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[i]=='0')
				p=p->st;
			else
				p=p->dr;

	}
}

void main()
{

	Huffman *msg;
	msg=new Huffman;
	codHuffman(msg);
	decodHuffman(msg);
	getch();
}
nu se aloca memorie pentru huffma->text lung ....



User avatar
Parametru
Junior
Junior
Posts: 6
Joined: 15 Jul 2012, 00:04
Judet: Buzău

Re: [C++] Need help!!!

Post by Parametru » 15 Jul 2012, 16:55

Nu prea inteleg ce vrei sa faci si ar fi fost bine sa ne dai si "huff.h" dar ma rog.

nod(){val=0;chr=NULL;st=NULL;dr=NULL;} Unde ai mai vazut asa ceva? Eu sincer nu am intalnit acest rand.

Post Reply