user-0 提问: 大神，求救哈夫曼树编码问题中的strcpy问题

回复:
user: user-1
content: 引用 4 楼 qq_34471305 的回复:[quote=引用 2 楼 cfjtaishan 的回复:]  #include<iostream>                                                                                                                                                                                          2 #include<string.h>   3     4 using namespace std;   5     6 #define n 8//此n为叶子的个数   7     8 typedef    char**    HuffmanCode;//表示HuffmanCode是char**的别名   9    10 typedef struct  11 {  12     int weight;  13     int parent, lchild, rchild;  14 }HTNode, *HuffmanTree;  15    16 //初始化数组  17 void Initial_HT(HTNode a[])  18 {  19     for (int i = 1; i < 2 * n; i++){  20     |   a[i].weight = a[i].parent = a[i].lchild = a[i].rchild = 0;  21     }  22 }  23    24    25 //挑选其中两个权值最小的  26 void Select_HT(HTNode a[], int K, int &M, int &N)//这个函数有问题  27 {  28     int min = 99999;  29     //这个赋值要满足不是原来的最小值，这就难办了，也可以这样说，这个最小值其父母一定是为零的,这个范围必定是在  30     for (int i = 1; i < K + 1; i++)  31     |   if (a[i].weight < min&&a[i].parent == 0){  32     |   |   min = a[i].weight;  33     |   |   M = i;  34     |   }  35     int min2 = 99999;  36     for (int j = 1; j < K + 1; j++){  37     |   if (min2> a[j].weight&&a[j].parent == 0 && j != M){  38     |   |   min2 = a[j].weight;  39     |   |   N = j;  40     |   }  41     }  42 }  43    44 //创建一哈夫曼树  45 void Create_HT(HTNode a[])  46 {  47     cout << "输进叶子结点的权值:" << endl;  48     for (int i = 1; i < n + 1; i++){  49     |   cin >> a[i].weight;  50     }  51     int M = 0, N = 0;  52     for (int j = n + 1; j < 2 * n; j++){  53     |   Select_HT(a, j - 1, M, N);//其作用将实现的功能有返回最小的两个下标//忘了除去已有的父母的结点了  54     |   a[M].parent = a[N].parent = j;  55     |   a[j].lchild = M;      |   a[j].rchild = N;  45     |   a[j].weight = a[N].weight + a[M].weight;  44     }  43 }  42 void Print_HT(HTNode a[])  41 {  40     for (int i = 1; i < 2 * n; i++){  39     |   cout << a[i].weight << "   ";  38     }  37 }  36    35 void Serect_HT(HTNode a[], HuffmanCode &HC)//求哈夫曼编码  34 {  33     char *cd = NULL;  32     HC = new char*[n + 1];  31     cd = new char[n];  30     cd[n - 1] = '\0';  29     for (int i = 1; i <= n; i++){  28     |   int start = n - 1;  27     |   int f = a[i].parent;  26     |   int c = a[i].weight;  25     |   while (f != 0)  24     |   {  23     |   |   --start;  22     |   |   if (c == a[f].lchild)  21     |   |   |   cd[start] = '0';  20     |   |   else cd[start] = '1';  19     |   |   |   c = a[f].weight;  18     |   |   f = a[f].parent;  17     |   }  16     |   //cout << "sdgsfsd" << endl;  15     |   HC[i] = new char[n - start];  14     |   strcpy(HC[i],&cd[start]);  13     }  12     delete cd;  11 }  10     9     8 void Print_HC(HuffmanCode &HC)   7 {   6     for (int i = 1; i < n + 1; i++)   5     {   4     |   cout << HC[i]<<"  ";   3     }   2 }   1   102 int main(void)                                                                                                                                                                                              1 {   2     HTNode a[2 * n];   3     Initial_HT(a);   4     Create_HT(a);   5     Print_HT(a);   6     HuffmanCode HC = NULL;   7     Serect_HT(a, HC);   2     Print_HC(HC);   1     return 0; 112 }  strcpy编译时有问题，还是运行的时候有问题？ strcpy编译时有问题 [/quote] 头文件格式有问题吧，对于系统的函数和变量声明头文件要用<>符号；对于自定义的头文件使用""符号！

