第一题:
#include <stdio.h>
#define LENGTH 5
int main(void)
{
float stu_score[LENGTH],score;
float avg_score,sum_score = 0.0f;
int grade_b = 0,grade_d = 0;
int i;
i = 0;
/* 输入20个学生的成绩 */
do
{
scanf("%f",&score);
if(score > 100)
{
printf("成绩不能超过100\n");
continue;
}
else if(score < 0) break; /* 成绩为负数,结束输入*/
stu_score[i] = score;
sum_score += stu_score[i]; /* 所有学生总成绩*/
if(stu_score[i] > 80) grade_b++;/* 成绩 > 80 的学生总人数*/
else if(stu_score[i] < 60) grade_d++;/* 成绩 < 60 的学生总人数*/
}while( i++ < LENGTH);
avg_score = sum_score / i; /* 平均成绩 */
printf("平均成绩=%.2f,成绩大于80分的人数=%d,不及格人数=%d\n",avg_score,grade_b,grade_d);
return 0;
}
第二题:
#include <stdio.h>
#define LENGTH 10
int main(void)
{
int i;
float score,max_score,min_score,sum_score,avg_score;
i = 0;
sum_score = 0.0f;
do
{
scanf("%f",&score);
if(score > 10 || score < 0)
{
printf("分数不超大于10,小于0\n");
continue;
}
if(i == 0)
{
max_score = min_score = score;
}
sum_score += score;
if(score >= max_score)
max_score = score;
else if(score <= min_score) min_score = score;
}while(++i < LENGTH);
avg_score = (sum_score - max_score - min_score) / (LENGTH - 2);
printf("%.2f,%.2f\n",max_score,min_score);
printf("平均成绩为:%.2f\n",avg_score);
return 0;
}
第三题:
#include <stdio.h>
#include <math.h>
double rectangle_area(double length,double width );
double rectangle_perimeter(double length,double width);
double rectangle_diagonal(double length,double width);
int main(void)
{
double length,width,area,perimeter,diagonal;
scanf("%lf%lf",&length,&width);
area = rectangle_area(length,width);
perimeter = rectangle_perimeter(length,width);
diagonal = rectangle_diagonal(length,width);
printf("area=%lf,perimeter=%lf,diagonal=%lf",area,perimeter,diagonal);
return 0;
}
/* 返回长方形面积 */
double rectangle_area(double length,double width )
{
return length * width;
}
/* 返回长方形周长 */
double rectangle_perimeter(double length,double width)
{
return (length + width) * 2;
}
/* 返回长方形对角线长 */
double rectangle_diagonal(double length,double width)
{
return sqrt(pow(length,2) + pow(width,2));
}