#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<windows.h>
typedef struct book{
char name[5];
char id[8]; //存八位编号
int loaned; //借出
int num; //总数
double price;
char publisher[5];
char author[5];
struct book*next;
}book;
typedef struct student{
char id[8];
char name[5];
int overdue; //逾期数
char borrow_book_name[5][8]; //借书数,最大为5,用编号存
int borrow_book_num;
struct student*next;
}student;
book* Initialisation_book(){
book *tail=NULL,*Head=NULL,*sd;
FILE*fp=NULL;
fp=fopen("library.dat","r");
if(fp==NULL){
printf("打开文件失败!");
exit(1);
}
while(!feof(fp)){
sd=(book*)malloc(sizeof(book));
sd->next=NULL;
fread(sd,sizeof(book),1,fp);
// printf("%s\n",sd->name);
if(Head){
tail->next=sd;
tail=tail->next;
}else{
Head=sd;
tail=sd;
}
}
tail=NULL;
return Head;
}
student*Initialisation_student(){
student *tail=NULL,*Head=NULL,*sd;
FILE*fp=NULL;
fp=fopen("student.dat","r");
if(fp==NULL){
printf("文件打开失败!");
exit(1);
}
while(!feof(fp)){
sd=(student*)malloc(sizeof(student));
sd->next=NULL;
fread(sd,sizeof(student),1,fp);
if(Head){
tail->next=sd;
tail=tail->next;
}else{
Head=sd;
tail=sd;
}
}
tail=NULL;
return Head;
}
/×udent.dat里面有
//"123456","asd",1,"格林",1
/Pbrary.dat里面有
//“asd”,“1001”,1,5,56,“yangguang”,“huangyi”
//Head是图书馆,borrow_book是要借的书的编号,Student是借书的同学的指针
void borrow(book*Head,char*borrow_book,student*Student){
book*pre=Head;
while(pre){
if(strcmp(pre->id,borrow_book)==0){ //if这一步没进去过,而且一直循环没出来
pre->loaned++;
strcpy(Student->borrow_book_name[Student->borrow_book_num],borrow_book);
break;
}
pre=pre->next;
}
printf("借书成功啦!");
}
int main(){
// student sd = {"123456","asd",1,"格林",1};
// FILE*fp=NULL;
// fp=fopen("student.dat","w");
// fwrite(&sd,sizeof(student),1,fp);
book *Head=Initialisation_book();
student*Student=Initialisation_student();
borrow(Head,"1001",Student);
return 0;
}
head你命为null了,下面if里面又是用的head,所以不进if;
改成!head
#include <stdio.h>
int main()
{
int day=0;
scanf("%d",&day);
if(1==day)
printf("星期一\n");
else if(2==day)
printf("星期二\n");
else if(3==day)
printf("星期三\n");
else if(4==day)
printf("星期四\n");
else if(5==day)
printf("星期五\n");
else if(6==day)
printf("星期六\n");
else if(7==day)
printf("星期日\n");
return 0;
}
易错警示: int定义后需要 ;
if或者else if后不需 ;
整体修改如下,改动处见注释,供参考:
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<windows.h>
typedef struct book{
char name[16]; //char name[5]; 修改
char id[8]; //存八位编号
int loaned; //借出
int num; //总数
double price;
char publisher[32]; //char publisher[5]; 修改
char author[32]; //char author[5]; 修改
struct book*next;
}book;
typedef struct student{
char id[8];
char name[16]; // char name[5]; 修改
int overdue; //逾期数
char borrow_book_name[5][8];//借书数,最大为5,用编号存
int borrow_book_num;
struct student*next;
}student;
book* Initialisation_book(){
book *tail=NULL,*Head=NULL,*sd;
FILE*fp=NULL;
fp=fopen("library.dat","r");
if(fp==NULL){
printf("打开文件失败!");
return NULL; //exit(1); 修改
}
while(1){ //while(!feof(fp)) 修改
sd=(book*)malloc(sizeof(book));
sd->next=NULL;
if (fread(sd,sizeof(book),1,fp) != 1){ // 修改
free(sd); // 修改
break; // 修改
}
//printf("%s\n",sd->name);
if(Head){
tail->next=sd;
tail=tail->next;
}else{
Head=sd;
tail=sd;
}
}
fclose(fp); // 修改
tail=NULL;
return Head;
}
student*Initialisation_student(){
student *tail=NULL,*Head=NULL,*sd;
FILE*fp=NULL;
fp=fopen("student.dat","r");
if(fp==NULL){
printf("文件打开失败!");
return NULL; //exit(1); 修改
}
while(1){ //while(!feof(fp)) 修改
sd=(student*)malloc(sizeof(student));
sd->next=NULL;
if (fread(sd,sizeof(student),1,fp) != 1){ //修改
free(sd); //修改
break; //修改
}
if(Head){
tail->next=sd;
tail=tail->next;
}else{
Head=sd;
tail=sd;
}
}
fclose(fp); // 修改
tail=NULL;
return Head;
}
//xudent.dat里面有
//"123456","asd",1,"格林",1
//Pbrary.dat里面有
//“asd”,“1001”,1,5,56,“yangguang”,“huangyi”
//Head是图书馆,borrow_book是要借的书的编号,Student是借书的同学的指针
void borrow(book*Head,char*borrow_book,student*Student){
book*pre=Head;
while(pre){
if(strcmp(pre->id,borrow_book)==0){//if这一步没进去过,而且一直循环没出来
pre->loaned++;
strcpy(Student->borrow_book_name[Student->borrow_book_num++],borrow_book); // 修改
//strcpy(Student->borrow_book_name[Student->borrow_book_num],borrow_book);
printf("借书成功啦!\n");//修改
return; //break; 修改
}
pre=pre->next;
}
printf("借书未成功!\n"); //修改
return;
}
int main(){
student sd = {"123456","asd",1,"1001",1};
FILE*fp=NULL;
fp=fopen("student.dat","w");
fwrite(&sd,sizeof(student),1,fp);
fclose(fp);
book bk = {"asd","1001",1,5,56.0,"yangguang","huangyi"};
fp=fopen("library.dat","w");
fwrite(&bk,sizeof(book),1,fp);
fclose(fp);
book *Head=Initialisation_book();
student*Student=Initialisation_student();
borrow(Head,"1001",Student);
borrow(Head,"1002",Student); // 修改
return 0;
}