有一批图书,每本图书要登记作者姓名、书名、出版社、出版年月、价格等信息,试编写一个程序完成下列任务:
⑴ 读入每本书的信息存入数组中。
⑵ 输出价格在19.80元以下的书名及出版社名。
⑶ 输出2006年以后出版的图书具体信息。
#include<stdio.h>
#define N 99
struct date
{
int year;
int month;
int day;
};
struct xinxi
{
char author[N];
char bookname[N];
char publisher[N];
struct date publish_date;
float price;
}xx[N];
void main()
{
int n;
printf("要登记书的数量:");
scanf("%d",&n);
int i,j;
struct xinxi t;
printf("分别输入%d本书的作者、书名、出版社、出版年月、价格:\n",n);
for(i=0; i<n; i++)
{
scanf("%s %s %s %d-%d-%d %f",xx[i].author,xx[i].bookname,xx[i].publisher,
&xx[i].publish_date.year,&xx[i].publish_date.month,&xx[i].publish_date.day,&xx[i].price);
}
for(i=0; i<n; i++)
{
if(xx[i].price < 19.80)
printf("价格在19.80元以下的书名及出版社名:%s %s\n",xx[i].bookname,xx[i].publisher);
if(xx[i].publish_date.year > 2006)
printf("2006年以后出版的图书具体信息:%s %s %s %d-%d-%d %.2f\n",xx[i].author,xx[i].bookname,xx[i].publisher,
xx[i].publish_date.year,xx[i].publish_date.month,xx[i].publish_date.day,xx[i].price);
}
}
有8名学生,每个学生包括学号、姓名和成绩,要求按成绩递增排序并输出。要求如下:
⑴ 学生信息的输入和输出在主函数内实现。
⑵ 按成绩递增排序在sort函数中实现。
#include <stdio.h>
#include <stdlib.h>
#define N 8
struct Student{
int num;
char name[20];
float score;
};
void input(Student *st, int n);
void print(Student *st, int n);
void sort(Student *st, int n);
int main()
{
Student *stu;
int n;
stu=(Student*)malloc(N*sizeof(Student));
input(stu, N);
sort(stu, N);
print(stu, N);
system("pause");
return 0;
}
void input(Student *st, int n)
{
int i;
for (i=0; i<n; i++){
printf("请输入第%d个学生的信息(学号,姓名,成绩):\n ", i+1);
scanf("%d %s %f", &st[i].num, st[i].name, &st[i].score);
}
}
void print(Student *st, int n)
{
Student *p;
for (p=st, printf("Sort by:\n"); p<st+n; printf("%d %-7s %.2f\n", p->num, p->name, p->score), p++);
}
void sort(Student *st, int n)
{
int i, j;
Student temp;
for (i=0; i<n; i++)
for (j=i+1; j<n; j++)
if (st[i].score>st[j].score)
{
temp=st[i], st[i]=st[j], st[j]=temp;
}
}