C/PTA —— 14.结构体1(课外实践)

  • 一.函数题
    • 6-1 选队长
    • 6-2 按等级统计学生成绩
    • 6-3 学生成绩比高低
    • 6-4 综合成绩
    • 6-5 利用“选择排序算法“对结构体数组进行排序
    • 6-6 结构体的最值
    • 6-7 复数相乘运算
  • 二.编程题
    • 7-5 一帮一
    • 7-6 考试座位号

一.函数题

6-1 选队长

void showCaptain(TeamMember team[], int n){TeamMember max;max = team[0];for (int i = 1; i < n; i++){if (max.ability < team[i].ability)max = team[i];}printf("%d %s %s %s %.2lf", max.id, max.lastname,max.firstname, max.sex, max.ability);}

6-2 按等级统计学生成绩

int set_grade(struct student* p, int n){int count = 0;for (int i = 0; i < n; i++){if (p[i].score >= 85 && p[i].score <= 100)p[i].grade = 'A';if (p[i].score >= 70 && p[i].score <= 84)p[i].grade = 'B';if (p[i].score >= 60 && p[i].score <= 69)p[i].grade = 'C';if (p[i].score >= 0 && p[i].score <= 59){p[i].grade = 'D';count++;} }return count;}

6-3 学生成绩比高低

int compareScore(const struct Student* s1, const struct Student* s2){if ((s1->C + s1->English) > (s2->C + s2->English))return 1;if ((s1->C + s1->English) < (s2->C + s2->English))return -1;if ((s1->C + s1->English) == (s2->C + s2->English)){if (s1->C > s2->C)return 1;if (s1->C < s2->C)return -1;if (s1->C == s2->C)return 0;}}

6-4 综合成绩

double getAverage(Applicant* a){double sum = 0.0;sum = a->computational * 1.0 * 0.4 + a->humanistic * 1.0 * 0.5 + a->logical * 1.0 * 0.3 + a->presentation * 1.0 * 0.6 + a->scientific * 1.0 * 0.8;return sum;}

6-5 利用“选择排序算法“对结构体数组进行排序

int min_idx = p1->score;for (p2 = p1 + 1; p2 < pData + n; p2++){if (p2->score > min_idx){min_idx = p2->score;p = p2;}}if (min_idx != p1->score){num = p1->num;score = p1->score;p1->num = p->num;p1->score = p->score;p->num = num;p->score = score;}

6-6 结构体的最值

ST* MaxST(ST d[], int n, int k){ST* max = NULL;for (int i = 0; i < n; i++){if (d[i].gender == k && (max == NULL || max->scored < d[i].scored)){max = &d[i];}}return max;}

6-7 复数相乘运算

PLEX multi(PLEX a, PLEX b){PLEX product;product.re = a.re * b.re - a.im * b.im;product.im = a.re * b.im + a.im * b.re;return product;}

二.编程题

7-5 一帮一

#include#includestruct student{int a;char name[20];};struct student1{int b;char name1[20];};int main(){struct students1[50];struct student1 s2[50];struct student1 s3[50];int i, n, j = 0, t = 0, c, d;scanf("%d", &n);for (i = 0; i < n; i++){scanf("%d %s", &s1[i].a, s1[i].name);}for (i = 0; i < n; i++){if (s1[i].a == 1){s2[j].b = i;strcpy(s2[j].name1, s1[i].name);j++;}if (s1[i].a == 0){s3[t].b = i;strcpy(s3[t].name1, s1[i].name);t++;}}c = n / 2 - 1, d = n / 2 - 1;j = 0, t = 0;for (i = 0; i < n / 2; i++){if (s3[j].b < s2[t].b){printf("%s %s\n", s3[j].name1, s2[c].name1);j++;c--;}else{printf("%s %s\n", s2[t].name1, s3[d].name1);t++;d--;}}return 0;}

7-6 考试座位号

#includestruct student{char number[17];int s;int k;};int main(){int N = 0;scanf("%d", &N);struct student stu[1001] = {0};for (int i = 0; i < N; i++){scanf("%s %d %d", stu[i].number, &stu[i].s, &stu[i].k);}int n = 0;scanf("%d", &n);int ret = 0;for (int i = 0; i < n; i++){scanf("%d", &ret);int j = 0;for (j = 0; j < N; j++){if (ret == stu[j].s){printf("%s %d\n", stu[j].number, stu[j].k);}}}return 0;}