user: user-2
content: 仅帮整理代码格式： #include<iostream> #include<string.h>  using namespace std;  #define n 8//此n为叶子的个数  typedef    char**    HuffmanCode;//表示HuffmanCode是char**的别名  typedef struct {     int weight;     int parent, lchild, rchild; }HTNode, *HuffmanTree;  //初始化数组 void Initial_HT(HTNode a[]) {     for (int i = 1; i < 2 * n; i++){         a[i].weight = a[i].parent = a[i].lchild = a[i].rchild = 0;     } }   //挑选其中两个权值最小的 void Select_HT(HTNode a[], int K, int &M, int &N)//这个函数有问题 {     int min1;     min1 = 99999;     //这个赋值要满足不是原来的最小值，这就难办了，也可以这样说，这个最小值其父母一定是为零的,这个范围必定是在     for (int i = 1; i < K + 1; i++)         if (a[i].weight < min1 && a[i].parent == 0){             min1 = a[i].weight;             M = i;         }      int min2;     min2 = 99999;     for (int j = 1; j < K + 1; j++){         if (a[j].weight < min2 && a[j].parent == 0 && j != M){             min2 = a[j].weight;             N = j;         }     } }  //创建一哈夫曼树 void Create_HT(HTNode a[]) {     cout << "输进叶子结点的权值:" << endl;     for (int i = 1; i < n + 1; i++){         cin >> a[i].weight;     }     int M = 0, N = 0;     for (int j = n + 1; j < 2 * n; j++){         Select_HT(a, j - 1, M, N);//其作用将实现的功能有返回最小的两个下标//忘了除去已有的父母的结点了         a[M].parent = a[N].parent = j;         a[j].lchild = M;         a[j].rchild = N;         a[j].weight = a[N].weight + a[M].weight;     } } void Print_HT(HTNode a[]) {     for (int i = 1; i < 2 * n; i++){         cout << a[i].weight << "   ";     } }  void Serect_HT(HTNode a[], HuffmanCode &HC)//求哈夫曼编码 {     char *cd = NULL;     HC = new char*[n + 1];     cd = new char[n];     cd[n - 1] = '\0';     for (int i = 1; i <= n; i++){         int start = n - 1;         int f = a[i].parent;         int c = a[i].weight;         while (f != 0)         {             --start;             if (c == a[f].lchild)                 cd[start] = '0';             else cd[start] = '1';                 c = a[f].weight;             f = a[f].parent;         }         //cout << "sdgsfsd" << endl;         HC[i] = new char[n - start];         strcpy(HC[i],&cd[start]);     }     delete cd; }   void Print_HC(HuffmanCode &HC) {     for (int i = 1; i < n + 1; i++)     {         cout << HC[i]<<"  ";     } }  int main(void) {     HTNode a[2 * n];     Initial_HT(a);     Create_HT(a);     Print_HT(a);     HuffmanCode HC = NULL;     Serect_HT(a, HC);     Print_HC(HC);     return 0; }   我这边试运行了一下： 提示：输进叶子结点的权值: 输入：1 2 3 4 5 6 7 8 输出：1   2   3   4   5   6   7   8   3   6   9   12   15   21   36   11000  11001  1100  110  111  110  10  11

user: user-2
content: 这样是否就对了？ //挑选其中两个权值最小的 void Select_HT(HTNode a[], int K, int &M, int &N) {     int wi[2*n],wt[2*n],t;     for (int i=1;i<K+1;i++) {         wi[i]=i;         wt[i]=a[i].weight;     }      for (int i=1;i<K;i++) {         for (int j=i+1;j<K+1;j++) {             if (wt[i]>wt[j]) {                 t=wt[i];wt[i]=wt[j];wt[j]=t;                 t=wi[i];wi[i]=wi[j];wi[j]=t;             }         }     }     M=wi[1];     N=wi[2]; }

user: user-2
content: #pragma warning(disable:4996)

