hhhhh112112138 2021-12-02 13:49 采纳率: 75%
浏览 31
已结题

成绩统计,自己写了代码,但是输出之后并没有得到预期效果,请问能否改正一下?主要是数组的运用

键盘输入10个学生学号(7位)姓名最长四个汉字2字节编码,成绩
分为四个小部分,(1)按学号降序打印及格名单;
(2)按成绩升序打印不及格名单
(3)按学号升序打印全部学生
(4)按成绩降序打印全部学生
(1)和(4)用两个char型二维字符数组表示10个学生的学号姓名
(2)和(3)用两个string类一维数组表示10个学生的学号姓名
排序用选择法或者冒泡法

#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
#include <string.h>
#define n 10
void  SwapFloat(float* x, float* y)
{
    float  temp;
    temp = *x;
    *x = *y;
    *y = temp;
}
void  SwapLong(long* x, long* y)
{
    long   temp;
    temp = *x;
    *x = *y;
    *y = temp;
}
void  SwapChar(char x[], char y[])
{
    char temp[10];
    strcpy(temp, x);
    strcpy(x, y);
    strcpy(y, temp);
}

void xuehao(long num[], char name[][10], float score[])
{
    int i, j, k;
    for (i = 0; i < n - 1; i++)
    {
        k = i;
        for (j = i + 1; j < n; j++)
        {
            if (num[j] < num[k])  k = j;
        }
        if (k != i)
        {
            SwapFloat(&score[k], &score[i]); /* 交换成绩 */
            SwapLong(&num[k], &num[i]);       /* 交换学号 */
            SwapChar(name[k], name[i]);       /* 交换姓名 */
        }
    }
}
void chengji(long num[], char name[][10], float score[])
{
    int i;
    for (i = 0; i < n; i++)
    {
        printf("%ld\t%s\t%.0f\n", num[i], name[i], score[i]);
    }
}
void ReadScore(long num[], char name[][10], float score[])
{
    int i;
    for (i = 0; i < n; i++)
    {
        scanf("%ld %s %f\n", &num[i], name[i], &score[i]);
        break;
    }
}
int main()
{
    int i;
    float score[10];
    long num[10];
    char name[10][10];
    for (i = 0; i < 10; i++)
    {
        printf("请输入第%d个人的学号、姓名、成绩\n", i + 1);
        ReadScore(num, name, score);
        continue;
        xuehao(num, name, score);
        chengji(num, name, score);
        break;
    }

    return 0;
   
}

  • 写回答

2条回答 默认 最新

  • qzjhjxj 2021-12-02 15:22
    关注

    修改如下,供参考:

    #define _CRT_SECURE_NO_WARNINGS
    #include <stdio.h>
    #include <string.h>
    #define n 3
    void  SwapFloat(float* x, float* y)
    {
        float  temp;
        temp = *x;
        *x = *y;
        *y = temp;
    }
    void  SwapLong(long* x, long* y)
    {
        long   temp;
        temp = *x;
        *x = *y;
        *y = temp;
    }
    void  SwapChar(char x[], char y[])
    {
        char temp[10];
        strcpy(temp, x);
        strcpy(x, y);
        strcpy(y, temp);
    }
    void xuehao(long num[], char name[][10], float score[])
    {
        int i, j, k;
        for (i = 0; i < n - 1; i++)
        {
            k = i;
            for (j = i + 1; j < n; j++)
            {
                if (num[j] < num[k])  k = j;
            }
            if (k != i)
            {
                SwapFloat(&score[k], &score[i]); /* 交换成绩 */
                SwapLong(&num[k], &num[i]);       /* 交换学号 */
                SwapChar(name[k], name[i]);       /* 交换姓名 */
            }
        }
    }
    void print(long num[], char name[][10], float score[])
    {
        int i;
        for (i = 0; i < n; i++)
        {
            printf("%ld\t%s\t%.0f\n", num[i], name[i], score[i]);
        }
    }
    void ReadScore(long num[], char name[][10], float score[])
    {
        int i;
        for (i = 0; i < n; i++)
        {
            printf("请输入第%d个人的学号、姓名、成绩\n", i + 1);
            scanf("%ld %s %f", &num[i], name[i], &score[i]);
            //scanf("%ld %s %f\n", &num[i], name[i], &score[i]);
            //break;
        }
    }
    int main()
    {
        int i;
        float score[10];
        long  num[10];
        char  name[10][10];
        //for (i = 0; i < 10; i++)
        //{
        //printf("请输入第%d个人的学号、姓名、成绩\n", i + 1);
        ReadScore(num, name, score);
            //continue;
        xuehao(num, name, score);
        print(num, name, score);
            //break;
        //}
        return 0;
    }
    
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

问题事件

  • 系统已结题 12月10日
  • 已采纳回答 12月2日
  • 创建了问题 12月2日

悬赏问题

  • ¥15 神经网络预测均方误差很小 但是图像上看着差别太大
  • ¥15 Oracle中如何从clob类型截取特定字符串后面的字符
  • ¥15 想通过pywinauto自动电机应用程序按钮,但是找不到应用程序按钮信息
  • ¥15 如何在炒股软件中,爬到我想看的日k线
  • ¥15 seatunnel 怎么配置Elasticsearch
  • ¥15 PSCAD安装问题 ERROR: Visual Studio 2013, 2015, 2017 or 2019 is not found in the system.
  • ¥15 (标签-MATLAB|关键词-多址)
  • ¥15 关于#MATLAB#的问题,如何解决?(相关搜索:信噪比,系统容量)
  • ¥500 52810做蓝牙接受端
  • ¥15 基于PLC的三轴机械手程序