【代码】
#include <stdio.h>
#define N 100
struct tele
{
char name[100];
char mobil[100];
char fix[100];
char email[100];
};
typedef struct tele telestru;
int input(telestru s[])
{
int n = -1, i;
printf("请按下列格式输入信息(行首输入q结束输入):\n");
printf("----------------------------------------------------------------------------------------\n");
printf("姓名\t\t移动电话\t\t固定电话\t\temail\n");
do {
n++;
scanf("%s", s[n].name);
if (s[n].name[0] == 'q') break;
scanf("%s", s[n].mobil);
scanf("%s", s[n].fix);
scanf("%s", s[n].email);
} while (1);
return n;
}
void print(telestru* s, int n)
{
int i, j;
if (n > 0)
{
printf("请按下列格式输入信息(行首输入q结束输入):\n");
printf("----------------------------------------------------------------------------------------\n");
printf("姓名\t\t移动电话\t\t固定电话\t\temail\n");
for (i = 0; i < n; i++, s++)
{
printf("%-s\t\t", s->name);
printf("%-s\t\t", s->mobil);
printf("%-s\t\t", s->fix);
printf("%-s\n", s->email);
}
}
}
int main()
{
telestru s[N];
int n;
n = input(s);//输入
print(s, n);//输出
return 0;
}
【注意】
定义结构体里面的字符串数组可以适当多给一些空间,一定不要把空间给得太紧凑了,不然存储会出现混乱。