用 C++ 语言编写的数据结构程序,在 visual 6.0 中运行总有个小错误,十分紧急哦,请大神帮下我好嘛?
这个程序整体运行总显示一个错误,我始终无法修改正确,请大家帮我找一下错哦。老师要求我们程序的每行一般不要超过60个字符,每个函数体一般不超过40行,最长不超过60行,否则应分割成较小的函数。该如何把程序分在Visual C++ 6.0的许多个文件中运行呢?可以帮我详细注明清楚吗,万分感谢。
#include"stdafx.h"
using namespace std;
struct contaxt
{
char name[10];
char sex[5];
char Adrre[50];
char xueli[20];
char Mobi[11];
char birth[8];
char gongzuonianyue[8];
struct contaxt *next;
};
//声明函数
typedef struct contaxt CONT;
CONT *InsertCon(CONT *head);////插入联系人的基本信息
CONT *AddContaxt(int n); ////初始化联系人的基本信息
CONT *FindCon(CONT *head); ////查找联系人的基本信息
CONT *AmendMessage(CONT *head);//修改联系人的基本信息
CONT *DeleteMessage(CONT *head);//删除联系人的基本信息
void showmessage(CONT *head); ////显示联系人的基本信息
int menu(CONT *head); //主菜单
CONT *saveFileCONT(CONT *head );//保存联系人的基本信息
CONT *readFileCONT(CONT *head);//读取联系人的基本信息
int choose;//定义一个选择序号数据
//系统入口
void main()
{
CONT *head;
head = NULL;
menu(head);
}
//系统主菜单
int menu(CONT *head)
{
cout << "请选择操作菜单!!!" << endl ;
do
{
cout << "-------------------------欢迎进入简单的职工管理系统------------------------"
<< "---------------------------------操作菜单---------------------------------------";
cout << "-------------------------------------------------------------------------------"
<< " 1 添加职工信息 "
<< " 2 插入职工信息 " << endl
<< " 3 查找职工信息 "
<< " 4 修改职工信息 " << endl
<< " 5 删除职工信息 "
<< " 6 显示职工信息 " << endl
<< " 7 退出本系统 " << endl
<< " 8 保存文件 " << endl
<< " 9 读取文件 " << endl
<< "--------------------------------------------------------------------------------" << endl;
cout << "请选择操作菜单!!!" << endl ;
int choose;
//choose = getchar();
fflush(stdin);
//return choose;
cin >> choose;
if(choose != 1&& choose != 2 && choose != 3 && choose !=4 && choose != 5 && choose !=6 && choose !=7&&choose != 8)
{
cout << "输入错误,请重新选择:"<<endl;
}
else
{
switch(choose)
{
case 1:
int n;
do
{
fflush(stdin);
cout << "请确定要添加几个职工:";
cin >> n;
}while(n <= 0);
head = AddContaxt(n);
break;
case 2:
system("CLS");
head = InsertCon(head);
break;
case 3:
head = FindCon(head);
break;
case 6:
system("CLS");
showmessage(head);
break;
case 7:
exit(0);
break;
case 4:
system("CLS");
AmendMessage(head);
break;
case 5:
system("CLS");
DeleteMessage(head);
break;
case 8:
system("CLS");
head = saveFileCONT(head );
break;
case 9:
// head = readFileCONT(head);
default:
system("CLS");
cout << "你输入错误,请重新选择!!!" << endl;
break;
}
}
}while(choose != 1&& choose != 2 && choose != 3 && choose !=4 && choose != 5 && choose !=6 && choose !=7);
return choose;
}
//初始化联系人的基本信息
CONT *AddContaxt(int n)
{
CONT *p,*q,*head;
int i;
system("CLS");
p = (CONT *)malloc(sizeof(CONT));
cout << "开始添加联系人信息:"<< endl;
cout << "输入姓名:";
cin >> p->name;
cout << "输入性别:";
cin >> p->sex ;
cout << "输入学历:";
cin >> p->xueli ;
cout << "输入住址:";
cin >> p->Adrre ;
cout << "输入手机号:";
cin >> p->Mobi ;
cout << "输入出生年月:";
cin >> p->birth ;
cout<<"输入工作年月:";
cin >> p->gongzuonianyue ;
head = p;
for(i = 2;i <= n;i++)
{
q = (CONT *)malloc(sizeof(CONT));
p->next = q ;
p = q;
system("CLS");
cout<< "总共需要输入"<< n << "个联系人的信息,"<<"现在你已输入" << i-1<< "个联系人信息."<< endl<< "请继续输入第"<<i << "个人的信息!!"<< endl;
cout << "输入姓名:";
cin >> p->name;
cout << "输入性别:";
cin >> p->sex ;
cout << "输入学历:";
cin >> p->xueli ;
cout << "输入住址:";
cin >> p->Adrre ;
cout << "输入手机号:";
cin >> p->Mobi ;
cout << "输入出生年月:";
cin >> p->birth ;
cout<<"输入工作年月:";
cin >> p->gongzuonianyue ;
}
p->next = NULL;
return head;
}
//按照不同的要求插入联系人的基本信息
CONT *InsertCon(CONT *head)
{
CONT *pc,*p,*q;
int choose ;
char name[10];
if(head ==NULL)
{
cout << "还没有添加任何联系人,请先添加联系人!!"<< endl;
return head;
}
pc = (CONT *)malloc(sizeof(CONT));
cout << "请依次输入被插入人的信息:姓名 /性别/学历/住址/手机号/出生年月/工作年月"<< endl;
cin >> pc->name >> pc->sex >> pc->xueli >> pc->Adrre >> pc->Mobi >> pc->birth>>pc->gongzuonianyue;
pc ->next = NULL;
cout << "请选择插入方式: "<< endl<< "1 插入在指定的人之前"<< endl << "2 插入在最后" << endl << "3 插入在开头" << endl;
do {
cin >> choose;
switch(choose)
{
case 1:
cout << "请输入指定人的名字:";
cin >> name ;
if(strcmp(head->name ,name)==0)
{
pc->next = head;
head = pc;
return head;
}
else
{
q = head;
p = head->next ;
while(strcmp(p->name,name)!=0 && p->next !=NULL)
{
q = p;
p = p->next ;
}
if(strcmp(p->name ,name)==0)
{
pc->next = p ;
q = pc;
return head;
}
else
{
p->next = pc;
return head;
}
}
break;
case 2:
//cout << "请输入指定人的名字:";
//cin >> name ;
p = head;
while(p->next != NULL)
p = p->next ;
p->next = pc;
return head;
break;
case 3:
//cout << "请输入指定人的名字:";
//cin >> name ;
pc->next = head;
head = pc;
return head;
break;
default :
cout<<"error"<< endl;
break;
}
}while(choose!=1 &&choose != 2 && choose != 3);
}
//查找联系人的基本信息
CONT *FindCon(CONT *head)
{
system("CLS");
char na[20];
CONT *p,*q;
if(head == NULL)
{
cout << "没有任何信息可查找,请先添加信息!!"<< endl;
return head;
}
cout << "请您输入要查找人的姓名:"<< endl;
cin >> na;
p = head;
while(p!= NULL)
{
if(strcmp(p->name ,na)==0)
{
q = p;
cout << "找到联系人,信息如下!!"<< endl;
cout << "姓名:" << q->name <<" 性别: " << q->sex <<" 学历: " <<q->xueli<<" 住址: " <<q->Adrre <<" 手机号: " <<q->Mobi <<" 出生年月: "
<< q->birth<< " 工作年月: " <<q->gongzuonianyue<< endl;
break;
}
else
p = p->next ;
}
return head;
}
//修改联系人的基本信息
CONT *AmendMessage(CONT *head)
{
CONT *p;
char name[10];
char sex[5];
char Adrre[50];
char xueli[20];
char Mobi[11];
char birth[8];
char gongzuonianyue[8];
if(head == NULL)
{
cout<< "没有任何信息,请返回添加信息!!!"<< endl;
return head;
}
else
p = head;
cout << "请输入要修改人的名字:";
cin >> nam;
while(p != NULL && p->name != nam)
{
if(strcmp(p->name ,nam)==0)
{
strcpy(p->name ,nam);
int choose;
do
{
cout << "请选择要修改哪一项的信息!!!"<< endl;
cout << "1 修改性别 2 修改学历 3 修改地址 4 修改手机号码 5 修改出生年月 6修改工作年月 " << endl;
cin >> choose;
switch(choose)
{
case 1:
cout << "请输入要修改后的性别:";
cin >> sex;
strcpy(p->sex , sex);
cout << "修改成功!!!"<< endl;
system("CLS");
return head;
break;
case 2:
cout << "请输入修改后的学历:";
cin >> xueli;
strcpy(p->xueli ,xueli);
system("CLS");
cout << "修改成功!!!"<< endl;
return head;
break;
case 3:
cout << " 请输入修改后的地址:";
cin >> Adrre;
strcpy(p->Adrre ,Adrre);
system("CLS");
cout << "修改成功!!!"<< endl;
return head;
break;
case 4:
cout << "请输入修改后的手机号码:";
cin >> Mobi;
strcpy(p->Mobi ,Mobi);
system("CLS");
cout << "修改成功!!!"<< endl;
return head;
break;
case 5:
cout << "请输入修改后的出生年月:";
cin >> birth;
strcpy(p->birth ,birth);
system("CLS");
cout << "修改成功!!!"<< endl;
return head;
break;
case 6:
cout << "请输入修改后的工作年月:";
cin >> gongzuonianyue;
strcpy(p-> gongzuonianyue , gongzuonianyue);
system("CLS");
cout << "修改成功!!!"<< endl;
return head;
break;
default:
cout << "输入错误,重新选择!!" << endl;
break;
}
}while(choose != 0);
}
else if(p->next != NULL && p->name != nam)
{
p = p->next ;
}
else
{
cout << "输入名字错误,请重新选择4修改!!!" << endl;
return head;
}
}
//return head;
}
//删除联系人的基本信息
CONT *DeleteMessage(CONT *head)
{
CONT *p,*q,*t;
char name[10];
char elem[10];
cout << "请输入要删除联系人的姓名:";
cin >> name;
if(head == NULL)
{
cout << "没有任何信息可删除,请添加信息!!"<< endl;
return head;
}
else if(strcmp(head->name,name)==0 )
{
t = head;
head = head->next ;
return head;
}
else
{
p = head->next ;
q = head;
}
while(p !=NULL && p->name != name)
{
q = p;
p = p->next ;
}
if(strcmp(p->name ,name)==0)
{
q->next = p->next ;
free(p);
//t = p;
}
else
{
cout<< "表中无该元素!" << endl;
return head;
}
if(t)
{
// elem = t->name ;
free(t);
}
return head;
}
//保存文件
CONT *saveFileCONT(CONT *head )
{
char *fileName;
FILE *fp;
CONT *p;
p = (CONT *)malloc(sizeof(CONT));
if(p=NULL)
{
cout <<"分配内存失败"<< endl;
// exit(0);
}
if(head ==NULL)
{
cout <<"没有任何信息,请先添加信息!!"<< endl;
return head;
}
else
{
p = head;
fileName = (char *)malloc(sizeof(char));
cout << "请输入文件名:" << endl;
cin >> fileName;
fileName = strcat(fileName,".txt");
if( (fp = fopen(fileName,"a")) == NULL)
cout << "can't open file" << endl;
do
{
//fwrite(head,sizeof(CONT),1,fp);
//fprintf(fp,p->name,p->sex,p->Class,p->Adrre,p->Mobi,p->qq );
fprintf(fp," 姓名:");
fprintf(fp, p->name );
fprintf(fp," 性别:");
fprintf(fp, p->sex);
fprintf(fp," 学历:");
fprintf(fp, p->xueli);
fprintf(fp," 地址:");
fprintf(fp, p->Adrre);
fprintf(fp," 手机号:");
fprintf(fp, p->Mobi);
fprintf(fp," 出生年月:");
fprintf(fp, p->birth);
fprintf(fp," 工作年月:");
fprintf(fp, p->gongzuonianyue );
fprintf(fp,"\n");
cout << endl;
cout << "--------------------------------------------------------------------------------";
cout << " "<<"姓名:" <<p->name <<" 性别:" <<p->sex << " 学历:" <<p->xueli<<" 住址:" <<p->Adrre <<"手机号:" << p->Mobi <<" 出生年月:" << p->birth <<" 工作年月:" << p->gongzuonianyue << endl;
cout << "--------------------------------------------------------------------------------";
p=p->next ;
}while(p!=NULL);
}
fclose(fp);
return head;
}
//读取文件内容
CONT *readFileCONT(CONT *head)
{
char *fileName;
CONT *p;
FILE *fp;
fileName = (char *)malloc(sizeof(char));
cout << "请输入要打开的文件名:";
cin >> fileName;
fileName = strcat(fileName,".txt");
fp = fopen(fileName,"r");
if(fp == NULL)
{
cout << "文件没有任何信息,请先输入信息!!"<< endl;
}
else
{
while(!feof(fp))
{
p = (CONT *)malloc(sizeof(CONT));
if(p == NULL)
{
cout << "分配内存失败!!"<< endl;
return head;
}
fscanf(fp,p->name );
fscanf(fp,p->sex );
fscanf(fp,p->xueli );
fscanf(fp,p->Adrre );
fscanf(fp,p->Mobi );
fscanf(fp,p->birth);
fscanf(fp,p->gongzuonianyue );
cout << "--------------------------------------------------------------------------------";
cout << " "<<"姓名:" <<p->name <<" 性别:" <<p->sex << " 学历:" <<p->xueli <<" 住址:" <<p->Adrre <<"手机号:" << p->Mobi <<" 出生年月:" << p->birth <<" 工作年月:" << p->gongzuonianyue << endl;
cout << "--------------------------------------------------------------------------------";
p->next = head;
head = p;
p = p->next ;
}
fclose(fp);
cout<< "读取信息成功!!!"<< endl;
}
fclose(fp);
return head;
}
//显示信息
void showmessage(CONT *head)
{
CONT *p;
int i=0;
if(head == NULL )
{
cout << "没有任何信息可输出,请添加信息!!!"<< endl;
}
else
{
p = head;
cout << "*******************************联系表如下***************************************";
do
{
i++;
cout << "--------------------------------------------------------------------------------";
cout << i << " "<<"姓名:" <<p->name <<" 性别:" <<p->sex << " 学历:" <<p->xueli <<" 住址:" <<p->Adrre <<"手机号:" << p->Mobi <<" 出生年月:" << p->birth <<" 工作年月:" << p->gongzuonianyue << endl;
cout << "--------------------------------------------------------------------------------";
p = p->next ;
}while(p!=NULL);
}
cout << "*********************************联系表***************************************";
cout << endl;
}