?? 集合.cpp
字號:
#include<stdio.h>
#include<stdlib.h>
#include<conio.h>
#define MAX 100
#define OUT 0
#define IN 1
typedef struct node
{
char d;
struct node *next;
}element;
typedef struct
{
element *head;
int len;
}linklist;
void creatlist(linklist &s,int n )
{
element *q;
char c1;
int i;
s.len=0;
s.head=NULL;
printf("please input the set%d=",n);
while((c1=getchar())!='\n')
{
q=(element *)malloc(sizeof(element));
if(q==NULL)
{
printf("error on malloc\n");
exit(1);
}
q->d=c1;
q->next=s.head;
s.head=q;
++s.len;
}
}
void swap (char s3[],int i,int j)
{
int temp;
temp=s3[i];
s3[i]=s3[j];
s3[j]=temp;
}
void qsort(char s3[],int left,int right )
{
int i,last;
if(left>=right)
return;
swap(s3,left,(left+right)/2);
last=left;
for(i=left+1;i<=right;i++)
if(s3[i]<s3[left])
swap(s3,++last,i);
swap(s3,left,last);
qsort(s3,left,last-1);
qsort(s3,last+1,right);
}
void xiao_chong(char s2[],int n3){
int i,j,n,state,n4=1;
char s3[MAX];
for(i=0;i<(n3);i++){
if(s2[i]>='a'&&s3[i]<='z'){
s3[0]=s2[i];
n=i;
break;}
else
;
}
for(i=n;i<(n3);i++){
for(j=0;j<n4;j++){
if(s2[i]!=s3[j])
state=OUT;
else{
state=IN;
break;
}
}
if(state==OUT&&s2[i]>='a'&&s2[i]<='z'){
s3[n4]=s2[i];
n4++;}
else
;
}
qsort(s3,0,n4-1);
for(i=0;i<n4;i++){
printf("%c",s3[i]);
}
printf("\n");}
void unionset( )
{ int i;
linklist a,b;
element *s,*t;
char w[MAX];
creatlist(a,1);
creatlist(b,2);
s=a.head;
t=b.head;
for(i=0;i<a.len;i++){
w[i]=s->d;
s=s->next;
}
for(i=0;i<=b.len;i++){
w[i+a.len]=t->d;
t=t->next;
}
printf("the final result of unionset is:");
xiao_chong(w,(a.len+b.len));
printf("\n");
printf("\n");
}
void intersection( )
{ int i,j,n3=0,state;
char s3[MAX];
linklist a,b;
element *s,*t;
char w[MAX],w1[MAX];
creatlist(a,1);
creatlist(b,2);
s=a.head;
t=b.head;
for(i=0;i<a.len;i++){
w[i]=s->d;
s=s->next;
}
for(i=0;i<=b.len;i++){
w1[i]=t->d;
t=t->next;
}
for(i=0;i<a.len;i++){
for(j=0;j<b.len;j++){
if(w[i]==w1[j]){
state=IN;
break;}
else
state=OUT;
}
if(state==OUT)
;
else if(state==IN){
s3[n3]=w[i];
n3++;}
}
printf("the final result of intersection is:");
xiao_chong(s3,n3);
printf("\n");
printf("\n");
}
void difference( )
{ int i,j,n3=0,state;
char s3[MAX];
linklist a,b;
element *s,*t;
char w[MAX],w1[MAX];
creatlist(a,1);
creatlist(b,2);
s=a.head;
t=b.head;
for(i=0;i<a.len;i++){
w[i]=s->d;
s=s->next;
}
for(i=0;i<=b.len;i++){
w1[i]=t->d;
t=t->next;
}
for(i=0;i<a.len;i++){
for(j=0;j<b.len;j++){
if(w[i]!=w1[j])
state=OUT;
else
{state=IN;
break; }
}
if(state==OUT){
s3[n3]=w[i];
n3++;}
else if(state==IN)
;
}
printf("the final result of difference is:");
xiao_chong(s3,n3);
printf("\n");
printf("\n");
}
void bu_set()
{
linklist a,b;
element *s,*t,*f;
int i,j,state[MAX];
state[0]=OUT;
creatlist(a,1);
creatlist(b,2);
s=a.head;
t=b.head;
for(i=0;i<a.len;i++){
t=b.head;
for(j=0;j<b.len;j++){
if(s->d!=t->d){
state[i]=OUT;
t=t->next;}
else
{state[i]=IN;
break;}
}
s=s->next;
}
f=a.head;
printf("the final bu_set of the set2 for set1 is:");
for(i=0;i<a.len;i++){
if(state[i]==OUT)
printf("%c",f->d);
f=f->next;}
printf("\n");
printf("\n");
printf("\n");
}
void judge_subset( )
{ linklist a,b;
element *s,*t;
int i,j;
int n1=0,n2=0;
creatlist(a,1);
creatlist(b,2);
s=a.head;
for(i=0;i<a.len;i++){t=b.head;
for(j=0;j<b.len;j++){
if(s->d!=t->d)
t=t->next;
else{
n1++;
break;}
}
s=s->next;
}
if(n1==a.len){
printf("the set1 is the subset of the set2\n");
}
else
{ printf("the set1 is not the subset of the set2\n");
printf("\n");
printf("\n");}
t=b.head;
t=t->next;
for(j=0;j<b.len;j++){
s=a.head;
s=s->next;
for(i=0;i<a.len;i++){
if(s->d!=t->d){
s=s->next;
}
else{
n2++;
break;}
}
t=t->next;}
if(n2==b.len)
{printf("the set2 is the subset of the set1\n");
printf("\n");
printf("\n");}
else
{printf("the set2 is not the subset of the set1\n");
}
}
void judge_element()
{
linklist a;
element *s;
char c;
int state,i;
creatlist(a,1);
printf("please input the element C:");
scanf("%c",&c);
s=a.head;
for(i=0;i<a.len;i++){
if(c!=s->d){
state=OUT;
s=s->next;
}
else{
state=IN;
break;}
}
if(state==IN){
printf("the C is the element of the set1\n");
printf("\n");
printf("\n");}
printf("\n");
printf("\n");
}
void main()
{ clrscr();
printf("-----------------------------------------------\n");
printf("welcome to use the function unionset\n");
unionset();
system("Pause");
printf("-----------------------------------------------\n");
printf("welcome to use the function intersection\n");
intersection( );
system("Pause");
printf("-----------------------------------------------\n");
printf("welcome to e use the function difference\n");
difference( );
system("Pause");
printf("-----------------------------------------------\n");
printf("welcome to use the function bu_set\n");
bu_set();
system("Pause");
printf("-----------------------------------------------\n");
printf("welcome to use the function judge_subset\n");
judge_subset();
system("Pause");
printf("-----------------------------------------------\n");
printf("welcome to use the function judge_element\n");
judge_element();
system("Pause");
}
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -