c语言while求a和b的和程序,数据结构实验1_C语言_输入集合A和B求并集、交集、差集(while +...

数据结构实验1_C语言_输入集合A和B求并集、交集、差集(while +

数据结构实验1_C语言_输入集合A和B求并集、交集、差集(while + switch + 功能函数))

实验1

(1)实验目的

通过该实验,让学生复习巩固C语言中的循环结构、循环控制条件、分支结构和数组/链表、函数的调用等有关内容,体会到用数组存储集合时,需要记录集合元素的个数,否则输出结果会出现数据越界现象。

(2)实验内容

通过键盘,分别输入两个数据元素类型为正整数的集合A和B,以负数输入为结束条件,输出两个集合的交、并、差。

(3)实验要求

从程序完善性上考虑,集合元素输入时,要有检查元素重复的功能,每个集合中不允许有重复的元素。集合可以用数组也可以用链表存储。

实现交、并、差运算时,分别把代码写成函数的形式,即实现交运算的函数,实现并运算的函数,实现差运算的函数,在主函数中分别调用三个函数。

使用菜单形式对应各个操作,应允许用户反复查看结果,想结束程序时,输入负数结束,使其编成一个完整的小软件。菜单参考示例如下:

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}

重点: 数组或线性表的使用

难点: 去重操作和函数调用

一、设计思想

首先在main函数中设置while循环,打印菜单,switch分支判断输入,在分支中调用相应函数完成功能。

设置全局变量 input_a[99]、input_b[99]存储相应集合,int i,int m 集合元素个数(至于为什么要用全局变量,不用局部变量,全局变量生存周期随程序,下面打印集合等功能更加方便)

声明函数

3.1 输入集合函数

3.2 求并集

3.3 求交集

3.4 求差集

3.5 打印集合

其中比较复杂的地方

4.1 选用全局变量而不是成员变量

4.2 求集合并集函数功能实现

二、主要源代码

# include

# include

# include

using namespace std;

int in_a();

int in_b();

int jiao_a_b();

int bing_a_b();

int cha_a_b();

//定义全局变量

int input_a[99] = {0};

int input_b[99] = {0};

//集合A、B的元素下标

int i = 0;

int m = 0;

int main()

{

int n = 1;//退出菜单的标记

while(n){

int s;

cout<

cout<

cout<

cout<

cout<

cout<

cout<

cout<

cout<

scanf("%d",&s);

switch(s){

case 1:

system("cls");

cout<

in_a();

in_b();

break;

case 2:

system("cls");

cout<

jiao_a_b();

printf("\n");

break;

case 3:

system("cls");

cout<

bing_a_b();

printf("\n");

break;

case 4:

system("cls");

cout<

cha_a_b();

printf("\n");

break;

case 5:

system("cls");

cout<

//输出集合A

printf("集合A:");

for(int k = 0; k

printf("%d ",input_a[k]);

}

printf("\n");

//输出集合B

printf("集合B:");

for(int k = 0; k

printf("%d ",input_b[k]);

}

printf("\n");

break;

default:

system("cls");

if(s<0){

n = 0;

cout<

break;

}

else

cout<

}

}

}

//1. 输入集合a函数

int in_a(){

int num;

//集合a的输入

cout<

while(scanf("%d",&num) > 0 && getchar() != '\n'){

i++;

if(i == 1)

input_a[i-1] = num;

else{

for(int y = 0;y <= i-2; y++){

//判断输入的数和前面的是否相等

if(num != input_a[y]){

input_a[i-1] = num;

}

else{

printf("你输入的集合a含重复的元素,请重新输入集合a~\n");

i = 0;

in_a();

}

}

}

}

}

//2. 输入集合b函数

int in_b(){

int num;

//集合b的输入

cout<

while(scanf("%d",&num) > 0 && getchar() != '\n'){

m++;

if(m == 1)

input_b[m-1] = num;

else{

for(int y = 0;y <= m-2;y++){

if(num != input_b[y]){

input_b[m-1] = num;

}

else{

printf("你输入的集合b含重复的元素,请重新输入集合b~\n");

m = 0;

in_b();

}

}

}

}

}

//3. 交函数

int jiao_a_b(){

printf("集合A交B为:");

for(int k = 0; k < i;k++){

for(int j = 0; j< m;j++){

if(input_a[k] == input_b[j]){

printf("%d ",input_a[k]);

}

}

}

printf("\n");

}

//4. 并函数

int bing_a_b(){

int t = 0;

printf("集合A并B为:");

for(int k = 0; k < i;k++){

printf("%d ",input_a[k]);

}

for(int j = 0;j< m;j++){

for(int k = 0;k

if(input_b[j] != input_a[k]){

t++;

}

}

if(t == i)

printf("%d ",input_b[j]);

t = 0;

}

printf("\n");

}

//5. 差函数

