问题描述
应用中有时需要验证来自不同地方的两个表的信息是否一致。本实验编写具有如下功能的程序:输入两个学生记录表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)
5
20120001 zhangli
20120002 wanglei
20120003 duyang
20120004 lixin
20120005 liufan
3
20120001 zhangli
20120006 lixin
20120002 wanglei
输出(1)
20120006 lixin is not in LIST1.
输入(2)
5
20120001 zhangli
20120002 wanglei
20120003 duyang
20120004 lixin
20120005 liufan
3
20120001 zhangli
20120004 wanglei
20120006 duyang
输出(2)
20120004 wanglei is not in LIST1.
20120006 duyang is not in LIST1.
输入(3)
5
20120001 zhangli
20120002 wanglei
20120003 duyang
20120004 lixin
20120005 liufan
3
20120001 zhangli
20120004 wanglei
20120006 duyang
输出(3)
the LIST2 is NULL.
输入(4)
5
20120001 zhangli
20120002 wanglei
20120003 duyang
20120004 lixin
20120005 liufan
0
输出(4)
the records of LIST2 are all in the LIST1.
代码
#include<stdio.h>
#include<string.h>
int main()
{
int m,n;
scanf("%d",&m);
struct person
{
long int id;
char name[25];
};
struct person list1[m+1];
for(int i=1;i<=m;i++)
{
memset(list1[i].name,0,sizeof(list1[i].name));
scanf("%ld ",&list1[i].id);
scanf("%s",list1[i].name);
}
scanf("%d",&n);
if(n==0)
{
printf("the LIST2 is NULL.\n");
}
else
{
int count=0;
int flag=1;
struct person tmp;
for(int i=1;i<=n;i++)
{
count=0;
scanf("%ld ",&tmp.id);
scanf("%s",tmp.name);
for(int j=1;j<=m;j++)
{
if((tmp.id==list1[j].id)&&(strcmp(tmp.name,list1[j].name)==0))
{
count=1;
break;
}
}
if(count==0)
{
printf("%ld %s is not in LIST1.\n",tmp.id,tmp.name);
flag=0;
}
}
if(flag==1)
{
printf("the records of LIST2 are all in the LIST1.\n");
}
}
return 0;
}