maodunzhanchang 2013-09-05 01:57 采纳率: 0%
浏览 993

C++初学问题!!求讲解

新手,我写了一个职工管理系统,没有语法错误。但运行不了,应该是类和链表有问题,但我太菜,不知道怎么解决,求大神帮忙。
以下是我的程序(因为有格式问题,可能略乱,跪求忽视)

#include "stdafx.h"
#include
#include
#include
#include
#include
using namespace std;
void MainMenuControl();
int MainMenu();

class CStaff
{
public:
CStaff( char id, char *name, char *birth, char *depart, char *title, char *rank, float tel, char sex= 'M' )
{
strcpy(this->id,id);
strcpy(this->name,name);
strcpy(this->birth,birth);
strcpy(this->depart,depart);
strcpy(this->title,title);
strcpy(this->rank,rank);
this->tel= tel;
this->sex= sex;
}
friend class CStaffList;
void ShowInfo()
{
cout<<"
*工号"<<"\t"<<"姓名"<<"\t"<<"性别"<<"\t"<<"出生年月"<<"\t"
<<"部门"<<"\t"<<"职称"<<"\t"<<"工资级别"<<"\t"<<"电话**"< coutid<<"\t"<name<<"\t"<<( this->sex == 'M'?"男":"女" )<<"\t"<birth<<"\t"
<depart<<"\t"<title<<"\t"<rank<<"\t"<tel<<"**"<<endl;
}
private:
char id[10];

char name[15];
char sex;

char birth[10];
char depart[10];
char title[10];
char rank[10];
float tel;
CStaff *next;
};

class CStaffList
{

private:
CStaff *Staff;
CStaff *p;
void Clear()
{
CStaff *pt= Staff;
while(pt)
{
Staff= pt->next;
delete pt;
pt= Staff;
}
}//重新载入的时候清除原内存
public:
CStaffList()
{
Staff= 0;
Load();
}
~CStaffList()
{
CStaff *pt;
pt= Staff;
while(pt)
{
pt= pt->next;
delete Staff;
Staff= pt;
}
Staff= 0;
}
void Add();//录入
void modify();//链接编辑和检索函数
void Modify();//修改
void Browsing();//显示到屏幕上
void Save(); //数据存盘
void Load(); //数据装入
void Retrieval();//检索
};

int main()
{
MainMenuControl();
return 0;
}
int MainMenu()
{
cout<<"********************************************************************************"< cout cout cout cout cout cout cout cout cout cout cout int choice; cin>>choice;
return choice;
}

void MainMenuControl()
{
while(1)
{
int choice= MainMenu();
CStaffList s1;
switch(choice)
{
case 1: s1.Browsing(); break;
case 2: s1.Retrieval();break;
case 3: s1.Add(); break;
case 4: s1.modify(); break;
case 5: s1.Load(); break;
case 6: s1.Save(); break;

case 0: cout<<"*************************系统退出,欢迎下次使用*********************************"<<endl; exit(0);
default: break;
}
system("cls");
}
}

void CStaffList::Load()//数据装入
{
char id[10]; char name[15];
char sex; char birth[10];
char depart[10]; char title[10];
char rank[10]; float tel;
fstream file;
file.open("staff_file.txt",ios::in);
if(!file)
{
cout<<"** 不能打开文件文件:staff_file.txt!! **"< exit(1);
}
file>>id>>name>>sex>>birth>>depart>>title>>rank>>tel;//预读第一行(项目名称),不录入
CStaffList::Clear();
CStaff *p1,*p0;//p1用于创建新的内存空间的,p0用于找到尾地址,链接作用
file>>id>>name>>sex>>birth>>depart>>title>>rank>>tel;//录入第二行 获得首地址
p1= new CStaff( id, name, birth, depart, title, rank, tel, sex );
p0= Staff= p1;
while( !file.eof())//从第三行开始循环录入
{
file>>id>>name>>sex>>birth>>depart>>title>>rank>>tel;
p1= new CStaff( id, name, birth, depart, title, rank, tel, sex );
p0->next= p1;
p0= p0->next;
}
p0= NULL;
file.close();
}

