|
发表于 2012-4-11 22:10:12
|
显示全部楼层
两处错误吧
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
typedef struct{
int year,month,day;
}DATE;
typedef struct book{
DATE date;
char name[20];
char author[10];
int type;
double price;//可以改成double
}BOOK;
typedef struct node{
BOOK book;
struct node *next;
}Node;
typedef Node *List;
BOOK books[200];
/*初始化*/
void InitializeList(List plist)
{
plist=NULL;
}
/*添加书本*/
void AddBook(List plist)
{
FILE *fp;
int n,m;
int i=0;
List a,b,head;
head=NULL;
a=b=(struct node *)malloc(sizeof(struct node));
printf("请输入要输入几本书:");
scanf("%d",&n);
while(i<n){
printf("请输入书本购买日期(年 月 日):");
scanf("%d %d %d",&a->book.date.year,&a->book.date.month,&a->book.date.day);
printf("请输入书本名字:");
scanf("%s",a->book.name);
printf("请输入作者名字:");
scanf("%s",a->book.author);
printf("请输入书本类型:");
scanf("%d",&a->book.type);
printf("请输入书本价格:");
scanf("%lf",&a->book.price); // scanf("%.2f",&a->book.price);这里是第一处错误的地方
if(i==0)head=a;
else b->next=a;
b=a;
a=(struct node *)malloc(sizeof(struct node));
i++;
};
a=head;
for(m=0;m<n;m++){
books[m].date.year=a->book.date.year;
books[m].date.month=a->book.date.month;
books[m].date.day=a->book.date.day;
strcpy(books[m].name,a->book.name);
strcpy(books[m].author,a->book.author);
books[m].type=a->book.type;
books[m].price=a->book.price;
a=a->next;
};
for(m=0;m<n;m++)
{
//fread(&books[m],sizeof(struct book),1,fp); //这里是第二处错误的地方
printf("%d %d %d\n",books[m].date.year,books[m].date.month,books[m].date.day);
printf("%s\n",books[m].name);
printf("%s\n",books[m].author);
printf("%d\n",books[m].type);
printf("%.2lf\n",books[m].price);
}}
void main()
{
List plist;
plist=(struct node *)malloc(sizeof(struct node));
InitializeList(plist);
AddBook(plist);
}
|
|