急!c语言学生名片管理系统
发布网友
发布时间:2022-04-26 15:24
我来回答
共1个回答
热心网友
时间:2023-10-11 17:32
给你一段基本的代码,你可以在这个基础上修改
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
struct person {char name[30]; /* full name */
unsigned birthday, /* 1..31 */
birthmonth, /* 1..12 */
birthyear; /* 4 digits */
};
int main(void)
{
struct person *personlist;
unsigned number_of_persons, num;
char buffer[30];
char *p;
int year, month, day;
int ok;
do
{
printf("Enter the number of persons: ");
fflush(stdout);
if (fgets(buffer, sizeof buffer, stdin) != NULL
&& sscanf(buffer, "%u", &number_of_persons) == 1)
{
ok = 1;
if (number_of_persons>0)
{
personlist = malloc(number_of_persons * sizeof(struct person));
if (personlist==NULL)
{
printf("Not enough memory to store that many persons!\n");
ok = 0;
}
}
}
else
{
ok = 0;
printf("Invalid number! Enter again...\n");
}
}
while (!ok);
if (number_of_persons==0)
{
printf("OK, perhaps another time!\n");
return 0;
}
for (num=0; num<number_of_persons; num++)
{
printf("\nEnter the information for person #%u:\n", num);
printf("Name: ");
fflush(stdout);
if (fgets(buffer, sizeof buffer, stdin) == NULL)
{
printf("Error reading from stdin; input aborted\n");
number_of_persons = num;
break;
}
p = strchr(buffer,'\n');
if (p!=NULL)
*p = '\0';
if (strlen(buffer)==0)
{
printf("Input stopped\n");
number_of_persons = num;
break;
}
strcpy(personlist[num].name, buffer);
do
{
printf("Birthday [YYYY-MM-DD]: ");
fflush(stdout);
if (fgets(buffer, sizeof buffer, stdin) != NULL
&& sscanf(buffer, "%d-%d-%d", &year, &month, &day) == 3
&& year>=1000 && year<=9999
&& month>=1 && month<=12
&& day>=1 && day<=31)
{
ok = 1;
}
else
{
ok = 0;
printf("Invalid birthday! Enter again...\n");
}
}
while (!ok);
personlist[num].birthyear = year;
personlist[num].birthmonth = month;
personlist[num].birthday = day;
}
printf("\nOK, thank you.\n");
printf("\nYou entered the following data:\n");
printf("\n%-10s%-30s%s\n", "Number", "Name", "Birthday");
for (num=0; num<number_of_persons; num++)
{
printf("%-10u%-30s%04d-%02d-%02d\n",
num,
personlist[num].name,
personlist[num].birthyear,
personlist[num].birthmonth,
personlist[num].birthday);
}
free(personlist);
return 0;
}