我是这个网站的新手,看起来很酷。
我有这个问题,我想创建一个程序,用户决定要声明多少本书,然后用户填写所请求的每本书的信息(例如:用户想要3本书,所以他填写了所有的信息这3本书)。
我想我需要一个数组结构,但我不知道如何循环这个填充过程。
这是我的代码:
struct book
{
char bname[20];
int pages;
char author[20];
long price;
};
printf("enter number of books to store");
scanf("%d",&unumber);
for (i=0;i<number_of_books;i++) {printf's and scanf's to fill each struct}
答案 0 :(得分:0)
有两种方法可以做到这一点。一个是简单的使用图书结构的数组另一个使用链表。下面,我给你两个解决方案:
使用结构数组的第一个解决方案:
#include <stdio.h>
#include <stdlib.h>
#define MAXBOOKS 100
struct book {
char bname[20];
int pages;
char author[20];
long price;
};
int main()
{
struct book books[MAXBOOKS];
int number_of_books;
printf("enter number of books to store\n");
scanf("%d",&number_of_books);
// writing books
for (int i=0;i<number_of_books;i++) {
printf("Enter new book record: Book_Name, Pages, Author, Price\n");
scanf("%s %d %s %ld",books[i].bname, &books[i].pages, books[i].author, &books[i].price);
}
//reading books
printf("Entered books records: Book_Name, Pages, Author, Price\n");
for (int i=0;i<number_of_books;i++) {
printf("%s\t %d\t %s\t %ld\n",books[i].bname, books[i].pages, books[i].author, books[i].price);
}
return 0;
}
使用关联列表的第二个解决方案: 您可以将所有图书存储为链接列表。添加一个额外的数据字段,然后指向下一个节点簿。
#include <stdio.h>
#include <stdlib.h>
struct book {
char bname[20];
int pages;
char author[20];
long price;
struct book *next;
};
int main()
{
int number_of_books;
printf("enter number of books to store");
scanf("%d",&number_of_books);
struct book *head, *temp,*curnode;
head=NULL;
temp=NULL;
for (int i=0;i<number_of_books;i++) {
// writing books
temp=(struct book *)malloc(sizeof(struct book));
printf("Enter new book record: Book_Name, Pages, Author, Price\n");
scanf("%s %d %s %ld",temp->bname, &temp->pages, temp->author, &temp->price);
temp->next=NULL;
if(head==NULL){
printf("in head if");
head=temp;
curnode=temp;
} else {
curnode->next=temp;
curnode=curnode->next;
}
}
//reading books
temp=head;
while(temp!=NULL){
printf("%s\t %d\t %s\t %ld",temp->bname, temp->pages, temp->author, temp->price);
printf("\n");
temp=temp->next;
}
return 0;
}
答案 1 :(得分:0)
在循环中读取输入时,一个主要关注点是确保在每次读取后刷新stdin
以防止尾随newline
被解释为下一个read
语句的输入。有很多方法可以做到这一点,但是,使用scanf
最简单的方法是通过精心设计的format
语句来消耗尾随newline
。以下说明了这种方法:
#include <stdio.h>
#define MAXS 256
struct book
{
char bname[20];
int pages;
char author[20];
long price;
};
int main () {
struct book books[MAXS] = {{ {0}, 0, {0}, 0 }}; /* initialize all values to zero (null) */
int nbooks = 0;
int i = 0;
printf ("\nEnter number of books to store: ");
scanf("%d%*c",&nbooks); /* read nbooks, and consume newline */
if (nbooks < 1) { /* validate number of books to enter */
fprintf (stderr, "error: invalid entry for 'nbooks'\n");
return 1;
}
for (i = 0; i < nbooks; i++) /* enter values for each book, use */
{ /* scanf to read each value AND the */
printf ("\n book[%2d] name : ", i + 1); /* newline character, emptying stdin */
scanf ("%[^\n]%*c", books[i].bname); /* after each read. */
printf (" book[%2d] pages : ", i + 1);
scanf ("%d%*c", &books[i].pages);
printf (" book[%2d] author: ", i + 1);
scanf ("%[^\n]%*c", books[i].author);
printf (" book[%2d] price : ", i + 1);
scanf ("%ld%*c", &books[i].price);
}
printf ("\n\nThe Books Entered Were:\n"); /* output info for each book entered */
i = 0;
while (*books[i].bname)
{
printf ("\n Book %-3d \"%s\"\n", i + 1, books[i].bname);
printf (" author : %s\n", books[i].author);
printf (" pages : %d\n", books[i].pages);
printf (" price : %ld\n", books[i].price);
i++;
}
printf ("\n");
return 0;
}
<强>输出:强>
$ ./bin/books
Enter number of books to store: 2
book[ 1] name : Tom Sawyer
book[ 1] pages : 321
book[ 1] author: Mark Twain
book[ 1] price : 2199
book[ 2] name : Huckelberry Finn
book[ 2] pages : 298
book[ 2] author: Mark Twain
book[ 2] price : 1999
The Books Entered Were:
Book 1 "Tom Sawyer"
author : Mark Twain
pages : 321
price : 2199
Book 2 "Huckelberry Finn"
author : Mark Twain
pages : 298
price : 1999