user-0 提问: C语言的求两个集合的合并运算—新手求dalao解答疑问

回复:
user: user-0

user: user-0
content: 请问一下，我现在把length的长度表现出来，发现长度有问题，结果也只是第一个集合的元素，请问该怎么改

user: user-1
content: 仅供参考：#include <algorithm> #include <iostream> #include <functional> #include <cstring> using namespace std; int main() {     char *Alphabet = "abcdefghijklmnopqrstuvwxyz" ;     char *Vowels   = "aeiou" ;     char *AlphaNum = "0123456789abcdef" ;     char result[45] ;     char *last ;      int lenA  = strlen(Alphabet) ;     int lenV  = strlen(Vowels  ) ;     int lenAN = strlen(AlphaNum) ;      cout << "Alphabet = " << Alphabet << endl ;     cout << "Vowels   = " << Vowels   << endl ;     cout << "AlphaNum = " << AlphaNum << endl ;      cout << "\nusing non-predicate versions" << endl ;      //non-predicate set_difference     last = set_difference(Alphabet, Alphabet+lenA,                           AlphaNum, AlphaNum+lenAN,                           result) ;     *last = 0 ;     cout << "set_difference(Alphabet, AlphaNum) =  " << result << endl ;      //non-predicate set_intersection     last = set_intersection(Alphabet, Alphabet+lenA,                             AlphaNum, AlphaNum+lenAN,                             result) ;     *last = 0 ;     cout << "set_intersection(Alphabet, AlphaNum) =  " << result << endl ;      //non-predicate set_symmetric_difference     last = set_symmetric_difference(Alphabet, Alphabet+lenA,                                     Vowels  , Vowels  +lenV,                                     result) ;     *last = 0 ;     cout << "set_symmetric_difference(Alphabet, Vowels) =  " << result << endl ;      //non-predicate set_union     last = set_union(Alphabet, Alphabet+lenA,                      AlphaNum, AlphaNum+lenAN,                      result) ;     *last = 0 ;     cout << "set_union(Alphabet, AlphaNum) =  " << result << endl ;      cout << "\nusing predicate versions" << endl ;      //predicate set_difference     last = set_difference(Alphabet, Alphabet+lenA,                           AlphaNum, AlphaNum+lenAN,                           result  , less<char>()) ;     *last = 0 ;     cout << "set_difference(Alphabet, AlphaNum) =  " << result << endl ;      //predicate set_intersection     last = set_intersection(Alphabet, Alphabet+lenA,                             AlphaNum, AlphaNum+lenAN,                             result  , less<char>()) ;     *last = 0 ;     cout << "set_intersection(Alphabet, AlphaNum) =  " << result << endl ;      //predicate set_symmetric_difference     last = set_symmetric_difference(Alphabet, Alphabet+lenA,                                     Vowels  , Vowels  +lenV,                                     result  , less<char>()) ;     *last = 0 ;     cout << "set_symmetric_difference(Alphabet, Vowels) =  " << result << endl ;      //predicate set_union     last = set_union(Alphabet, Alphabet+lenA,                      AlphaNum, AlphaNum+lenAN,                      result  , less<char>()) ;     *last = 0 ;     cout << "set_union(Alphabet, AlphaNum) =  " << result << endl ;      return 0 ; }

user: user-2
content: 好好检查下你程序的各个子函数的功能，先确定下有没有逻辑错误，再检查其他的错误，编好程序后，少输入一些数据，单步测试一下。

user: user-3
content: 错误太多了， 比如说 i = 0; ch=0; while (ch!= '\n') { scanf("%d", &La.data[i]); ch=getchar(); } 你的i一直是0 ,所以一直输入的是La的第一个元素，还有输入后La的length信息没有赋值，所以La的长度信息不对，Lb同La，以后union计算肯定出错

user: user-0
content: 引用 13 楼 dark9527 的回复:你的链表没有初始化void init_list(SqList* l) {  	memset(l->data,0,MAXSIZE); 	l->length=0; } 我初始化后没乱码， 但是没有结果

user: user-2
content: 你的链表没有初始化void init_list(SqList* l) {  	memset(l->data,0,MAXSIZE); 	l->length=0; }

user: user-0
content: 好吧，谢谢了......

user: user-4
content: 引用 9 楼 Reimu__Hakurei的回复:看得见，我是初始化了的，for (i = 0; i < La.length; i++)， 在上课，不好改代码。

user: user-4
content: int main(void) { SqList La,Lb; int i; char ch; printf("请输入集合1的元素:"); i = 0; ch=0; while (ch!= '\n') { scanf("%d", &La.data[i]); ch=getchar(); } printf("请输入集合2的元素:");  i = 0; ch=0; while (ch!= '\n') { scanf("%d", &Lb.data[i]); ch=getchar(); } Union(&La,Lb); printf("集合合并后的所有元素:");  for (i = 0; i < La.Listlength(); i++) { printf("%d ", La.data[i]); } return 0; }   void Union(SqList *La,SqList Lb) { /* 将所有在表Lb中但不在La中的数据元素插入到La中 */    ElemType e;    int La_len,Lb_len;    int i;    La_len = ListLength(*La); /* 求表La的长度 */    Lb_len = ListLength(Lb);    for(i = 0; i <= Lb_len; i++)    {      GetElem(Lb, i, &e); /* 取Lb中第i个数据元素赋给e */      if(LocateElem(*La, e)!=1)/* La中不存在和e相同的元素,则插入之 */      {         ListInsert(La, ++La_len, e);      }     } }

user: user-0
content: 看得见，我是初始化了的，for (i = 0; i < La.length; i++)，

user: user-4
content: for (i = 0; i < La.Listlength(); i++) { printf("%d ", La.data[i]); } 还有合并哪里  i初始化为0

user: user-4
content: for (i = 0; i < La.Listlength; i++) { printf("%d ", La.data[i]); }

user: user-4
content: 兄弟，你看的到上面的吗?不知道为什么回复只有一点点内容显示

user: user-0
content: 改后变成这样，还是有问题..... =

user: user-4
content: SqList La,Lb; memset(La.data,0,sizeof(int)*50); memset(Lb.data,0,sizeof(int)*50); 感觉就是没初始化

user: user-0
content: 还有，那个请问可以把改的东西发一下吗

user: user-0
content: 忘传运行结果了

user: user-4
content: 你好！可以看下data数组有没有全部初始化为0。