ADD file via upload

main
pngtmzsyq 1 year ago
parent 84493e42d3
commit 70062fe30b

@ -0,0 +1,106 @@
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
typedef struct Student{
char id[100];
int cl;
char name[100];
float score1;
float score2;
float score3;
float total_score;
}Student;
void getinput(int *type,int *p1,int *p2,int *p3){
scanf("%d",type);
if(*type==1||*type==2){
scanf("%d-%d",p1,p2);
}else if(*type==3){
char temp[20];
scanf("%s",temp);
*p1=temp[0];
}else if(*type==5){
scanf("%d.%d-%d",p1,p2,p3);
}else{
scanf("%d",p1);
}
}
void query(Student *students,int numStudents,int type,int p1,int p2,int p3,Student *results,int *numResults){
int i;
*numResults = 0;
char temp1[20], temp2[20],temp3[20];
sprintf(temp1, "%d", p1);
sprintf(temp2, "%d", p2);//格式化数据写入临时字符串
sprintf(temp3,"%d",p3);
for(i=0; i<numStudents; i++){
students[i].total_score = students[i].score1 + students[i].score2 + students[i].score3;
switch(type){
case 1:
if(students[i].cl>=p1&&students[i].cl<=p2){
results[(*numResults)++] = students[i];//班级号在p1-p2内
}
break;
case 2:
if(strcmp(students[i].id,temp1)>=0 && strcmp(students[i].id,temp2)<=0){
results[(*numResults)++] = students[i];//学号在p1-p2内
}
break;
case 3:
if(students[i].name[0]==p1){
results[(*numResults)++] = students[i];//名字以特定字母开头的学生
}
break;
case 4:
if(students[i].total_score>=p1){
results[(*numResults)++] = students[i];//总分大于等于特定分数的学生
}
break;
case 5:
if(students[i].cl==p1 && strcmp(students[i].id,temp2)>=0 && strcmp(students[i].id,temp3)<= 0){
results[(*numResults)++] = students[i];//特定班级、特定学号范围的学生
}
break;
}
}
}
void sort(Student student[],int n){
int i,j;
for(i=0;i<n-1;i++){
for(j=0;j<n-1-i;j++){
if(student[j].cl>student[j + 1].cl||(student[j].cl==student[j + 1].cl&&student[j].total_score<student[j + 1].total_score)){
Student temp=student[j];
student[j]=student[j+1];
student[j+1]=temp;
}
}
}
}
void printResults(Student *results, int numResults){
int i;
for(i=0; i<numResults; i++){
printf("%s %d %s %.1f %.1f %.1f\n", results[i].id, results[i].cl, results[i].name, results[i].score1, results[i].score2, results[i].score3);
}
}
int main(){
Student students[7] = {
// 定义全局学生数组
{"10001", 11, "Zhang", 99.5, 88.5, 89.5},
{"10002", 12, "Yang", 77.9, 56.5, 87.5},
{"10003", 11, "Liang", 92.5, 99.0, 60.5},
{"10004", 11, "Cai", 89.6, 56.9, 90.5},
{"10005", 14, "Fu", 55.6, 67.9, 98.9},
{"10006", 12, "Mao", 22.1, 45.9, 99.2},
{"10007", 13, "Zhan", 35.6, 67.9, 88.0}
};
int type;
int p1,p2,p3;
printf("请输入查询类型和值(格式:类型 值):");
getinput(&type,&p1,&p2,&p3);
Student results[7];
int numResults;
query(students,7,type,p1,p2,p3,results,&numResults);
sort(results,numResults);
printResults(results,numResults);
return 0;
}
Loading…
Cancel
Save