69,371
社区成员
发帖
与我相关
我的任务
分享
#include<stdio.h>
#define MAXTITLE 2 //标题
#define MAXAUTL 2 //作者
#define MAXBKS 2 //最多可以容纳图书册数
struct book //建立book模板
{
char title[MAXTITLE];
char author[MAXAUTL];
float value;
};
int main(void)
{
struct book library[MAXBKS];
int count=0;
int index;
printf("please enter the books title.\n");
printf("please [enter] at the start of aline to stop.\n");
while(count<MAXBKS && gets(library[count].title)!=NULL && library[count].title[0]!='\0')
{
printf("Now enter the book author.\n");
gets(library[count].author);
printf("Now enter the value.\n");
scanf("%f",&library[count++].value);
while(getchar()!='\n')
continue;
if(count<MAXBKS)
printf("Enter the next title.\n");
}
if(count>0)
{
printf("Here is the list of your books:\n");
for(index=0;index<count;index++)
printf("%s by %s: $ %.2f \n",library[index].title,library[index].author,library[index].value);
}
else
printf("No book? Too bad!");
return 0;
}
lease enter the books title
please [enter] at the start of aline to stop
111
Now enter the book author
kkk
Now enter the value
12
Enter the next title
222
Now enter the book author
eee
Now enter the value
32
Here is the list of your books
11kk by kk: $ 12.00 应该是;111 by kkk &12.00
22ee by ee: $ 32.00 222 by eee &32.00
#include<stdio.h>
#define MAXTITLE 4 //这里
#define MAXAUTL 4 //这里
#define MAXBKS 2 //最多可以容纳图书册数
struct book //建立book模板
{
char title[MAXTITLE];
char author[MAXAUTL];
float value;
};
int main(void)
{
struct book library[MAXBKS];
int count=0;
int index;
printf("please enter the books title.\n");
printf("please [enter] at the start of aline to stop.\n");
while(count<MAXBKS && gets(library[count].title)!=NULL && library[count].title[0]!='\0')
{
library[count].title[MAXTITLE-1] = '\0'; //输出字符串得在字符数组末尾加'\0'
printf("Now enter the book author.\n");
gets(library[count].author);
library[count].author[MAXAUTL-1] = '\0';
printf("Now enter the value.\n");
scanf("%f",&library[count++].value);
while(getchar()!='\n')
continue;
if(count<MAXBKS)
printf("Enter the next title.\n");
}
if(count>0)
{
printf("Here is the list of your books:\n");
for(index=0;index<count;index++)
printf("%s by %s: $ %.2f \n",library[index].title,library[index].author,library[index].value);
}
else
printf("No book? Too bad!");
return 0;
}