int cha_a_b(){

int t = 0;

printf("集合A差B为:");

for(int k = 0; k < i;k++){

for(int j = 0; j< m;j++){

if(input_a[k] != input_b[j])

t++;

}

if(t == m)

printf("%d ",input_a[k]);

t = 0;

}

printf("\n");

}

部分截图

7c6ff0e67076e69d24289f11b50bd938.png

53a7da539fd7c1f6f5f3f5491b76f384.png

dffb59d551223d03f48a97ba41f0246c.png

求并集的原理调试图

就是使用t记录B集合内的某元素是否和A中的每一个元素都不同(A中共i个元素),不同就输出

afeeec2ceffab93c5543c6493f510c49.png

282cbc832951c996f24410bd3afaf9e0.png

41662d5fb69a828e7facac3b571920f6.png

三、实验总结

巩固了菜单小程序的总体体系(while + switch + 功能函数)

查阅学习了C中全局变量和局部变量的区别,参考:http://c.biancheng.net/view/1858.html

熟悉了while(scanf(…) != EOF){…}等语法操作

求并集函数和求差集函数采用 计数判断 法

求并集:先打印A集合全部,然后嵌套循环判断集合B的元素,是否计数为5

(找出和A中完全不等的元素)打印

求交集:嵌套判断A集合中多余B集合的元素,是否计数为5(找出A中独有的元素)打印

数据结构实验1_C语言_输入集合A和B求并集、交集、差集(while +相关教程

  • 0
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
以下是一个用链表和 C 语言实现计算集合交集并集差集和补集的代码: ```c #include <stdio.h> #include <stdlib.h> // 定义链表结点 typedef struct Node { int value; struct Node *next; } Node; // 定义链表头结点 typedef struct { Node *head; Node *tail; } List; // 初始化链表 void initList(List *list) { list->head = NULL; list->tail = NULL; } // 在链表尾部插入一个新结点 void insert(List *list, int value) { Node *newNode = (Node *) malloc(sizeof(Node)); newNode->value = value; newNode->next = NULL; if (list->head == NULL) { list->head = newNode; } else { list->tail->next = newNode; } list->tail = newNode; } // 判断链表中是否存在某个元素 int find(List *list, int value) { Node *p = list->head; while (p != NULL) { if (p->value == value) { return 1; } p = p->next; } return 0; } // 链表的交集运算 List intersection(List *list1, List *list2) { List result; initList(&result); Node *p1 = list1->head; while (p1 != NULL) { if (find(list2, p1->value)) { insert(&result, p1->value); } p1 = p1->next; } return result; } // 链表的并集运算 List unionSet(List *list1, List *list2) { List result; initList(&result); Node *p1 = list1->head; while (p1 != NULL) { insert(&result, p1->value); p1 = p1->next; } Node *p2 = list2->head; while (p2 != NULL) { if (!find(&result, p2->value)) { insert(&result, p2->value); } p2 = p2->next; } return result; } // 链表的差集运算 List difference(List *list1, List *list2) { List result; initList(&result); Node *p1 = list1->head; while (p1 != NULL) { if (!find(list2, p1->value)) { insert(&result, p1->value); } p1 = p1->next; } return result; } // 链表的补集运算 List complement(List *list1, List *list2) { List result; initList(&result); Node *p1 = list1->head; while (p1 != NULL) { if (!find(list2, p1->value)) { insert(&result, p1->value); } p1 = p1->next; } Node *p2 = list2->head; while (p2 != NULL) { if (!find(list1, p2->value)) { insert(&result, p2->value); } p2 = p2->next; } return result; } // 打印链表中的元素 void printList(List *list) { Node *p = list->head; while (p != NULL) { printf("%d ", p->value); p = p->next; } printf("\n"); } int main() { List list1, list2; initList(&list1); initList(&list2); insert(&list1, 1); insert(&list1, 2); insert(&list1, 3); insert(&list2, 2); insert(&list2, 3); insert(&list2, 4); printf("List 1: "); printList(&list1); printf("List 2: "); printList(&list2); printf("Intersection: "); List result1 = intersection(&list1, &list2); printList(&result1); printf("Union: "); List result2 = unionSet(&list1, &list2); printList(&result2); printf("Difference (list1 - list2): "); List result3 = difference(&list1, &list2); printList(&result3); printf("Complement: "); List result4 = complement(&list1, &list2); printList(&result4); return 0; } ``` 在上述代码中,我们定义了链表结点类型 `Node` 和链表类型 `List`,其中 `List` 包含链表头结点和尾结点。我们使用 `initList` 函数初始化链表,使用 `insert` 函数在链表尾部插入一个新结点。`find` 函数用于判断链表中是否存在某个元素。 我们实现了四个函数:`intersection`、`unionSet`、`difference` 和 `complement`,分别用于计算集合交集并集差集和补集。这些函数都返回一个链表类型的结果。最后,我们使用 `printList` 函数打印链表中的元素。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值