#include"common.h"
void addMember(pInfoHead phead)
{
pMEMBER addNewMember;
bool iscontinueAdd=true; //定义一个持续添加的 布尔目标志量
do
{
if(NULL==(addNewMember=newMember(phead)))
{
fprintf(stderr,"\n添加取消!\a\n");
getch();
CLS();
return;
}
insertMember(phead,addNewMember);
//基本信息存入头结点
//以下变量作用的定义见头文件 common.h
phead->total[addNewMember->role]++;
phead->totalMember++;
if(0!=addNewMember->role)
phead->totalAge+=addNewMember->age;
phead->totalSalary+=addNewMember->salary;
//基本信息添加完毕
writeToFile(phead);//每添加一员,便写入文件保存,防止程序非正常结束,丢失数据
puts("添加成功!文件已保存!");
puts("是否继续添加?(Y=是,其它=否)");
if(tolower(getch())=='y')
iscontinueAdd=true;
else
iscontinueAdd=false;
}while(iscontinueAdd);
CLS();
}
pMEMBER newMember(pInfoHead phead)
{
char choose;
bool isExistMember; //检测是否已存在
float float_temp;
int items;
Date date;
pMEMBER pnewMember,ptailMember;
ptailMember=phead->next;
pnewMember=(pMEMBER)malloc(_SIZE_);
if(NULL==pnewMember)
{
fprintf(stderr,"\n警告:内存申请不成功!\a\a\n");
return NULL;
}
pnewMember->next;
CLS();
puts("☆☆☆☆☆☆☆☆☆☆☆");
puts("☆ ☆");
puts("☆ 1.教练 ☆");
puts("☆ 2.前锋 ☆");
puts("☆ 3.中锋 ☆");
puts("☆ 4.后卫 ☆");
puts("☆ 5.守门员 ☆");
puts("☆ 0.返回 ☆");
puts("☆ ☆");
puts("☆☆☆☆☆☆☆☆☆☆☆\n");
printf("请选择添加角色:");
do{
while(strchr("123450",choose=getch())==NULL) //检测输入的选择字符的范围
continue;
}while(0==choose);
putchar(choose);
if('0'==choose)
{
free(pnewMember);
return NULL;
}
pnewMember->role=choose-'0'-1;
if(0==pnewMember->role) //如果是教练,就用 #号码 方式给定编号
{
pnewMember->number[0]='#'; //totalDrillmaster为教练总个数
if(phead->total[0]<10)
pnewMember->number[1]='1'+phead->total[0];
else
pnewMember->number[2]='A'+phead->total[0];
pnewMember->number[2]='\0';
putchar('\n');
}
else
{
do
{
isExistMember=false;
printf("\n请输入编号(前6位非空白字符有效):");
scanf("%6s",pnewMember->number);
EatLine('\n');
if('\0'==pnewMember->number[0])
{
isExistMember=true;
continue;
}
while(ptailMember!=NULL)
{
if(strcmpi(ptailMember->number,pnewMember->number)==0)
{
puts("记录已存在!\a");
printf("退出请输入E,其它键重新输入");
if(tolower(getch())=='e')
{
free(pnewMember);
//puts("\n添加已取消!");
return NULL;
}
isExistMember=true;
break;
}
else
ptailMember=ptailMember->next;
}
}while(isExistMember);
}
do
{
printf("请输入姓名(1~14位字符有效): ");
gets(pnewMember->name);
}while(strlen(pnewMember->name)>14||strlen(pnewMember->name)<1);
do
{
printf("请输入年龄[15~100](如:25): ");
items=scanf("%d",&pnewMember->age);
EatLine('\n');
}while(0==items||pnewMember->age<15||pnewMember->age>100);
do
{
printf("请输入身高(cm)[150~250](如:175.5): ");
items=scanf("%f",&float_temp);
EatLine('\n');
}while(0==items||float_temp<150||float_temp>250);
pnewMember->height=float_temp;
do
{
printf("请输入体重(kg)[40~150](如:70): ");
items=scanf("%f",&float_temp);
EatLine('\n');
}while(0==items||float_temp<40||float_temp>150);
pnewMember->weight=float_temp;
do
{
printf("请输其国籍(1~10位字符有效): ");
gets(pnewMember->nation);
}while(strlen(pnewMember->nation)>10||strlen(pnewMember->nation)<1);
do
{
printf("请输入其年薪(dollar)(如:120000): ");
items=scanf("%d",&pnewMember->salary);
EatLine('\n');
}while(0==items||pnewMember->salary<0); //限定其薪水范围 (不低于0)dollar
do
{
printf("请输入队日期(正确格式:2007-09-10):");
items=scanf("%d-%d-%d",&date.year,&date.month,&date.day);
if(getchar()!='\n') //防止多余的输入
{
EatLine('\n');
items=0;
continue;
}
else
EatLine('\n');
}while(items!=3 || !isRightFormat(&date));
pnewMember->joinTeamDate=date;
return pnewMember;
}
bool isRightFormat(Date *pdate)
{
bool isDeepYear;
int maxDayNumber;
if(pdate->year<0||pdate->year>2100||pdate->month<1||pdate->month>12)
return false;
if(pdate->year%400==0)
isDeepYear=true;
else if(pdate->year%100!=0&&pdate->year%4==0)
isDeepYear=true;
else
isDeepYear=false;
switch(pdate->month)
{
case 1:case 3:case 5:case 7:case 8:case 10:case 12:
maxDayNumber=31;break;
case 4:case 6:case 9:case 11:
maxDayNumber=30;break;
case 2:
maxDayNumber=isDeepYear?29:28;break;
}
if(pdate->day<1||pdate->day>maxDayNumber)
return false;
return true;
}
评论0