c++编程。。。。。。

 

#include <bits/stdc++.h>
using namespace std;
#include <stdio.h>
#include <stdlib.h>
#define N 30
/**
为 struct students定义了一个新的名字 STU
与 typedef struct students STU 等价
**/
typedef struct students
{
    long id;
    char name[10];
    float score;
}STU;
void addscore(STU stu[],int n);
void showscore(STU stu[],int n);
void lowTOhigh(STU stu[],int n);
void highTOlow(STU stu[],int n);
int showlist(void);
int main()
{
    char choose;
    int n;
    STU stu[N];
    printf("你想输入多少名同学的信息:");
    scanf("%d",&n);
    while(1){
        choose=showlist();
        switch(choose)
    {
        case 1:addscore(stu,n);
            break;
        case 2:showscore(stu,n);
            break;
        case 3:highTOlow(stu,n);
               showscore(stu,n);
            break;
        case 4:lowTOhigh(stu,n);
                showscore(stu,n);
            break;
        default:printf("Input error!\n");
    }
}
   return 0;
}
void addscore(STU stu[],int n){
    int i;
    printf("***请输入学生的学号、姓名和成绩:\n(注意输入一项数据按一下回车键)\n");
    for(i=0;i<n;i++){
        scanf("%ld",&stu[i].id);
        scanf("%s",stu[i].name);
        scanf("%f",&stu[i].score);
    }
}
int showlist(void){
    int choose;
    printf("1.增加学生信息\n");
    printf("2.显示所有学生信息\n");
    printf("3.成绩由高到低排序\n");
    printf("4.成绩由低到高排序\n");
    printf("\n请选择:");
    scanf("%d",&choose);
    return choose;
}
void showscore(STU stu[],int n){
    int i;
    for(i=0;i<n;i++){
        printf("%ld%5s%6.1f\n",stu[i].id,
                                stu[i].name,
                                stu[i].score);
    }
}
void highTOlow(STU stu[],int n){
    int i,j,k;
    float temp1,temp2;
    for(i=0;i<n-1;i++){
        k=i;
        for(j=i+1;j<n;j++){
            if(stu[j].score>stu[k].score) k=j;
        }
        if(k!=i){
            temp1=stu[k].score;
            stu[k].score=stu[i].score;
            stu[i].score=temp1;
            temp2=stu[k].id;
            stu[k].id=stu[i].id;
            stu[i].id=temp2;
        }
    }
}
void lowTOhigh(STU stu[],int n){
    int i,j,k;
    float temp1,temp2;
    for(i=0;i<n-1;i++){
        k=i;
        for(j=i+1;j<n;j++){
            if(stu[j].score<stu[k].score) k=j;
        }
        if(k!=i){
            temp1=stu[k].score;
            stu[k].score=stu[i].score;
            stu[i].score=temp1;
            temp2=stu[k].id;
            stu[k].id=stu[i].id;
            stu[i].id=temp2;
        }
    }
}

 

您好,我是有问必答小助手,您的问题已经有小伙伴解答了,您看下是否解决,可以追评进行沟通哦~

如果有您比较满意的答案 / 帮您提供解决思路的答案,可以点击【采纳】按钮,给回答的小伙伴一些鼓励哦~~

ps: 问答会员年卡【8折】购 ,限时加赠IT实体书,即可 享受50次 有问必答服务,了解详情>>>https://t.csdnimg.cn/RW5m