#include<stdio.h> #include<string.h> //10.04 struct student { char name[10]; int num; int score; }; int main() { struct student stu[5] = { {"bangyan",1,703},{"angyan",1,723}, {"cgyan",1,603}, {"gyan",1,753}, {"wyan",1,537} }; struct student temp; for (int i = 0; i < 5; i++) { for (int j = i; j < 5; j++) { if (strcmp(stu[j].name, stu[i].name)>0)//按照从大到小的顺序排列 //第一次写的时候犯了一个致命的错误 以为负数算假 所以一直比较错 负数算真 { //printf("%d ",strcmp("asd","asd")); temp = stu[i]; stu[i] = stu[j]; stu[j] = temp; } } } for (int i = 0; i < 5; i++) printf("%s\n", stu[i].name); return 0; }
负数算真 只有零算假,所有的非负数都算作真
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】凌霞软件回馈社区,博客园 & 1Panel & Halo 联合会员上线
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】博客园社区专享云产品让利特惠,阿里云新客6.5折上折
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步