鱼C论坛

 找回密码
 立即注册
查看: 2066|回复: 11

[已解决]教材P233第9章课后习题题三(5):通讯录排序,试完成程序中空缺代码

[复制链接]
发表于 2022-12-9 16:02:39 | 显示全部楼层 |阅读模式

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

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

x
【问题描述】教材P233第9章课后习题题三(5):通讯录排序,试完成程序中空缺代码。
【输入形式】输入整数n及n个人的基本信息。
【输出形式】输出按照年龄由大到小的排序结果。
【样例输入】:图片地址:

                               
登录/注册后可看大图

【样例说明】出生日期要求按照给定日期格式输入
补充下列代码:
#include"stdio.h"
#define N 10
struct Date
{    int year;
     int month;
     int day;
};
struct Mate
{    char name[10];

   此处补充: ________________________________________

      char tel[12];
};
struct Mate classmate[N];
int Datecomp(struct Date d1,struct Date d2);//d1年龄比d2大返回1,相等返回0,小则返回-1
int main()
{    int i,j,k,n;
     struct Mate m;
     scanf("%d",&n);
     for(i=0;i<n;i++)   //输入n个同学的信息
     {   scanf("%s",classmate[i].name);
         scanf("%d-%d-%d",&classmate[i].birthday.year,&classmate[i].birthday.month,&classmate[i].birthday.day);
         scanf("%s",classmate[i].tel);
      }
      for(i=0;i<n-1;i++) //按照年龄降序排列
      {    k=i;

此处补充:______________________________________________

           if(k!=i)
          {    m=classmate[i];
               classmate[i]=classmate[k];
               classmate[k]=m;
           }
       }
       for(i=0;i<n;i++)
           printf("name:%10s,%d-%d-%d,%12s\n",classmate[i].name,classmate[i].birthday.year,classmate[i].birthday.month,classmate[i].birthday.day,classmate[i].tel);
       return 0;
}

int Datecomp(struct Date d1,struct Date d2)
{


此处补充:________________________________________________


}


求助大佬!!!
最佳答案
2022-12-9 20:39:06
zzs2963584895 发表于 2022-12-9 20:11
大佬,貌似编译有点问题

我又忘了这个是C语言
  1. #include"stdio.h"
  2. #define N 10
  3. struct Date
  4. {    int year;
  5.      int month;
  6.      int day;
  7. };
  8. struct Mate
  9. {    char name[10];

  10.     /*此处填充:*/ struct Date birthday;

  11.       char tel[12];
  12. };
  13. struct Mate classmate[N];
  14. int Datecomp(struct Date d1,struct Date d2);
  15. int main()
  16. {    int i,j,k,n;
  17.      struct Mate m;
  18.      scanf("%d",&n);
  19.      for(i=0;i<n;i++)   
  20.      {   scanf("%s",classmate[i].name);
  21.          scanf("%d-%d-%d",&classmate[i].birthday.year,&classmate[i].birthday.month,&classmate[i].birthday.day);
  22.          scanf("%s",classmate[i].tel);
  23.       }
  24.       for(i=0;i<n-1;i++)
  25.       {    /*这里的排序写的有问题,我没见过这么排序的,照这么做应该用冒泡排序*/
  26.         k=i;
  27.         for (; k < n - 1; ++k) {
  28.                         if (Datecomp(classmate[k].birthday, classmate[k + 1].birthday)) {
  29.                                 m = classmate[k];
  30.                                 classmate[k] = classmate[k + 1];
  31.                                 classmate[k + 1] = m;
  32.                                 
  33.                         }
  34.                 }
  35.               
  36.        }
  37.        for(i=0;i<n;i++)
  38.            printf("name:%10s,%d-%d-%d,%12s\n",classmate[i].name,classmate[i].birthday.year,classmate[i].birthday.month,classmate[i].birthday.day,classmate[i].tel);
  39.        return 0;
  40. }

  41. int Datecomp(struct Date d1,struct Date d2)
  42. {   /*此处填充:*/
  43.         if (d1.year != d2.year) return d1.year < d2.year;
  44.         if (d1.month != d2.month) return d1.month < d2.month;
  45.         return d1.day < d2.day;
  46.                  
  47. }
