我初学者。老师狠心叫我们编个系统。
以下是我编的通讯录管理系统。编译出来input output save这3个模块错误很多。
可是看不懂。。麻烦各位高手帮忙看看。。谢谢各位了!!!
#include<stdio.h>
#include<string.h>
#define MAX 100
#define PAGE 2
#define PRINT1 printf("--------------------------------------\n")
#define PRINT2 printf("NUM---NAME---AGE---MOBILE---ADDRESS\n")
#define PRINT3 printf("%d,%s,%d,%s,%s,\n",message[i].num,message[i].name,message[i].age,message[i].mobile[15],message[i].address);
int sum; /*用于记录人数*/
struct message_type
{
int num;
char name;
int age;
char mobile[15];
char address;
}message[MAX];
/*主模块*/
main()
{
int choice;
sum=read_message();
bioskey(0);
do
{clrscr();
printf("\n\n\n ********通讯录管理系统********\n\n" );
printf(" 1.创建资料\n\n");
printf(" 2.输出资料\n\n");
printf(" 3.资料查询\n\n");
printf(" 4.资料添加\n\n");
printf(" 5.资料删除\n\n");
printf(" 6.资料修改\n\n");
printf(" 7.资料整理\n\n");
printf(" 0.退出系统\n\n");
printf("请选择(0-7):");
scanf("%d",&choice);
switch(choice)
{case 1: input_message();break;
case 2: output_message();break;
case 3: inquire_message();break;
case 4: insert_message();break;
case 5: del_message();break;
case 6: revise_message();break;
case 7: sort_message();break;
case 0: break;
}
}while(choice!=0);
save_message(sum);
printf("\n -以上资料已保存!-(文件名:messagenotes.txt)\n");
}
/*保存信息*/
save_message(int sum)
{FILE *fp;
int i;
if((fp=fopen("messagenotes.txt","wb"))==NULL)
{printf("读文件错误!\n");
return;
}
for(i=0;i<sum;i++)
if(fwrite(&message[i],sizeof(struct message_type),1,fp)!=1)
printf("写文件错误!\n");
fclose(fp);
}
/*创建模块*/
input_message()
{int i=0,s=MAX;
clrsrc();
printf("n\n 录入通讯录\n",s);
printf("\n -------------------------------\n");
do
{printf("\n 第%d号录入",i+1);
printf("\n 编号:");
scanf("%d",&message[i].num);
if(message[i].num==0) break;
printf("\n 姓名:");
scanf("%s",message[i].name);
printf("\n 年龄:");
scanf("%d",&message[i].age);
printf("\n 电话:");
scanf("%s",&message[i].mobile[15]);
printf("\n 地址:");
scanf("%s",message[i].address);
i++;
}while(i<MAX);
printf("\n --第%d号录入完毕 --\n",i);
sum=i;
printf("\n 按任意键返回住菜单!");
}
/*读取信息*/
int read_message()
{ FILE *fp;
int i=0;
if((fp=fopen("messagenotes.txt","rb"))==NULL)
{ printf("\n\n*****暂时没有任何保存信息,按任意键进入主菜单选择进行信息录入!*****\n");
return;
}
while(feof(fp)!=1)
{ fread(&message[i],sizeof(struct message_type),1,fp);
if(message[i].num==0) break;
else i++;
}
fclose(fp);
return(i);
}
/*输出模块*/
output_message()
{int i=0,j=0,count,page=1;
clrscr();
printf("\n\n --通讯录资料-- 第%d页\n\n",page);
PRINT2;
PRINT1;
do
{if(message[i].num!=0)
{j++;
if(j%PAGE!=0)
{PRINT3;
PRINT1;
}
else
{PRINT3;
PRINT1;
if(message[i+1].num!=0)
{print("按任意键继续!");
bioskey(0);
clrscr();
printf("\n\n --通讯录资料-- 第%d页\n\n",++page);
PRINT2;
PRINT1;
}
}
}
i++;
}while(message[i].num!=0);
printf("按任意键返回主菜单!");
bioskey(0);
}
/*添加模块*/
insert_message()
{int i=sum,j,flag=0;
char choice;
clrscr();
printf("\n\n 添加一条资料\n");
printf(" ---------------------------\n");
do
{message[i].num=++sum;
printf("\n 新资料编号:%d",message[i].num);
printf("\n 姓名:");
scanf("%s",message[i].name);
printf("\n 年龄:");
scanf("%d",&message[i].age);
printf("\n 电话:");
scanf("%s",&message[i].mobile[15]);
printf("\n 地址:");
scanf("%s",message[i].address);
printf("\n 要继续吗?(Y/N)");
choice=getch();
if (choice=='y'||choice=='Y')
{printf("\n 继续!\n");
i++;
flag=1;
}
else flag=0;
}while(flag==1);
printf("\n 按任意键返回主菜单!");
bioskey(0);
}
/*删除模块*/
del_message()
{int i=sum,j,flag=0;
char choice;
clrscr();
printf("\n\n 删除一条资料\n");
printf(" ---------------------------\n");
do
{message[i].num=++sum;
printf("\n 删除资料编号:%d",message[i].num);
printf("\n 姓名:");
scanf("%s",message[i].name);
printf("\n 年龄:");
scanf("%d",&message[i].age);
printf("\n 电话:");
scanf("%s",&message[i].mobile[15]);
printf("\n 地址:");
scanf("%s",message[i].address);
printf("\n 要继续吗?(Y/N)");
choice=getch();
if (choice=='y'||choice=='Y')
{printf("\n 继续!\n");
i++;
flag=1;
}
else flag=0;
}while(flag==1);
printf("\n 按任意键返回主菜单!");
bioskey(0);
}
/*修改模块*/
revise_message()
{int i=0,choice,revise_num,flag;
do
{clrscr();
printf("\n 输入要修改的资料编号:");
scanf("%d",&revise_num);
for(i=0;i<sum;i++)
if(message[i].num==revise_num)
{printf("\n --资料信息--\n");
PRINT1;
PRINT2;
PRINT3;
printf("\n 您要修改哪一项?\n");
printf("\n 1.姓名\n");
printf("\n 2.年龄\n");
printf("\n 3.电话\n");
printf("\n 4.地址\n");
printf("\n 请选择(1-4):");
scanf("%d",&choice);
switch(choice)
{case 1: printf("\n 请输入修改后的姓名:");
scanf("%s",&message[i].name);break;
case 2: printf("\n 请输入修改后的年龄:");
scanf("%d",&message[i].age);break;
case 3: printf("\n 请输入修改后的手机:");
scanf("%s",&message[i].mobile[15]);break;
case 4: printf("\n 请输入修改后的地址:");
scanf("%s",&message[i].address);break;
}
PRINT1;
PRINT2;
PRINT3;
break;
}
if (i==sum)
{printf("\n 无该资料!");
bioskey(0);
}
printf("\n\n 继续修改吗?(Y/N)");
choice=getch();
if (choice=='Y'||choice=='y')
{flag=1;
printf("\n 继续!\n");
}
else flag=0;
}while(flag==1);
printf("\n 按任意键返回主菜单!");
bioskey(0);
}
/*按姓名查询*/
inquire_name()
{int i,choice,j=0,
char inquire_name;
clrscr();
printf("\n 请输入要查询的姓名:");
scanf("%s",inquire_name);
for(i=0;i<sum;i++)
if(strcmp(message[i].name,inquire_name)==0&&message[i].num!=0)
{printf("\n --资料--\n");
PRINT1;
PRINT2;
PRINT3;
}else j++;
if(j==sum) printf("\n 查询信息不存在!");
bioskey(0);
}
/*按年龄查询*/
inquire_age()
{int i,choice,j=0;
char inquire_age;
clrscr();
printf("\n 请输入要查询的年龄:");
scanf("%s",inquire_name);
for(i=0;i<sum;i++)
if(strcmp(message[i].name,inquire_name)==0&&message[i].num!=0)
{printf("\n --资料--\n");
PRINT1;
PRINT2;
PRINT3;
}else j++;
if(j==sum) printf("\n 查询信息不存在!");
bioskey(0);
}
/*查询模块*/
inquire_message()
(int choice; )
do
{clrscr();
printf("\n\n\n **********查询资料**********\n\n\n");
printf(" 1.按姓名查询\n\n");
printf(" 2.按年龄查询\n\n");
scanf("%d",&choice);
switch(choice)
{case 1: inquire_name;break;
case 2: inquire_age;break;
case 0: break;
}
}while(choice!=0);
}
/*排序模块*/
sort_num()
{struct message_type *p,*q[MAX];
int i,j,k;
clrscr();
printf("\n 按编号排序\n");
printf("---------------------------------------------\n");
printf("\n 排名 编号 姓名 年龄 手机 地址\n");
p=message;
for(i=0;i<sum;i++)
{k=i;
for(j=i+1;j<sum;j++)
if(q[k]->num<q[j]->num)
k=j;
if(k!=i)
{p=q[i];q[i]=q[k];q[k]=p;}
}
for(i=0;i<sum;i++)
printf("\n%d %d %s %d %s %s\n",
i+1,q[i]->num,q[i]->name,q[i]->age,q[i]->mobile[15],q[i]->address);
bioskey(0);
}
[此贴子已经被作者于2007-9-13 16:01:42编辑过]