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;
}
#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;
}
相关文章推荐
- c/c++学习之c++ 中的list <>容器
- 读取DXF格式文件的C++代码
- C语言知识概述
- 简单项目开发----数字采选系统(c++编写)
- C++作业4.20
- C++作业4.19
- c++ 使用 gsoap 调用 java WebService 中文乱码
- 小结两种在Python中导入C语言扩展库的方法
- C++中 explicit的用法
- HDU-1034-Candy Sharing Game(C++ && 简单模拟)
- C/C++如何使用密码输入而不回显--getch()函数
- C++作业4.18
- 【干货】国外程序员整理的 C++ 资源大全–日常工作,我觉得用处确实很大,所以分享
- 【干货】国外程序员整理的 C++ 资源大全–日常工作,我觉得用处确实很大,所以分享
- C++ 写leetcode遇到的一些问题总结Implement strStr()
- C++读取并逐行显示txt内容
- C语言特性
- C/C++计时方法及精度
- C++中placement new
- VC++6.0 IDE的工程用Code::Blocks来打开、编译、调试终极配置方案