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 真我手机蓝牙传输进度消息被关闭了,怎么打开?(关键词-消息通知)
  • ¥15 下图接收小电路,谁知道原理
  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
  • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
  • ¥15 手机接入宽带网线,如何释放宽带全部速度
  • ¥30 关于#r语言#的问题:如何对R语言中mfgarch包中构建的garch-midas模型进行样本内长期波动率预测和样本外长期波动率预测
  • ¥15 ETLCloud 处理json多层级问题
  • ¥15 matlab中使用gurobi时报错
  • ¥15 这个主板怎么能扩出一两个sata口
  • ¥15 不是,这到底错哪儿了😭