验证表(10分)
成绩 | 10 | 开启时间 | 2018年10月4日 星期四 15:50 |
折扣 | 0.8 | 折扣时间 | 2018年10月24日 星期三 15:50 |
允许迟交 | 否 | 关闭时间 | 2018年11月4日 星期日 15:50 |
验证表(10分)
成绩: 10 / 折扣: 0.8
应用中有时需要验证来自不同地方的两个表的信息是否一致。本实验编写具有如下功能的程序:输入两个学生记录表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.
#include "stdio.h"
#include "stdlib.h"
#include "string.h"
typedef struct LIST{
long id;
char name[15];
struct LIST *next;
} NODE;
int main(int argc, char const *argv[])
{
NODE *head, *p, *q, *head2;
int n, flag = 0, count = 0;
scanf("%d", &n);
head = (NODE *)malloc(sizeof(NODE));
head->next = NULL;
q = head;
for (int i = 0; i < n; i++){
p = (NODE*)malloc(sizeof(NODE));
for (int k = 0; k < 15; k++)
(*p).name[k] = '\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++){
flag = 0;
p = (NODE*)malloc(sizeof(NODE));
//存储这个数据
for (int k = 0; k < 15; k++)
(*p).name[k] = '\0';
scanf("%ld %s", &p->id, &p->name);
q = head;
do{
q = q->next;
if (q->id == p->id && strcmp(p->name, q->name) == 0){
//printf("%8d %15s is not in LIST1\n", p->id, p->name);
flag = 1;
count++;
break;
}
} while (q->next != NULL);
if(flag == 0){
printf("%8d %s is not in LIST1.\n", p->id, p->name);
}
}
if(count == n){
printf("the records of LIST2 are all in the LIST1.\n");
}
}
}