通訊錄管理
問題描述
撰寫一個簡單的通訊錄管理程式。通訊錄記錄有姓名,地址(省、市(縣)、街道),電話號碼,郵政編碼等四項。
基本要求
程式應提供的基本功能有:
1)添加:即增加一個人的記錄到通信錄中
2)顯示:即在螢屏上顯示所有通信錄中的人員資訊,應能分屏顯示。
3)存盤:即將通訊錄資訊保存在一個檔案中。
4)裝入:即將檔案中的資訊讀入程式。
5)查詢:可根據姓名查找某人的相關資訊,若找到顯示其姓名、地址、電話號碼和郵政編碼。
6)修改:可修改一個人的除姓名外其它資訊。
測驗資料
程式應輸入不少于10個人員的通訊錄資訊,應考慮到人員可以同名的情況。
實作提示
程式可用一個單向鏈表來管理人員資訊,每個人員的姓名,地址,電話號碼和郵政編碼用一個類Cperson來實作,作為鏈表的值指標指向這些Cperson類物件,通過鏈表的遍歷可以操作這些資料。
選做內容
為了加快資料定位查找的速度,采用常用優先的方法對鏈表的各個節點進行排序,即一旦操作了一個人員的資料,他的資料就將被呼叫到鏈表的鏈首。這樣經過有限次操作,經常查閱的人員的資訊就將排在鏈表的前端。雖然不能說鏈首的節點一定是最常用的,但常用的節點一定會排在較靠前的部分,鏈表查找時所要走的平均距離一定較短。
#include<iostream>
#include<string>
#include<cstdlib>
#include<iomanip>
#include<fstream>
using namespace std;
class Cperson{
public: void set() { cin>>name>>addr>>tel>>post; }
void sign(Cperson &s) {
s.name=name;
s.addr=addr;
s.tel=tel;
s.post=post; }
string getname()
{ return name; }
void revise() {
cout<<"請輸入要修改內容:"<<endl; cout<<"地址\t\t電話號碼\t郵政編碼"<<endl; cin>>addr>>tel>>post;
cout<<"修改成功"<<endl; }
void disp() { cout<<name<<"\t"<<addr<<"\t"<<tel<<"\t"<<post<<endl; }
private:
string name;
string addr;
int tel;
int post;};
struct List{Cperson mem;List *next;};
void memory(List *p)
{ List *t; Cperson s; ofstream output("address.txt",ios::binary);
if(!output)
{ cout<<"File cannot be opened."<<endl;
return; }
t=p->next;
while(t) {
nb=t->mem.getname();
if(na==nb)
{t->mem.revise();break;} t=t->next; }
if(t==NULL)
cout<<"找不到該聯系人"<<endl; cout<<"輸出結果:"<<endl;
cout<<"名字\t地址\t電話號碼\t郵政編碼"<<endl; t=p->next;
while(t)
{ t->mem.disp(); t=t->next; }
free(t);}
void dis(List *p)
{ memory(p);
Cperson s;
ifstream input("address.txt",ios::binary); if(!input) {
cout<<"File cannot be opened."<<endl; return; }
cout<<"通訊錄中所有聯系人的資訊:"<<endl; cout<<"名字\t地址\t電話號碼\t郵政編碼"<<endl; input.read((char*)&s,sizeof(Cperson));
while(input) {
s.disp(); input.read((char*)&s,sizeof(Cperson)); }
input.close();}
int main()
{List *per;
per=(struct List*)malloc(sizeof(struct List));
per->next=NULL;
int sel;
while(1){
cout<<"\n\n";
cout<<"\t\t**************通訊錄管理系統***************"<<endl;
cout<<endl;cout<<"\t\t1:添加聯系人到通訊錄 2:輸出所有聯系人資料"<<endl;
cout<<"\t\t3:按名字查詢資訊 4:修改已有聯系人的資訊"<<endl;
cout<<"\t\t 5:插入新的聯系人 6:退出系統"<<endl;cout<<"\t\t*******************************************"<<endl;
cout<<"請輸入你的選擇:";
cin>>sel;
switch(sel){
case 1: { set(per);break; }
case 2: { dis(per);break; }
case 3: { che(per);break; }
case 4: { rev(per);break; }
case 5: { insert(per);break; }
case 6:exit(1);}}List *t=per;while(per->next!=NULL){ t=per; per=t->next; free(t);}free(per);return 0;}
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/42899.html
標籤:Eclipse