user: user-1
content: #include<iostream>                                                                                                                                                                                          2 #include<string.h>   3     4 using namespace std;   5     6 #define n 8//此n为叶子的个数   7     8 typedef    char**    HuffmanCode;//表示HuffmanCode是char**的别名   9    10 typedef struct  11 {  12     int weight;  13     int parent, lchild, rchild;  14 }HTNode, *HuffmanTree;  15    16 //初始化数组  17 void Initial_HT(HTNode a[])  18 {  19     for (int i = 1; i < 2 * n; i++){  20     |   a[i].weight = a[i].parent = a[i].lchild = a[i].rchild = 0;  21     }  22 }  23    24    25 //挑选其中两个权值最小的  26 void Select_HT(HTNode a[], int K, int &M, int &N)//这个函数有问题  27 {  28     int min = 99999;  29     //这个赋值要满足不是原来的最小值，这就难办了，也可以这样说，这个最小值其父母一定是为零的,这个范围必定是在  30     for (int i = 1; i < K + 1; i++)  31     |   if (a[i].weight < min&&a[i].parent == 0){  32     |   |   min = a[i].weight;  33     |   |   M = i;  34     |   }  35     int min2 = 99999;  36     for (int j = 1; j < K + 1; j++){  37     |   if (min2> a[j].weight&&a[j].parent == 0 && j != M){  38     |   |   min2 = a[j].weight;  39     |   |   N = j;  40     |   }  41     }  42 }  43    44 //创建一哈夫曼树  45 void Create_HT(HTNode a[])  46 {  47     cout << "输进叶子结点的权值:" << endl;  48     for (int i = 1; i < n + 1; i++){  49     |   cin >> a[i].weight;  50     }  51     int M = 0, N = 0;  52     for (int j = n + 1; j < 2 * n; j++){  53     |   Select_HT(a, j - 1, M, N);//其作用将实现的功能有返回最小的两个下标//忘了除去已有的父母的结点了  54     |   a[M].parent = a[N].parent = j;  55     |   a[j].lchild = M;      |   a[j].rchild = N;  45     |   a[j].weight = a[N].weight + a[M].weight;  44     }  43 }  42 void Print_HT(HTNode a[])  41 {  40     for (int i = 1; i < 2 * n; i++){  39     |   cout << a[i].weight << "   ";  38     }  37 }  36    35 void Serect_HT(HTNode a[], HuffmanCode &HC)//求哈夫曼编码  34 {  33     char *cd = NULL;  32     HC = new char*[n + 1];  31     cd = new char[n];  30     cd[n - 1] = '\0';  29     for (int i = 1; i <= n; i++){  28     |   int start = n - 1;  27     |   int f = a[i].parent;  26     |   int c = a[i].weight;  25     |   while (f != 0)  24     |   {  23     |   |   --start;  22     |   |   if (c == a[f].lchild)  21     |   |   |   cd[start] = '0';  20     |   |   else cd[start] = '1';  19     |   |   |   c = a[f].weight;  18     |   |   f = a[f].parent;  17     |   }  16     |   //cout << "sdgsfsd" << endl;  15     |   HC[i] = new char[n - start];  14     |   strcpy(HC[i],&cd[start]);  13     }  12     delete cd;  11 }  10     9     8 void Print_HC(HuffmanCode &HC)   7 {   6     for (int i = 1; i < n + 1; i++)   5     {   4     |   cout << HC[i]<<"  ";   3     }   2 }   1   102 int main(void)                                                                                                                                                                                              1 {   2     HTNode a[2 * n];   3     Initial_HT(a);   4     Create_HT(a);   5     Print_HT(a);   6     HuffmanCode HC = NULL;   7     Serect_HT(a, HC);   2     Print_HC(HC);   1     return 0; 112 }  strcpy编译时有问题，还是运行的时候有问题？

user: user-0
content: 引用 5 楼 qq_34471305 的回复:[quote=引用 3 楼 zhao4zhong1 的回复:] #pragma warning(disable:4996)    什么意思？[/quote]  引用 2 楼 cfjtaishan 的回复: #include<iostream>                                                                                                                                                                                          2 #include<string.h>   3     4 using namespace std;   5     6 #define n 8//此n为叶子的个数   7     8 typedef    char**    HuffmanCode;//表示HuffmanCode是char**的别名   9    10 typedef struct  11 {  12     int weight;  13     int parent, lchild, rchild;  14 }HTNode, *HuffmanTree;  15    16 //初始化数组  17 void Initial_HT(HTNode a[])  18 {  19     for (int i = 1; i < 2 * n; i++){  20     |   a[i].weight = a[i].parent = a[i].lchild = a[i].rchild = 0;  21     }  22 }  23    24    25 //挑选其中两个权值最小的  26 void Select_HT(HTNode a[], int K, int &M, int &N)//这个函数有问题  27 {  28     int min = 99999;  29     //这个赋值要满足不是原来的最小值，这就难办了，也可以这样说，这个最小值其父母一定是为零的,这个范围必定是在  30     for (int i = 1; i < K + 1; i++)  31     |   if (a[i].weight < min&&a[i].parent == 0){  32     |   |   min = a[i].weight;  33     |   |   M = i;  34     |   }  35     int min2 = 99999;  36     for (int j = 1; j < K + 1; j++){  37     |   if (min2> a[j].weight&&a[j].parent == 0 && j != M){  38     |   |   min2 = a[j].weight;  39     |   |   N = j;  40     |   }  41     }  42 }  43    44 //创建一哈夫曼树  45 void Create_HT(HTNode a[])  46 {  47     cout << "输进叶子结点的权值:" << endl;  48     for (int i = 1; i < n + 1; i++){  49     |   cin >> a[i].weight;  50     }  51     int M = 0, N = 0;  52     for (int j = n + 1; j < 2 * n; j++){  53     |   Select_HT(a, j - 1, M, N);//其作用将实现的功能有返回最小的两个下标//忘了除去已有的父母的结点了  54     |   a[M].parent = a[N].parent = j;  55     |   a[j].lchild = M;      |   a[j].rchild = N;  45     |   a[j].weight = a[N].weight + a[M].weight;  44     }  43 }  42 void Print_HT(HTNode a[])  41 {  40     for (int i = 1; i < 2 * n; i++){  39     |   cout << a[i].weight << "   ";  38     }  37 }  36    35 void Serect_HT(HTNode a[], HuffmanCode &HC)//求哈夫曼编码  34 {  33     char *cd = NULL;  32     HC = new char*[n + 1];  31     cd = new char[n];  30     cd[n - 1] = '\0';  29     for (int i = 1; i <= n; i++){  28     |   int start = n - 1;  27     |   int f = a[i].parent;  26     |   int c = a[i].weight;  25     |   while (f != 0)  24     |   {  23     |   |   --start;  22     |   |   if (c == a[f].lchild)  21     |   |   |   cd[start] = '0';  20     |   |   else cd[start] = '1';  19     |   |   |   c = a[f].weight;  18     |   |   f = a[f].parent;  17     |   }  16     |   //cout << "sdgsfsd" << endl;  15     |   HC[i] = new char[n - start];  14     |   strcpy(HC[i],&cd[start]);  13     }  12     delete cd;  11 }  10     9     8 void Print_HC(HuffmanCode &HC)   7 {   6     for (int i = 1; i < n + 1; i++)   5     {   4     |   cout << HC[i]<<"  ";   3     }   2 }   1   102 int main(void)                                                                                                                                                                                              1 {   2     HTNode a[2 * n];   3     Initial_HT(a);   4     Create_HT(a);   5     Print_HT(a);   6     HuffmanCode HC = NULL;   7     Serect_HT(a, HC);   2     Print_HC(HC);   1     return 0; 112 }  strcpy编译时有问题，还是运行的时候有问题？ 没用啊

user: user-2
content: 引用 5 楼 qq_34471305 的回复:[quote=引用 3 楼 zhao4zhong1 的回复:] #pragma warning(disable:4996)    什么意思？[/quote] 屏蔽提示strcpy不安全提示，如果有。

