#include<iostream> #include<string> #include<fstream> #include<iomanip> using
namespace std; #define MAXPEOPLE 100 static int scount=0; class telephone {
char name[20]; //姓名 char number[12]; //电话号码 char addr[20]; //地址 public:
telephone(){}; void instial(char na[20],char nu[20],char ad[20]){
strcpy_s(name,na); strcpy_s(number,nu); strcpy_s(addr,ad); }; void setname(char
na[20]){ strcpy_s(name,na); } void setnumber(char nu[20]){ strcpy_s(number,nu);
} void setaddr(char ad[20]){ strcpy_s(addr,ad); } char* getname() //获得姓名 {
return name;//数组返回类型通过指针来实现 }; char* getnumber() //获得电话号码 { return number; };
char* getaddr(){ return addr; }; }; class manage{ telephone tel[MAXPEOPLE];
public: void input(); //添加1 void read(); //读入2 void save(); //保存3 void
search(); //查询4 void sort(); //排序5 void insert(); //插入6 void Delete(); //删除7
void change(); //修改8 void display(); //显示0 }; void menu() //定义菜单函数 {
cout<<"***********1.添加联系人*************"<<endl;
cout<<"***********2.读入联系人信息*********"<<endl;
cout<<"***********3.保存联系人信息*********"<<endl;
cout<<"***********4.查询联系人*************"<<endl;
cout<<"***********5.排序联系人*************"<<endl;
cout<<"***********6.插入联系人*************"<<endl;
cout<<"***********7.删除联系人*************"<<endl;
cout<<"***********8.修改联系人*************"<<endl;
cout<<"***********0.显示联系人*************"<<endl; } void manage::input(){ //添加1
char name[20],number[20],addr[20]; cout<<"输入姓名:"<<endl; cin>>name;
cout<<"输入号码:"<<endl; cin>>number; cout<<"输入单位:"<<endl; cin>>addr;
tel[scount].instial(name,number,addr); scount++; display(); } void
manage::read() { //读入2 ifstream ifile; ifile.open("e:\\tel.txt"); for(int
i=0;!ifile.eof();i++){
ifile>>tel[i].getname()>>tel[i].getnumber()>>tel[i].getaddr(); scount++; }
ifile.close(); cout<<"读入成功"<<endl; display(); } void manage::save() { //保存3
ofstream ofile; ofile.open("e:\\tel1.txt"); for(int i=0;i<scount;i++) {
ofile<<"名字:"<<tel[i].getname()<<setw(10)<<"号码:"<<tel[i].getnumber()<<setw(10)<<"单位:"<<tel[i].getaddr()<<endl;
} ofile.close (); } void manage::search(){ //查询4 char name1[20]; char
name2[20]; int q=-1; cout<<"请输入要查询人姓名:"<<endl; cin>>name1; for(int
i=0;i<scount;i++) { strcpy_s(name2,tel[i].getname());
if(strcmp(name1,name2)==0){
cout<<"名字:"<<tel[i].getname()<<setw(10)<<"号码:"<<tel[i].getnumber()<<setw(10)<<"单位:"<<tel[i].getaddr()<<endl;
q=1; } } if(q!=1) {cout<<"该用户不存在!"<<endl;} } void manage::sort(){ //排序5
telephone var; for(int i=0;i<scount;i++){ for(int j=i+1;j<scount;j++){
if(strcmp(tel[j].getnumber(),tel[i].getnumber())<0){
var=tel[i];tel[i]=tel[j];tel[j]=var; } } } display(); } void manage::insert(){
//插入6 int h; char name[20],number[20],addr[20]; cout<<"请选择插入到第几行"<<endl;
cin>>h; cout<<"输入姓名:"<<endl; cin>>name; cout<<"输入号码:"<<endl; cin>>number;
cout<<"输入单位:"<<endl; cin>>addr; scount++; telephone var; for(int
i=scount-1;i>h-1;i--){ //关键排序 tel[i]=tel[i-1]; }
tel[h-1].instial(name,number,addr); display(); } void manage::Delete(){ //删除7
int index=0; cout<<"请输入要删除的号码数:"<<endl; cin>>index; for(int
i=index-1;i<scount;i++) { tel[i]=tel[i+1]; } scount=scount-1; } void
manage::change(){ //修改8 char name[20];int index=0; char number[20],addr[20];int
q=-1; cout<<"请输入要修改人姓名:"<<endl; cin>>name; for(int i=0;i<scount;i++) {
if(strcmp(name,tel[i].getname())==0){ index=i; q=1; } } if(q!=1) {
index=scount; scount++; cout<<"联系人不存在,自动创建新用户!"<<endl;
tel[index].setname(name); } cout<<"请输入电话:"<<endl; cin>>number;
tel[index].setnumber(number); cout<<"请输入地址:"<<endl; cin>>addr;
tel[index].setaddr(addr); cout<<"修改成功!"<<endl; display(); } void
manage::display(){ //显示0 for(int i=0;i<scount;i++) {
cout<<i+1<<setw(10)<<"名字:"<<tel[i].getname()<<setw(10)<<"号码:"<<tel[i].getnumber()<<setw(10)<<"单位:"<<tel[i].getaddr()<<endl;
} } int main() { manage manager; //读入选项 while(1) { int choice; menu();
cout<<"please input your choice:"; cin>>choice; switch(choice) { case
1:manager.input(); break; case 2:manager.read(); break; case
3:manager.save();break; case 4:manager.search();break; case 5:manager.sort();
break; case 6:manager.insert();break; case 7:manager.Delete();break; case
8:manager.change();break; case 0:manager.display(); break; default:break; } }
return 0; }