ADD file via upload

main
pvl836cjw 1 year ago
parent e855191657
commit 3c9bd2291f

@ -0,0 +1,138 @@
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
struct Student{
int stunumber;
int class;
char name[10];
float grade1;
float grade2;
float grade3;
float sum;
}students[7]={
{10001,11,"Zhang",99.5,88.5,89.5,99.5+88.5+89.5},
{10002,12,"Yang",77.9,56.5,87.5,77.9+56.5+87.5},
{10003,11,"Liang",92.5,99.0,60.5,92.5+99.0+60.5},
{10004,11,"Cai",89.6,56.9,90.5,89.6+56.9+90.5},
{10005,14,"Fu",55.6,67.9,98.9,55.6+67.9+98.9},
{10006,12,"Mao",22.1,45.9,99.2,22.1+45.9+99.2},
{10007,13,"Zhan",35.6,67.9,88.0,35.6+67.9+88.60}};
//一系列索引变量
int class_idx,min_idx,max_idx,i,n,j;
float sum_idx;
char name_idx[10];
void selection_sort(struct Student arr[], int n) {
int i, j, min_idx;
//一一检索每个结构体中class的值
for (i = 0; i < n-1; i++) {
// 找到最小值的位置
min_idx = i;
for (j = i+1; j < n; j++)
{
if (arr[j].class < arr[min_idx].class)
{
min_idx = j;
}
else if(arr[j].class == arr[min_idx].class)
{
if(arr[j].sum > arr[min_idx].sum)
{
min_idx=j;
}
}
}
// 通过交换位置实现排序
struct Student temp = arr[min_idx];
arr[min_idx] = arr[i];
arr[i] = temp;
}
}
void print_student(struct Student *s) {
printf("%d %d %s %.1f %.1f %.1f \n", s->stunumber, s->class, s->name, s->grade1, s->grade2, s->grade3);
}
int main(){
void selection_sort(struct Student arr[], int n);
void print_student(struct Student *s);
selection_sort(students,7);
int Section_idx;
printf("请输入类别\n");
scanf("%d",&Section_idx);//输入类别
switch(Section_idx)
{
case 1:goto section1;break;
case 2:goto section2;break;
case 3:goto section3;break;
case 4:goto section4;break;
case 5:goto section5;break;
default:printf("输入错误");
}
return 0;
section1://查找指定班级范围内的学生
printf("请输入您要查找的信息(格式:班级-班级)\n"); //班级-班级
scanf("%d-%d",&min_idx,&max_idx);
for(i=0;i < 7;i++)
{
if(students[i].class>=min_idx&&students[i].class<=max_idx)
{
printf("%d %d %s %.1f %.1f %.1f \n", students[i].stunumber, students[i].class, students[i].name, students[i].grade1, students[i].grade2, students[i].grade3);
}
}
return 0;
section2://查找指定学号范围的学生
printf("请输入您要查找的信息(格式:学号-学号)\n"); //学号-学号
scanf("%d-%d",&min_idx,&max_idx);
for(i=0;i < 7;i++)
{
if(students[i].stunumber >= min_idx&&students[i].stunumber <= max_idx)
printf("%d %d %s %.1f %.1f %.1f \n", students[i].stunumber, students[i].class, students[i].name, students[i].grade1, students[i].grade2, students[i].grade3);
}
return 0;
section3://查找指定名字开头字母的学生
printf("请输入您要查找的信息(格式:开头字母+*\n"); //字母*
scanf("%s",name_idx);
for(i=0;i<7;i++)
{
n=strlen(name_idx)-1;
for(j=0;j<strlen(name_idx)-1;j++)
{
if(name_idx[j]==students[i].name[j])
{
n--;
}
}
if(n==0)
{
printf("%d %d %s %.1f %.1f %.1f \n", students[i].stunumber, students[i].class, students[i].name, students[i].grade1, students[i].grade2, students[i].grade3);
}
}
return 0;
section4://查找大于指定总成绩的学生
printf("请输入您要查找的信息(格式:总成绩(小数形式)\n"); //班级.学号-学号
scanf("%f",&sum_idx);
for(i = 0;i < 7;i++)
{
if(students[i].sum>=sum_idx)
{
printf("%d %d %s %.1f %.1f %.1f \n", students[i].stunumber, students[i].class, students[i].name, students[i].grade1, students[i].grade2, students[i].grade3);
}
}
return 0;
section5://查找指定班级下指定学号范围内的学生
printf("请输入您要查找的信息(格式:班级+.+学号-学号)\n");
scanf("%d.%d-%d",&class_idx,&min_idx,&max_idx);
for(i = 0;i < 7;i++)
{
if(students[i].class==class_idx&&students[i].stunumber>=min_idx&&students[i].stunumber<=max_idx)
{
printf("%d %d %s %.1f %.1f %.1f \n", students[i].stunumber, students[i].class, students[i].name, students[i].grade1, students[i].grade2, students[i].grade3);
}
}
return 0;
}
Loading…
Cancel
Save