哪位帮忙调试一下这个程序(自定义类型的问题)
哪位帮忙调试一下这个程序
自定义类型的问题
但我实在不知道错在哪里
请各位高手帮忙
这是一个哈夫曼编码程序
#define MAXVALUE 1000
#define MAXCHAR 100 //最大输入字符个数
#define MAXBIT 10
#include <stdio.h>
#include <string.h>
#include <ctype.h>
#include <malloc.h>
typedef struct hnodetype
{
int weight;
int flag;
int parent;
int lchild;
int rchild;
char node;
}hnodetype;
typedef struct hcodetype
{
int bit[MAXBIT];
int end;
}hcodetype;
typedef struct temptype
{
int weight;
char node;
}temptype;
int bianma()
{
int n,count,i,j;
char input[MAXCHAR],temp;
printf("请输入欲编码字符:\n");
for(count=0;(temp=getchar())!='\r';count++) //输入字符
{
input[count]=temp;
}
input[count+1]='\0';
temptype *strtemp;
strtemp=(temptype *)malloc((n+1)*sizeof(temptype));
for(i=0;i<count+1;i++)//求叶节点的字符和权值
{
int m=1,k;
for(j=i+1;j<count+1;j++)
{
if(input[i]==input[j])
{
for(k=j;input[k]!='\0';k++)
{
input[k]=input[k+1];
}
count--;
m++;
}
}
strtemp[i].weight=m;
strtemp[i].node=input[i];
}
n=count+1;
hnodetype *huffnode;
huffnode=(hnodetype *)malloc((2*n-1)*sizeof(hnodetype));
hcodetype *huffcode;
huffcode=(hcodetype *)malloc(n*sizeof(hcodetype));
for(i=0;i<2*n-1;i++)
{
huffnode[i].weight=0;
huffnode[i].parent=0;
huffnode[i].flag=0;
huffnode[i].lchild=-1;
huffnode[i].rchild=-1;
huffnode[i].node='\0';
}
for(i=0;i<n;i++)
{
huffnode[i].weight=strtemp[i].weight;
huffnode[i].node=strtemp[i].node;
}
for(i=0;i<n-1;i++)//构造哈夫曼树
{
int m2,m2,x1,x2;
m1=m2=MAXVALUE;
x1=x2=0;
for(j=0;j<n+i;j++)
{
if(huffnode[j].weight<m1&huffnode[j].flag==0)
{
m2=m1;
x2=x1;
m1=huffnode[j].weight;
x1=j;
}
else if(huffnode[j].weight<m2&&huffnode[j].flag==0)
{
m2=huffnode[j].weight;
x2=j;
}
}
huffnode[x1].parent=n+i;
huffnode[x2].parent=n+i;
huffnode[x1].flag=1;
huffnode[x2].flag=1;
huffnode[n+i].weight=huffnode[x1].weight+huffnode[x2].weight;
huffnode[n+i].lchild=x1;
huffnode[n+i].rchild=x2;
}
for(i=0;i<n;i++)
{
int p,c;
huffcode[i].end=0;
c=i;
p=huffnode[i].parent;
while(p!=0)
{
if(huffnode[p].lchild==c)
huffcode[i].bit[huffcode[i].end]=0;
else
huffcode[i].bit[huffcode[i].end]=1;
huffcode[i].end++;
c=p;
p=huffnode[c].parent;
}
}
FILE *fp;
if((fp=fopen("huffcode.huf","wb"))==NULL)
{printf("不能打开文件!\n");
return 0;
}
fprintf(fp,"%d",n);//将哈夫曼编码和字符的对应信息输出到文件
for(i=0;i<n;i++)
{
fprintf(fp,"%c",huffnode[i].node);
fprintf(fp,"%d",huffcode[i].end);
for(j=huffcode[i].end-1;j>=0;j--)
{
fprintf(fp,"%d",huffcode[i].bit[j]);
}
}
fclose(fp);
for(i=0;i<n;i++)//屏幕输出
{
printf("%c\t\t\t",huffnode[i].node);
for(j=huffcode[i].end-1;j>=0;j--)
{
printf("%1d",huffcode[i].bit[j]);
}
printf("\n");
}
}
int yima()
{
FILE *fp;
int count=0,i,j,n,m;
if((fp=fopen("huffcode.huf","rb"))==NULL)
{
printf("不能打开文件!\n");
return 0;
}
fscanf(fp,"%d",&n);
hnodetype *huffnode;
huffnode=(hnodetype *)malloc((2*n-1)*sizeof(hnodetype));
hcodetype *huffcode,*huffcodein;
huffcode=(hcodetype *)malloc(n*sizeof(hcodetype));
huffcodein=(hcodetype *)malloc(n*sizeof(hcodetype));
for(i=0;i<2*n-1;i++)
{
huffnode[i].weight=0;
huffnode[i].parent=0;
huffnode[i].flag=0;
huffnode[i].lchild=-1;
huffnode[i].rchild=-1;
huffnode[i].node='\0';
}
for(i=0;i<n;i++)
{
fscanf(fp,"%c",&huffnode[i].node);
fscanf(fp,"%d",&huffcode[i].end);
for(j=o;j<huffcode[i].end;j++)
{
fscanf(fp,"%d",&huffcode[i].bit[j]);
}
}
fclose(fp);
printf("\n请输入欲翻译的哈夫曼编码的个数:\n");
scanf("%d",&m);
printf("请输入哈夫曼编码 以逗号间隔!");
for(i=0;i<m;i++)
{
count=0;
for(j=0;!isspace(getchar());j++)
{
scanf("%1d",&huffcodein[i].bit[j]);
count++;
}
huffcodein[i].end=count;
}
for(i=0;i<n;i++)
{
int k;
k=huffcodein[i].end-1;
if(huffcode[i].end==huffcodein[i].end)
{
for(j=0;j<huffcode[i].end;j++)
{
k--;
if(huffcode[i].bit[j]!=huffcodein[i].bit[j])
break;
}
if(k=0)
printf("%c",huffnode[i].node);
}
}
}
main()
{
bianma();
yima();
}
自定义类型的问题
但我实在不知道错在哪里
请各位高手帮忙
这是一个哈夫曼编码程序
#define MAXVALUE 1000
#define MAXCHAR 100 //最大输入字符个数
#define MAXBIT 10
#include <stdio.h>
#include <string.h>
#include <ctype.h>
#include <malloc.h>
typedef struct hnodetype
{
int weight;
int flag;
int parent;
int lchild;
int rchild;
char node;
}hnodetype;
typedef struct hcodetype
{
int bit[MAXBIT];
int end;
}hcodetype;
typedef struct temptype
{
int weight;
char node;
}temptype;
int bianma()
{
int n,count,i,j;
char input[MAXCHAR],temp;
printf("请输入欲编码字符:\n");
for(count=0;(temp=getchar())!='\r';count++) //输入字符
{
input[count]=temp;
}
input[count+1]='\0';
temptype *strtemp;
strtemp=(temptype *)malloc((n+1)*sizeof(temptype));
for(i=0;i<count+1;i++)//求叶节点的字符和权值
{
int m=1,k;
for(j=i+1;j<count+1;j++)
{
if(input[i]==input[j])
{
for(k=j;input[k]!='\0';k++)
{
input[k]=input[k+1];
}
count--;
m++;
}
}
strtemp[i].weight=m;
strtemp[i].node=input[i];
}
n=count+1;
hnodetype *huffnode;
huffnode=(hnodetype *)malloc((2*n-1)*sizeof(hnodetype));
hcodetype *huffcode;
huffcode=(hcodetype *)malloc(n*sizeof(hcodetype));
for(i=0;i<2*n-1;i++)
{
huffnode[i].weight=0;
huffnode[i].parent=0;
huffnode[i].flag=0;
huffnode[i].lchild=-1;
huffnode[i].rchild=-1;
huffnode[i].node='\0';
}
for(i=0;i<n;i++)
{
huffnode[i].weight=strtemp[i].weight;
huffnode[i].node=strtemp[i].node;
}
for(i=0;i<n-1;i++)//构造哈夫曼树
{
int m2,m2,x1,x2;
m1=m2=MAXVALUE;
x1=x2=0;
for(j=0;j<n+i;j++)
{
if(huffnode[j].weight<m1&huffnode[j].flag==0)
{
m2=m1;
x2=x1;
m1=huffnode[j].weight;
x1=j;
}
else if(huffnode[j].weight<m2&&huffnode[j].flag==0)
{
m2=huffnode[j].weight;
x2=j;
}
}
huffnode[x1].parent=n+i;
huffnode[x2].parent=n+i;
huffnode[x1].flag=1;
huffnode[x2].flag=1;
huffnode[n+i].weight=huffnode[x1].weight+huffnode[x2].weight;
huffnode[n+i].lchild=x1;
huffnode[n+i].rchild=x2;
}
for(i=0;i<n;i++)
{
int p,c;
huffcode[i].end=0;
c=i;
p=huffnode[i].parent;
while(p!=0)
{
if(huffnode[p].lchild==c)
huffcode[i].bit[huffcode[i].end]=0;
else
huffcode[i].bit[huffcode[i].end]=1;
huffcode[i].end++;
c=p;
p=huffnode[c].parent;
}
}
FILE *fp;
if((fp=fopen("huffcode.huf","wb"))==NULL)
{printf("不能打开文件!\n");
return 0;
}
fprintf(fp,"%d",n);//将哈夫曼编码和字符的对应信息输出到文件
for(i=0;i<n;i++)
{
fprintf(fp,"%c",huffnode[i].node);
fprintf(fp,"%d",huffcode[i].end);
for(j=huffcode[i].end-1;j>=0;j--)
{
fprintf(fp,"%d",huffcode[i].bit[j]);
}
}
fclose(fp);
for(i=0;i<n;i++)//屏幕输出
{
printf("%c\t\t\t",huffnode[i].node);
for(j=huffcode[i].end-1;j>=0;j--)
{
printf("%1d",huffcode[i].bit[j]);
}
printf("\n");
}
}
int yima()
{
FILE *fp;
int count=0,i,j,n,m;
if((fp=fopen("huffcode.huf","rb"))==NULL)
{
printf("不能打开文件!\n");
return 0;
}
fscanf(fp,"%d",&n);
hnodetype *huffnode;
huffnode=(hnodetype *)malloc((2*n-1)*sizeof(hnodetype));
hcodetype *huffcode,*huffcodein;
huffcode=(hcodetype *)malloc(n*sizeof(hcodetype));
huffcodein=(hcodetype *)malloc(n*sizeof(hcodetype));
for(i=0;i<2*n-1;i++)
{
huffnode[i].weight=0;
huffnode[i].parent=0;
huffnode[i].flag=0;
huffnode[i].lchild=-1;
huffnode[i].rchild=-1;
huffnode[i].node='\0';
}
for(i=0;i<n;i++)
{
fscanf(fp,"%c",&huffnode[i].node);
fscanf(fp,"%d",&huffcode[i].end);
for(j=o;j<huffcode[i].end;j++)
{
fscanf(fp,"%d",&huffcode[i].bit[j]);
}
}
fclose(fp);
printf("\n请输入欲翻译的哈夫曼编码的个数:\n");
scanf("%d",&m);
printf("请输入哈夫曼编码 以逗号间隔!");
for(i=0;i<m;i++)
{
count=0;
for(j=0;!isspace(getchar());j++)
{
scanf("%1d",&huffcodein[i].bit[j]);
count++;
}
huffcodein[i].end=count;
}
for(i=0;i<n;i++)
{
int k;
k=huffcodein[i].end-1;
if(huffcode[i].end==huffcodein[i].end)
{
for(j=0;j<huffcode[i].end;j++)
{
k--;
if(huffcode[i].bit[j]!=huffcodein[i].bit[j])
break;
}
if(k=0)
printf("%c",huffnode[i].node);
}
}
}
main()
{
bianma();
yima();
}
...全文
相关推荐
易语言DLL返回自定义类型源码-易语言
DLL函数可以抛出异常,调用端需要捕获这些异常,以处理可能出现的问题。7. **编译与调试**:创建好DLL源码后,需要编译生成DLL文件。同时,为了确保DLL与调用程序之间的交互正确,需要进行调试。
传智播客struts2.1源代码_自定义类型转换器
**调试与测试** 调试自定义类型转换器通常涉及到设置断点,观察转换过程中的变量状态,以及通过单元测试确保转换逻辑的正确性。
易语言源码子程序返回数组演示(自定义类型实现)
当需要通过子程序返回一组具有相同结构的数据时,单纯使用基本数据类型(如整数型、文本型)构成的数组存在局限性,无法承载多字段、多属性的复杂信息。因此,自定义类型成为解决该问题的关键技术路径。
调试查看器
无论是List<T>还是自定义类型的列表,都能进行遍历和分析。用户可以查看每个元素的值,甚至可以直接编辑它们,实时观察修改对程序行为的影响。
Ibatis调用Oracle存储过程返回自定义类型
-%m%nlog4j.logger.java.sql.PreparedStatement=DEBUGlog4j.logger.com=DEBUG```上述日志配置用于记录执行SQL时的详细信息,有助于调试和追踪问题


