极力求助啊!有一处错误,找不出来!
#define N 3#define M 100
#ifndef STRU_STUDENT
#define STRU_STUDENT
typedef struct student
{ char phone[20];
char name[15];
char adress[50];
char email[30];
char bir[3];
}STU;
#endif
extern STU stu[M];
#include "stdio.h"
#include "string.h"
#include "math.h"
#include "ctype.h"
#include "stdlib.h"
#include "conio.h"
void key()
{ int i=1;
char password[20];
while(1)
{
printf("input the key number:");
gets(password);
if(strcmp(password,"lsy0221")!=0)
printf("wrong!press any key to continue!\n");
else break;
getch();
i++;
if(i==3)
exit(0);
}
}
void menu()
{
printf("\n---------------welcome to contact system------------------\n");
printf(" 1.Add\n");
printf(" 2.Delete\n");
printf(" 3.Mondify\n");
printf(" 4.Search\n");
printf(" 5.Exit");
printf("\n---------------welcome to contact system------------------\n");
printf("please choice:");
}
void add()
{
int i=0,k,sum,m=0,flag=0,n=0,flag1,flag2;
char ch;
FILE *fp;
STU stu1[M/2];
if((fp=fopen("e:\\student.dat","a+"))==NULL)
{ printf("open file error!\n");
exit(0);
}
for(i=0;fread(&stu1[i],sizeof(STU),1,fp)!=0;i++);
n=i;
fclose(fp);
i=0;
while(1)
{ printf ("\nplease input number%d's information:\n",i+1);
do
{
flag=0;
printf("phone: ");
scanf("%s",stu[i].phone);
for(k=i-1;k>=0;k--)
if(strcmp(stu[k].phone,stu[i].phone)==0)
{ printf("the phone number is exist,enter again!\n");
flag=1;
break;
}
for(k=0;k<n;k++)
if(strcmp(stu1[k].phone,stu[i].phone)==0)
{ printf("the num is exist,enter again!\n");
flag=1;
break;
}
sum=strlen(stu[i].phone);
for(k=0;k<sum;k++)
if(!isdigit(stu[i].phone[k]))
{ printf("num error,enter again!\n");
flag=1;
break;
}
}while(flag);
do
{
flag=0;
printf("name: ");
scanf("%s",stu[i].name);
sum=strlen(stu[i].name);
for(k=0;k<sum;k++)
if(!isalpha(stu[i].name[k]))
{ printf("name error,enter again!\n");
flag=1;
break;
}
}while(flag);
do
{
flag=0;
printf("email: ");
scanf("%s",stu[i].email);
for(k=i-1;k>=0;k--)
if(strcmp(stu[k].email,stu[i].email)==0)
{ printf("the email adress is exist,enter again!\n");
flag=1;
break;
}
for(k=0;k<n;k++)
if(strcmp(stu1[k].email,stu[i].email)==0)
{ printf("the email adress is exist,enter again!\n");
flag=1;
break;
}
sum=strlen(stu[i].email);
for(k=0;k<sum;k++)
if(!isdigit(stu[i].email[k]))
{ printf("num error,enter again!\n");
flag=1;
break;
}
}while(flag);
do
{
printf("his or her birthday:\n");
printf("Year:");
scanf("%d",&stu[i].bir[0]);
if(stu[i].bir[0]%4==0&&stu[i].bir[0]%100!=0||stu[i].bir[0]%400==0)
flag=1;
else flag=0;
do
{
flag1=0;
printf("Month:");
scanf("%d",&stu[i].bir[1]);
while(stu[i].bir[1]>12||stu[i].bir[1]<0)
{
printf("wrong!please input again!\n");
flag1=1;
break;
}while(flag1);
}while(flag);
do
{
printf("Day:");
scanf("%d",&stu[i].bir[2]);
switch(stu[i].bir[1])
{
case 1:{if(stu[i].bir[2]>0&&stu[i].bir[2]<=31)
flag2=1;
else flag2=0;
break;}
case 2:{if(flag==1&&stu[i].bir[2]==29)flag2=1;
else if(stu[i].bir[2]>0&&stu[i].bir[2]<=28)flag2=1;
else flag2=0;break;}
case 3:{if(stu[i].bir[2]>0&&stu[i].bir[2]<=31)
flag2=1;
else flag2=0;
break;}
case 4:if(stu[i].bir[2]>0&&stu[i].bir[2]<=30)flag2=1;
else flag2=0;break;
case 5:{if(stu[i].bir[2]>0&&stu[i].bir[2]<=31)
flag2=1;
else flag2=0;
break;}
case 6:if(stu[i].bir[2]>0&&stu[i].bir[2]<=30)flag2=1;
else flag2=0;break;
case 7:{if(stu[i].bir[2]>0&&stu[i].bir[2]<=31)
flag2=1;
else flag2=0;
break;}
case 8:{if(stu[i].bir[2]>0&&stu[i].bir[2]<=31)
flag2=1;
else flag2=0;
break;}
case 9:if(stu[i].bir[2]>0&&stu[i].bir[2]<=30)flag2=1;
else flag2=0;break;
case 10:{if(stu[i].bir[2]>0&&stu[i].bir[2]<=31)
flag2=1;
else flag2=0;
break;}
case 11:if(stu[i].bir[2]>0&&stu[i].bir[2]<=30)flag2=1;
else flag2=0;break;
case 12:{if(stu[i].bir[2]>0&&stu[i].bir[2]<=31)
flag2=1;
else flag2=0;
break;}
}
while(flag2==0)
{
printf("wrong!please input again!\n");
flag2=1;
break;
}
}while(flag2);
}while(flag);
printf("adress:");
scanf("%s",stu[i].adress);
m++;
i++;
printf("continue to create?(yes--y or Y,no--others)\n");
getchar();
scanf("%c",&ch);
if(ch!='y'&&ch!='Y') break;
}
if((fp=fopen("e:\\student.dat","a"))==NULL)
{ printf("open error!\n");
exit(0);
}
for(i=0;i<m;i++)
if(fwrite(&stu[i],sizeof(stu[0]),1,fp)!=1)
printf("file write error\n");
fclose(fp);
}
void delete()
{
void delete_one();
void delete_all();
int m;
printf("delete one or delete all,please choice 1 or 2");
scanf("%d",&m);
switch(m)
{
case 1:delete_one();break;
case 2:delete_all();break;
}
}
void delete_one()
{
int i,j,flag,n;
char s[15];
char ch;
FILE *fp;
if ((fp=fopen("e:\\student.dat","r"))==NULL)
{
printf("The error file");
exit(0);
}
for(i=0;fread(&stu[i],sizeof(STU),1,fp)!=0;i++)
n=i;
fclose(fp);
do
{
printf("\nplease input the person's name who you want to delete:");
scanf("%s",s);
for(flag=1,i=0;flag&&i<n;i++)
{
if(strcmp(s,stu[i].name)==0)
{
printf("the deleted record is:\n");
printf("%12s%12s%12s%12s%12s\n","Name","Phone","Email","Adress",
"Birthday");
printf("%12s%12s%12s",stu[i].name,stu[i].phone,stu[i].adress);
for(j=0;j<N;j++)
printf("%3d",stu[i].bir[j]);
printf("\n");
if(i==n-1)
{
n--;
flag=0;
}
else
{
for(j=i;j<n-1;j++)
stu[j]=stu[j+1];
flag=0;
n--;
}
}
}
if (flag==0)
printf("\n the person has deleted\n");
else
printf("\n The person is not exit\n");
printf("continue to delete?(yes-y,no-others)\n");
getchar();
ch=getchar();
}while(ch=='y'||ch=='Y');
if((fp=fopen("e:\\student.dat","w"))==NULL)
{
printf("open file error!\n");
exit(0);
}
for(i=0;i<n;i++)
fwrite(&stu[i],sizeof(struct student),1,fp);
fclose (fp);
}
void delete_all()
{
char ch1;
FILE *fp;
if((fp=fopen("e:\\student.dat","w"))==NULL)
{ printf("open file error!\n");
exit(0);
}
printf("delete all,really?(yes--y or Y,no--others)\n");
getchar();
scanf("%c",&ch1);
if(ch1=='y'||ch1=='Y')
printf("delete all is successful!\n");
printf("press any key to back menu...\n");
getch();
fclose(fp);
}
void mondify()
{
int i,j,flag,n,flag1,flag2;
char ch,ch1,num[15];
FILE *fp;
flag=1;
printf("input the person's name to be modified:");
scanf("%s",num);
if((fp=fopen("e:\\student.dat","r"))==NULL)
{ printf("the file can not open\n");
exit(0);
}
for(i=0;fread(&stu[i],sizeof(STU),1,fp)!=0;i++);
n=i;
fclose(fp);
for(i=0;i<n;i++)
if(strcmp(num,stu[i].name)==0)
{ flag=0;
printf("the modified record is:\n");
printf("%12s%12s%12s%12s%12s\n","Name","Phone","Email","Adress","Birthday");
printf("%12s%12s%12s%12s",stu[i].name,stu[i].phone,stu[i].email,stu[i].adress);
for(j=0;j<N;j++)
printf("%3d",stu[i].bir[j]);
printf("\n");
while(1)
{
printf("do you modify the name?\n");
printf("please input y or n:\n");
ch=getch( );
if(ch=='y'||ch=='Y')
{ printf("input the name:\n");
scanf("%s",stu[i].name);
}else break;
}
while(1)
{
printf("do you mldify the phone?\n");
printf("input y or n:\n");
ch=getch();
if(ch=='y'||ch=='Y')
{ printf("input a new phone number:\n") ;
getchar();
scanf("%c",&stu[i].phone);
}else break;
}
while(1)
{
printf("do you modify the email adress?\n");
printf("please input y or n:\n");
ch=getch( );
if(ch=='y'||ch=='Y')
{ printf("input a new email adress:\n");
scanf("%s",stu[i].email);
}else break;
}
while(1)
{
printf("do you mldify the adress?\n");
printf("input y or n:\n");
ch=getch();
if(ch=='y'||ch=='Y')
{ printf("input a new adress:\n") ;
getchar();
scanf("%s",&stu[i].adress);
}else break;
}
while(1)
{
printf("do you want to modify the birthday?\n");
printf("input y or n:\n");
ch=getch();
if(ch=='y'||ch=='Y')
{
do
{
printf("his or her birthday:\n");
printf("Year:");
scanf("%d",&stu[i].bir[0]);
if(stu[i].bir[0]%4==0&&stu[i].bir[0]%100!=0||stu[i].bir[0]%400==0)
flag=1;
else flag=0;
do
{
flag1=0;
printf("Month:");
scanf("%d",&stu[i].bir[1]);
while(stu[i].bir[1]>12||stu[i].bir[1]<0)
{
printf("wrong!please input again!\n");
flag1=1;
break;
}
}while(flag1);
do
{
printf("Day:");
scanf("%d",&stu[i].bir[2]);
switch(stu[i].bir[1])
{
case 1:{if(stu[i].bir[2]>0&&stu[i].bir[2]<=31)
flag2=1;
else flag2=0;
break;}
case 2:{if(flag==1&&stu[i].bir[2]==29)flag2=1;
else if(stu[i].bir[2]>0&&stu[i].bir[2]<=28)flag2=1;
else flag2=0;break;}
case 3:{if(stu[i].bir[2]>0&&stu[i].bir[2]<=31)
flag2=1;
else flag2=0;
break;}
case 4:if(stu[i].bir[2]>0&&stu[i].bir[2]<=30)flag2=1;
else flag2=0;break;
case 5:{if(stu[i].bir[2]>0&&stu[i].bir[2]<=31)
flag2=1;
else flag2=0;
break;}
case 6:if(stu[i].bir[2]>0&&stu[i].bir[2]<=30)flag2=1;
else flag2=0;break;
case 7:{if(stu[i].bir[2]>0&&stu[i].bir[2]<=31)
flag2=1;
else flag2=0;
break;}
case 8:{if(stu[i].bir[2]>0&&stu[i].bir[2]<=31)
flag2=1;
else flag2=0;
break;}
case 9:if(stu[i].bir[2]>0&&stu[i].bir[2]<=30)flag2=1;
else flag2=0;break;
case 10:{if(stu[i].bir[2]>0&&stu[i].bir[2]<=31)
flag2=1;
else flag2=0;
break;}
case 11:if(stu[i].bir[2]>0&&stu[i].bir[2]<=30)flag2=1;
else flag2=0;break;
case 12:{if(stu[i].bir[2]>0&&stu[i].bir[2]<=31)
flag2=1;
else flag2=0;
break;}
}
while(flag2==0)
{
printf("wrong!please input again!\n");
flag2=1;
break;
}
}while(flag2);
}while(flag);
}
else break;
}
if(flag)
printf("The person's name you input is error!\n");
printf("continue to modify?(yes--y or Y,no--others)\n");
getchar( );
ch1=getchar( );
}while(ch1=='y'||ch1=='Y');
rewind(fp);
if((fp=fopen("e:\\student.dat","w"))==NULL)
{ printf("open file error!\n");
exit(0);
}
for(i=0;i<n;i++)
fwrite(&stu[i],sizeof(struct student),1,fp);
fclose(fp);
}
void search()
{
int flag,i,j,n,flag1;
FILE *fp;
char ch,str[10];
if((fp=fopen("e:\\student.dat","r"))==NULL)
{
printf("Open file failed!\n");
exit(0);
}
for(i=0;fread(&stu[i],sizeof(STU),1,fp)!=0;i++);
n=i;
do
{
flag1=0;
printf("Please enter the name!\n");
scanf("%s",str);
for(i=0;i<n;i++)
if(strcmp(stu[i].name,str)==0)
{
flag1=1;
printf("%12s%12s%12s%12s%12s\n","Name","Phone","Email","Adress","Birthday");
printf("%12s%12s%12s%12s",stu[i].name,stu[i].phone,stu[i].email,stu[i].adress);
for(j=0;j<N;j++)
printf("%3d",stu[i].bir[3]);
printf("\n");
}
if(flag1==0)
printf("this record doesn't exist!\n");
printf("You need continue to input the name?(yes-y or Y,no-others)\n");
getchar();
scanf("%c",&ch);
if((ch=='Y')||(ch=='y'))
flag=1;
else
flag=0;
}while(flag);
fclose(fp);
}
void main()
{
int n;
menu();
scanf("%d",&n);
switch(n)
{
case 1:key();add();break;
case 2:key();delete();break;
case 3:key();mondify();break;
case 4:search();break;
case 5:break;
default:printf("choice error!\n");
}
}