C Primer Plus(第五版) 第十四章 课后习题 4

本文展示了一个使用C语言结构体来存储个人信息的例子,包括姓名和编号等字段,并提供了初始化和显示这些信息的函数。


#include <stdio.h>
#define MAX 20

struct name
{
 char First[MAX];
 char Middle[MAX];
 char Last[MAX];
};
struct human
{
 char NO[MAX];
 struct name names;
};
void init(struct human *a,int num);
void show(struct human *a,int num);
void eatline()
{
 while(getchar()!='\n')
  continue;
}

int main(int argc, char **argv)
{
 //printf("hello world\n");
 struct human a[5];
 init(a,5);
 show(a,5);
 return 0;
}
void show(struct human *a,int num)
{
 int i=4;
 puts("Current information in table.");
 while(i<num)
 {
  printf("%s, %s",a->names.First,a->names.Last);
  if(a->names.Middle[0]!='\0')
   printf("%c.",a->names.Middle[0]);
  printf("-%s\n",a->NO);
  i++;
  a++;
 }
}
void init(struct human *a,int num)
{
 int i=4;
 while(i<num)
 {
  puts("please input the number of society!");
  gets(a->NO);
  //eatline();
  
  puts("Please input the First Name");
  gets(a->names.First);
  //eatline();
  
  puts("Please input the Middle Name");
  gets(a->names.Middle);
  //eatline();
  
  puts("Please input the Last Name");
  gets(a->names.Last);
  //eatline();
  
  
  a++;
  i++;
  if(i<num)
   puts("add another one:");
 }
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值