(1)实验目的 通过该实验,让学生复习巩固C语言中的分支结构、循环结构、数组、链表、函数的调用等有关内容,体会到用数组存储集合时,需要记录集合元素的个数,否则输出结果会出现数据越界现象。 (2)实验内容 通过键盘,分别输入集合A和B中的数据元素,要求数据元素类型为整数类型,输出两个集合的交、并、差。 (3)实验要求 a)从程序完善性上考虑,集合元素输入时,要有检查元素重复的功能,每个集合中不允许有重复的元素。集合可以用数组存储,也可以用链表存储。 b)实现交、并、差运算时,分别把代码写成函数的形式,即实现交运算的函数,实现并运算的函数,实现差运算的函数,在主函数中分别调用三个函数。 c)使用菜单形式对应各个操作,应允许用户反复查看结果,想结束程序时,输入负数结束,使其编成一个完整的小软件。菜单参考示例如下: 1---输入集合A和B 2---求集合A交B 3---求集合A并B 4---求集合A-B 退出,输入一个负数! 菜单:建议不要做成清屏和刷屏的效果,否则后面截图会比较麻烦。 (4)验收/测试用例 如:输入: A={1,2,3,4,5} B={3,4,5,6,7} 要注意输入的过程中,每输入一个元素都要检查输入的这个元素是否和前面的元素重复,如果重复,要求用户重新输入当前元素。 验收测试时要测试这种重复的情况。 输出:A交B={3, 4, 5} A并B={1,2,3,4,5,6,7} A-B={1, 2} ![]() ![]() ![]() 源代码 #include<iostream> #include<cstdlib> using namespace std; #define ERROR 0 #define LIST_INIT_SIZE 100 #define LIST_INCREASE 10 #define OK 1 typedef int Elemtype; typedef int status; int flag=0; // 判断是否初始化的成功 int fg1=0,fg2=0; //接收函数的返回状态 typedef struct { Elemtype *elem; int length; int listsize; }Sqlist; // chu shi hua xian xing biao status Initlist(Sqlist * L){ L->elem = (Elemtype*)malloc(sizeof(Elemtype)); if(!L)return ERROR; L->length=0; L->listsize=LIST_INIT_SIZE; return OK; } //cha ru shu ju status Insertlist(Sqlist * L){ int n=0,j=0; cout<<endl; cout<<"please a series of integer numbers:"<<endl; cout<<"you can input -1 when you don`t want to do this!"<<endl; cin>>n; while(n!=-1){ L->elem[j]=n; //qian tao for xun huan dui chong fu shu zi jin xing jian cha for(int i=0;i<j;i++){ if(L->elem[j]==L->elem[i]){ cout<<"此数列中已有此值,请重新输入"<<endl; cin>>n; if(n==-1 || n==L->elem[j]){ cout<<"n的值在这里不能等于-1或者还是此值,重新输入!!!"<<endl; L->length--; j--; break; } else{ L->elem[j]=n; break; }
} } L->length++; j++; cin>>n; } return OK; } void output(Sqlist * L){ cout<<"these are the data in the sqlist "<<endl; for(int i=0;i<L->length;i++){ cout<<L->elem[i]<<" "; } cout<<endl; } //jiao ji void Intersection(Sqlist * la,Sqlist *lb){ cout<<endl; cout<<"this is the intersection about the two linear tables "<<endl; for(int i=0;i<la->length;i++){ for(int j=0;j<lb->length;j++){ if(la->elem[i]==lb->elem[j]){ cout<<la->elem[i]<<" "; } } } cout<<endl; } // bing ji void Union(Sqlist * la,Sqlist * lb){ cout<<endl; cout<<"this is the union about the two linear tables: "<<endl; //xian shu chu la zhong yu lb zhong bu tong de int flag2=0; for(int i=0;i<la->length;i++){ for(int j=0;j<lb->length;j++){ if(la->elem[i]==lb->elem[j]){ flag2=1; break; }else { flag2=0; } } if(flag2==0){ cout<<la->elem[i]<<" "; } } // zhi jie shu chu lb zhong de shu ju for(int i=0;i<lb->length;i++){ cout<<lb->elem[i]<<" "; } cout<<endl; } // cha ji void Subtract(Sqlist * la,Sqlist * lb){ cout<<endl; // cha ji jiu shi bing ji han shu zhong de qian ban bu fen cout<<"this is the subtraction about the two linear tables: "<<endl; int flag1=0; for(int i=0;i<la->length;i++){ for(int j=0;j<lb->length;j++){ if(la->elem[i]==lb->elem[j]){ flag1=1; break; }else { flag1=0; } } if(flag1==0){ cout<<la->elem[i]<<" "; } } cout<<endl; } int main(){ int n=0; // zhi xing de cao zuo ma cout<<"1---chu shi hua xian xing biao "<<endl; cout<<"2---输入集合A和B "<<endl; cout<<"3---求集合A交B"<<endl; cout<<"4---求集合A bing B"<<endl; cout<<"5---求集合A jian B"<<endl; cout<<"you can input -1 end up with this task"<<endl; cout<<endl; cout<<"Please enter the action you want to take:"<<endl; cin>>n; Sqlist l,li; Sqlist *la =&l,*lb=&li; while(n!=-1){ switch(n){ case 1: if(Initlist(la)==OK && Initlist(lb)==OK){ cout<<"Initialization succeeded!"<<endl; flag=1; break; }else{ exit(-1); // wei cheng gong zhi jie tui chu cheng xu } case 2: if(flag==0){ cout<<"请先初始化集合la和lb"<<endl; break; } cout<<"please input array la:"<<endl; fg1=Insertlist(la); cout<<"please input array lb:"<<endl; fg2=Insertlist(lb); if(fg1==OK && fg2==OK ){ //这里记得不能写函数 否则会继续调用函数 cout<<"insert successful!"<<endl; output(la); output(lb); } break; case 3: if(flag==0){ cout<<"请先初始化集合la和lb"<<endl; break; } Intersection( la,lb); break; case 4: if(flag==0){ cout<<"请先初始化集合la和lb"<<endl; break; } Union(la, lb); break; case 5: if(flag==0){ cout<<"请先初始化集合la和lb"<<endl; break; } Subtract(la,lb); break;
} cout<<"Please enter the action you want to take:"<<endl; cin>>n; } } |