| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 556 人关注过本帖
标题:各位看看我的C++数据结构为什么出错呢???
只看楼主 加入收藏
qulongjun
Rank: 1
等 级:新手上路
帖 子:3
专家分:0
注 册:2012-2-23
结帖率:0
收藏
 问题点数:0 回复次数:3 
各位看看我的C++数据结构为什么出错呢???
程序代码:
#include<iostream>
#include<cstdio>
#include<cstdlib>
#include<cstring>
using namespace std;
typedef struct Node{          

    char *word;              

    int count;                

    int len;                  

    int weight;               

    struct Node *left,*right; 

}Node,*BiTree;                


typedef struct node{    

  char *word;
int weight;
int parent;
int lchild,rchild;

}HuffmanTree[9999];     

int *p;
int searchBst(BiTree T,char *keyword,BiTree *p)
{  

    int cmpres=0;
    BiTree ptr;
    *p=0;ptr=T;
    while(ptr)
    {
        cmpres=strcmp(ptr->word,keyword);
        if(cmpres==0){*p=ptr;return 0;}
        else{*p=ptr;ptr=cmpres>0? ptr->left: ptr->right;}
    }
    return -1;
}

int insertBst(BiTree *t,char *keyword)
{
    BiTree s,p;
    if(searchBst(*t,keyword,&p)==-1)
    {
        s=(Node *)malloc(sizeof(Node));
        if(!s){printf("存储分配失败!\n");return -1;}
        s->word=(char *)malloc(strlen(keyword));
        s->len=strlen(keyword);
        strcpy(s->word,keyword);
        s->left=0;s->right=0;s->count=1;s->weight=s->len;
        if(p==0) *t=s;
        else if(strcmp(p->word,keyword)<0)
                p->right=s;
        else p->left=s;
    }
    else {p->count++; p->weight=p->count*p->len;}
    return 0;
}

void FindWords(BiTree *root,char FileName[],char *c1[],int *counts1)
{
    char ch,*word,buffer[40],string[2];

    FILE *fin;                                   

    int i=0;

    fin=fopen(FileName,"r");
    if(!fin){printf("打开文件%s错误!\n",FileName);return;}
    while(!feof(fin))
    {
        ch=fgetc(fin);
        while((!feof(fin))&&(ch>='a'&&ch<='z')||(ch>='A'&&ch<='Z'))
        {buffer[i++]=ch;ch=fgetc(fin);}
       

        if(i!=0)
        {
            buffer[i]='\0';
            word=(char *)malloc(strlen(buffer));
            strcpy(word,buffer);
            c1[(*counts1)++]=word;
            if(insertBst(root,word)==-1)return;
             i=0;
        }
       

        if((!feof(fin))&&(ch!=' '))
        {
            string[0]=ch;
            string[1]='\0';

            word=(char *)malloc(2);
            strcpy(word,string);
            c1[(*counts1)++]=word;
            if(insertBst(root,word)==-1)return;
        }
        if(i!=0)
        {
            buffer[i]='\0';
            word=(char *)malloc(strlen(buffer));
            strcpy(word,buffer);
            c1[(*counts1)++]=word;
            if(insertBst(root,word)==-1)return;
             i=0;
        }
    }
    fclose(fin);

}


void InOrder(BiTree root,char *c[],int w[],int *counts)
{
   

    if(root)
    {
       InOrder(root->left,c,w,counts);
       c[*counts]=root->word; w[*counts]=root->weight; (*counts)++;
        InOrder(root->right,c,w,counts);
    }
}

void select(HuffmanTree HT,int *s1,int *s2,int n)
{   


    for(int i=0;i<n;i++)
        if(HT[i].parent==0)
            *s1=i;
    for(int i=0;i<n;i++)
          if((HT[i].parent==0)&&(HT[*s1].weight>HT[i].weight)) *s1=i;

    HT[*s1].parent=-1; 

    for(int j=0;j<n;j++)
        if(HT[j].parent==0)
            *s2=j;
    for(j=0;j<n;j++)
          if((HT[j].parent==0)&&(HT[*s2].weight>HT[j].weight)) *s2=j;
}

void createHTree(HuffmanTree HT,char *c[], int w[],int counts)
{
    int i,s1,s2;
    if(counts<=1) return;
    for(i=0;i<counts;i++)
    {   

    HT[i].word=c[i]; HT[i].weight=w[i];        HT[i].parent=HT[i].lchild=HT[i].rchild=0;
    }
    for(;i<2*counts-1;i++)
    {
        HT[i].parent=0;HT[i].lchild=0;HT[i].rchild=0;
    }
    for(i=counts;i<2*counts-1;i++)

    {
       select(HT,&s1,&s2,i); 

        HT[s1].parent=i;  HT[s2].parent=i;
        HT[i].lchild=s1;  HT[i].rchild=s2;
        HT[i].weight=HT[s1].weight+HT[s2].weight;
    }
}