user: user-0
content: 引用 2 楼 cfjtaishan 的回复: #include<iostream>                                                                                                                                                                                          2 #include<string.h>   3     4 using namespace std;   5     6 #define n 8//此n为叶子的个数   7     8 typedef    char**    HuffmanCode;//表示HuffmanCode是char**的别名   9    10 typedef struct  11 {  12     int weight;  13     int parent, lchild, rchild;  14 }HTNode, *HuffmanTree;  15    16 //初始化数组  17 void Initial_HT(HTNode a[])  18 {  19     for (int i = 1; i < 2 * n; i++){  20     |   a[i].weight = a[i].parent = a[i].lchild = a[i].rchild = 0;  21     }  22 }  23    24    25 //挑选其中两个权值最小的  26 void Select_HT(HTNode a[], int K, int &M, int &N)//这个函数有问题  27 {  28     int min = 99999;  29     //这个赋值要满足不是原来的最小值，这就难办了，也可以这样说，这个最小值其父母一定是为零的,这个范围必定是在  30     for (int i = 1; i < K + 1; i++)  31     |   if (a[i].weight < min&&a[i].parent == 0){  32     |   |   min = a[i].weight;  33     |   |   M = i;  34     |   }  35     int min2 = 99999;  36     for (int j = 1; j < K + 1; j++){  37     |   if (min2> a[j].weight&&a[j].parent == 0 && j != M){  38     |   |   min2 = a[j].weight;  39     |   |   N = j;  40     |   }  41     }  42 }  43    44 //创建一哈夫曼树  45 void Create_HT(HTNode a[])  46 {  47     cout << "输进叶子结点的权值:" << endl;  48     for (int i = 1; i < n + 1; i++){  49     |   cin >> a[i].weight;  50     }  51     int M = 0, N = 0;  52     for (int j = n + 1; j < 2 * n; j++){  53     |   Select_HT(a, j - 1, M, N);//其作用将实现的功能有返回最小的两个下标//忘了除去已有的父母的结点了  54     |   a[M].parent = a[N].parent = j;  55     |   a[j].lchild = M;      |   a[j].rchild = N;  45     |   a[j].weight = a[N].weight + a[M].weight;  44     }  43 }  42 void Print_HT(HTNode a[])  41 {  40     for (int i = 1; i < 2 * n; i++){  39     |   cout << a[i].weight << "   ";  38     }  37 }  36    35 void Serect_HT(HTNode a[], HuffmanCode &HC)//求哈夫曼编码  34 {  33     char *cd = NULL;  32     HC = new char*[n + 1];  31     cd = new char[n];  30     cd[n - 1] = '\0';  29     for (int i = 1; i <= n; i++){  28     |   int start = n - 1;  27     |   int f = a[i].parent;  26     |   int c = a[i].weight;  25     |   while (f != 0)  24     |   {  23     |   |   --start;  22     |   |   if (c == a[f].lchild)  21     |   |   |   cd[start] = '0';  20     |   |   else cd[start] = '1';  19     |   |   |   c = a[f].weight;  18     |   |   f = a[f].parent;  17     |   }  16     |   //cout << "sdgsfsd" << endl;  15     |   HC[i] = new char[n - start];  14     |   strcpy(HC[i],&cd[start]);  13     }  12     delete cd;  11 }  10     9     8 void Print_HC(HuffmanCode &HC)   7 {   6     for (int i = 1; i < n + 1; i++)   5     {   4     |   cout << HC[i]<<"  ";   3     }   2 }   1   102 int main(void)                                                                                                                                                                                              1 {   2     HTNode a[2 * n];   3     Initial_HT(a);   4     Create_HT(a);   5     Print_HT(a);   6     HuffmanCode HC = NULL;   7     Serect_HT(a, HC);   2     Print_HC(HC);   1     return 0; 112 }  strcpy编译时有问题，还是运行的时候有问题？ strcpy编译时有问题

user: user-0
content: 引用 3 楼 zhao4zhong1 的回复:#pragma warning(disable:4996)    什么意思？

user: user-3
content: 崩溃的时候在弹出的对话框按相应按钮进入调试，按Alt+7键查看Call Stack即“调用堆栈”里面从上到下列出的对应从里层到外层的函数调用历史。双击某一行可将光标定位到此次调用的源代码或汇编指令处，看不懂时双击下一行，直到能看懂为止。  代码功能归根结底不是别人帮自己看或讲解或注释出来的；而是被自己静下心来花足够长的时间和精力亲自动手单步或设断点或对执行到某步获得的中间结果显示或写到日志文件中一步一步分析出来的。 提醒：再牛×的老师也无法代替学生自己领悟和上厕所！ 单步调试和设断点调试（VS IDE中编译连接通过以后，按F10或F11键单步执行，按Shift+F11退出当前函数；在某行按F9设断点后按F5执行停在该断点处。）是程序员必须掌握的技能之一。