鱼C论坛

 找回密码
 立即注册
查看: 2660|回复: 4

[技术交流] 共用体作业

[复制链接]
发表于 2016-6-16 19:12:23 | 显示全部楼层 |阅读模式

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

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

x
自编,不带排错功能。
  1. #include <stdio.h>

  2. struct
  3. {
  4.         int num;
  5.         char name[20];
  6.         char sex;
  7.         char job;
  8.         union
  9.         {
  10.                 int banji;
  11.                 char position[10];
  12.         }category;
  13. }person[2];

  14. void main()
  15. {
  16.         int i;
  17.         for(i=0; i<2; i++)
  18.         {
  19.                 printf("Please input the num: ");
  20.                 scanf("%d", &person[i+1].num);
  21.                 printf("Please input the name: ");
  22.                 scanf("%s", &person[i+1].name);
  23.                 while(getchar() != '\n')
  24.                 {
  25.                         ;
  26.                 }
  27.                 printf("Please input the sex(M/F): ");
  28.                 scanf("%c", &person[i+1].sex);
  29.                 while(getchar() != '\n')
  30.                 {
  31.                         ;
  32.                 }
  33.                 printf("Please input the job(s/t): ");
  34.                 scanf("%c", &person[i+1].job);
  35.                 while(getchar() != '\n')
  36.                 {
  37.                         ;
  38.                 }
  39.                 if(person[i+1].job == 's')
  40.                 {
  41.                         printf("Please input the class: ");
  42.                         scanf("%d", &person[i+1].category.banji);
  43.                 }
  44.                 else if(person[i+1].job == 't')
  45.                 {
  46.                         printf("Please input the position: ");
  47.                         scanf("%s", &person[i+1].category.position);
  48.                 }
  49.                 else
  50.                 {
  51.                         printf("输入错");
  52.                 }
  53.                 putchar('\n');
  54.         }

  55.         printf("No.\tname\tsex\tjob\tclass/position\n");
  56.         for(i=0; i<2; i++)
  57.         {
  58.                 if(person[i+1].job == 's')
  59.                 {
  60.                         printf("%d\t%s\t%c\t%c\t%d\n",
  61.                                 person[i+1].num,
  62.                                 person[i+1].name,
  63.                                 person[i+1].sex,
  64.                                 person[i+1].job,
  65.                                 person[i+1].category.banji);
  66.                 }
  67.                 else if(person[i+1].job == 't')
  68.                 {
  69.                         printf("%d\t%s\t%c\t%c\t%s\n",
  70.                                 person[i+1].num,
  71.                                 person[i+1].name,
  72.                                 person[i+1].sex,
  73.                                 person[i+1].job,
  74.                                 person[i+1].category.position);
  75.                 }
  76.         }
  77. }
复制代码
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

发表于 2016-6-17 15:22:07 | 显示全部楼层
同求同求同求同求同求同求
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2016-6-18 00:35:02 | 显示全部楼层
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

 楼主| 发表于 2016-6-18 14:22:07 | 显示全部楼层

好吧 我知道不忍直视,只是为了作业而已,哈哈~
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2016-9-2 18:41:57 | 显示全部楼层
给楼主点赞
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

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

本版积分规则

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

GMT+8, 2025-6-12 20:49

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

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