各位帮忙看看这个程序错在哪里?
#include <stdio.h>#include <conio.h>
#include <stdlib.h>
#include <string.h>
//=========================//
typedef struct node
{
int no, studentsnumber, tel;
char name[20], sex[4], address[40], classnumber[4], grade[4] ;
struct node *next;
struct node *prev;
}Node_type;
int used = 0;
int a = 0;
int b = 0;
//=========================//
Node_type *head = NULL;
Node_type *tail = NULL;
Node_type *nowptr = NULL;
Node_type *before = NULL;
Node_type *newptr = NULL;
Node_type *tmp = NULL;
FILE *fp;
char fname[15] = "addrbook.txt";
int ptr;
//=========================
void Showmenu (void);
void Inputitem (void);
void Viewpersonalinfor (void);
void Broseclassinfor (void);
void Savetofile (void);
void Loadfromfile (void);
void Exitprogram (void);
void Getfilename (char *filename);
void Freeall (void);
void Showitem (Node_type *nodaddr);
//main part
#include "ADDRESS.h"
int main(void)
{
head=NULL;
tail=NULL;
int button =0;
while(1)
{
Showmenu ();
button = getche () - 48;
switch(button)
{
case 1:
Inputitem ();
a = 1;
break;
case 2:
Viewpersonalinfor ();
break;
case 3:
Broseclassinfor ();
break;
case 4:
Savetofile ();
b = 1;
break;
case 5:
Loadfromfile ();
break;
case 6:
Exitprogram ();
return 0;
break;
}
}
return 0;
}
void Showmenu (void)
{
printf("*======================================*\n\n");
printf(" [班级通讯录] \n");
printf(" 一、输入条目……………………【输入 1】\n");
printf(" 二、查询个人信息………………【输入 2】\n");
printf(" 三、浏览班级成员信息…………【输入 3】\n");
printf(" 四、保存文件……………………【输入 4】\n");
printf(" 五、读取文件……………………【输入 5】\n");
printf(" 六、退出程序……………………【输入 6】\n\n");
printf("*======================================*\n");
return;
}
void Inputitem (void)
{
char yn = 'n';
used = 1;
Freeall();
while(1)
{
newptr = (Node_type *)malloc(sizeof(Node_type));
printf("\n输入下列信息\n");
printf("N.O.: \n ");
scanf("%d", &(newptr -> no));
//================================================================================================================
printf("Name(不超过20个字): ");
scanf("%s", newptr -> name);
while(strlen(newptr -> name) > 20)
{
printf("Name 不超过20个字\n");
printf("请重新输入 Name: ");
scanf("%s", newptr -> name);
}
//================================================================================================================
printf("Student's number: ");
scanf("%d",&(newptr ->studentsnumber));
//================================================================================================================
printf("Class(输入班号少于4字): ");
scanf("%s",newptr ->classnumber);
while(strlen(newptr ->classnumber) > 4)
{
printf("Class 输入班号少于4字\n");
printf("请重新输入 Class: ");
scanf("%s",newptr ->classnumber);
}
//================================================================================================================
printf("Grade(输入年级号少于4字): ");
scanf("%s",newptr ->grade);
while(strlen(newptr ->grade) > 4)
{
printf("Grade 输入年级号少于4字\n");
printf("请重新输入 Grade: ");
scanf("%s",newptr ->grade);
}
//================================================================================================================
printf("Sex(少于2字): ");
scanf("%s",newptr ->sex);
while(strlen(newptr ->sex) > 2)
{
printf("Sex 少于2字\n");
printf("请重新输入 Sex: ");
scanf("%s", newptr ->sex);
}
//================================================================================================================
printf("Telephone: ");
scanf("%d",&(newptr -> tel));
//================================================================================================================
printf("Address(少于40字): ");
scanf("%s",newptr ->address);
while(strlen(newptr ->address) > 40)
{
printf("Address 少于40字\n");
printf("请重新输入 Address: ");
scanf("%s", newptr ->address);
}
//================================================================================================================
if(head == NULL)
{
head=newptr;
tail=newptr;
nowptr=newptr;
nowptr->next=NULL;
}
else
{
nowptr->next=newptr;
newptr->next=NULL;
newptr->prev=nowptr;
nowptr=newptr;
tail=newptr;
}
printf("若退出请输入n, 若继续请输入任意键。\n");
yn = getche();
if(yn == 'n')
return;
}
return;
}
void Viewpersonalinfor (void)
{
int querry = 0;
printf("已进入 2\n");
if(head == NULL)
{
printf("暂无数据,返回主菜单。\n");
return;
}
else
{
printf("请输入查找的人的序号N.O.: ");
scanf("%d",&querry);
nowptr=head;
while(nowptr->next!=NULL)
{
if(querry == nowptr->no)
{
Showitem (nowptr);
}
nowptr=nowptr->next;
}
nowptr = tail;
if(querry == nowptr->no)
{
Showitem (nowptr);
}
}
return;
}
void Broseclassinfor (void)
{
printf("已进入 3\n");
if(head==NULL)
{
printf("暂无数据。\n");
return;
}
else
{
nowptr=head;
while(1)
{
while(nowptr->next!=NULL)
{
if(nowptr==head)
{
Showitem (nowptr);
printf("向下翻请按2,若退出请按3\n");
scanf("%d",&ptr);
if(ptr==2)
{
nowptr=nowptr->next;
}
if(ptr==3)
{
return;
}
}
else
{
Showitem (nowptr);
printf("向上翻请按1 ,向下翻请按2,若返回主菜单请按3。\n");
scanf("%d",&ptr);
if(ptr==1)
{
nowptr=nowptr->prev;
}
if(ptr==2)
{
nowptr=nowptr->next;
}
if(ptr==3)
{
return;
}
}
}
nowptr = tail;
Showitem (nowptr);
printf("向上翻请按1 ,若返回主菜单请按。3\n");
scanf("%d",&ptr);
if(ptr==1)
{
nowptr=nowptr->prev;
while(nowptr->next!=NULL && nowptr!=head)
{
Showitem (nowptr);
printf("向上翻请按1 ,向下翻请按2,若返回主菜单请按3。\n");
scanf("%d",&ptr);
if(ptr==1)
{
nowptr=nowptr->prev;
}
if(ptr==2)
{
nowptr=nowptr->next;
}
if(ptr==3)
{
return;
}
}
}
if(ptr==3)
{
printf("返回主菜单。\n");
return;
}
}
}
return;
}
void Savetofile (void)
{
int cnt = 0;
char name[20];
Getfilename (name);
fp=fopen(name,"w");
if(fp==NULL)
{
printf("打开文件失败,返回主菜单。\n");
return;
}
printf("\t正在保存文件……\n");
nowptr=head;
while((nowptr->next)!=NULL)
{
fwrite(nowptr,sizeof(Node_type),1,fp);
nowptr=nowptr->next;
cnt++;
}
fwrite(tail, sizeof(Node_type), 1, fp);
cnt++;
printf("\t文件 %s 保存完毕,共 %d 条记录\n",name, cnt);
fclose(fp);
return;
}
void Loadfromfile (void)
{
int cnt = 0;
int cc=0;
fpos_t num=0;
char name[20];
Getfilename (name);
fp=fopen(name,"r");
if(fp==NULL)
{
printf("打开文件失败,返回主菜单。\n");
return;
}
printf("正在加载文件……\n");
if(used)
{
Freeall ();
}
cc=sizeof(Node_type);
while(!feof(fp))
{
newptr = (Node_type *)malloc(sizeof(Node_type));//+++
if(fread(newptr,sizeof(Node_type),1,fp)>0)//+++
{
cnt++;
if(head == NULL)
{
head=newptr;
tail=newptr;
nowptr=newptr;
nowptr->next=NULL;
Showitem (head);
}
else
{
nowptr->next=newptr;
newptr->next=NULL;
newptr->prev=nowptr;
nowptr=newptr;
tail=newptr;
Showitem (nowptr);
}
}
}
printf("文件 %s 加载完毕,读取%d个记录\n",name,cnt);
return;
}
void Exitprogram (void)
{
printf("已进入 6\n");
//
if(a == 1&&b!=1)
{
printf("\t\t\t请保存文件\n");
printf("\t\t\t----------\n\n");
Savetofile ();
}
else
{
return;
}
Freeall ();
return;
}
void Getfilename (char *filename)
{
printf("输入新文件名 字数小于15 或按回车使用默认文件名addrbook.txt\n");
scanf("%s",filename);
if(strlen(filename) == 0)
{
strcpy(filename, fname);
}
else
{
while(strlen(filename) >= 15)
{
printf("输入新文件名 字数小于15!\n重新输入文件名: ");
gets(filename);
}
}
return;
}
void Freeall (void)
{
if(head != NULL)
{
nowptr=head;
while((nowptr->next)!=NULL)
{
tmp=nowptr->next;
free(nowptr);
nowptr=tmp;
}
free(tail);
printf("释放内存……\n");
}
head=NULL;
tail=NULL;
nowptr=NULL;
newptr=NULL;
tmp=NULL;
return;
}
void Showitem (Node_type *nodaddr)
{
if(nodaddr!=NULL)
{
printf("※※※※※※※※※※※※※※※※※※※※※※※\n");
printf("\t这是您所查询的信息。\n\n");
printf("\tN.O.: %d\n",nodaddr->no);
printf("\tName: %s\n",nodaddr->name);
printf("\tStudent's number: %d\n",nodaddr->studentsnumber);
printf("\tClass: %s\n",nodaddr->classnumber);
printf("\tGrade: %s\n",nodaddr->grade);
printf("\tSex: %s\n",nodaddr->sex);
printf("\tTelephone: %d\n",nodaddr->tel);
printf("\tAddress: %s\n\n",nodaddr->address);
printf("※※※※※※※※※※※※※※※※※※※※※※※\n");
}
return;
}