void CStaffList::Save()
{
CStaff p1;
fstream file;
file.open("staff_file.txt",ios::trunc);
if(!file)
{

cout<<"
* 不能打开文件文件:staff_file.txt!! "< exit(1);
}
if(Staff)
{
file for( p1= Staff; p1 != NULL; p1= p1->next )
file<id<<"\t"<name<<"\t"<<( p1->sex == 'M'?"男":"女" )<<"\t"<birth<<"\t"
<depart<<"\t"<title<<"\t"<rank<<"\t"<tel<<"\n";
}
else
{

cout<<"
无成员,请添加:staff_file.txt!! "<<endl;
return;
}
file.close();
//返回主菜单

cout<<"
按任意键返回主菜单 **"<<endl;
char ch; ch= getch();
return;
}

void CStaffList::Add()
{
CStaff p1;
char id[10]; cout<<">> 工号:"; cin>>id;
char name[15]; cout<<">> 姓名:"; cin>>name;
char sex; cout<<">> 性别(M/W):"; cin>>sex;
char birth[10]; cout<<">> 生日(1970.1.1):";cin>>birth;
char depart[10];cout<<">> 部门:"; cin>>depart;
char title[10]; cout<<">> 职称:"; cin>>title;
char rank[10]; cout<<">> 工资级别:"; cin>>rank;
float tel; cout<<">> 电话:"; cin>>tel;
p1= new CStaff( id, name, birth, depart, title, rank, tel, sex );
p1->next= 0;
if(Staff)
{
CStaff *p0;
for( p0= Staff; p0->next!= NULL; p0= p0->next );
p0->next= p1;
}
else
{ Staff= p1; }
//链接到save函数进行保存和文件数据同步

cout<<"
* 请按『s』键进行保存 **"< char c; cin>>c;

while(1)
{
if( c== 's'||'S' )
{ CStaffList::Save();return; }
else
{ cout<<" 输入有误,重新输入:"; cin>>c; }

}

}

