m0_67463421 2022-05-21 15:39 采纳率: 66.7%
浏览 23
已结题

我这个程序没有复制完整但是应该不影响,我想请问一下,我是用这种冒泡对结构体数组的每一个元素一个一个排序的,如何简单一点让结构体数组一下全部交换

#include "stdio.h"
#include <string.h>
#define NC 100
struct studentRed { //定义学生记录,即对应表格的一行
char studName[20]; //姓名对应第一列的Student Name
char studID[20]; //学号对应第二列的Student ID 用20个字符编码,可用字母做学号
float compProgram; // score for Computer programming 用浮点数可输入小数分数
float physEducat; //score for Computer programming
float commResech; //score for Communication and Research
float averageScore; //average score
}*p, students[NC] = {//给数组变量赋值
{"John","zy001",84,86,78,0},
{"Xiaoming","zy002",77,82,90,0},
{"Xiaohua","zy003",79,82,85,0},
{"Jianguo","zy004",84,86,78,0},
{"Xiaodong","zy005",60,55,40,0},
};

        int m, j, n;
        float temp;
        float temp1;
        float temp2;
        float temp3;
        char jiaohuan[10];
        char jiaohuan1[10];
        //冒泡排序,将各科目分数与学生名和学生学号一起交换
        for (m = 0; m < 5; m++) {//这一个冒泡输出compProgram科目的分数排名
            for (j = 0; j < 5 - 1; j++) {
                if (students[j].compProgram < students[j + 1].compProgram) {

                    temp = students[j].compProgram;
                    students[j].compProgram = students[j + 1].compProgram;
                    students[j + 1].compProgram = temp;
                    temp1 = students[j].physEducat;
                    students[j].physEducat = students[j + 1].physEducat;
                    students[j + 1].physEducat = temp1;
                    temp2 = students[j].commResech;
                    students[j].commResech = students[j + 1].commResech;
                    students[j + 1].commResech = temp2;
                    temp3 = students[j].averageScore;
                    students[j].averageScore = students[j + 1].averageScore;
                    students[j + 1].averageScore = temp3;
                    strcpy_s(jiaohuan, 10, students[j].studName);
                    strcpy_s(students[j].studName, 10, students[j + 1].studName);
                    strcpy_s(students[j + 1].studName, 10, jiaohuan);
                    strcpy_s(jiaohuan1, 10, students[j].studID);
                    strcpy_s(students[j].studID, 10, students[j + 1].studID);
                    strcpy_s(students[j + 1].studID, 10, jiaohuan1);
                }
            }
        }
        printf("Name\t    ID\t\tComputer\n");
        for (n = 0; n < 5; n++) {
            printf("%-12s%s\t%f\n", students[n].studName, students[n].studID, students[n].compProgram);
        }
  • 写回答

1条回答 默认 最新

  • 逍遥xiaoy 2022-05-21 15:51
    关注

    如果想一次全部交换的话,可以用结构体指针实现,现在数组这种形式没有办法直接赋值的。

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

问题事件

  • 系统已结题 5月29日
  • 已采纳回答 5月21日
  • 创建了问题 5月21日

悬赏问题

  • ¥15 使用ue5插件narrative时如何切换关卡也保存叙事任务记录
  • ¥20 软件测试决策法疑问求解答
  • ¥15 win11 23H2删除推荐的项目,支持注册表等
  • ¥15 matlab 用yalmip搭建模型,cplex求解,线性化处理的方法
  • ¥15 qt6.6.3 基于百度云的语音识别 不会改
  • ¥15 关于#目标检测#的问题:大概就是类似后台自动检测某下架商品的库存,在他监测到该商品上架并且可以购买的瞬间点击立即购买下单
  • ¥15 神经网络怎么把隐含层变量融合到损失函数中?
  • ¥15 lingo18勾选global solver求解使用的算法
  • ¥15 全部备份安卓app数据包括密码,可以复制到另一手机上运行
  • ¥20 测距传感器数据手册i2c