建立一個借書卡的結構體,以借書號爲關鍵碼,對其進行排序。並且記錄某個院系借書卡的數目。這個代碼有些出錯,請各位大佬幫忙看看

#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#define M 100
typedef struct node{
char major[M];
long int card;
char name[M];
struct node *next;
}student;
typedef student *book;
book sort(book head,char m[M],long int c,char n[M])
{
int i,j;
student *p,*pre;
pre=head;
if(head==NULL)
{
p=(student *)malloc(sizeof(node));
p->card=c;
strcpy(p->name,n);
strcpy(p->major,m);
pre->next=p;
p->next=NULL;
}
else
{
p=pre->next;
while(p->card<c&&!p)
{
pre=p;
p=p->next;
}
if§
{
p=(student *)malloc(sizeof(node));
p->card=c;
strcpy(p->name,n);
strcpy(p->major,m);
p->next=pre->next;
pre->next=p;
}
else
{
p=(student *)malloc(sizeof(node));
p->card=c;
strcpy(p->name,n);
strcpy(p->major,m);
pre->next=p;
p->next=NULL;
}
}
return head;
}
void print(book head)
{
student *p=head->next;
while§
{
printf("%s",p->major);
printf("%5ld",p->card);
printf("%5s",p->name);
p=p->next;
}
}
int check(book head,char m[M])
{
student *p;
int i=0;
p=head;
while§
{
if(strcmp(p->major,m)==0)
{
printf("%ld%s\n",p->card,p->name);
i++;
}
p=p->next;
}
return i;
}
int main()
{
book head;
long int c;
char m[M],n[M],j[M];
int number,i,s;
head=(student *)malloc(sizeof(node));
head=NULL;
printf(“請輸入借書證的個數:\n”);
scanf("%d",&s);
for(i=1;i<=s;i++)
{
printf(“請輸入第%d位借書卡信息:\n”,i);
printf(“院系:\n”);
scanf("%s",m);
printf(“卡號:\n”);
scanf("%ld",&c);
printf(“姓名:\n”);
scanf("%s",n);
head=sort(head,m,c,n);
}
printf(“信息整理完畢\n”);
printf(“按卡號排序結果爲:\n”);
print(head);
printf(“請輸入您要查詢的院系\n”);
scanf("%s",j);
number=check(head,j);
printf(“該院系的借書證個數有%d個”,number);
return 0;
}

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章