第四次作业

6-1 按等级统计学生成绩
1.设计思路
(1)第一步:观察题意了解各个参数与所需函数在题目中的意义;
第二步:设计算法编写函数,让函数的功能实现题目中所需的功能;
第三步:运用面向对象的指向,运行程序检测是否错误。
(2)流程图

2.实验代码

//项目名称:按等级统计学生成绩

```#include <stdio.h>

define MAXN 10

struct student{ //构造结构体
int num;
char name[20];
int score;
char grade;
};

int set_grade( struct student *p, int n );

int main()
{ struct student stu[MAXN], *ptr;
int n, i, count;

ptr = stu;
scanf("%d\n", &n);
for(i = 0; i < n; i++){
   scanf("%d%s%d", &stu[i].num, stu[i].name, &stu[i].score);
}

count = set_grade(ptr, n);
printf("The count for failed (<60): %d\n", count);
printf("The grades:\n");
for(i = 0; i < n; i++)
printf("%d %s %c\n", stu[i].num, stu[i].name, stu[i].grade);
return 0;
}

int set_grade( struct student *p, int n )
{
int i,count = 0;
for(i=0;i<n;i++,p++)
{
if(p->score<60) {
p->grade='D';
count++;
}
else if((p->score<70)&&(p->score>=60))
{
p->grade='C';
}
else if((p->score<85)&&(p->score>=70))
{
p->grade='B';
}
else{
p->grade='A';
}
}
return count;
}

```
3.本题调试过程碰到问题及解决办法
错误信息1:无
错误原因:无
改正方法:无
git地址:https://coding.net/u/zhong123456/p/pta4/git/blob/master/6-1?public=true

1358233-20180422191750367-556675361.png

6-2 结构体数组按总分排序
1.设计思路
(1)第一步:观察题意了解各个参数与所需函数在题目中的意义;
第二步:设计算法编写函数,让函数的功能实现题目中所需的功能;
第三步:此题运用冒泡排序以及结构体里面向对象的指向,运行程序检测是否错误。
(2)流程图

2.实验代码

```#include <stdio.h>
struct student
{
int num;
char name[15];
float score[3];
float sum;
};
void calc(struct student p,int n);
void sort(struct student
p,int n);
int main()
{
struct student stu[5];
int i,j;
float f;
for(i=0;i<5;i++)
{
scanf("%d%s",&stu[i].num,stu[i].name);
for(j=0;j<3;j++)
{
scanf("%f",&f);
stu[i].score[j]=f;
}
}
calc(stu,5);
sort(stu,5);
for(i=0;i<5;i++)
{
printf("%5d%15s",stu[i].num,stu[i].name);
printf(" %.1f %.1f %.1f %.1f\n",stu[i].score[0],stu[i].score[1],stu[i].score[2], stu[i].sum);
}
return 0;
}

void calc(struct student *p,int n){
int i;
for(i=0;i<5;i++,p++)
{
p->sum=p->score[0]+p->score[1]+p->score[2];
}
}

void sort(struct student p,int n){
struct student max;
int i,j;
for(i=0;i<n-1;i++){
for(j=0;j<n-1-i;j++)
if((p+j)->sum<(p+j+1)->sum)
{
max =
(p+j);
(p+j)=(p+j+1);
*(p+j+1)=max;
}
}
}

```
3.本题调试过程碰到问题及解决办法
错误信息1:无
错误原因:无
改正方法:无
git地址:https://coding.net/u/zhong123456/p/pta4/git/blob/master/6-2?public=true

1358233-20180422191804035-402555379.png

转载于:https://www.cnblogs.com/lh-123/p/8908967.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值