编制一个能演示执行集合的并,交和差运算的程序,,我写了个,不对求大神帮忙改下

回家uii 2015-12-19 07:46:44
(1)集合的元素必须为小写。
(2)以用户和计算机对话的形式执行。
(3)集合1:maganize 集合2:paper
1并2:aegimnprz; 1交2:ae 1差2:gimnz
这是我写的,可以运行,输出不对,求改三个运算函数


#include<iostream>
using namespace std;
#include<stdlib.h>
typedef struct LNode//结构体
{
char data;
struct LNode *next;
}*linklist;
void in_put(linklist &head)//输入
{
linklist p;
char tmp=cin.get();
cin>>tmp;
while(tmp!='\n')
{ p=(linklist)malloc(sizeof(struct LNode));
p->data=tmp;
p->next=head->next;
head->next=p;
tmp=cin.get();
}
}
void out_put(linklist head)//输出
{
linklist p;
p=head->next;
while(p!=NULL)
{
cout<<p->data;
p=p->next;
}
cout<<"\n";
}
void bingji(linklist head1,linklist head2,linklist head3)//集合的并集
{
linklist p1;
linklist p2;
linklist p3;
p1=head1->next;
while(p1!=NULL)
{
p3=(linklist)malloc(sizeof(struct LNode));
p3->data=p1->data;
p3->next=head3->next;
head3->next=p3;
p1=p1->next;
}
p2=head2->next;
while(p2!=NULL)
{
p1=head1->next;
while((p1!=NULL)&&(p1->data!=p2->data))
p1=p1->next;
if (p1==NULL)
{
p3=(linklist)malloc(sizeof(struct LNode));
p3->data=p2->data;
p3->next=head3->next;
head3->next=p3;
}
p2=p2->next;
}
}
void jiaoji(linklist head1,linklist head2,linklist head3)//集合的交集
{
linklist p1,p2,p3;
p1=head1->next;
while(p1!=NULL)
{
p2=head2->next;
while((p2!=NULL)&&(p2->data!=p1->data))
p2=p2->next;
if((p2!=NULL)&&(p2->data==p1->data))
{
p3=(linklist)malloc(sizeof(struct LNode));
p3->data=p1->data;
p3->next=head3->next;
head3->next=p3;
}
p1=p1->next;
}
}
void chaji(linklist head1,linklist head2,linklist head3)//集合的差集
{
linklist p1,p2,p3;
p1=head1->next;
while(p1!=NULL)
{
p2=head2->next;
while((p2!=NULL)&&(p2->data!=p1->data))
p2=p2->next;
if(p2==NULL)
{
p3=(linklist)malloc(sizeof(struct LNode));
p3->data=p1->data;
p3->next=head3->next;
head3->next=p3;
}
p1=p1->next;
}
}
int main()//主函数
{
int x;
int i;
linklist head1,head2,head3;
head1=(linklist)malloc(sizeof(struct LNode));
head1->next=NULL;
head2=(linklist)malloc(sizeof(struct LNode));
head2->next=NULL;
head3=(linklist)malloc(sizeof(struct LNode));
head3->next=NULL;
cout<<"请输入集合1:";
in_put(head1);
cout<<"请输入集合2:";
in_put(head2);
do
{cout<<"1.并集\n";
cout<<"2.交集\n";
cout<<"3.差集\n";
cout<<"0.退出程序\n";
cin>>x;
switch(x)
{case 1:
cout<<"两集合的并是";
bingji(head1,head2,head3);//并集
out_put(head3);
head3->next=NULL;
break;
case 2:
cout<<"两集合的交是";
jiaoji(head1,head2,head3);//交集
out_put(head3);
head3->next=NULL;
break;
case 3:
cout<<"两集合的差是";
chaji(head1,head2,head3);//差集
out_put(head3);
head3->next=NULL;
break;
case 4:
break;
default:
cout<<"选择错误,请重新输入\n";
break;}
}
while(x!=4);
}
...全文
1115 10 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
10 条回复
切换为时间正序
请发表友善的回复…
发表回复
赵4老师 2015-12-21
  • 打赏
  • 举报
回复
仅供参考:
#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 ;
}
yhlzxq 2015-12-20
  • 打赏
  • 举报
回复
删了出错了,应该注意
paschen 版主 2015-12-19
  • 打赏
  • 举报
回复
引用 7 楼 u012938616 的回复:
主要是那三个交并差函数有点问题,,帮忙看看
你是不是指有重复元素 看了下你代码,重复元素是来源于你head1与head2里本身有重复的 而你在添加到head3的时候没有判断(你只判断了head2中的元素如果head1中已经有了则不添加) 改的思路可以是:在添加到head3中时判断,如果已经在head3中了,则不添加 不知你有没明白我意思,睡觉了,你先改下,还有问题再回复
回家uii 2015-12-19
  • 打赏
  • 举报
回复
主要是那三个交并差函数有点问题,,帮忙看看
paschen 版主 2015-12-19
  • 打赏
  • 举报
回复
什么错啊,不会啊
回家uii 2015-12-19
  • 打赏
  • 举报
回复
删了出错了。。
paschen 版主 2015-12-19
  • 打赏
  • 举报
回复
删了你程序好像可以出来
回家uii 2015-12-19
  • 打赏
  • 举报
回复
哦哦,郁闷,必须让用有序链表
paschen 版主 2015-12-19
  • 打赏
  • 举报
回复
in_put中删掉 cin>>tmp; 这句啊
paschen 版主 2015-12-19
  • 打赏
  • 举报
回复
等下帮你看下,不过感觉这种没必要用链表会更简洁高效

65,187

社区成员

发帖
与我相关
我的任务
社区描述
C++ 语言相关问题讨论,技术干货分享,前沿动态等
c++ 技术论坛(原bbs)
社区管理员
  • C++ 语言社区
  • encoderlee
  • paschen
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
  1. 请不要发布与C++技术无关的贴子
  2. 请不要发布与技术无关的招聘、广告的帖子
  3. 请尽可能的描述清楚你的问题,如果涉及到代码请尽可能的格式化一下

试试用AI创作助手写篇文章吧