void HuffmanCoding(HuffmanTree HT,char *HC[],char *c[],char *c1[],int counts,int counts1)
{
    char *cd;
    int i,start,cc,f;
    if(counts<=1) return;
    cd=(char *)malloc(counts);
    cd[counts-1]='\0';
    for(i=0;i<counts;i++)
    {
        start=counts-1;
        for(cc=i,f=HT[i].parent;f!=0;cc=f,f=HT[f].parent)
            if(HT[f].lchild==cc) cd[--start]='0';
            else cd[--start]='1';
        HC[i]=(char*)malloc(counts-start);
        strcpy(HC[i],&cd[start]);
    }
    free(cd);

    FILE *out;
    out=fopen("编码.txt","w");
    if(!out){printf("打开文件%s错误!\n","编码.txt");return;}
    printf("编码后的结果输出在文件:编码.txt中.\n");
    for( i=0;i<counts1;i++)
    {
       for(int j=0;j<counts;j++)
       {
           if(strcmp(c1[i],c[j])==0)
               fputs(HC[j],out);
       }   

    }
    fclose(out);
}

void Decoding(HuffmanTree HT,int counts)
{
     int i=0;
     char ch,buffer[9999];
    FILE *in;
     in=fopen("编码.txt","r");
     if(!in){printf("打开文件%s错误!\n","编码.txt");return;}
    while(!feof(in))
     {
        ch=fgetc(in);
        buffer[i++]=ch;
     }
    fclose(in);
    int j=i;
    int p=2*counts-2;
    FILE *fout;
     char null=' ';
     fout=fopen("译码.txt","w");
    if(!fout){printf("打开文件%s错误!\n","译码.txt");return;}
    printf("译码后的结果输出在文件:译码.txt中.\n");
     for(i=0;buffer[i]!='\0'&&i<j;i++)
     {
        if((buffer[i])=='0') p=HT[p].lchild;
        else p=HT[p].rchild;
        if(HT[p].lchild==0&&HT[p].rchild==0)
        {
            fputs(HT[p].word,fout);
            fputc(null,fout);
            p=2*counts-2;
        }
     }
    fclose(fout);
}


void main()
{
    BiTree root=0;
    HuffmanTree HT;
char *c[9999];
int w[9999]; 

char *c1[9999];
int counts=0;  

int counts1=0; 

char* HC[9999];
char FileName[40];
    printf("请输入要进行霍夫曼编码的英文文章的文件名:");
    gets(FileName);
    FindWords(&root,FileName,c1,&counts1);

    InOrder(root,c,w,&counts);      

createHTree(HT,c,w,counts);    

HuffmanCoding(HT,HC,c,c1,counts,counts1); 

    Decoding(HT,counts);       

}


搜索更多相关主题的帖子: word 数据 
2012-03-20 10:56
榴紫丫
Rank: 3Rank: 3
等 级:论坛游侠
帖 子:33
专家分:135
注 册:2011-11-3
收藏
得分:0 
for(int i=0;i<n;i++)
        if(HT[i].parent==0)
            *s1=i;
    for(int i=0;i<n;i++)
同一层的for循环,i重复定义,把第二个int 去掉就可以了!
2012-03-20 12:54
qulongjun
Rank: 1
等 级:新手上路
帖 子:3
专家分:0
注 册:2012-2-23
收藏
得分:0 
回复 2楼 榴紫丫
刚刚调试了一下,发现还是报错……

