标签:code dmi count 等级 col 图片 学生 %s 处理
part1
ex1-2
#include <stdio.h> #include <stdlib.h> const int N=5; // 定义结构体类型struct student,并定义STU为其别名 typedef struct student { long no; char name[20]; int score; }STU; // 函数声明 void input(STU s[], int n); int findMinlist(STU s[], STU t[], int n); void output(STU s[], int n); int main() { STU stu[N], minlist[N]; int count; printf("录入%d个学生信息\n", N); input(stu, N); printf("\n统计最低分人数和学生信息...\n"); count = findMinlist(stu, minlist, N); printf("\n一共有%d个最低分,信息如下:\n", count); output(minlist, count); system("pause"); return 0; } // 输入n个学生信息,存放在结构体数组s中 void input(STU s[], int n) { int i; for(i=0; i<n; i++) scanf("%ld %s %d", &s[i].no, s[i].name, &s[i].score); } // 输出结构体s中n个元素信息 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); } // 在结构体数组s中,查找最低分学生的记录,将其存入结构体数组t中 // 形参n是结构体数组s中元素个数 // 函数返回最低分的学生人数 int findMinlist(STU s[], STU t[], int n) { // 补足函数实现 int i,j,m,h=0; STU k; for(i=0;i<n;i++) for(j=0;j<n-1-i;j++) if(s[j].score>s[j+1].score) { k=s[j+1]; s[j+1]=s[j]; s[j]=k; } for(m=0,h=0;s[m].score==s[m+1].score&&m<n;m++) { t[h]=s[m]; t[h+1]=s[m+1]; h++; } h=h+1; if(h==1) t[h-1]=s[m]; return h; }
ex1-3
#include <stdio.h> #include <stdlib.h> #include <string.h> const int N = 10; // 定义结构体类型struct student,并定义其别名为STU typedef struct student { long int id; char name[20]; float objective; /*客观题得分*/ float subjective; /*操作题得分*/ float sum; char level[10]; }STU; // 函数声明 void input(STU s[], int n); void output(STU s[], int n); void process(STU s[], int n); int main() { STU stu[N]; printf("录入%d个考生信息: 准考证号,姓名,客观题得分(<=40),操作题得分(<=60)\n", N); input(stu, N); printf("\n对考生信息进行处理: 计算总分,确定等级\n"); process(stu, N); printf("\n打印考生完整信息: 准考证号,姓名,客观题得分,操作题得分,总分,等级\n"); output(stu, N); system("pause"); return 0; } // 录入考生信息:准考证号,姓名,客观题得分,操作题得分 void input(STU s[], int n) { // 补足代码 int i; for(i=0;i<n;i++) scanf("%ld %s %f %f\n",&s[i].id,s[i].name,&s[i].objective,&s[i].subjective); } //输出考生完整信息: 准考证号,姓名,客观题得分,操作题得分,总分,等级 void output(STU s[], int n) { // 补足代码 int i; printf("准考证号 姓名 客观题得分 操作题得分 总分 等级\n"); for(i=0;i<n;i++) printf("%5ld %10s %5.2f %8.2f %8.2f %8s\n",s[i].id,s[i].name,s[i].objective,s[i].subjective,s[i].sum,s[i].level); } // 对考生信息进行处理:计算总分,排序,确定等级 void process(STU s[], int n) { // 补足代码 int i,j,c,m; STU k; for(c=0;c<n;c++) s[c].sum=s[c].objective+s[c].subjective; for(i=0;i<n;i++) for(j=0;j<n-1-i;j++) if(s[j].sum<s[j+1].sum) { k=s[j+1]; s[j+1]=s[j]; s[j]=k; } for(m=0;m<n/10;m++) strcpy(s[m].level,"优秀"); for(;m<n/2;m++) strcpy(s[m].level,"合格"); for(;m<n;m++) strcpy(s[m].level,"不合格"); }
共用体与结构体类型区别:
共用体是几个不同变量共占用一段内存,且每次只有一个能使用;
结构体是每个成员都会有存储空间,且可以同时使用。
枚举型是描述整型常量数据的;
不能
不能 能
总结与体会:
1.编写第二个代码时,改了无数遍,然后又借鉴其他人正确的代码,但是在这里运行的时候输入的数据竟然是11个,无法理解,找不出自己错在哪里;
2.编写时,有时候会漏掉双引号;
3.总体来说,感觉多编代码还是挺好的,起码熟悉了知识点。
标签:code dmi count 等级 col 图片 学生 %s 处理
原文地址:https://www.cnblogs.com/2206664660-lgz/p/12089176.html