8. 验证表

成绩10开启时间2021年09月20日 星期一 15:50
折扣0.8折扣时间2021年09月30日 星期四 23:59
允许迟交关闭时间2021年10月17日 星期日 23:59
  应用中有时需要验证来自不同地方的两个表的信息是否一致。本实验编写具有如下功能的程序:输入两个学生记录表 LIST1 和 LIST2,在表 LIST2 中找出所有没有在表 LIST1 中出现的学生记录(设表LIST1为基础数据表,非空)。

每一个学生记录元素包含两个数据项:学号(整数),姓名;

如果学生记录表LIST2中的记录都包含在LIST1中,则输出the records of LIST2 are all in the LIST1.

如果学生记录表LIST2中的存在学号,姓名不能与表LIST1完全匹配的记录,则输出 学号(%8d)姓名(%15s)is not in LIST1.

如果LIST2为空表,则输出the LIST2 is NULL.

 测试输入期待的输出时间限制内存限制额外进程
测试用例 1以文本方式显示
  1. 5↵
  2. 20120001 zhangli↵
  3. 20120002 wanglei↵
  4. 20120003 duyang↵
  5. 20120004 lixin↵
  6. 20120005 liufan↵
  7. 3↵
  8. 20120001 zhangli↵
  9. 20120006 lixin↵
  10. 20120002 wanglei↵
以文本方式显示
  1. 20120006 lixin is not in LIST1.↵
1秒64M0
测试用例 2以文本方式显示
  1. 5↵
  2. 20120001 zhangli↵
  3. 20120002 wanglei↵
  4. 20120003 duyang↵
  5. 20120004 lixin↵
  6. 20120005 liufan↵
  7. 3↵
  8. 20120001 zhangli↵
  9. 20120004 wanglei↵
  10. 20120006 duyang↵
以文本方式显示
  1. 20120004 wanglei is not in LIST1.↵
  2. 20120006 duyang is not in LIST1.↵
1秒64M0
测试用例 3以文本方式显示
  1. 5↵
  2. 20120001 zhangli↵
  3. 20120002 wanglei↵
  4. 20120003 duyang↵
  5. 20120004 lixin↵
  6. 20120005 liufan↵
  7. 0↵
以文本方式显示
  1. the LIST2 is NULL.↵
1秒64M0
测试用例 5以文本方式显示
  1. 5↵
  2. 20120001 zhangli↵
  3. 20120002 wanglei↵
  4. 20120003 duyang↵
  5. 20120004 lixin↵
  6. 20120005 liufan↵
  7. 3↵
  8. 20120002 wanglei↵
  9. 20120001 zhangli↵
  10. 20120004 lixin↵
以文本方式显示
  1. the records of LIST2 are all in the LIST1.↵
1秒64M0

代码

 还是两份,学长+我的。

我的代码配合注释应该很好懂,最近事情比较多,不多解释了。

学长代码

#include"stdio.h"  
#include"stdlib.h"  
#include"string.h"  
  
typedef struct stu{  
    long id;  
    char name[15];  
    struct stu *next;  
}stu;  
  
int main(){  
    stu *head,*p,*q;  
    int n;  
    int sum=0,flag=0;  
      
    scanf("%d",&n);  
      
    head=(stu*)malloc(sizeof(stu));  
    head->next=NULL;  
    q=head;  
      
    for(int i=0;i<n;i++){  
        p=(stu*)malloc(sizeof(stu));  
        for(int j=0;j<15;j++){  
            (*p).name[j]='\0';  
        }  
          
        scanf("%ld %s",&p->id,&p->name);  
          
        p->next=NULL;  
        q->next=p;  
        q=q->next;  
    }  
      
    scanf("%d",&n);  
    if(n==0){  
        printf("the LIST2 is NULL.\n");  
    }  
      
    else{  
        for(int i=0;i<n;i++){  
            p=(stu*)malloc(sizeof(stu));  
            flag=0;  
            for(int j=0;j<15;j++){  
                (*p).name[j]='\0';  
            }  
            q=head;  
            scanf("%ld %s",&p->id,&p->name);  
            do{  
                q=q->next;  
                if(q->id==p->id&&strcmp(q->name,p->name)==0){  
                    flag=1;  
                    sum++;  
                    break;  
                }  
            }while(q->next!=NULL);  
              
            if(flag==0){  
                printf("%8d %s is not in LIST1.\n",p->id,p->name);  
            }  
        }  
        if(sum==n){  
            printf("the records of LIST2 are all in the LIST1.\n");   
        }  
    }  
} 

我的代码

#include"stdio.h"
#include"stdlib.h"
#include"string.h"
typedef struct student{
	int id;
	char name[15];
	struct student *next;
}stu;

void create(stu *list,int len){
	for(int i=0;i<len;i++){
		scanf("%d %s",&list->id,&list->name);
		list->next=(stu*)malloc(sizeof(stu));
		list=list->next;
	}
}

void search(stu *p1,stu *p2,int len1,int len2){
	int num_in=0;//list2在list1中的元素个数 
	stu *list2=p2; 
	for(int i=0;i<len2;i++){//list2中每一个元素 ,在list1中找相同 
		stu *list1=p1;
		
		int flag=0;//找到相同了没有 
		//对比list1 
		if(list2->id==list1->id&&strcmp(list2->name,list1->name)==0){
			//假如list2的当前元素和list1的第一个元素相同 
			num_in++;
			list2=list2->next;//开始从list2下一个找起 
			continue;//不用对比了,直接下一个 
			
		}
		else{//否则依次找list1的其他元素 
			for(int j=1;j<len1;j++){
	
				list1=list1->next;
				if(list2->id==list1->id&&strcmp(list2->name,list1->name)==0){//找到相同了 
					num_in++;
					flag=1;
					continue;//不用对比了,直接下一个 
				}
			}
			if(flag==0){//自始至终没有找到相同 
				printf("%d %s is not in LIST1.\n",list2->id,list2->name);
			}
				
		}

		list2=list2->next;
		
	}
	if(num_in==len2){
		printf("the records of LIST2 are all in the LIST1.\n");
	}
}

int main(){

	stu *list1,*list2;
	list1=(stu*)malloc(sizeof(stu));
	list2=(stu*)malloc(sizeof(stu));
	
	int len1;
	scanf("%d",&len1);
	create(list1,len1);
	
	int len2;
	scanf("%d",&len2);
	if(len2==0){
		printf("the LIST2 is NULL.\n");
		return 0;
	}
	create(list2,len2);
	
	search(list1,list2,len1,len2);
	
}

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值