复制代码
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

 楼主| 发表于 2022-12-9 16:25:39 | 显示全部楼层
求助大佬!!!
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2022-12-9 17:54:46 | 显示全部楼层
用代码格式,我看得吃力的很
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 1 反对 0

使用道具 举报

发表于 2022-12-9 18:06:08 | 显示全部楼层
  1. #include"stdio.h"
  2. #define N 10
  3. struct Date
  4. {    int year;
  5.      int month;
  6.      int day;
  7. };
  8. struct Mate
  9. {    char name[10];

  10.     /*此处填充:*/ Date birthday;

  11.       char tel[12];
  12. };
  13. struct Mate classmate[N];
  14. int Datecomp(struct Date d1,struct Date d2);
  15. int main()
  16. {    int i,j,k,n;
  17.      struct Mate m;
  18.      scanf("%d",&n);
  19.      for(i=0;i<n;i++)   
  20.      {   scanf("%s",classmate[i].name);
  21.          scanf("%d-%d-%d",&classmate[i].birthday.year,&classmate[i].birthday.month,&classmate[i].birthday.day);
  22.          scanf("%s",classmate[i].tel);
  23.       }
  24.       for(i=0;i<n-1;i++)
  25.       {    /*这里的排序写的有问题,我没见过这么排序的,照这么做应该用冒泡排序*/
  26.         k=i;
  27.         for (; k < n - 1; ++k) {
  28.                         if (Datecomp(classmate[k].birthday, classmate[k + 1].birthday)) {
  29.                                 m = classmate[k];
  30.                                 classmate[k] = classmate[k + 1];
  31.                                 classmate[k + 1] = m;
  32.                                
  33.                         }
  34.                 }
  35.              
  36.        }
  37.        for(i=0;i<n;i++)
  38.            printf("name:%10s,%d-%d-%d,%12s\n",classmate[i].name,classmate[i].birthday.year,classmate[i].birthday.month,classmate[i].birthday.day,classmate[i].tel);
  39.        return 0;
  40. }

  41. int Datecomp(struct Date d1,struct Date d2)
  42. {   /*此处填充:*/
  43.         if (d1.year != d2.year) return d1.year < d2.year;
  44.         if (d1.month != d2.month) return d1.month < d2.month;
  45.         return d1.day < d2.day;
  46.                  
  47. }
复制代码
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2022-12-9 20:11:09 | 显示全部楼层

大佬,貌似编译有点问题
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2022-12-9 20:34:56 | 显示全部楼层
本帖最后由 jhq999 于 2022-12-9 20:43 编辑

楼主有没有样例?
  1. #include "stdio.h"

  2. struct Date
  3. {    int year;
  4.      int month;
  5.      int day;
  6. };
  7. struct Mate
  8. {
  9.     char name[10];
  10.     struct Date birthday;//////////////
  11.     char tel[12];
  12. };
  13. #define N 10
  14. struct Mate classmate[N];
  15. int Datecomp(struct Date d1,struct Date d2);//d1年龄比d2大返回1,相等返回0,小则返回-1
  16. int main()
  17. {    int i,j,k,n;
  18.      struct Mate m;
  19.      scanf("%d",&n);
  20.      for(i=0;i<n;i++)   //输入n个同学的信息
  21.      {   scanf("%s",classmate[i].name);
  22.          scanf("%d-%d-%d",&classmate[i].birthday.year,&classmate[i].birthday.month,&classmate[i].birthday.day);
  23.          scanf("%s",classmate[i].tel);
  24.       }
  25.       for(i=0;i<n-1;i++) //按照年龄降序排列
  26.       {    k=i;

  27.             for(j=i+1;j<n;j+=1)///////////////////////
  28.             {
  29.                 if(Datecomp(classmate[j].birthday,classmate[k].birthday)==1)k=j;
  30.             }
  31.             if(k!=i)
  32.             {  m=classmate[i];
  33.                classmate[i]=classmate[k];
  34.                classmate[k]=m;
  35.             }
  36.        }
  37.        for(i=0;i<n;i++)
  38.            printf("name:%10s,%d-%d-%d,%12s\n",classmate[i].name,classmate[i].birthday.year,classmate[i].birthday.month,classmate[i].birthday.day,classmate[i].tel);
  39.        return 0;
  40. }

  41. int Datecomp(struct Date d1,struct Date d2)
  42. {
  43.     if(d1.year>d2.year)return 1;/////////
  44.     else if(d1.year<d2.year)return -1;
  45.     else
  46.     {
  47.         if(d1.month>d2.month)return 1;
  48.         else if(d1.month<d2.month)return -1;
  49.         else
  50.         {
  51.             if(d1.day>d2.day)return 1;
  52.             else if(d1.day<d2.day)return -1;
  53.             else
  54.                 return 0;
  55.         }
  56.     }
  57. }
