#include<iostream> #include<string> #include<fstream> #include<iomanip> using
namespace std; #define MAXPEOPLE 100 static int scount=0; class telephone {
char name[20]; // full name char number[12]; // Telephone number char addr[20]; // address 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() // Get name {
return name;// Array return type is realized by pointer }; char* getnumber() // Get a phone number { return number; };
char* getaddr(){ return addr; }; }; class manage{ telephone tel[MAXPEOPLE];
public: void input(); // add to 1 void read(); // Read in 2 void save(); // preservation 3 void
search(); // query 4 void sort(); // sort 5 void insert(); // insert 6 void Delete(); // delete 7
void change(); // modify 8 void display(); // display 0 }; void menu() // Define menu functions {
cout<<"***********1. Add a Contact *************"<<endl;
cout<<"***********2. Read in contact information *********"<<endl;
cout<<"***********3. Save contact information *********"<<endl;
cout<<"***********4. Query contact *************"<<endl;
cout<<"***********5. Sort contacts *************"<<endl;
cout<<"***********6. Insert contact *************"<<endl;
cout<<"***********7. Delete Contact *************"<<endl;
cout<<"***********8. Modify contact *************"<<endl;
cout<<"***********0. Show contacts *************"<<endl; } void manage::input(){ // add to 1
char name[20],number[20],addr[20]; cout<<" Enter name :"<<endl; cin>>name;
cout<<" enter number :"<<endl; cin>>number; cout<<" Input unit :"<<endl; cin>>addr;
tel[scount].instial(name,number,addr); scount++; display(); } void
manage::read() { // Read in 2 ifstream ifile; ifile.open("e:\\tel.txt"); for(int
ifile>>tel[i].getname()>>tel[i].getnumber()>>tel[i].getaddr(); scount++; }
ifile.close(); cout<<" Read in successfully "<<endl; display(); } void manage::save() { // preservation 3
ofstream ofile; ofile.open("e:\\tel1.txt"); for(int i=0;i<scount;i++) {
ofile<<" name :"<<tel[i].getname()<<setw(10)<<" number :"<<tel[i].getnumber()<<setw(10)<<" Company :"<<tel[i].getaddr()<<endl;
} ofile.close (); } void manage::search(){ // query 4 char name1[20]; char
name2[20]; int q=-1; cout<<" Please enter the name of the person you want to query :"<<endl; cin>>name1; for(int
i=0;i<scount;i++) { strcpy_s(name2,tel[i].getname());
cout<<" name :"<<tel[i].getname()<<setw(10)<<" number :"<<tel[i].getnumber()<<setw(10)<<" Company :"<<tel[i].getaddr()<<endl;
q=1; } } if(q!=1) {cout<<" The user does not exist !"<<endl;} } void manage::sort(){ // sort 5
telephone var; for(int i=0;i<scount;i++){ for(int j=i+1;j<scount;j++){
var=tel[i];tel[i]=tel[j];tel[j]=var; } } } display(); } void manage::insert(){
// insert 6 int h; char name[20],number[20],addr[20]; cout<<" Please select the line to insert "<<endl;
cin>>h; cout<<" Enter name :"<<endl; cin>>name; cout<<" enter number :"<<endl; cin>>number;
cout<<" Input unit :"<<endl; cin>>addr; scount++; telephone var; for(int
i=scount-1;i>h-1;i--){ // Key ranking tel[i]=tel[i-1]; }
tel[h-1].instial(name,number,addr); display(); } void manage::Delete(){ // delete 7
int index=0; cout<<" Please enter the number of numbers to delete :"<<endl; cin>>index; for(int
i=index-1;i<scount;i++) { tel[i]=tel[i+1]; } scount=scount-1; } void
manage::change(){ // modify 8 char name[20];int index=0; char number[20],addr[20];int
q=-1; cout<<" Please enter the name of the person to be modified :"<<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<<" Contact does not exist , Automatically create new users !"<<endl;
tel[index].setname(name); } cout<<" Please enter the phone number :"<<endl; cin>>number;
tel[index].setnumber(number); cout<<" Please enter the address :"<<endl; cin>>addr;
tel[index].setaddr(addr); cout<<" Modified successfully !"<<endl; display(); } void
manage::display(){ // display 0 for(int i=0;i<scount;i++) {
cout<<i+1<<setw(10)<<" name :"<<tel[i].getname()<<setw(10)<<" number :"<<tel[i].getnumber()<<setw(10)<<" Company :"<<tel[i].getaddr()<<endl;
} } int main() { manage manager; // Read in options 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; }