查看完整版本: 一个关于程序设计的问题

talentwei8899 2008-9-5 15:16

一个关于程序设计的问题

各位高手,今日编写了一端程序设计代码. 但在使用时发现总是出现内存不能读的错误 由于本人为初学者,水平有限,希望各位赐教.不胜感激~!
要求:模拟手机通讯录管理系统,实现对手机中的通讯录进行管理,功能 (1)要求:A办公类,B个人类 ,C商务类。 当选中某类时,显示出此类所有数据中的姓名和电话号码。 (2)增加功能:能录入新数据,一个结点包括:姓名,电话号码,分类(办公类,个人类,商务类),电子邮件,类如:杨春 13567772898 商务类 [email]chuny@126.com[/email]。当录入重复的姓名和电话号码,则提示数据录入重复并取消录入,当通讯录中超过15条信息时存储空间已满,不能再录入新数据,录入的数据能按递增的顺序自动进行编号 (3)修改功能:选中某个人的姓名时,可对此人的相应数据进行修改。 (4)删除功能:选中某个人的姓名时,可对此人的相对数据进行删除并自动调整后续条目的编号,
有问题的代码见下:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define Max 15
struct tele {
int num;
char name[20];
char tel_no[15];
char sim_no;
char arch;
char e_addr[20];
struct tele *front_Point;
struct tele *next_Point;
};
struct tele *head_Point; /*头节点*/
struct tele *end_Point; /*尾节点*/
struct tele *now_Point; /*当前节点*/
void UpPoint(); /*当前节点上移一条记录*/
void DownPoint(); /*当前节点下移一条记录*/
void Save();
void New();  
void Dial();
void Change();
void Del();  
void List(); /*显示所有的记录*/
void Point(); /*显示当前节点所指向的记录*/
void Quit(); /*退出程序(推荐选项)*/
void Find(); /*查找记录,并且把节点指向找到的记录.有两种查找方式(按名字,按电话)*/
void Load(); /*装载文件*/
void commandline();
/*命令行,你所有的指令都由它来理解,执行*/
void ShowA(struct tele *);
void ShowB(struct tele *);
void ShowC(struct tele *);
void Show(struct tele *); /*打印记录*/
void Trade(struct tele*,struct tele*); /*交换两个记录在链表中的位置UpPoint和DownPoint用*/
struct tele *search(char *,int); /*查找记录*/
void main()
{
Load();
commandline();
}
void commandline()
{
char ch;
do{
cout < <endl < <endl < <"欢迎使用您的通讯录!" < <endl;
cout < <endl < <endl < <"请选择操作:";
cout < <endl < <endl < <"1.显示通讯录";
cout < <endl < <endl < <"2.删除通讯录";
cout < <endl < <endl < <"3.添加通讯录";
cout < <endl < <endl < <"4.编辑通讯录";
cout < <endl < <endl < <"5.拨号功能";
cout < <endl < <endl < <"6.保存刚才的操作并退出";
cout < <endl;
cout < <endl < <"请选择:" < <endl;
cin>>ch;
switch(ch)
{
case '1':
List();
break;
case '2':
Del();
break;
case '3':
New();
break;
case '4':
Change();
break;
case '5':
Dial();
break;
case '6':
Save();
exit(0);
break;
default:
cout < <endl < <"_________________________________" < <endl;
cout < <endl < <"请输入数字1-6!!" < <endl;
cout < <endl < <"__________________________________" < <endl;
break;
}
}while(1);
}
void ShowA(struct tele *show_Point)
{
while(show_Point->arch!=NULL)
{
if(show_Point->arch=='A')
Show(show_Point);
}
}
void ShowB(struct tele *show_Point)
{
while(show_Point->arch!=NULL)
{
if(show_Point->arch=='B')
Show(show_Point);
}
}
void ShowC(struct tele *show_Point)
{
while(show_Point->arch!=NULL)
{
if(show_Point->arch=='C')
Show(show_Point);
}
}
void Show(struct tele *show_Point)
{
printf("\n_______________________________\n");
printf("NO. : : %s\n",show_Point->num);
printf("NAME : %s\n",show_Point->name);
printf("TEL : %s\n",show_Point->tel_no);
printf("SIM_NO : %s\n",show_Point->sim_no);
printf("ARCH : %s\n",show_Point->arch);
printf("E_ADDR : %s\n",show_Point->e_addr);
printf("\n_______________________________\n");
}
void List()
{
struct tele *List_Point;
char ch1;
cout < <"A:办公类" < <"B:个人类" < <"C:商务类" < <endl;
cout < <"请选择类型:" < <endl;
cin>>ch1;
switch(ch1)
{
case'A':
ShowA(List_Point);
break;
case'B':
ShowB(List_Point);
break;
case'C':
ShowC(List_Point);
break;
default:
cout < <"错误" < <endl;
}
int count=0;
if( head_Point->next_Point == end_Point )
{
cout < <"是空的!" < <endl;
return;
}
List_Point=head_Point->next_Point;
for(;List_Point->next_Point!=NULL;List_Point=List_Point->next_Point)
{
Show(List_Point);
count++;
}
printf("总数为 %d\n\n",count);
}
void Point()
{
Show(now_Point);
}
void New()
{
struct tele *New_Point;
New_Point=(struct tele*)malloc(sizeof(struct tele));
New_Point->next_Point=now_Point->next_Point;
New_Point->front_Point=now_Point;
now_Point->next_Point=New_Point;
now_Point=New_Point;
cout < <"输入编号:";
cin>>New_Point->num;
cout < <"输入姓名:";
cin>>New_Point->name;
cout < <"输入电话:";
cin>>New_Point->tel_no;
cout < <"输入单键拨号号码:";
cin>>New_Point->sim_no;
cout < <"输入归类:";
cin>>New_Point->arch;
cout < <"输入电子邮件地址:";
cin>>New_Point->e_addr;
cout < <"添加成功!" < <endl;
Show(New_Point);
cout < <endl;
}
void Find()
{
struct tele *Find_Point;
char words[255];
cout < <"查找" < <endl;
printf("请输入查找内容(name/tel) :");
gets(words);
if( strcmp(words,"name")== 0 )
{
cout < <"输入姓名:";
gets(words);
Find_Point=search(words,0);
if(Find_Point==NULL)
{
cout < <"出错了!" < <endl;
return;
}
else
{
Show(Find_Point);
now_Point=Find_Point;
return;
}
}
if(strcmp(words,"tel")== 0)
{
printf("输入号码:");
gets(words);
Find_Point=search(words,1);
if(Find_Point==NULL)
{
cout < <"出错" < <endl;
return;
}
else
{
Show(Find_Point);
now_Point=Find_Point;
return;
}
}
cout < <"出错了!" < <endl < <endl;
}
struct tele * search(char *words,int type)
{
struct tele *search_Point;
search_Point=head_Point->next_Point;
if( type == 0 )
{
for(;search_Point->next_Point!=NULL;search_Point=search_Point->next_Point)
{
if( strcmp(search_Point->name,words) == 0 )
{
return search_Point;
}
}
return NULL;
}
if( type == 1 )
{
for(;search_Point->next_Point!=NULL;search_Point=search_Point->next_Point)
{
if( strcmp(search_Point->tel_no,words) == 0 )
{
return search_Point;
}
}
return NULL;
}
}
void Quit()
{
char words[10];
printf("Quit,are you sure?(yes/no)");
gets(words);
if( (strcmp(words,"yes"))!=0 )
{
printf("Drop action!\n\n");
return;
}
exit(0);
}
void Del()
{
struct tele *temp_Point;
char words[255];
if(head_Point->next_Point->next_Point==NULL)
{
cout < <"出错了!" < <endl;
return;
}
Show(now_Point);
cout < <"将删除联系人,确定吗?(yes/no) ";
gets(words);
if( strcmp(words,"yes")!=0 )
{
cout < <"操作取消!" < <endl;
return;
}
cout < <"已删除联系人!" < <endl < <endl;
temp_Point=now_Point->front_Point;
now_Point->next_Point->front_Point=now_Point->front_Point;
now_Point->front_Point->next_Point=now_Point->next_Point;
free(now_Point);
now_Point=temp_Point;
}
void Load()
{
FILE *fp;
struct tele *Load_Point;
struct tele *temp_Point;
if( (fp=fopen("teleSave.dat","rb"))==NULL )
{
cout < <"建立一个新的通讯录!";
if( (fp=fopen("teleSave.dat","wb"))==NULL)
{
cout < <"对不起,无法建立!";
return;
}
cout < <"建立成功!";
fclose(fp);
}
head_Point=(struct tele*)malloc(sizeof(struct tele));
end_Point=(struct tele*)malloc(sizeof(struct tele));
head_Point->front_Point=NULL;
head_Point->next_Point=end_Point;
end_Point->front_Point=head_Point;
end_Point->next_Point=NULL;
strcpy(head_Point->name,"HEAD");
strcpy(head_Point->tel_no,"HEAD");
strcpy(end_Point->name,"END");
strcpy(end_Point->tel_no,"END");
now_Point=head_Point;
while(!feof(fp))
{
Load_Point=(struct tele*)malloc(sizeof(struct tele));
fread(Load_Point,sizeof(struct tele),1,fp);
Load_Point->next_Point=now_Point->next_Point;
Load_Point->front_Point=now_Point;
now_Point->next_Point=Load_Point;
now_Point=Load_Point;
}
fclose(fp);
temp_Point=now_Point->front_Point;
now_Point->next_Point->front_Point=now_Point->front_Point;
now_Point->front_Point->next_Point=now_Point->next_Point;
free(now_Point);
now_Point=temp_Point;
cout < <"载入成功!";
return;
}
void Save()
{
FILE *fp;
struct tele *Save_Point;
char words[10];
printf("It will change teleSave.mmd file,are you sure?(yes/no)");
gets(words);
if( (strcmp(words,"yes"))!= 0)
{
printf("Drop action!\n\n");
return;
}
printf("Saving...\n");
if( (fp=fopen("teleSave.mmd","wb"))==NULL )
{
printf("Can't Save files!\n");
return;
}
Save_Point=head_Point->next_Point;
for(;Save_Point->next_Point!=NULL;Save_Point=Save_Point->next_Point)
{
fwrite(Save_Point,sizeof(struct tele),1,fp);
}
printf("Save finished!\n\n");
}
void UpPoint()
{
if( now_Point->front_Point==head_Point )
{
printf("Sorry can not move TOP!\n\n");
return;
}
printf("Trade teles:");
Show(now_Point);
Show(now_Point->front_Point);
Trade(now_Point->front_Point,now_Point);
printf("Trade finished!\n\n");
}
void DownPoint()
{
if( now_Point->next_Point==end_Point )
{
printf("Sorry can not move END!\n\n");
return;
}
printf("Trade teles:");
Show(now_Point);
Show(now_Point->next_Point);
Trade(now_Point,now_Point->next_Point);
printf("Trade finished!\n\n");
}
void Trade(struct tele *a_Point,struct tele *b_Point)
{
a_Point->front_Point->next_Point=b_Point;
b_Point->next_Point->front_Point=a_Point;
a_Point->next_Point=b_Point->next_Point;
b_Point->front_Point=a_Point->front_Point;
a_Point->front_Point=b_Point;
b_Point->next_Point=a_Point;
}
void Change()
{
int num1;
char name1[20];
char tel_no1[15];
char sim_no1;
char arch1;
char e_addr1[20];
struct tele *Change_Point;
struct tele *temp_Point;
cout < <"请输入需要修改的姓名:" < <endl;
cin>>name1;
Change_Point=head_Point;
while(Change_Point!=NULL&&strcmp(name1,Change_Point->name)!=0)
Change_Point=Change_Point->next_Point;
if(Change_Point==NULL)
cout < <endl < <"您选择的用户不存在!" < <endl;
else
{
cout < <endl < <"请输入该用户的新信息!" < <endl;
cout < <"姓名:";
cin>>name1;
cout < <"电话:";
cin>>tel_no1;
cout < <"单键拨号号码:";
cin>>sim_no1;
cout < <"分类:";
cin>>arch1; cout < <"电子邮件地址:";
cin>>e_addr1;
while(temp_Point!=NULL&&
strcmp(name1,temp_Point->name)!=0&&
strcmp(tel_no1,temp_Point->tel_no)!=0)
temp_Point=temp_Point->next_Point;
if(temp_Point==NULL)
{
Change_Point->num=num1;
strcpy(Change_Point->name,name1);
strcpy(Change_Point->tel_no,tel_no1);
strcpy(Change_Point->e_addr,e_addr1);
Change_Point->sim_no=sim_no1;
Change_Point->arch=arch1;
}
else
cout < <"出错了!" < <endl;
}
}
void Dial()
{
int no;
cout < <"1.直接拨叫用户:" < <endl;
cout < <"2.使用单键拨号:" < <endl;
cin>>no;
}

大道至简 2008-9-5 15:47

你不至于要别人一行一行看完代码吧。你一点也不能缩小范围吗?

jason 2008-9-8 18:46

在哪down的源码
改下头文件
再把“< <”全部替换成“<<”就行了

zhuling 2008-9-16 15:59

如楼上所说
开头加一个#include <iostream.h>
然后把“< <"改成"<<”就OK了~
页: [1]
查看完整版本: 一个关于程序设计的问题