void CStaffList::Retrieval()
{

while(1)
{

cout<<"** 请选择查找方式 "<<endl;
cout<<"
1. 按工号进行搜索 Search by ID "<<endl;
cout<<"
2. 按姓名进行搜素 Search by Name "<<endl;
cout<<"
0. 返回主菜单 Return MainMenu "< int choice; cin>>choice;
CStaff *p1;
p1= Staff;
char c;
switch(choice)
{
case 1:
//按id筛选查找结点
char id[10]; cin>>id;
for( ; p1 != NULL; p1= p1->next )
if( strcmp( p1->id,id) == 0 )
break;
if( p1 == NULL )
{ cout<<"
您所查找的用户不存在 "<<endl;break; }
else
{

cout<<"
你所查找的信息如下: **"< p1->ShowInfo();
}
//在检索的基础上进行编辑操作
cout<<" 是否对该用户进行编辑?Y/N"< cin>>c;
while(1)
{
if( c == 'y'||'Y')

{ p= p1; CStaffList::Modify(); }
else
if( c == 'n'||'N')
break;
else

{ cout<<" 输入有误,请重新输入:"; cin>>c; }
}
break;

    case 2:     
        //按姓名找结点
        char name[10];  cin>>name;
        for( ; p1 != NULL; p1= p1->next )
            if( strcmp( p1->name,name) == 0 )
                break;
        if( p1 == NULL )
        { cout<<"**  您所查找的用户不存在                                                          **"<<endl;break; }
        else   
            p1->ShowInfo();
        cout<<"    是否对该用户进行编辑?Y/N:";
        cin>>c;
        while(1)
        {
            if( c == 'y'||'Y')  
            { p= p1;    CStaffList::Modify(); }
            else
                if( c == 'n'||'N')
                    break;
                else       
                { cout<<"    输入有误,请重新输入:";    cin>>c; }
        }
        break;
    case 0: return;
    }
}

}

void CStaffList::modify()//没有实际作用,连接到检索菜单
{

cout<<"** 1.检索所要编辑的职工 "<<endl;
cout<<"
0. 返回主菜单 "< int choice;
while(1)
{
cout cin>>choice;
switch(choice)
{
case 1: CStaffList::Retrieval();
case 0: return;
default:cout<<"
输入有误,请重新输入! "<<endl; break;
}

}
}
void CStaffList::Modify()
{
while(1)
{

cout<<"
请选择你所需要修改信息,请填序号: "<<endl;
cout<<"
1. 工号 2.姓名 "<<endl;
cout<<"
3. 性别 4.出生日期 "<<endl;
cout<<"
5. 部门 6.职称 "<<endl;
cout<<"
7. 工资级别 8.电话 "< int choice; cin>>choice;
switch(choice)
{
case 1: cout<<">> 工号:"; cin>>p->id; break;
case 2: cout<<">> 姓名:"; cin>>p->name; break;
case 3: cout<<">> 性别(M/W):"; cin>>p->sex; break;
case 4: cout<<">> 出生年月(1970.1.1):";cin>>p->birth; break;
case 5: cout<<">> 部门:"; cin>>p->depart;break;
case 6: cout<<">> 职称:"; cin>>p->title; break;
case 7: cout<<">> 工资等级:"; cin>>p->rank; break;
case 8: cout<<">> 电话:"; cin>>p->tel; break;
}

cout<<"
已修改,信息如下: :"< p->ShowInfo();
char c; cin>>c;
while(1)
{

cout<<" 是否保存并退出编辑?Y/N:";
if( c == 'y'||'Y' )

{ CStaffList::Save();return; }
else
if( c == 'n'||'N' )break;
else
{

cout<<"
输入有误,请重新输入: **";
cin>>c;
}
}
}
p= Staff;
}

void CStaffList::Browsing()
{

CStaff p1;
cout<<"
* 职工信息如下 "<<endl;
cout<<"
工号"<<"\t"<<"姓名"<<"\t"<<"性别"<<"\t"<<"出生年月"<<"\t"
<<"部门"<<"\t"<<"职称"<<"\t"<<"工资级别"<<"\t"<<"电话**"< for( p1= Staff; p1!= NULL; p1= p1->next )
cout<<"**"<id<<"\t"<name<<"\t"<<(p1->sex=='M'?"男":"女")<<"\t"<birth<<"\t"
<depart<<"\t"<title<<"\t"<rank<<"\t"<tel<<"**"<<endl;
}

  • 写回答

1条回答 默认 最新

  • CSDN-Ada助手 CSDN-AI 官方账号 2022-09-20 21:32
    关注
    不知道你这个问题是否已经解决, 如果还没有解决的话:

    如果你已经解决了该问题, 非常希望你能够分享一下解决方案, 写成博客, 将相关链接放在评论区, 以帮助更多的人 ^-^
    评论

报告相同问题?

悬赏问题

  • ¥15 gwas 分析-数据质控之过滤稀有突变中出现的问题
  • ¥15 没有注册类 (异常来自 HRESULT: 0x80040154 (REGDB_E_CLASSNOTREG))
  • ¥15 知识蒸馏实战博客问题
  • ¥15 用PLC设计纸袋糊底机送料系统
  • ¥15 simulink仿真中dtc控制永磁同步电机如何控制开关频率
  • ¥15 用C语言输入方程怎么
  • ¥15 网站显示不安全连接问题
  • ¥15 51单片机显示器问题
  • ¥20 关于#qt#的问题:Qt代码的移植问题
  • ¥50 求图像处理的matlab方案