简单通讯录管理系统
C++版本一?
/* *@Author: STZG *@Language: C++ */ #include<iostream> #include <stdio.h> #include <stdlib.h> #include <string.h> #include<fstream> using namespace std; struct list {int id; /* 標識這個元素方便查找 */char name[100];char tele[20];char sex[10];int data; /* 鏈表中包含的元素 */int num;struct list *next; /* 指向下一個鏈表的指針 */ }; int all = 0; struct list *list_head = NULL; struct list *lists = NULL; struct list temp_list; int list_id = 0; void list_add(struct list **head, struct list *list){struct list *temp;if(NULL == *head){*head = list;(*head)->next = NULL;}else{temp = *head;while(temp){if(NULL == temp->next){temp->next = list;list->next = NULL;}temp = temp->next;}} } void out(struct list* obj){cout<<"姓名:"<<obj->name<<"\t"<<"性別:"<<obj->sex<<"\t"<<"聯系方式:"<<obj->tele<<endl; } void list_print(struct list **head){struct list *temp;temp = *head;if(temp==NULL){cout<<"通訊錄為空!"<<endl;return;}cout<<"通訊錄全部記錄如下:"<<endl;while(temp){out(temp);temp = temp->next;} } void list_fileprint(struct list **head,ofstream &outfile){struct list *temp;temp = *head;if(temp==NULL){outfile<<"通訊錄為空!"<<endl;return;}while(temp){outfile<<"姓名:"<<temp->name<<"\t"<<"性別:"<<temp->sex<<"\t"<<"聯系方式:"<<temp->tele<<endl;temp = temp->next;} } int list_query(struct list **head,int id_que){struct list *temp;temp = *head;if(NULL == temp){cout<<"通訊錄為空!"<<endl;return -1;}else{if(id_que== temp->id){out(temp);return temp->id;}while(temp->next){temp = temp->next;if(id_que == temp->id){out(temp);return temp->id;}}return -1;}return -1; } int list_namequery(struct list **head,char *name_que){struct list *temp;temp = *head;if(NULL == temp){cout<<"通訊錄為空!"<<endl;return -1;}else{if(strcmp(name_que,temp->name)==0 ){out(temp);return temp->id;}while(temp->next){temp = temp->next;if(strcmp(name_que,temp->name)==0){out(temp);return temp->id;}}return -1;}return -1; } int list_del(struct list **head, int id_del){struct list *temp, *p;temp = *head;if(NULL == temp){cout<<"通訊錄為空!"<<endl;return -1;}else{if(id_del== temp->id){*head = temp->next;return 0;}while(temp->next){p = temp;temp = temp->next;if(id_del == temp->id){p->next = temp->next;return 0;}}return 0;}return -1; } void err(){cout<<"----無效操作----"<<endl; } void memu(){cout<<"---請選擇操作---"<<endl;cout<<"1、添加記錄"<<endl;cout<<"2、查詢記錄"<<endl;cout<<"3、查看全部"<<endl;cout<<"4、刪除記錄"<<endl;cout<<"5、修改記錄"<<endl;cout<<"6、離線存儲"<<endl;cout<<"7、退出系統"<<endl;cout<<"----------------"<<endl; } void add(){all++;cout<<"正在新建ID:"<<++list_id<<"的記錄"<<endl;cout<<"請輸入姓名:"<<endl;cin>>lists[all].name;cout<<"請輸入性別:"<<endl;cin>>lists[all].sex;cout<<"請輸入聯系方式:"<<endl;cin>>lists[all].tele;lists[all].id = list_id;cout<<"-----存儲中-----"<<endl;list_add(&list_head, &lists[all]);cout<<"----存儲完成----"<<endl; } int allquery(){int op;cout<<"1、ID"<<endl;cout<<"2、姓名"<<endl;cin>>op;int result;switch(op){case 1:int id;cout<<"----請輸入ID----"<<endl;cin>>id;result=list_query(&list_head,id);break;case 2:char name[100];cout<<"---請輸入姓名---"<<endl;cin>>name;result=list_namequery(&list_head,name);break;default:err();break;}return result; } void query(){cout<<"-請選擇查詢方式-"<<endl;if(allquery()==-1){cout<<"----查無此人----"<<endl;} } void del(){cout<<"-請選擇刪除方式-"<<endl;int result=allquery();if(result==-1){cout<<"----查無此人----"<<endl;}else{cout<<"----正在刪除----"<<endl;list_del(&list_head,result);cout<<"----刪除完成----"<<endl;} } void print(){list_print(&list_head);cout<<"----查詢完成----"<<endl; } void updata(){cout<<"-請選擇更新方式-"<<endl;int result=allquery();if(result==-1){cout<<"----查無此人----"<<endl;}else{cout<<"--請輸入新信息--"<<endl;cout<<"請輸入姓名:"<<endl;cin>>lists[result].name;cout<<"請輸入性別:"<<endl;cin>>lists[result].sex;cout<<"請輸入聯系方式:"<<endl;cin>>lists[result].tele;cout<<"----更新完成----"<<endl;} } void save(){cout<<"----建立文件----"<<endl;ofstream outfile("list.dat");cout<<"----寫入文件----"<<endl;list_fileprint(&list_head,outfile);cout<<"----關閉文件----"<<endl;outfile.close();cout<<"----導出完成----"<<endl; } void exit(){cout<<"----退出系統----"<<endl;exit(0); } bool applymemory(){lists = (struct list*)malloc(sizeof(struct list) * 20);if(NULL == lists){printf("malloc error!\n");return 0;}return 1; } void init(){cout<<"---正在初始化---"<<endl;cout<<"---建立通訊錄---"<<endl;cout<<"----申請存儲----"<<endl;if(applymemory()){cout<<"--申請存儲成功--"<<endl;}else{exit();}cout<<"---初始化完成---"<<endl;cout<<"------歡迎------"<<endl; } int main() {init();while(1){memu();int op;cin>>op;switch(op){case 1:add();break;case 2:query();break;case 3:print();break;case 4:del();break;case 5:updata();break;case 6:save();break;case 7:exit();break;default:err();break;}}//cout << "Hello world!" << endl;return 0; }C++版本二
// ConsoleApplication.cpp : 此文件包含 "main" 函數。程序執行將在此處開始并結束。 ////#include "pch.h" #include <iostream> #include <stdio.h> #include <string>using namespace std;typedef struct Person { //定義聯系人信息結構體string name;string address;string telephone; }DataType;typedef struct Node { //定義鏈表節點結構體DataType data;struct Node *next; }Node;Node *getptr(Node *head, int position) //獲取鏈表指定位置元素 {Node *p = head;if (p == NULL || position == 0) //p==NULL表示鏈表為空{return head;}for (int i = 0; p != NULL && i < position; i++){p = p->next;}return p; }int getSize(Node *head) //獲取單鏈表中的節點個數 {int size = 0;Node *p = head;while (p != NULL){size++;p = p->next;}return size; }bool insert(Node **head, int position, DataType d) //在單鏈表指定位置插入元素 {//注意上面的形參一定要使用指針的指針//因為head本身存放的就是地址,只有這樣才能對存放head的實際地址進行操作if (position<0 || position>getSize(*head)){return false;}Node *node = (Node *)malloc(sizeof(Node));/*node->data = d;*/memcpy(&node->data, &d, sizeof(node->data));node->next = NULL;if (position == 0){node->next = *head;*head = node;return true;}Node *p = getptr(*head, position - 1);Node *r = p->next;node->next = r;p->next = node;return true; }bool erase(Node **head, int position) //在單鏈表指定位置刪除元素 {//注意上面的形參一定要使用指針的指針//因為head本身存放的就是地址,只有這樣才能對存放head的實際地址進行操作if (position < 0 || position >= getSize(*head)){return false;}Node *p = *head;if (position == 0){*head = (*head)->next;free(p);p = NULL;return true;}p = getptr(*head, position - 1);Node *q = p->next;p->next = q->next;free(p);q = NULL;return true; }void reverse(Node **head) //單鏈表的反轉函數 {Node *p = *head;Node *q = p->next;if (q == NULL){return;}Node *r = q->next;if (p == *head){p->next = NULL;}while (true){q->next = p;if (r == NULL){*head = q;break;}else{p = q;q = r;r = r->next;}} }void trave(Node *head, void(*fun)(DataType)) //單鏈表的遍歷函數 {Node *p = head;while (p != NULL){fun(p->data);p = p->next;} }void print(DataType d) //用于打印的函數,供其他函數調用 {cout << "姓名:" << d.name << endl;cout << "地址:" << d.address << endl;cout << "電話:" << d.telephone << endl; }void write(DataType *d) {cout << "姓名:";getline(cin, d->name);cout << "地址:";getline(cin, d->address);cout << "電話:";getline(cin, d->telephone);cout << d->name << endl;cout << d->address << endl;cout << d->telephone << endl;/*cout << "姓名:";cin >> d->name;cout << "地址:";cin >> d->address;cout << "電話:";cin >> d->telephone;*/ }int main() {cout << "歡迎使用浙江理工大學通訊錄管理系統" << endl;int index;Node *head = NULL;DataType temp;do {cout << "請選擇您需要進行的操作:" << endl;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 << "9.退出通訊錄" << endl;cout << "當前節點個數:" << getSize(head) << endl;cin >> index;getchar();switch (index){case 1:write(&temp);insert(&head, getSize(head), temp);cout << "聯系人創建成功!" << endl;break;case 2:break;case 3:break;case 4:break;case 5:trave(head, print);break;case 6:break;case 7:break;case 8:break;case 9:break;default:break;}cout << endl;} while (index != 9);cout << "謝謝使用!" << endl;return 0; }// 運行程序: Ctrl + F5 或調試 >“開始執行(不調試)”菜單 // 調試程序: F5 或調試 >“開始調試”菜單// 入門提示: // 1. 使用解決方案資源管理器窗口添加/管理文件 // 2. 使用團隊資源管理器窗口連接到源代碼管理 // 3. 使用輸出窗口查看生成輸出和其他消息 // 4. 使用錯誤列表窗口查看錯誤 // 5. 轉到“項目”>“添加新項”以創建新的代碼文件,或轉到“項目”>“添加現有項”以將現有代碼文件添加到項目 // 6. 將來,若要再次打開此項目,請轉到“文件”>“打開”>“項目”并選擇 .sln 文件C++版本三
類與對象版本
main.cpp
/* *@Author: STZG *@Language: C++ */ #include<iostream> #include <stdio.h> #include <stdlib.h> #include <string.h> #include<fstream> #include"DemoSystem.h" using namespace std;int main() {DemoSystem Demo;Demo.init();Demo.run();//cout << "Hello world!" << endl;return 0; }DemoSystem.cpp
#include "DemoSystem.h"DemoSystem::DemoSystem() {op=0;all = 0;list_head = NULL;lists = NULL;list_id = 0;//ctor }DemoSystem::~DemoSystem() {//dtor } void DemoSystem::list_add(struct list **head, struct list *list){struct list *temp;if(NULL == *head){*head = list;(*head)->next = NULL;}else{temp = *head;while(temp){if(NULL == temp->next){temp->next = list;list->next = NULL;}temp = temp->next;}} } void DemoSystem::out(struct list* obj){cout<<"姓名:"<<obj->name<<"\t"<<"性別:"<<obj->sex<<"\t"<<"聯系方式:"<<obj->tele<<endl; } void DemoSystem::list_print(struct list **head){struct list *temp;temp = *head;if(temp==NULL){cout<<"通訊錄為空!"<<endl;return;}cout<<"通訊錄全部記錄如下:"<<endl;while(temp){out(temp);temp = temp->next;} } void DemoSystem::list_fileprint(struct list **head,ofstream &outfile){struct list *temp;temp = *head;if(temp==NULL){outfile<<"通訊錄為空!"<<endl;return;}while(temp){outfile<<"姓名:"<<temp->name<<"\t"<<"性別:"<<temp->sex<<"\t"<<"聯系方式:"<<temp->tele<<endl;temp = temp->next;} } int DemoSystem::list_query(struct list **head,int id_que){struct list *temp;temp = *head;if(NULL == temp){cout<<"通訊錄為空!"<<endl;return -1;}else{if(id_que== temp->id){out(temp);return temp->id;}while(temp->next){temp = temp->next;if(id_que == temp->id){out(temp);return temp->id;}}return -1;}return -1; } int DemoSystem::list_namequery(struct list **head,char *name_que){struct list *temp;temp = *head;if(NULL == temp){cout<<"通訊錄為空!"<<endl;return -1;}else{if(strcmp(name_que,temp->name)==0 ){out(temp);return temp->id;}while(temp->next){temp = temp->next;if(strcmp(name_que,temp->name)==0){out(temp);return temp->id;}}return -1;}return -1; } int DemoSystem::list_del(struct list **head, int id_del){struct list *temp, *p;temp = *head;if(NULL == temp){cout<<"通訊錄為空!"<<endl;return -1;}else{if(id_del== temp->id){*head = temp->next;return 0;}while(temp->next){p = temp;temp = temp->next;if(id_del == temp->id){p->next = temp->next;return 0;}}return 0;}return -1; } void DemoSystem::err(){cout<<"----無效操作----"<<endl; } void DemoSystem::memu(){cout<<"---請選擇操作---"<<endl;cout<<"1、添加記錄"<<endl;cout<<"2、查詢記錄"<<endl;cout<<"3、查看全部"<<endl;cout<<"4、刪除記錄"<<endl;cout<<"5、修改記錄"<<endl;cout<<"6、離線存儲"<<endl;cout<<"7、退出系統"<<endl;cout<<"----------------"<<endl; } void DemoSystem::add(){all++;cout<<"正在新建ID:"<<++list_id<<"的記錄"<<endl;cout<<"請輸入姓名:"<<endl;cin>>lists[all].name;cout<<"請輸入性別:"<<endl;cin>>lists[all].sex;cout<<"請輸入聯系方式:"<<endl;cin>>lists[all].tele;lists[all].id = list_id;cout<<"-----存儲中-----"<<endl;list_add(&list_head, &lists[all]);cout<<"----存儲完成----"<<endl; } int DemoSystem::allquery(){int op;cout<<"1、ID"<<endl;cout<<"2、姓名"<<endl;cin>>op;int result;switch(op){case 1:int id;cout<<"----請輸入ID----"<<endl;cin>>id;result=list_query(&list_head,id);break;case 2:char name[100];cout<<"---請輸入姓名---"<<endl;cin>>name;result=list_namequery(&list_head,name);break;default:err();break;}return result; } void DemoSystem::query(){cout<<"-請選擇查詢方式-"<<endl;if(allquery()==-1){cout<<"----查無此人----"<<endl;} } void DemoSystem::del(){cout<<"-請選擇刪除方式-"<<endl;int result=allquery();if(result==-1){cout<<"----查無此人----"<<endl;}else{cout<<"----正在刪除----"<<endl;list_del(&list_head,result);cout<<"----刪除完成----"<<endl;} } void DemoSystem::print(){list_print(&list_head);cout<<"----查詢完成----"<<endl; } void DemoSystem::updata(){cout<<"-請選擇更新方式-"<<endl;int result=allquery();if(result==-1){cout<<"----查無此人----"<<endl;}else{cout<<"--請輸入新信息--"<<endl;cout<<"請輸入姓名:"<<endl;cin>>lists[result].name;cout<<"請輸入性別:"<<endl;cin>>lists[result].sex;cout<<"請輸入聯系方式:"<<endl;cin>>lists[result].tele;cout<<"----更新完成----"<<endl;} } void DemoSystem::save(){cout<<"----建立文件----"<<endl;ofstream outfile("list.dat");cout<<"----寫入文件----"<<endl;list_fileprint(&list_head,outfile);cout<<"----關閉文件----"<<endl;outfile.close();cout<<"----導出完成----"<<endl; } void DemoSystem::exit(){cout<<"----退出系統----"<<endl;std::exit(0); } bool DemoSystem::applymemory(){lists = (struct list*)malloc(sizeof(struct list) * 20);if(NULL == lists){printf("malloc error!\n");return 0;}return 1; } void DemoSystem::init(){cout<<"---正在初始化---"<<endl;cout<<"---建立通訊錄---"<<endl;cout<<"----申請存儲----"<<endl;if(applymemory()){cout<<"--申請存儲成功--"<<endl;}else{exit();}cout<<"---初始化完成---"<<endl;cout<<"------歡迎------"<<endl; } void DemoSystem::setop(int x){op=x; } int DemoSystem::getop(){return op; } void DemoSystem::run(){while(1){memu();cin>>op;switch(op){case 1:add();break;case 2:query();break;case 3:print();break;case 4:del();break;case 5:updata();break;case 6:save();break;case 7:exit();break;default:err();break;}} }DemoSystem.h
#ifndef DEMOSYSTEM_H #define DEMOSYSTEM_H #include<iostream> #include <stdio.h> #include <stdlib.h> #include <string.h> #include<fstream> using namespace std; struct list {int id; /* 標識這個元素方便查找 */char name[100];char tele[20];char sex[10];int data; /* 鏈表中包含的元素 */int num;struct list *next; /* 指向下一個鏈表的指針 */ }; class DemoSystem {public:DemoSystem();virtual ~DemoSystem();void list_add(struct list **head, struct list *list);void out(struct list* obj);void list_print(struct list **head);void list_fileprint(struct list **head,ofstream &outfile);int list_query(struct list **head,int id_que);int list_namequery(struct list **head,char *name_que);int list_del(struct list **head, int id_del);void err();void memu();void add();int allquery();void query();void del();void print();void updata();void save();void exit();bool applymemory();void init();int getop();void setop(int x);void run();protected:private:int op;int all;struct list *list_head;struct list *lists;struct list temp_list;int list_id;};#endif // DEMOSYSTEM_H?
總結
- 上一篇: Count
- 下一篇: Little Sub and Apple