ARTICLE DETAIL

资讯详情

深耕网站建设、视觉设计与SEO优化的一线实战洞察。

【C/PTA —— 15.结构体2(课内实践)】

【C/PTA —— 15.结构体2(课内实践)】

C/PTA —— 15.结构体2(课内实践)

  • 7-1 计算职工工资
  • 7-2 计算平均成绩
  • 7-3 找出总分最高的学生
  • 7-4 通讯录的录入与显示

7-1 计算职工工资

在这里插入图片描述

#include<stdio.h>
#include<stdlib.h>
typedef struct GZ
{char name[6];double j;double f;double z;double s;
}GZ;int main()
{int n = 0;scanf("%d", &n);GZ gz[100];for (int i = 0; i < n; i++){scanf("%s %lf %lf %lf", gz[i].name, &gz[i].j, &gz[i].f, &gz[i].z);}for (int i = 0; i < n; i++){gz[i].s = (gz[i].j + gz[i].f) - gz[i].z;printf("%s %.2lf", gz[i].name, gz[i].s);}return 0;
}

7-2 计算平均成绩

在这里插入图片描述

#include <stdio.h>
#include<stdlib.h>
//定义结构体
struct student
{char num[20]; //学号char nam[20];  //姓名int g;  //成绩
};
int main()
{struct student st[10];  //结构体数组int n;int i;float sum, aver;  //要求输出小数scanf("%d", &n);for (i = 0; i < n; i++){scanf("%s %s %d", &st[i].num, &st[i].nam, &st[i].g);sum += st[i].g; //录入的同时,计算成绩之和}aver = sum / n;  //计算平均成绩printf("%.2f\n", aver);for (i = 0; i < n; i++){if (st[i].g < aver){  //判断小于平均成绩,并输出姓名和学号printf("%s %s\n", st[i].nam, st[i].num);}}
}

7-3 找出总分最高的学生

在这里插入图片描述

#include<stdio.h>
#include<stdlib.h>
struct student {char num[6];char name[11];int score1;int score2;int score3;
};
int main()
{int i, n;struct student* p;scanf("%d", &n);p = (struct student*)malloc(sizeof(struct student));for (i = 0; i < n; i++){scanf("%s %s %d %d %d", p[i].num, p[i].name, &p[i].score1, &p[i].score2, &p[i].score3);}int s[20];for (i = 0; i < n; i++){s[i] = p[i].score1 + p[i].score2 + p[i].score3;}int max, j;max = s[0];for (i = 0; i < n; i++){if (s[i] >= max){max = s[i];j = i;}}printf("%s %s %d", p[j].name, p[j].num, max);free(p);return 0;}

7-4 通讯录的录入与显示

在这里插入图片描述

#include <stdio.h>
#include <stdlib.h>
int main() {struct inf {char name[20];char birthday[20];char sex[10];char tel[20];//固话 char  num[20];//手机 };int N = 0;scanf("%d", &N);struct inf list[N];int i = 0;for (; i < N; i++){scanf("%s %s %s %s %s\n", &list[i].name, &list[i].birthday, &list[i].sex, &list[i].tel, &list[i].num);}int k = 0;scanf("%d", &k);//要查询的个数 i = 0;int q[15] = { 0 };//记录要查询哪几条for (; i < k; i++){scanf("%d", &q[i]);}i = 0;for (; i < k; i++){if (q[i] >= 0 && q[i] < N)//要考虑到查查询的记录的有效性 printf("%s %s %s %s %s\n", list[q[i]].name, list[q[i]].tel, list[q[i]].num, list[q[i]].sex, list[q[i]].birthday);elseprintf("Not Found\n");}return 0;}
返回列表