请问:问题出在哪里???

叶卷 2010-06-11 08:55:00
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <conio.h>
struct record
{
char id[10];
char name[20];
char phone[20];
char adress[40];
char postcode[10];
char e_mail[30];
}student[500];
int num=0;//外部变量num为文件中的纪录数
FILE *fp;
void mainmenu();//主菜单
void newrecord();//新添纪录
void searchmenu();//查询菜单
void searchbyid();//按学号查询
void searchbyname();//按姓名查询
void searchbyphone();//按电话查询
void deletemenu();//删除菜单
void deleteall();//删除所有
void deleteone();//删除单个
void showall();//显示所有
void readfromfile();//读取文件
void writetofile();//写入文件
void deletebyid();//按学号删除
void deletebyname();//按姓名删除
void listbyid();//按学号排序
void listbyname();//按姓名排序
void listmenu();//排序菜单
int main()
{
//readfromfile();
while (1)
{
mainmenu();
}
return 0;
}
void readfromfile()//从文件导入
{
if((fp=fopen("student.bin","rb"))==NULL)
{
printf("\n\t\t通讯录文件不存在");
if ((fp=fopen("student.bin","wb"))==NULL)
{
printf("\n\t\t建立失败");
exit(0);
}
else
{
printf("\n\t\t通讯录文件已建立");
printf("\n\t\t按任意键进入主菜单");
getchar();
//return 0;
}
exit(0);
}
fseek(fp,0,2); /*文件位置指针移动到文件末尾*/
if (ftell(fp)>0) /*文件不为空*/
{
rewind(fp); /*文件位置指针移动到文件开始位置*/
for (num=0;!feof(fp) && fread(&student[num],sizeof(struct record),1,fp);num++);
printf("\n\t\t文件导入成功");
printf("\n\t\t按任意键返回主菜单");
getch();
//return 0;
}
printf("\n\t\t文件导入成功");
printf("\n\t\t通讯录文件中无任何纪录");
printf("\n\t\t按任意键返回主菜单");
getchar();
//return 0;
}
void mainmenu()//主菜单
{
char choic;
system("cls");
printf("\n\t\t******************** 主菜单 ********************");
printf("\n\t\t*********** 1-新添纪录 2-查询菜单 ************");
printf("\n\t\t*********** 3-删除菜单 4-记录排序 ************");
printf("\n\t\t*********** 5-保存退出 6-不保存退出***********");
printf("\n\t\t************************************************");
printf("\n\t\t请选择:");
choic=getch();
switch (choic)
{
case '1':newrecord();break;
case '2':searchmenu();break;
case '3':deletemenu();break;
case '4':listmenu();break;
case '5':writetofile();break;
case '6':exit(0);
default:mainmenu();
}
}
void searchmenu()//查询菜单
{
char choic;
system("cls");
printf("\n\t\t******************* 查询菜单 *******************");
printf("\n\t\t********** 1-显示所有 2-按学号查询 ************");
printf("\n\t\t********** 3-按姓名查询4-按电话查询 ************");
printf("\n\t\t********** 5-返回主菜单 ************");
printf("\n\t\t************************************************");
printf("\n\t\t请选择:");
choic=getch();
switch (choic)
{
case '1':showall();break;
case '2':searchbyid();break;
case '3':searchbyname();break;
case '4':searchbyphone();break;
case '5':mainmenu();break;
}
}
void deletemenu()//删除菜单
{
char choic;
if(num==0)
{
printf("\n\t\t对不起,文件中无任何纪录");
printf("\n\t\t按任意键返回主菜单");
getch();
return;
}
system("cls");
printf("\n\t\t******************* 删除菜单 *******************");
printf("\n\t\t*********** 1-删除所有 2-删除单个 ***********");
printf("\n\t\t*********** 3-返回主菜单 ***********");
printf("\n\t\t************************************************");
printf("\n\t\t请选择:");
choic=getch();
switch (choic)
{
case '1':deleteall();break;
case '2':deleteone();break;
case '3':mainmenu();break;
default:mainmenu();break;
}
}
void deleteall()//删除所有
{
printf("\n\t\t确认删除?(y/n)");
if (getch()=='y')
{
fclose(fp);
if ((fp=fopen("student.bin","wb"))==NULL)
{
printf("\n\t\t不能打开文件,删除失败");
readfromfile();
}
num=0;
printf("\n\t\t纪录已删除,按任意键返回主菜单");
getch();
return;
}
else
return;
}
void deletebyname()//按姓名删除
{
int a=0;
int findmark=0;
int j;
int deletemark=0;
int i;
char name[20];
printf("\n\t\t请输入要删除学生姓名:");
scanf("%s",name);
for (i=a;i<num;i++)
{
if (strcmp(student[i].name,name)==NULL)
{
printf("\n\t\t以下是您要删除的学生纪录:");
findmark++;
printf("\n\t\t________________________________");
printf("\n\t\t学号: %s",student[i].id);
printf("\n\t\t姓名: %s",student[i].name);
printf("\n\t\t电话: %s",student[i].phone);
printf("\n\t\t地址: %s",student[i].adress);
printf("\n\t\te-mail:%s",student[i].e_mail);
printf("\n\t\t________________________________");
printf("\n\t\t是否删除?(y/n)");
if (getch()=='y')
{
for (j=i;j<num-1;j++) /*纪录移动,从stud数组中删除之*/
student[j]=student[j+1];
num--;
deletemark++;
printf("\n\t\t删除成功");
if((i+1)<num)
{
printf("\n\t\t是否继续删除相同姓名的同学信息?(y/n)");
if (getch()=='y')
{
a=i;
continue;
}
}
printf("\n\t\t是否继续删除?(y/n)");
if (getch()=='y')
deletebyname();
return;
}
if((i+1)<num)
{
printf("\n\t\t是否继续删除相同姓名的同学信息?(y/n)");
if (getch()=='y')
{
a=i;
continue;
}
}
}
else
continue;
}
if ((deletemark==0)&&(findmark==0))
{
printf("\n\t\t没有该同学的纪录");
printf("\n\t\t是否继续删除?(y/n)");
if (getch()=='y')
deletebyid();
return;
return;
}
else if (findmark!=0)
{
printf("\n\t\t没有重名信息");
printf("\n\t\t没有该同学的纪录");
printf("\n\t\t是否继续删除?(y/n)");
if (getch()=='y')
deletebyid();
return;
return;
}
}
void deletebyid()//按学号删除
{
int i,j;
int deletemark=0;
char id[20];
printf("\n\t\t请输入要删除学生学号:");
scanf("%s",id);
if(num==0)
{
printf("\n\t\t对不起,文件中无任何纪录");
printf("\n\t\t按任意键返回主菜单");
getch();
return;
}
for (i=0;i<num;i++)
{
if (strcmp(student[i].id,id)==NULL)
{
printf("\n\t\t以下是您要删除的学生纪录:");
printf("\n\t\t学号: %s",student[i].id);
printf("\n\t\t姓名: %s",student[i].name);
printf("\n\t\t电话: %s",student[i].phone);
printf("\n\t\t地址: %s",student[i].adress);
printf("\n\t\te-mail:%s",student[i].e_mail);
printf("\n\t\t是否删除?(y/n)");
if (getch()=='y')
{
for (j=i;j<num-1;j++) /*纪录移动,从stud数组中删除之*/
student[j]=student[j+1];
num--;
deletemark++;
printf("\n\t\t删除成功");
printf("\n\t\t是否继续删除?(y/n)");
if (getch()=='y')
deletebyid();
return;
}
else
return;
}
continue;

}
if (deletemark==0)
{
printf("\n\t\t没有该同学的纪录");
printf("\n\t\t是否继续删除?(y/n)");
if (getch()=='y')
deletebyid();
return;
}
}
void newrecord()//添加纪录
{
printf("\n\t\t**************** 请输入学生信息 ****************\n");
printf("\n\t\t输入学号:");
scanf("%s",&student[num].id);
printf("\n\t\t输入姓名:");
scanf("%s",&student[num].name);
printf("\n\t\t输入电话号码:");

scanf("%s",&student[num].phone);
printf("\n\t\t输入地址:");
scanf("%s",&student[num].adress);
printf("\n\t\t输入邮编:");
scanf("%s",&student[num].postcode);
printf("\n\t\t输入e-mail:");
scanf("%s",&student[num].e_mail);
num++;
printf("\n\t\t是否继续添加?(Y/N):");
if (getch()=='y')
newrecord();
return;
}
void showall()//显示所有
{
int i;
system("cls");
if(num!=0)
{
printf("\n\t\t*************** 以下为通讯录所有信息************");
for (i=0;i<num;i++)
{
printf("\n\t\t学号: %s",student[i].id);
printf("\n\t\t姓名: %s",student[i].name);
printf("\n\t\t电话: %s",student[i].phone);
printf("\n\t\t地址: %s",student[i].adress);
printf("\n\t\t邮编: %s",student[i].postcode);
printf("\n\t\te-mail:%s",student[i].e_mail);

printf("\t\t");
if (i+1<num)
{
printf("\n\t\t__________________________");
system("pause");
}
}
printf("\n\t\t************************************************");
}
else
printf("\n\t\t通讯录中无任何纪录");
printf("\n\t\t按任意键返回主菜单:");
getch();
return;
}