1>------ 已启动生成: 项目: 数据结构课程设计, 配置: Debug Win32 ------
1>生成启动时间为 2012/3/20 下午 4:34:51。
1>InitializeBuildStatus:
1>  正在对“Debug\数据结构课程设计.unsuccessfulbuild”执行 Touch 任务。
1>ClCompile:
1>  哈夫曼编码译码器.cpp
1>c:\users\qulongjun-pc\desktop\数据结构课程设计\数据结构课程设计\数据结构课程设计\哈夫曼编码译码器.cpp(44): warning C4996: 'strcpy': This function or variable may be unsafe. Consider using strcpy_s instead. To disable deprecation, use _CRT_SECURE_NO_WARNINGS. See online help for details.
1>          d:\microsoft visual studio 10.0\vc\include\string.h(105) : 参见“strcpy”的声明
1>c:\users\qulongjun-pc\desktop\数据结构课程设计\数据结构课程设计\数据结构课程设计\哈夫曼编码译码器.cpp(61): warning C4996: 'fopen': This function or variable may be unsafe. Consider using fopen_s instead. To disable deprecation, use _CRT_SECURE_NO_WARNINGS. See online help for details.
1>          d:\microsoft visual studio 10.0\vc\include\stdio.h(234) : 参见“fopen”的声明
1>c:\users\qulongjun-pc\desktop\数据结构课程设计\数据结构课程设计\数据结构课程设计\哈夫曼编码译码器.cpp(73): warning C4996: 'strcpy': This function or variable may be unsafe. Consider using strcpy_s instead. To disable deprecation, use _CRT_SECURE_NO_WARNINGS. See online help for details.
1>          d:\microsoft visual studio 10.0\vc\include\string.h(105) : 参见“strcpy”的声明
1>c:\users\qulongjun-pc\desktop\数据结构课程设计\数据结构课程设计\数据结构课程设计\哈夫曼编码译码器.cpp(85): warning C4996: 'strcpy': This function or variable may be unsafe. Consider using strcpy_s instead. To disable deprecation, use _CRT_SECURE_NO_WARNINGS. See online help for details.
1>          d:\microsoft visual studio 10.0\vc\include\string.h(105) : 参见“strcpy”的声明
1>c:\users\qulongjun-pc\desktop\数据结构课程设计\数据结构课程设计\数据结构课程设计\哈夫曼编码译码器.cpp(93): warning C4996: 'strcpy': This function or variable may be unsafe. Consider using strcpy_s instead. To disable deprecation, use _CRT_SECURE_NO_WARNINGS. See online help for details.
1>          d:\microsoft visual studio 10.0\vc\include\string.h(105) : 参见“strcpy”的声明
1>c:\users\qulongjun-pc\desktop\数据结构课程设计\数据结构课程设计\数据结构课程设计\哈夫曼编码译码器.cpp(118): error C2065: “i”: 未声明的标识符
1>c:\users\qulongjun-pc\desktop\数据结构课程设计\数据结构课程设计\数据结构课程设计\哈夫曼编码译码器.cpp(118): error C2065: “i”: 未声明的标识符
1>c:\users\qulongjun-pc\desktop\数据结构课程设计\数据结构课程设计\数据结构课程设计\哈夫曼编码译码器.cpp(118): error C2065: “i”: 未声明的标识符
1>c:\users\qulongjun-pc\desktop\数据结构课程设计\数据结构课程设计\数据结构课程设计\哈夫曼编码译码器.cpp(119): error C2065: “i”: 未声明的标识符
1>c:\users\qulongjun-pc\desktop\数据结构课程设计\数据结构课程设计\数据结构课程设计\哈夫曼编码译码器.cpp(119): error C2228: “.parent”的左边必须有类/结构/联合
1>c:\users\qulongjun-pc\desktop\数据结构课程设计\数据结构课程设计\数据结构课程设计\哈夫曼编码译码器.cpp(120): error C2065: “i”: 未声明的标识符
1>c:\users\qulongjun-pc\desktop\数据结构课程设计\数据结构课程设计\数据结构课程设计\哈夫曼编码译码器.cpp(128): error C2065: “j”: 未声明的标识符
1>c:\users\qulongjun-pc\desktop\数据结构课程设计\数据结构课程设计\数据结构课程设计\哈夫曼编码译码器.cpp(128): error C2065: “j”: 未声明的标识符
1>c:\users\qulongjun-pc\desktop\数据结构课程设计\数据结构课程设计\数据结构课程设计\哈夫曼编码译码器.cpp(128): error C2065: “j”: 未声明的标识符
1>c:\users\qulongjun-pc\desktop\数据结构课程设计\数据结构课程设计\数据结构课程设计\哈夫曼编码译码器.cpp(129): error C2065: “j”: 未声明的标识符
1>c:\users\qulongjun-pc\desktop\数据结构课程设计\数据结构课程设计\数据结构课程设计\哈夫曼编码译码器.cpp(129): error C2228: “.parent”的左边必须有类/结构/联合
1>c:\users\qulongjun-pc\desktop\数据结构课程设计\数据结构课程设计\数据结构课程设计\哈夫曼编码译码器.cpp(129): error C2065: “j”: 未声明的标识符
1>c:\users\qulongjun-pc\desktop\数据结构课程设计\数据结构课程设计\数据结构课程设计\哈夫曼编码译码器.cpp(129): error C2228: “.weight”的左边必须有类/结构/联合
1>c:\users\qulongjun-pc\desktop\数据结构课程设计\数据结构课程设计\数据结构课程设计\哈夫曼编码译码器.cpp(129): error C2065: “j”: 未声明的标识符
1>
1>生成失败。
1>
1>已用时间 00:00:02.66
========== 生成: 成功 0 个,失败 1 个,最新 0 个,跳过 0 个 ==========
2012-03-20 16:35
榴紫丫
Rank: 3Rank: 3
等 级:论坛游侠
帖 子:33
专家分:135
注 册:2011-11-3
收藏
得分:0 
你这个没有错,只不过你使用的是2010版本的,比如说 strcpy  函数,这个版本说可能不安全,可以改为strcpy_s  ,
用6.0的完全没有问题
2012-03-20 17:07
快速回复:各位看看我的C++数据结构为什么出错呢???
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.018567 second(s), 8 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved