结构体数组

/**
    该程序使用结构体构造了一个简单的书籍结构体
    主要是结构体数组的使用方法 
*/
#include <stdio.h> 
#define MAX_TITLE_SIZE 30
#define MAX_AUTHOR_SIZE 40
#define MAX_SIZE 2
//构造一个BOOK结构体,用于存放title,author,price  
struct book
{
   char title[MAX_TITLE_SIZE];
   char author[MAX_AUTHOR_SIZE];
   float price;    
};
int main()
{
    //设置一个计数器,用来计数输入的次数 
	int count=0;
    //设置另外一个计数器,用来遍历显示输入的book 
    int index=0;
    struct book lib[MAX_SIZE];
    printf("Set Book Title \n");
    //对相关的参量进行数据输入 
    while(count<MAX_SIZE)
	{
		printf("title is :");
		gets(lib[count].title);
		printf("SET Your AUthor :");
		gets(lib[count].author);
		printf("SET BOOKS price :");
		scanf("%f",&lib[count].price);
		count++;
		//如果不为\n,就继续下一次的数据输入 
		while(getchar()!='\n')
		{
			continue;
		}
            
		if(count<MAX_SIZE)
		{
			printf("Enter to next LINE to set title\n");
                    
		}
	}
	if(count>0)
	{
		printf("Here are book lists \n");
		//遍历结构体数组 
		for(index=0;index<count;index++)
		{
			printf("The Title is %s And The Author is %s And price is %f \n"
                            ,lib[index].title,lib[index].author,lib[index].price);           
		}    
	}
    return 0;
}

  

posted @ 2018-03-13 10:01  f826539110  阅读(812)  评论(0编辑  收藏  举报