您的位置:首页 > 编程语言 > C语言/C++

C语言之结构数组图书管理

2015-05-29 18:19 369 查看
/*包含多本书的图书目录*/

#include<stdio.h>

#define MAXBKS 2
//定义最多容纳的图书册数

struct book // 结构体名为bool

{

char title[50];

char author[50];

float value;

};

int main()

{

struct book library[MAXBKS]; //定义结构数组

int count=0;

int index;

printf("input a title\n");

printf("press[enter]at the begin to end\n");

gets(library[count].title);

while(count<MAXBKS&&library[count].title[0]!='\0')

{

printf("input author\n");

gets(library[count].author);

printf("input value\n");

scanf("%f",&library[count].value);

count++;

while(getchar()!='\n')

continue;

/*按入回车时不进入while循环,相当于某种意义上的清除回车,若没有这句话,会输入回车,然后跳出while循环*/

if(count<MAXBKS)

{printf("input next title\n");

gets(library[count].title);}

}



if(count>0)

{

printf("here are 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\n");

return 0;

}



在看一个例子

输入4名学生的各3门成绩,然后求出每个学生的总成绩和平均成绩并打印出来

#include<stdio.h>>

struct student2

{

int number;

char name[10];

char sex;

float score[3];//3科成绩

float sum;

float ave;

};

int main()

{

int i,j;

struct student2 stu[4];//结构体数组

for(i=0;i<4;i++)

{

printf("input\n");

scanf("%d %s %c",&stu[i].number,&stu[i].name,&stu[i].sex);//注意别落了取地址符&

stu[i].sum=0;

stu[i].ave=0;

printf("input score\n");

for(j=0;j<3;j++)

{

scanf("%f",&stu[i].score[j]);

stu[i].sum+=stu[i].score[j];

}

stu[i].ave=stu[i].sum/3;

}

printf("学号 姓名 性别 语文 数学 英语 总分 均分\n");

for(i=0;i<4;i++)

{

printf("%d %s %c",stu[i].number,stu[i].name,stu[i].sex);

for(j=0;j<3;j++)

{

printf(" %.2f",stu[i].score[j]);

}

printf(" %.2f %.2f\n",stu[i].sum,stu[i].ave);

}

return 0;

}

内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: