鱼C论坛

 找回密码
 立即注册
查看: 812|回复: 3

[已解决]结构体排序

[复制链接]
发表于 2021-3-11 10:49:22 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能^_^

您需要 登录 才可以下载或查看,没有账号?立即注册

x
#include<stdio.h>
#include<stdlib.h>
struct commodity
{
        char number[10];
        char name[10];
        int the_first_quarter;
        int the_second_quarter;
        int the_third_quarter;
        int average;
}com[3];
int main()
{
        FILE        *fp;
        struct commodity a;
    scanf("%s,%s,%d,%d,%d,%d",com[0].number,com[0].name,&com[0].the_first_quarter,&com[0].the_second_quarter,&com[0].the_third_quarter,&com[0].average);
        a=com[0];
        printf("%s,%s,%d,%d,%d,%d",a.number,a.name,a.the_first_quarter,a.the_second_quarter,a.the_third_quarter,a.average);
        return 0;
}


//为什么输入和输出不一样?
最佳答案
2021-3-11 11:19:58
  1. #include<stdio.h>
  2. #include<stdlib.h>
  3. struct commodity
  4. {
  5.         char number[10];
  6.         char name[10];
  7.         int the_first_quarter;
  8.         int the_second_quarter;
  9.         int the_third_quarter;
  10.         int average;
  11. }com[3];
  12. int main()
  13. {
  14.         FILE  *fp;
  15.         struct commodity a;
  16.         scanf("%s", com[0].number);
  17.         getchar();
  18.         scanf("%s", com[0].name);
  19.         getchar();
  20.             scanf("%d,%d,%d,%d",&com[0].the_first_quarter,&com[0].the_second_quarter,&com[0].the_third_quarter,&com[0].average);
  21.         a=com[0];
  22.         printf("%s\n", a.number);
  23.         printf("%s\n%s\n%d,%d,%d,%d",a.number,a.name,a.the_first_quarter,a.the_second_quarter,a.the_third_quarter,a.average);
  24.         return 0;
  25. }
复制代码
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

发表于 2021-3-11 11:17:21 | 显示全部楼层
你输入的内容都赋值给com[0].number了,系统认为你输入的是字符串
前10个是com[0].number,因为其只占10个字节,所以剩下的会开始分配给剩下的
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2021-3-11 11:19:58 | 显示全部楼层    本楼为最佳答案   
  1. #include<stdio.h>
  2. #include<stdlib.h>
  3. struct commodity
  4. {
  5.         char number[10];
  6.         char name[10];
  7.         int the_first_quarter;
  8.         int the_second_quarter;
  9.         int the_third_quarter;
  10.         int average;
  11. }com[3];
  12. int main()
  13. {
  14.         FILE  *fp;
  15.         struct commodity a;
  16.         scanf("%s", com[0].number);
  17.         getchar();
  18.         scanf("%s", com[0].name);
  19.         getchar();
  20.             scanf("%d,%d,%d,%d",&com[0].the_first_quarter,&com[0].the_second_quarter,&com[0].the_third_quarter,&com[0].average);
  21.         a=com[0];
  22.         printf("%s\n", a.number);
  23.         printf("%s\n%s\n%d,%d,%d,%d",a.number,a.name,a.the_first_quarter,a.the_second_quarter,a.the_third_quarter,a.average);
  24.         return 0;
  25. }
复制代码
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2021-3-11 19:01:47 | 显示全部楼层

我....想起来了,说的好,%S不能连用否则,会把,当成字符串
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Archiver|鱼C工作室 ( 粤ICP备18085999号-1 | 粤公网安备 44051102000585号)

GMT+8, 2024-4-24 17:38

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表