您的位置:首页 > 编程语言 > C语言/C++

c++课程设计

2013-03-12 13:31 411 查看
实现通讯录的一些功能:

View Code

#include<iostream>
#include<string>
#include<cstring>
#include<iomanip>
#include<conio.h>
#include<fstream>
#include<cstdlib>
using namespace std;
#include"List.cpp"
int main()
{
system("color 3E");
int flag=0,n;            //flag:标志;
char ch;               //n:操作步骤
string sname;
char pass[15];
List informationlist;
do
{
cout<<"\t\tPlease input the password:";
if(informationlist.get_password(pass))
{
cout<<"密码正确\n";
flag=1;break;
}
else
cout<<"密码错误,您只有查看的权限.\n";
cout<<"继续输入密码请输入 Y,否则 N :";
cin>>ch;
}while(ch!='N');
informationlist.menu();
informationlist.load();
while((cin>>n)&&n!=0)
{
if(flag==0)
{
if((n>=1&&n<=4)||(n==7))
n=10;
}
switch(n)
{
case 1:
informationlist.Creatlist();
cout<<"操作成功,";system("pause");
system("cls");informationlist.menu();
break;
case 2:
cout<<"输入要修改的学生的名字: "; cin>>sname;
informationlist.Change(sname);
cout<<"操作成功,";system("pause");
system("cls");informationlist.menu();
break;
case 3:
informationlist.Creatlist();
cout<<"操作成功,";system("pause");
system("cls");informationlist.menu();
break;
case 4:
cout<<"请输入删除对象的名字: ";
cin>>sname;
informationlist.Delete(sname);
cout<<"操作成功,";system("pause");
system("cls");informationlist.menu();
break;
case 5:
cout<<"请输入查询的对象的名字:";
cin>>sname;
informationlist.SearchName(sname);
cout<<"操作成功,";system("pause");
system("cls");informationlist.menu();
break;
case 6:
//informationlist.Showallinformation();
cout<<informationlist;
cout<<"操作成功,";system("pause");
system("cls");informationlist.menu();
break;
case 7:
informationlist.save();
cout<<"操作成功,";system("pause");
system("cls");informationlist.menu();
break;
default:
cout<<"没有此操作或您没此权限,请按照提示操作!"<<endl;
cout<<"操作成功,";system("pause");
system("cls");informationlist.menu();
break;
}
}
return 0;
}
  

#ifndef  LIST_H
#define LIST_H
#include "Node.h"
#include"Node.cpp"
class List    //链表类.
{
private:
Node *head,*pos;
static int num;
static int count;
static char password[15];
public:
List();
~List();
void menu();
bool get_password(char* pass);                              //菜单
void Creatlist();                          //建立链表及增加节点;
void Delete(string name);                  //删除
void Change(string name);                  //修改
void SearchName(string name);              //姓名查找
friend ostream& operator<<(ostream&,List&); //显示所有人员信息
void save();
void load();
};
#endif
  

#ifndef STUDENT_H
#define  STUDENT_H
class Person       //人员数据信息类
{
protected:
string name;   //姓名
string phone;  //电话
string address;//地址
string email;  //邮箱
};
class Node:public Person //节点类
{
public:
Node *next;
friend istream& operator>>(istream&,Node *);
void diplay();
//friend void save();    //友元成员函数。
//friend void load();    //友元成员函数。
void change(string sname,string sphone,string saddress,string semail);
friend class List;  //友元类
};
#endif
  

#include"Node.h"
istream& operator>>(istream& fin,Node * TT)  //重载输出运算符。
{
fin>>TT->name>>TT->phone>>TT->address>>TT->email;
return fin;
}
void Node::diplay()
{
cout<<name<<"\t"<<phone<<"\t"<<address<<"\t"<<email<<endl;
}
void Node::change(string sname,string sphone,string saddress,string semail)  //修改数据。
{
name=sname;
phone=sphone;
address=saddress;
email=semail;
}
  

