适用于河南大学软件学院并集、交集和差集的实验1(有错别怪我)
在实验初我有两种想法,一是使用数组好像很方便,二是使用链表;因为链表我还不是很熟悉所以我选择了链表(手动狗头)。
实验过程就是利用一个数和一个链表进行数据对比,根据集合的定义得出我们的结果,但是出现了一个小问题
由于链表并不是很熟悉没有实现链表中查找出重复数据直接重复输入而是利用人工进行下一步调出菜单选择栏时手动输入5结束程序(我觉得问题不大后期可以改进,但是我想一想好像还要加不少行代码暂时放下了后面假期有空再改进)
#include<stdio.h>
#include<iostream>
#define _CRT_SECURE_NO_WARNINGS
#include<stdlib.h>
using namespace std;
typedef struct node1
{
int data;
struct node1* next;
}node;
void menu(int *ch);
void xunhuan(int *tingzhi,int* shu,node *head1,node *head2);
node *create(void);
void chachong(int *tingzhi,node *head);
void jiaoji(node *head1,node *head2);
void bingji(node *head1,node *head2);
void chaji(node *head1,node *head2);
void output(node* head);
int main(void)
{
int shu;
int *tingzhi;
tingzhi=0;
node *head1,*head2;
menu(&shu);
xunhuan(tingzhi,&shu,head1,head2);
}
void menu(int* shu)
{
char a01[]="(1)输入两个数组开始操作(感觉多此一步)";
char a02[]="(2)求交集";
char a03[]="(3)求并集";
char a04[]="(4)求差集";
char a05[]="(5)退出";
printf("%-50s",a01);
printf("%-50s\n",a02);
printf("%-50s",a03);
printf("%-50s\n",a04);
printf("%-50s\n",a05);
cout<<"请输入你的选择:";
scanf("%d",shu);
// cout<<"您输入的数是"<<*shu<<endl;作为过程中的一个检测
}
void xunhuan(int *tingzhi,int* shu,node *head1,node *head2)
{
while(1)
{
if(*shu==1){
cout << "请输入数组一(输入负数时停止输入)" << endl;
head1=create();
chachong(tingzhi,head1);
if(*tingzhi==1)
{
break;
}
cout << "请输入数组二(输入负数时停止输入)" << endl;
head2=create();
chachong(tingzhi,head2);
if(*tingzhi==1)
{
break;
}
cout<<"请重新选择功能:";
cin>>*shu;
xunhuan(tingzhi,shu,head1,head2);
}else if(*shu==2){
jiaoji(head1,head2);
cout<<"请重新选择功能:";
cin>>*shu;
xunhuan(tingzhi,shu,head1,head2);}
else if(*shu==3){
bingji(head1,head2);
cout<<"请重新选择功能:";
cin>>*shu;
xunhuan(tingzhi,shu,head1,head2);
}else if(*shu==4){
chaji(head1,head2);
cout<<"请重新选择功能:";
cin>>*shu;
xunhuan(tingzhi,shu,head1,head2);
}else if(*shu==5){
break;
}
}
}
node *create(void)
{
int x;
node *head,*pre,*temp;
head = pre = new node;
while (scanf("%d", &x) && x > 0)
{
temp = new node;
temp->data = x;
temp->next = NULL;
pre->next = temp;
pre = temp;
}
return head;
}
void chachong(int *tingzhi,node *head)
{
node *temp=head->next;
int a=temp->data;
int zengliang=1;//用于帮助a的值向后流动
while(temp->next!=NULL)//最后一个数字没有查重的必要
{
while(temp->next!=NULL)
{
temp=temp->next;//记住这个位置,准确记住这个链表的向后传递的位置 !!
if(a==temp->data)
{
printf("您输入的第%d个数和后面的数发生了重复(不想改了)",zengliang);
*tingzhi=1;
break;
}
}
int j;
temp=head->next;
for(j=0;j<zengliang;j++)
{
temp=temp->next;
// a=temp->data;这个东西应该在外边
}
a=temp->data;
zengliang++;
}
}
void jiaoji(node *head1,node *head2)
{
node *jiao,*pre,*tempjiao;
jiao = pre = new node;
node *temp1=head1->next;
node *temp2=head2->next;
while(temp1!=NULL)//请注意观察
{
while(temp2!=NULL)
{
if(temp1->data==temp2->data)
{
tempjiao = new node;
tempjiao->data = temp1->data;
tempjiao->next = NULL;
pre->next = tempjiao;
pre = tempjiao;
}
temp2=temp2->next;
}
temp1=temp1->next; //这个一定要注意!要把temp2再次加入到循环里
temp2=head2->next;
}
cout<<"交集结果是:"<<endl;
output(jiao);
}
void bingji(node *head1,node *head2)
{ //从这到下面好多就是copy的jiao函数
node *jiao,*pre,*tempjiao;
jiao = pre = new node;
node *temp1=head1->next;
node *temp2=head2->next;
while(temp1!=NULL)//请注意观察
{
while(temp2!=NULL)
{
if(temp1->data==temp2->data)
{
tempjiao = new node;
tempjiao->data = temp1->data;
tempjiao->next = NULL;
pre->next = tempjiao;
pre = tempjiao;
}
temp2=temp2->next;
}
temp1=temp1->next; //这个一定要注意!要把temp2再次加入到循环里
temp2=head2->next;}
output(jiao); //copy结束开始干活!笑死这不就是魔改 !!!
node *bing1,*pre1,*tempbing1;
bing1 = pre1 = new node;
temp1=head1->next;
tempjiao=jiao->next;
while(temp1!=NULL)//请注意观察
{
while(tempjiao!=NULL)
{
if(temp1->data==tempjiao->data) //这个判断有点难
{
break;
}
tempjiao=tempjiao->next;
}
if(tempjiao==NULL)
{
tempbing1 = new node;
tempbing1->data = temp1->data;
tempbing1->next = NULL;
pre1->next = tempbing1;
pre1 = tempbing1;
}
tempjiao=jiao->next;
temp1=temp1->next;
}
output(bing1); //开始继续复制
node *bing2,*pre2,*tempbing2;
bing2 = pre2 = new node;
temp2=head2->next;
tempjiao=jiao->next;
while(temp2!=NULL)//请注意观察
{
while(tempjiao!=NULL)
{
if(temp2->data==tempjiao->data) //这个判断有点难
{
break;
}
tempjiao=tempjiao->next;
}
if(tempjiao==NULL)
{
tempbing2 = new node;
tempbing2->data = temp2->data;
tempbing2->next = NULL;
pre2->next = tempbing2;
pre2 = tempbing2;
}
tempjiao=jiao->next;
temp2=temp2->next;
}
output(bing2);
//核心代码结束
}
void chaji(node *head1,node *head2)
{
cout<<"head1减head2的差集是:";
node *jiao,*pre,*tempjiao;
jiao = pre = new node;
node *temp1=head1->next;
node *temp2=head2->next;
while(temp1!=NULL)//请注意观察
{
while(temp2!=NULL)
{
if(temp1->data==temp2->data)
{
tempjiao = new node;
tempjiao->data = temp1->data;
tempjiao->next = NULL;
pre->next = tempjiao;
pre = tempjiao;
}
temp2=temp2->next;
}
temp1=temp1->next; //这个一定要注意!要把temp2再次加入到循环里
temp2=head2->next;}
node *bing1,*pre1,*tempbing1;
bing1 = pre1 = new node;
temp1=head1->next;
tempjiao=jiao->next;
while(temp1!=NULL)//请注意观察
{
while(tempjiao!=NULL)
{
if(temp1->data==tempjiao->data) //这个判断有点难
{
break;
}
tempjiao=tempjiao->next;
}
if(tempjiao==NULL)
{
tempbing1 = new node;
tempbing1->data = temp1->data;
tempbing1->next = NULL;
pre1->next = tempbing1;
pre1 = tempbing1;
}
tempjiao=jiao->next;
temp1=temp1->next;
}
output(bing1); //开始继续复制
cout<<"head2减head1的差集是:";
node *bing2,*pre2,*tempbing2;
bing2 = pre2 = new node;
temp2=head2->next;
tempjiao=jiao->next;
while(temp2!=NULL)//请注意观察
{
while(tempjiao!=NULL)
{
if(temp2->data==tempjiao->data) //这个判断有点难
{
break;
}
tempjiao=tempjiao->next;
}
if(tempjiao==NULL)
{
tempbing2 = new node;
tempbing2->data = temp2->data;
tempbing2->next = NULL;
pre2->next = tempbing2;
pre2 = tempbing2;
}
tempjiao=jiao->next;
temp2=temp2->next;
}
output(bing2);
//核心代码结束
}
void output(node* head)
{
node *temp = head->next;
while (temp != NULL)
{
cout<<temp->data <<" ";
temp = temp->next;
}
}
|