1. 实验任务1
#define _CRT_SECURE_NO_WARNINGS #include<stdio.h> #include<stdlib.h> #include<string.h> #define N 3 typedef struct student{ int id; char name[20]; char subject[20]; double perf;//平时 double mid; double final; double total; char level[10]; }STU; void input(STU s[], int n); void output(STU s[], int n); void calculate(STU s[], int n); int fail(STU[], STU[], int n); void sort(STU s[],int n); int main() { STU st[N], fst[N]; int k; printf("录入学生成绩\n"); input(st, N); printf("\n学生成绩处理\n"); calculate(st, N); k = fail(st, fst, N); sort(st, N); printf("\n学生成绩排名:\n"); output(st, N); printf("\n不及格学生信息:\n"); output(fst, k); return 0; } void input(STU s[], int n) { int i; for (i = 0; i < n; i++) { scanf_s("%d %s %s %lf %lf %lf", &s[i].id, s[i].name,20, s[i].subject,20, &s[i].perf, &s[i].mid, &s[i].final); } return; } void output(STU s[], int n) { int i; printf("-------------------\n"); printf("学号 姓名 科目 平时 期中 期末 总评 等级\n"); for (i = 0; i < n; i++) { printf(" % d %-6s %-4s %-4.0lf %-4.0lf %-4.0lf\n", s[i].id, s[i].name, s[i].subject, s[i].perf, s[i].mid, s[i].final); } return; } void calculate(STU s[], int n) { int i; for (i = 0; i < n; i++) { s[i].total = s[i].perf * 0.2 + s[i].mid * 0.2 + s[i].final * 0.6; if (s[i].total >= 90) { strcpy(s[i].level, "优"); } else if (s[i].total >= 80&&s[i].total<90) { strcpy(s[i].level, "良"); } else if (s[i].total >= 70 && s[i].total < 80) { strcpy(s[i].level, "中"); } else if (s[i].total >= 60 && s[i].total < 70) { strcpy(s[i].level, "及格"); } else { strcpy(s[i].level, "不及格"); } } } int fail(STU s[], STU t[], int n) { int i, k = 0; for (i = 0; i < n; i++) { if (s[i].total < 60) { t[k++] = s[i]; } } return k; } void sort(STU s[], int n) { int i, j; STU t; for (i = 0; i < n-1; i++) { for (j = 0; j < n - i-1; j++) { if (s[j].total < s[j + 1].total) { t = s[j]; s[j] = s[j + 1]; s[j + 1] = t; } } } }2. 实验任务2
#include<stdio.h> #include<stdlib.h> #include<string.h> #define N 10 #define M 80 typedef struct stdent { char name[M]; char author[M]; }BOOK; int main() { BOOK x[N] = { {"《一九八四》", "乔治.奥威尔"}, {"《美丽新世界》", "赫胥黎"}, {"《昨日的世界》", "斯蒂芬.茨威格"}, {"《万历十五年》", "黄仁宇"}, {"《一只特立独行的猪》", "王小波"}, {"《百年孤独》", "马尔克斯"}, {"《情人》", "玛格丽特.杜拉斯"}, {"《只是孩子》", "帕蒂.史密斯"}, {"《刀锋》", "毛姆"}, {"《沉默的大多数》", "王小波"} }; BOOK* ptr = x; char author[20]; for (ptr = x; ptr < x + N; ptr++) { printf("%-30s %-30s\n", ptr->name, ptr->author); } printf("输入作者名字\n"); gets_s(author,20); printf("其著作为:\n"); for (ptr = x; ptr < ptr + N; ptr++) { if (strcmp(author, ptr->author) == 0) { printf("%-30s %-30s\n", ptr->name,ptr->author); } } return 0; }3. 实验任务3
#include<stdio.h> #include<stdlib.h> #include<string.h> #define N 100 typedef struct student { char num[10]; int s1; int s2; double sum; char level[10]; }STU; int fun(STU a[], int n, STU h[]); int main() { STU s[N] = { {"GA05", 85, 76}, {"GA03", 76, 90}, {"GA02", 69, 90}, {"GA04", 85, 56}, {"GA01", 91, 95}, {"GA07", 72, 80}, {"GA08", 64, 45}, {"GA06", 87, 98}, {"GA015", 85, 86}, {"GA013", 91, 97} }; STU h[N]; int i, k, n = 10; k = fun(s, n, h); printf("There are:\n"); for (i = 0; i < k; i++) { printf("%s %d %d %.2f %s\n", h[i].num, h[i].s1, h[i]. s2, h[i].sum, h[i].level); } return 0; } int fun(STU a[], int n, STU h[]) { int i,count=0; double avg=0; for (i = 0; i < n; i++) { a[i].sum = a[i].s1 * 0.7 + a[i].s2 * 0.3; avg += a[i].sum; } avg /= n; for (i = 0; i < n; i++) { if (a[i].sum > avg) { strcpy_s(a[i].level, 10, "均分以上"); h[count++] = a[i]; } } return count; }4. 实验任务4
#include<stdio.h> #include<stdlib.h> #include<string.h> #define N 5 typedef struct stdent { char name[10]; int num; int maths; int computer; int english; int sum; char level[10]; }STU; void fun(STU a[],int n); int main() { STU s[6 * N] = { {"A001", 1, 34, 67, 80}, {"B003", 3, 78, 87, 90}, {"A002", 2, 90, 98, 99}, {"B002", 4, 56, 78, 98}, {"A005", 5, 35, 67, 79} }; int i; fun(s, N); for (i = 0; i < N; i++) { printf("%s %d %d %d %d %s\n", s[i].name, s[i].num, s[i].maths, s[i].computer, s[i].sum, s[i].level); } return 0; } void fun(STU a[], int n) { int i, max = 0, min = 300; for (i = 0; i < n; i++) { a[i].sum = a[i].maths + a[i].computer + a[i].english; if (max < a[i].sum) { max = a[i].sum; } if (min > a[i].sum) { min = a[i].sum; } } for (i = 0; i < n; i++) { if (a[i].sum == max) { strcpy_s(a[i].level, 10, "优秀"); } else if (a[i].sum == min) { strcpy_s(a[i].level, 10, "不及格"); } else{ strcpy_s(a[i].level, 10, "合格"); } } }5. 实验任务5
#include<stdio.h> #define N 5 typedef struct student { long no; char name[20]; int score; }STU; void input(STU s[], int n); int find_min_list(STU s[], STU t[], int n); void output(STU s[], int n); int main() { STU stu[N], min_list[N]; int count; printf("录入%d个学生信息\n", N); input(stu, N); printf("\n统计最低分人数和学生信息...\n"); count = find_min_list(stu, min_list, N); output(min_list, count); return 0; } void input(STU s[], int n){ int i; for (i = 0; i < n; i++) { scanf_s("%ld %s %d", &s[i].no, s[i].name, 20,&s[i].score); } return; } int find_min_list(STU s[], STU t[], int n) { int i,min=100; int count=0; for (i = 0; i < n; i++) { if (min > s[i].score) { min = s[i].score; } } for (i = 0; i < n; i++) { if (s[i].score==min) { t[count++] = s[i]; } } return count; } void output(STU s[], int n) { int i; for (i = 0; i < n; i++) { printf("%ld %s %d\n", s[i].no, s[i].name,s[i].score); } return; }
标签:int,void,++,STU,实验,printf,sum From: https://www.cnblogs.com/waterwhy/p/16995580.html