...全文
110 11 打赏 收藏 转发到动态 举报
写回复
用AI写文章
11 条回复
切换为时间正序
请发表友善的回复…
发表回复
ForestDB 2010-06-11
  • 打赏
  • 举报
回复
提问的智慧。
qlzfyj 2010-06-11
  • 打赏
  • 举报
回复
来看头像
[Quote=引用 9 楼 zhao4zhong1 的回复:]
引用 7 楼 mskmc_mc 的回复:
引用 1 楼 gz_qmc 的回复:
这么长的内容
这么少的分
这么低的结贴率


强顶

这么假的漂亮头像!
[/Quote]
赵4老师 2010-06-11
  • 打赏
  • 举报
回复
[Quote=引用 7 楼 mskmc_mc 的回复:]
引用 1 楼 gz_qmc 的回复:
这么长的内容
这么少的分
这么低的结贴率


强顶
[/Quote]
这么假的漂亮头像!
elegant87 2010-06-11
  • 打赏
  • 举报
回复
路过 围观一下
mskmc_mc 2010-06-11
  • 打赏
  • 举报
回复
[Quote=引用 1 楼 gz_qmc 的回复:]
这么长的内容
这么少的分
这么低的结贴率

[/Quote]

强顶
piedgogo 2010-06-11
  • 打赏
  • 举报
回复
LZ说,你们猜我遇到什么问题了?猜对了也没分的哦。
wade_2003 2010-06-11
  • 打赏
  • 举报
回复
问题出在你没说清楚有什么不对的地方
zhj44789 2010-06-11
  • 打赏
  • 举报
回复
LZ是不是出问题考大家哈, 你遇到啥问题了
liujiaji 2010-06-11
  • 打赏
  • 举报
回复
1楼,回复的太好了!
wolffan3150 2010-06-11
  • 打赏
  • 举报
回复
看不出啥问题,
就是有几个函数没实习,writetofile
gz_qmc 2010-06-11
  • 打赏
  • 举报
回复
这么长的内容
这么少的分
这么低的结贴率


69,371

社区成员

发帖
与我相关
我的任务
社区描述
C语言相关问题讨论
社区管理员
  • C语言
  • 花神庙码农
  • 架构师李肯
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