求解答
#include<stdio.h>
#include<malloc.h>
#define N 100
void menu(){
printf("----------------------------------\n");
printf(" 1.创建 2.入库 3.显示 \n");
printf(" 4.借书 5.还书 0.退出 \n");
printf("----------------------------------\n");
}
typedef struct{//顺序表;
char idA[20];
char nameA[20];
char authorA[15];
int nowA;
int addA;
}bookA;
typedef struct{
bookA *Adata;
int lengthA;
}SeqListA;
typedef struct{//顺序表;
char idB[10];
char nameB[20];
char authorB[15];
int nowB;
int addB;
}bookB;
typedef struct{
bookB *Bdata;
int lengthB;
}SeqListB;
typedef struct{//顺序表;
char idC[10];
char nameC[20];
char authorC[15];
int nowC;
int addC;
}bookC;
typedef struct{
bookC *Cdata;
int lengthC;
}SeqListC;
typedef struct{//顺序表;
char idD[10];
char nameD[20];
char authorD[15];
int nowD;
int addD;
}bookD;
typedef struct{
bookD *Ddata;
int lengthD;
}SeqListD;
void InitList(SeqListA *AL, SeqListB *BL,SeqListC *CL,SeqListD *DL){//顺序表的创建;
AL->Adata=(bookA*)malloc(sizeof(bookA)*N);
AL->lengthA=0;
BL->Bdata=(bookB*)malloc(sizeof(bookB)*N);
BL->lengthB=0;
CL->Cdata=(bookC*)malloc(sizeof(bookC)*N);
CL->lengthC=0;
DL->Ddata=(bookD*)malloc(sizeof(bookD)*N);
DL->lengthD=0;
}
typedef struct{//链表;
char number[8];
int date;
int Location;
}reader;
typedef struct Linknode{
reader *elem;
struct Linknode *next;
}Linklist;
Linklist* Initlist(){//链表的创建;
Linklist *S;
S=(Linklist*)malloc(sizeof(Linklist));
S->next=NULL;
return S;
}
void InsList(Linklist *S,SeqListA *AL,SeqListB*BL,SeqListC*CL,SeqListD*DL,char r[]){
Linklist *p;
Linklist *s;
int j=0;
p=S;
while(p->next!=NULL){
p=p->next;
j++;
}
if(p!=NULL){
s=(Linklist *)malloc(sizeof(Linklist));
s->elem->Location=Locate(AL,BL,CL,DL,r);
scanf("%s",&s->elem->number ) ;
scanf("%d",&s->elem->date );
s->next = p->next ;
p->next =s;
printf("ok");
}else
printf("error!");
}
void CreateList(SeqListA *AL,SeqListB *BL,SeqListC *CL,SeqListD *DL){//顺序表的初始化;
int i;
int n;
char input;
do{
printf("please input you need bookstack;input 0 exit\n");
getchar();
scanf("%c",&input);
switch(input){
case 'A':
printf("how many intput bookdata in SeqListA->");
scanf("%d",&n);
for(i=0;i<n;i++){
printf("\n");
printf("intput No.%d id->",i+1);
scanf("%s",&AL->Adata[i].idA);
printf("intput No.%d name->",i+1);
scanf("%s",&AL->Adata[i].nameA);
printf("intput No.%d author->",i+1);
scanf("%s",&AL->Adata[i].authorA);
printf("intput No.%d now->",i+1);
scanf("%d",&AL->Adata[i].nowA);
printf("intput No.%d add->",i+1);
scanf("%d",&AL->Adata[i].addA);
AL->lengthA=i+1;
}
break;
case 'B':
printf("how many intput bookdata in SeqListB->");
scanf("%d",&n);
for(i=0;i<n;i++){
printf("\n");
printf("intput No.%d id->",i+1);
scanf("%s",&BL->Bdata[i].idB);
printf("intput No.%d name->",i+1);
scanf("%s",&BL->Bdata[i].nameB);
printf("intput No.%d author->",i+1);
scanf("%s",&BL->Bdata[i].authorB);
printf("intput No.%d now->",i+1);
scanf("%d",&BL->Bdata[i].nowB);
printf("intput No.%d add->",i+1);
scanf("%d",&BL->Bdata[i].addB);
BL->lengthB=i+1;
}
case 'C':
printf("how many intput bookdata in SeqListC->");
scanf("%d",&n);
for(i=0;i<n;i++){
printf("\n");
printf("intput No.%d id->",i+1);
scanf("%s",&CL->Cdata[i].idC);
printf("intput No.%d name->",i+1);
scanf("%s",&CL->Cdata[i].nameC);
printf("intput No.%d author->",i+1);
scanf("%s",&CL->Cdata[i].authorC);
printf("intput No.%d now->",i+1);
scanf("%d",&CL->Cdata[i].nowC);
printf("intput No.%d add->",i+1);
scanf("%d",&CL->Cdata[i].addC);
CL->lengthC=i+1;
}
break;
case 'D':
printf("how many intput bookdata in SeqListD->");
scanf("%d",&n);
for(i=0;i<n;i++){
printf("\n");
printf("intput No.%d id->",i+1);
scanf("%s",&DL->Ddata[i].idD);
printf("intput No.%d name->",i+1);
scanf("%s",&DL->Ddata[i].nameD);
printf("intput No.%d author->",i+1);
scanf("%s",&DL->Ddata[i].authorD);
printf("intput No.%d now->",i+1);
scanf("%d",&DL->Ddata[i].nowD);
printf("intput No.%d add->",i+1);
scanf("%d",&DL->Ddata[i].addD);
DL->lengthD=i+1;
}
break;
case 'E':
i=0;
break;
default:
printf("input error,please input A B C D");
}
}while(i);
}
void DispList(SeqListA*AL,SeqListB*BL,SeqListC*CL,SeqListD*DL){//显示顺序表内容;
int i;
for(i=0;i<AL->lengthA;i++){
printf("%s/%s/%s/%d/%d/",AL->Adata[i].idA ,AL->Adata[i].nameA,AL->Adata[i].authorA,AL->Adata[i].nowA,AL->Adata[i].addA);
printf("\n");
}
printf("____________________________________________\n");
for(i=0;i<BL->lengthB;i++){
printf("%s/%s/%s/%d/%d/",BL->Bdata[i].idB ,BL->Bdata[i].nameB,BL->Bdata[i].authorB,BL->Bdata[i].nowB,BL->Bdata[i].addB);
printf("\n");
}
printf("____________________________________________\n");
for(i=0;i<CL->lengthC;i++){
printf("%s/%s/%s/%d/%d/",CL->Cdata[i].idC ,CL->Cdata[i].nameC,CL->Cdata[i].authorC,CL->Cdata[i].nowC,CL->Cdata[i].addC);
printf("\n");
}
printf("____________________________________________\n");
for(i=0;i<DL->lengthD;i++){
printf("%s/%s/%s/%d/%d/",DL->Ddata[i].idD ,DL->Ddata[i].nameD,DL->Ddata[i].authorD,DL->Ddata[i].nowD,DL->Ddata[i].addD);
printf("\n");
}
printf("____________________________________________\n");
}
int Locate(SeqListA *AL,SeqListB *BL,SeqListC *CL,SeqListD *DL,char r[]){//形式索引查找;
int i=0;
if( r[0]== 'A' ) {
while(i<AL->lengthA && !strcmp(AL->Adata[i].idA, r[10]))
i++;
if(i>=AL->lengthA)
return 0;
else
return i;
}
if( r[0]=='B') {
while(i<BL->lengthB && !strcmp(BL->Bdata[i].idB, r[10]))
i++;
if(i>=BL->lengthB)
return 0;
else
return i;
}
if( r[0]=='C') {
while(i<CL->lengthC && !strcmp(CL->Cdata[i].idC, r[10]))
i++;
if(i>=CL->lengthC)
return 0;
else
return i;
}
if( r[0]=='D') {
i++;
while(i<DL->lengthD && !strcmp(DL->Ddata[i].idD, r[10]))
if(i>=DL->lengthD)
return 0;
else
return i;
}
}
void Inbook(SeqListA *AL,SeqListB *BL,SeqListC *CL,SeqListD *DL,char r[]){
if(Locate(AL,BL,CL,DL,r)==0){
if(r[0]='A'){
if(AL->lengthA>=N){
printf("this is full");
}
else{
printf("\n");
printf("intput No.%d id->",AL->lengthA );
scanf("%s",&AL->Adata[AL->lengthA].idA);
printf("intput No.%d name->",AL->lengthA );
scanf("%s",&AL->Adata[AL->lengthA].nameA);
printf("intput No.%d author->",AL->lengthA );
scanf("%s",&AL->Adata[AL->lengthA].authorA);
printf("intput No.%d now->",AL->lengthA );
scanf("%d",&AL->Adata[AL->lengthA].nowA);
printf("intput No.%d add->",AL->lengthA );
scanf("%d",&AL->Adata[AL->lengthA].addA);
AL->lengthA++;
}
}
if(r[0]='B'){
if(BL->lengthB>=N){
printf("this is full");
}
else{
printf("\n");
printf("intput No.%d id->",BL->lengthB );
scanf("%s",&BL->Bdata[BL->lengthB].idB);
printf("intput No.%d name->",BL->lengthB );
scanf("%s",&BL->Bdata[BL->lengthB].nameB);
printf("intput No.%d author->",BL->lengthB );
scanf("%s",&BL->Bdata[BL->lengthB].authorB);
printf("intput No.%d now->",BL->lengthB );
scanf("%d",&BL->Bdata[BL->lengthB].nowB);
printf("intput No.%d add->",BL->lengthB );
scanf("%d",&BL->Bdata[BL->lengthB].addB);
BL->lengthB++;
}
}
if(r[0]='C'){
if(CL->lengthC>=N){
printf("this is full");
}
else{
printf("\n");
printf("intput No.%d id->",CL->lengthC );
scanf("%s",&CL->Cdata[CL->lengthC].idC);
printf("intput No.%d name->",CL->lengthC );
scanf("%s",&CL->Cdata[CL->lengthC].nameC);
printf("intput No.%d author->",CL->lengthC );
scanf("%s",&CL->Cdata[CL->lengthC].authorC);
printf("intput No.%d now->",CL->lengthC );
scanf("%d",&CL->Cdata[CL->lengthC].nowC);
printf("intput No.%d add->",CL->lengthC );
scanf("%d",&CL->Cdata[CL->lengthC].addC);
CL->lengthC++;
}
}
if(r[0]='D'){
if(DL->lengthD>=N){
printf("this is full");
}
else{
printf("\n");
printf("intput No.%d id->",DL->lengthD );
scanf("%s",&DL->Ddata[DL->lengthD].idD);
printf("intput No.%d name->",DL->lengthD );
scanf("%s",&DL->Ddata[DL->lengthD].nameD);
printf("intput No.%d author->",DL->lengthD );
scanf("%s",&DL->Ddata[DL->lengthD].authorD);
printf("intput No.%d now->",DL->lengthD );
scanf("%d",&DL->Ddata[DL->lengthD].nowD);
printf("intput No.%d add->",DL->lengthD );
scanf("%d",&DL->Ddata[DL->lengthD].addD);
DL->lengthD++;
}
}
}
if(Locate(AL,BL,CL,DL,r)!=0){
int n;
if(r[0]='A'){
printf("Existing the book,please input n");
scanf("%d",&n);
AL->Adata[Locate(AL,BL,CL,DL,r)].addA=AL->Adata[Locate(AL,BL,CL,DL,r)].addA+n;
AL->Adata[Locate(AL,BL,CL,DL,r)].nowA=AL->Adata[Locate(AL,BL,CL,DL,r)].nowA+n;
}
if(r[0]='B'){
printf("Existing the book,please input n");
scanf("%d",&n);
BL->Bdata[Locate(AL,BL,CL,DL,r)].addB=BL->Bdata[Locate(AL,BL,CL,DL,r)].addB+n;
BL->Bdata[Locate(AL,BL,CL,DL,r)].nowB=BL->Bdata[Locate(AL,BL,CL,DL,r)].nowB+n;
}
if(r[0]='C'){
printf("Existing the book,please input n");
scanf("%d",&n);
CL->Cdata[Locate(AL,BL,CL,DL,r)].addC=CL->Cdata[Locate(AL,BL,CL,DL,r)].addC+n;
CL->Cdata[Locate(AL,BL,CL,DL,r)].nowC=CL->Cdata[Locate(AL,BL,CL,DL,r)].nowC+n;
}
if(r[0]='D'){
printf("Existing the book,please input n");
scanf("%d",&n);
DL->Ddata[Locate(AL,BL,CL,DL,r)].addD=DL->Ddata[Locate(AL,BL,CL,DL,r)].addD+n;
DL->Ddata[Locate(AL,BL,CL,DL,r)].nowD=DL->Ddata[Locate(AL,BL,CL,DL,r)].nowD+n;
}
}
}
void borrowbook(Linklist *S,SeqListA *AL,SeqListB *BL,SeqListC *CL,SeqListD *DL){
char r[10];
Linklist *P;
printf("please input you needed book");
scanf("%s",&r[10]);
if(r[0]=='A'){
if(Locate(AL,BL,CL,DL,r)==0)
printf("the book dose not exist");
else{
if(AL->Adata[Locate(AL,BL,CL,DL,r)].nowA>=1){
AL->Adata[Locate(AL,BL,CL,DL,r)].nowA--;
InsList(S,AL,BL,CL,DL, r);}
else
printf("There are no books");
}
}
if(r[0]=='B'){
if(Locate(AL,BL,CL,DL,r)==0)
printf("the book dose not exist");
else{
if(BL->Bdata[Locate(AL,BL,CL,DL,r)].nowB>=1){
BL->Bdata[Locate(AL,BL,CL,DL,r)].nowB--;
InsList(S,AL,BL,CL,DL, r);}
else
printf("There are no books");
}
}
if(r[0]=='C'){
if(Locate(AL,BL,CL,DL,r)==0)
printf("the book dose not exist");
else{
if(CL->Cdata[Locate(AL,BL,CL,DL,r)].nowC>=1){
CL->Cdata[Locate(AL,BL,CL,DL,r)].nowC--;
InsList(S,AL,BL,CL,DL, r);}
else
printf("There are no books");
}
}
if(r[0]=='D'){
if(Locate(AL,BL,CL,DL,r)==0)
printf("the book dose not exist");
else{
if(DL->Ddata[Locate(AL,BL,CL,DL,r)].nowD>=1){
DL->Ddata[Locate(AL,BL,CL,DL,r)].nowD--;
InsList(S,AL,BL,CL,DL, r);}
else
printf("There are no books");
}
}
}
void DelList(Linklist *S){
int j=0;
char D[10];
Linklist *p;
Linklist *s;
p=S;
printf("please input your boooknumber->");
scanf("%s",D);
while(p->next!=NULL&&!strcmp(p->elem->number,D)){
p=p->next ;
j++;
}
if(p->next!=NULL){
s=p->next ;
p->next = s->next ;
free(s);
printf("ok");
}
else
printf("error");
}
void main(){
SeqListA AL;
SeqListB BL;
SeqListC CL;
SeqListD DL;
Linklist *S;
S=Initlist();
int G;
do{
menu();
char r[10];
printf("please input Menu->");
scanf("%d",&G);
switch(G){
case 1:
CreateList(&AL,&BL,&CL,&DL);
break;
case 2:
Inbook(&AL,&BL,&CL,&DL,r);
break;
case 3:
DispList(&AL,&BL,&CL,&DL);
break;
case 4:
borrowbook(S,&AL,&BL,&CL,&DL);
break;
case 5:
DelList(S);
case 0:
printf("you choose Exit");
break;
default:
printf("you choose error(0-5)");
}
}while(G);
}