#include"List.h"
int List::count=0;
int List::num=0;
char List::password[15]={'3','3','0','6','2','0'};
List::List()
{
head=pos=NULL;
}
List::~List()   //从头节点开始删除;
{
Node *p;
if(head)
{
p=head;
head=head->next;
delete p;
}
}
void List::menu()  //菜单。
{
cout.width(60);
cout<<"--------------------【菜单】-----------------"<<endl;
cout<<setw(57)<<"1.建立学生通讯录    2.修改学生通讯录\n"<<endl;
cout<<setw(57)<<"3.增加学生通讯录    4.删除学生通讯录\n"<<endl;
cout<<setw(59)<<"5.查询学生通讯录    6.显示所有学生信息\n"<<endl;
cout<<setw(51)<<"7.保存信息          0.退出操作\n"<<endl;
cout.width(60);
cout<<"---------------------------------------------"<<endl;
cout<<"请输入操作的步骤:";
}
bool List::get_password(char* pass)
{
int i;
for(i=0;(pass[i]=getche())!='\r'&&i<9;i++) //回车键表示密码输入完成;
cout<<"\b*";
pass[i]='\0';
cout<<"\n";
if(strcmp(pass,password)==0)
return true;
return false;
}
void List::Creatlist() //建立链表;
{
Node *p=new Node;
cout<<"请输入的信息姓 名 、电 话 、地 址 、邮 箱 、:\n";
cin>>p;
p->next=NULL;
if(head==NULL)
head=p;
else
pos->next=p;
pos=p;
count++;
}
void List::Delete(string sname)//删除
{
Node *p=head,*q;
int flag=0;
while(p)
{
if(p->name==sname)
{
if(p==head)
head=p->next;
else
q->next=p->next;
delete p;
flag=1;
break;
}
q=p;
p=p->next;
}
if(flag==0)
cout<<"没有信息"<<endl;
}
void List::Change(string sname)//修改信息
{
Node *p=head;
int flag=0;
while(p)
{
if(p->name==sname)
{
string sphone;   //电话
string saddress; //地址
string semail;   //邮箱
cout<<"请输入新的信息姓 名 、电 话 、地 址 、邮 箱 、:\n";
cin>>sname>>sphone>>saddress>>semail;
p->change(sname,sphone,saddress,semail);
flag=1;
}
p=p->next;
}
if(flag==0)
cout<<"没有信息!"<<endl;
}
void List::SearchName(string sname)  // 查找。
{
Node *p=head;
int flag=0;
while(p)
{
if(p->name==sname)
{
cout<<"姓名\t电话\t\t地址\t邮箱\n\n";
cout<<p->name<<"\t"<<p->phone<<"\t"<<p->address<<"\t"<<p->email<<endl;
flag++;
}
p=p->next;
}
if(flag==0)
cout<<"没有信息!"<<endl;
else
cout<<setw(65)<<"共"<<flag<<"条信息\n";
}
ostream& operator<<(ostream& out,List& T)
{
out<<"\t\t\t信息表\n";
out<<"姓名\t电话\t\t地址\t邮箱\n";
int flag=0;
Node *p=T.head;
while(p)
{
p->diplay();
out<<endl;
p=p->next;
flag++;
}
out<<setw(65)<<"共"<<flag<<"条信息\n";
return out;
}
void List::save()
{
if(!head)
cout<<"没有信息,请输入信息."<<endl;
ofstream fout("d:\\test.txt",ios_base::binary);
if(!fout)
{
cout<<"打开文件出错."<<endl;
system("pause");
}
int num1=0;
Node *p=head;
while(p)
{
num1++;
fout<<p->name<<"\n"<<p->phone<<"\n"<<p->address<<"\n"<<p->email;
if(num1!=count+num)
fout<<endl;
p=p->next;
}
fout.close();
}
void List::load()
{
ifstream fin("d:\\test.txt",ios_base::binary);
if(!fin)
return ;
while(fin.good())
{
Node *p=new Node;
//fin>>p->name>>p->phone>>p->address>>p->email;
getline(fin,p->name);getline(fin,p->phone);
getline(fin,p->address);getline(fin,p->email);
p->next=NULL;
if(head==NULL)  head=p;
else  pos->next=p;
pos=p;
num++;
}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: