C语言练习题
练习一:C语言程序设计: 任意输入20个整数,分别统计奇奇数的个数、偶数的个数以及输出奇数和偶数。
#include "stdio.h"
int main()
{
int a[10];
int b[10], c[10], i, j = 0, k = 0, t;
printf("请输入所要输入整数的:");
for (i = 0; i < 10; i++)
{
scanf("%d", &a[i]);
if (a[i] % 2 == 0)
{
b[j] = a[i];
j++;
}
else
{
c[k] = a[i];
k++;
}
}
printf("偶数:\n");
for (t = 0; t < j; t++)
printf("%6d", b[t]);
printf("\n");
printf("奇数:\n");
for (t = 0; t < k; t++)
printf("%6d", c[t]);
printf("\n");
printf("奇数个数为:%d,偶数个数为:%d\n", j, k);
}
练习二:调用InputO函数从键盘上读入5本教材的书名(char(10))及价格(float),要求价格不能为负数,否则重新读入价格信息。调用Find0函数输出价格最高的教材的所有信息
请编写完整的程序实现上述功能,要求用结构体数组完成。
函数定义如下:
void Input(struct book *p,int n);
int Find(struct book *p,int n);
#include <stdio.h>
struct book
{
int book1, a;
char c[99];
} p[5];
void input(struct book *p, int n)
{
int i;
for (i = 0; i < n; i++)
scanf("%d%s%d", &p[i].book1, p[i].c, &p[i].a);
if (&p[i].a > 0)
{
printf("请重新输入\n");
for (i = 0; i < n; i++)
scanf("%d%s%d", &p[i].book1, p[i].c, &p[i].a);
}
}
int Find(struct book *p, int n)
{
struct book t;
int i, j, max;
max = 0;
for (i = 0; i < n; i++)
{
if (p[max].a < p[i].a)
max = i;
}
printf("%5d%s%5d\n", p[max].book1, p[max].c, p[max].a);
}
int main()
{
const int n = 5;
struct book p[n];
void input(struct book[], int);
int Find(struct book[], int);
input(p, n);
Find(p, n);
return 0;
}