复制代码
  1. int Datecomp(struct Date d1,struct Date d2)
  2. {
  3.     if(d1.year==d2.year)//同龄的比较多,所以先==
  4.    {
  5.          if(d1.month>d2.month)return 1;
  6.         else if(d1.month<d2.month)return -1;
  7.         else
  8.         {
  9.             if(d1.day>d2.day)return 1;
  10.             else if(d1.day<d2.day)return -1;
  11.             else
  12.                 return 0;
  13.         }
  14.     }
  15.     else if(d1.year>d2.year)return 1;
  16.     else return -1;
  17.    
  18. }
复制代码
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2022-12-9 20:39:06 | 显示全部楼层    本楼为最佳答案   
zzs2963584895 发表于 2022-12-9 20:11
大佬,貌似编译有点问题

我又忘了这个是C语言
  1. #include"stdio.h"
  2. #define N 10
  3. struct Date
  4. {    int year;
  5.      int month;
  6.      int day;
  7. };
  8. struct Mate
  9. {    char name[10];

  10.     /*此处填充:*/ struct Date birthday;

  11.       char tel[12];
  12. };
  13. struct Mate classmate[N];
  14. int Datecomp(struct Date d1,struct Date d2);
  15. int main()
  16. {    int i,j,k,n;
  17.      struct Mate m;
  18.      scanf("%d",&n);
  19.      for(i=0;i<n;i++)   
  20.      {   scanf("%s",classmate[i].name);
  21.          scanf("%d-%d-%d",&classmate[i].birthday.year,&classmate[i].birthday.month,&classmate[i].birthday.day);
  22.          scanf("%s",classmate[i].tel);
  23.       }
  24.       for(i=0;i<n-1;i++)
  25.       {    /*这里的排序写的有问题,我没见过这么排序的,照这么做应该用冒泡排序*/
  26.         k=i;
  27.         for (; k < n - 1; ++k) {
  28.                         if (Datecomp(classmate[k].birthday, classmate[k + 1].birthday)) {
  29.                                 m = classmate[k];
  30.                                 classmate[k] = classmate[k + 1];
  31.                                 classmate[k + 1] = m;
  32.                                 
  33.                         }
  34.                 }
  35.               
  36.        }
  37.        for(i=0;i<n;i++)
  38.            printf("name:%10s,%d-%d-%d,%12s\n",classmate[i].name,classmate[i].birthday.year,classmate[i].birthday.month,classmate[i].birthday.day,classmate[i].tel);
  39.        return 0;
  40. }

  41. int Datecomp(struct Date d1,struct Date d2)
  42. {   /*此处填充:*/
  43.         if (d1.year != d2.year) return d1.year < d2.year;
  44.         if (d1.month != d2.month) return d1.month < d2.month;
  45.         return d1.day < d2.day;
  46.                  
  47. }
复制代码
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 1 反对 0

使用道具 举报

发表于 2022-12-9 20:42:03 | 显示全部楼层
可恶的C++,我终于明白为什么C和C++是两种语言了
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2022-12-9 20:44:26 | 显示全部楼层
现在问题解决了吗
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2022-12-9 20:49:18 | 显示全部楼层

解决了
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2022-12-9 20:51:14 | 显示全部楼层
zhangjinxuan 发表于 2022-12-9 20:39
我又忘了这个是C语言

能不能帮我看看最新的一个问题
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2022-12-9 20:59:20 | 显示全部楼层
zzs2963584895 发表于 2022-12-9 20:51
能不能帮我看看最新的一个问题

好的
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

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

本版积分规则

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

GMT+8, 2025-4-22 22:26

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

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