C++ 通讯录学习总结
生活随笔
收集整理的這篇文章主要介紹了
C++ 通讯录学习总结
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
用c++寫的簡易學生通訊錄
http://blog.csdn.net/yuzhihui_no1/article/details/43530445 ?? ? ? ? 昨天我一大學室友找到我說他親戚要個學生通訊錄程序,一定要c++來寫,而他是學Java的,對c++的基本語法都忘干凈了。因為我是做c方面開發的,所以問我能不能搞定。雖然我也半年多沒用c++寫過東西了,但作為室友怎么能拒絕呢,再個看了下他的需求感覺挺簡單的(沒涉及到數據庫操作),于是就應承下來了。
? ? ? ? 他給我的需求是這樣的:
? ? ? ? 學生通訊錄系統
? ? ? ? 學生通信錄信息包括:姓名、學號、年齡、性別、家庭住址、聯系電話、寢室號等信息。
? ? ? ? 系統以菜單方式工作,使之能提供以下功能::
? ? ? ? 學生通信錄信息的輸入
? ? ? ? 學生的通信錄信息刪除和修改
? ? ? ? 學生的通信錄信息查詢和統計功能
? ? ? ? 學生的通信錄信息輸出顯示?
? ? ? ? 而且數據都保存在內存中;
? ? ? ? 我在Linux下用了半上午時間給他寫了個簡易的通訊錄程序,下面我把代碼貼出來,希望可以給那些大一大二想寫這個程序的同學一點思路。其中不規范的地方還望大家指出了(因為為了盡快完成功能,所以一些規范就沒太注意),謝謝!
[cpp] view plain copy
#include<iostream> ?
#include<cstdlib> //主要是用到exit()退出進程函數 ??
#include<string.h>//字符串頭文件 ?
??
#define NoFind -1 ?
#define NoOperation -2 ?
#define Fill ?-3 ?
#define Exist -4 ?
??
using namespace std; ?
??
class student ?
{ ?
? public: ?
? ? void printStudent(); // print a student information ?
??
? ? void setName(); ?
? ? string getName(); ?
? ? ??
? ? void setId(); ?
? ? unsigned int getId(); ?
??
? ? void setAge(); ?
? ? unsigned int getAge(); ?
??
? ? void setSex(); ?
? ? char getSex(); ??
??
? ? void setAddr(); ?
? ? string getAddr(); ?
??
? ? void setPhone(); ?
? ? string getPhone(); ?
??
? ? void setRoom(); ?
? ? string getRoom(); ?
??
? private: ?
? ? string name; ?
? ? unsigned int ?id; ?
? ? unsigned int ?age; ?
? ? char sex; ?
? ? string addr; ?
? ? string phone; ?
? ? string room; ?
}; ?
??
void student::setId() ?
{ ?
? cout<<"Id:"; ?
? cin>>id; ?
} ?
??
unsigned int student::getId() ?
{ ?
? return id; ?
} ?
??
void student::setName() ?
{ ?
? cout<<"Name:"; ?
? cin>>name; ?
} ?
??
string student::getName() ?
{ ?
? return name; ?
} ?
??
void student::setAge() ?
{ ?
? cout<<"Age:"; ?
? cin>>age; ?
} ?
??
unsigned int student::getAge() ?
{ ?
? return age; ?
} ?
??
void student::setSex() ?
{ ?
? cout<<"Sex:"; ?
? cin>>sex; ?
} ?
??
char student::getSex() ?
{ ?
? return sex; ?
} ?
??
void student::setAddr() ?
{ ?
? cout<<"Addr:"; ?
? cin>>addr; ?
} ?
??
string student::getAddr() ?
{ ?
? return addr; ?
} ?
??
void student::setPhone() ?
{ ?
? cout<<"Phone:"; ?
? cin>>phone; ?
} ?
??
string student::getPhone() ?
{ ?
? return phone; ?
} ?
??
void student::setRoom() ?
{ ?
? cout<<"Room:"; ?
? cin>>room; ?
} ?
??
string student::getRoom() ?
{ ??
? return room; ?
} ?
??
void student::printStudent() ?
{ ?
? cout<<"Id:"<<id<<endl; ?
? cout<<"Name:"<<name<<endl; ?
? cout<<"Age:"<<age<<endl; ?
? cout<<"Sex:"<<sex<<endl; ?
? cout<<"Addr:"<<addr<<endl; ?
? cout<<"Phone:"<<phone<<endl; ?
? cout<<"Room:"<<room<<endl; ?
? cout<<endl; ?
} ?
//上面都是學生類,以及類的屬性設置和獲取函數 ?
?
??
#define LEN 1024 ?
??
//定義一個結構體,保存類對象和數據有效性標志 ?
typedef struct Node{ ?
? ? ?student s; ?
? ? ?int flag;//如果為0 表示該結構體中的對象無效 ?
}Node; ?
??
static int studentNum = 0; ?
static Node buff[LEN] = {};//用來存放上面結構體對象的,一個對象表示一個同學的信息 ?
??
//在數組中得到一個空閑的元素,返回數組小標;是否空閑可以查看flag標志位 ?
int getArrayFree() ?
{ ?
? ? int i; ?
? ? for(i = 0; i < LEN; i++) ?
? ? { ?
? ? ? ? if(0 == buff[i].flag) ?
? ? ? ? ? ? return i; ?
? ? } ?
? ? return Fill; ?
} ?
??
//在數組中查找指定學生的信息,該函數被刪除和修改函數調用 ?
//用學號查詢和姓名查詢兩種方式,返回查找到的學生在數組中下標號 ?
int getArrayIndex() ?
{ ?
? ? unsigned int select; ?
? ? unsigned int id = 0; ?
? ? string name = ""; ?
??
? ? cout<<"~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~"<<endl; ?
? ? cout<<"select operation student way, id or name?"<<endl; ?
? ? cout<<"****************************"<<endl; ?
? ? cout<<" ? ? ? 1 ? Use ?id ? ? ? ? ?"<<endl; ?
? ? cout<<" ? ? ? 2 ? Use ?name ? ? ? ?"<<endl; ?
? ? cout<<" ? ? ? 3 ? Break ? ? ? ? ? ?"<<endl; ?
? ? cout<<" ? ? ? 4 ? Exit ? ? ? ? ? ? "<<endl; ?
? ? cout<<"****************************"<<endl; ?
? ? cin>>select; ?
??
? ? switch(select) ?
? ? { ?
? ? ? ? case 1: ?
? ? ? ? ? ? cout<<"please enter the student Id:"; ?
? ? ? ? ? ? cin>>id; ?
? ? ? ? ? ? break; ?
? ? ? ? case 2: ?
? ? ? ? ? ? cout<<"please enter the student Name:"; ?
? ? ? ? ? ? cin>>name; ?
? ? ? ? ? ? break; ?
? ? ? ? case 3: ?
? ? ? ? ? ? return NoOperation; ?
? ? ? ? case 4: ?
? ? ? ? ? ? cout<<"exit process!"<<endl;exit(0); ?
? ? ? ? default: ?
? ? ? ? ? ? cout<<"other select will go break!"<<endl; ?
? ? ? ? ? ? return NoOperation; ?
? ? } ?
??
? ? int i; ?
? ? for(i = 0; i < LEN; i++) ?
? ? { ?
? ? ? ? if(!(buff[i]).flag) continue; ?
? ? ? ? if(0 == id) ?
? ? ? ? { ?
? ? ? ? ? ? if(name == buff[i].s.getName()) ?
? ? ? ? ? ? ? ? return i; ?
? ? ? ? } ?
??
? ? ? ? if(id == buff[i].s.getId()) ?
? ? ? ? ? ? return i; ?
? ? } ?
? ? return NoFind; ?
} ?
??
//判斷該id是否存在,姓名可以相同,但學號一定不能相同 ?
int isExist(int id) ?
{ ?
? ? int i; ?
? ? int count = 0; ?
? ? for(i = 0; i < LEN; i++) ?
? ? { ?
? ? ? ? if(id == buff[i].s.getId()) ?
? ? ? ? ? ?count++; ?
? ? } ?
? ? return count; ?
} ?
??
//增加一個學生的信息到數組中,也即是通訊錄中增加一條通訊錄 ?
int addStudentInfo() ?
{ ?
? ? student newStd; ?
? ? string name; ?
? ? int i; ?
? ? int index; ?
? ? char yORn; ?
? ? ??
? ? cout<<endl; ?
? ? cout<<"-----------------------------------"<<endl; ? ?
? ? cout<<"addStudentInfo:"<<endl; ?
? ? newStd.setId(); ?
? ? newStd.setName(); ?
? ? newStd.setAge(); ?
? ? newStd.setSex(); ?
? ? newStd.setAddr(); ?
? ? newStd.setPhone(); ?
? ? newStd.setRoom(); ?
??
? ? cout<<endl; ?
? ? newStd.printStudent(); ?
??
? ? cout<<"Are you sure this information is correct?[y or N]"<<endl; ?
? ? cin>>yORn; ?
? ? if(!(('y' == yORn) || ('Y' == yORn))) ?
? ? ? ? return 0; ?
??
? ? if( -1 == (index = getArrayFree()) ) ?
? ? { ?
? ? ? ? cout<<"The contacts filled!"<<endl; ?
? ? ? ? return Fill; ?
? ? } ?
??
? ? if(isExist(newStd.getId())) ?
? ? { ?
? ? ? ? cout<<"The id is exist!"<<endl; ?
? ? ? ? return Exist; ?
? ? } ?
??
? ? buff[index].s = newStd; ?
? ? buff[index].flag = 1; ?
? ? studentNum++; ?
? ? cout<<endl; ?
? ? cout<<"Success"<<endl;cout<<endl; ?
? ? return 0; ?
??
} ?
??
//刪除指定學生的通訊錄信息,只要flag置0 ?
int delStudentInfo() ?
{ ?
? ? int index; ?
??
? ? index = getArrayIndex(); ?
??
? ? if(NoFind == index) ?
? ? { ?
? ? ? ? cout<<"No find the student!"<<endl; ?
? ? ? ? return 0; ?
? ? } ?
??
? ? if(NoOperation == index) return 0; ?
??
? ? buff[index].flag = 0; ?
? ? studentNum--; ?
??
? ? cout<<"--------------------------------------"<<endl; ?
? ? cout<<"Success"<<endl; ?
? ? return 0; ? ??
} ?
??
//修改指定學生的信息 ?
int updateStudentInfo() ?
{ ?
? ? int index; ?
? ? int select; ?
? ? int count = 2; ?
??
? ? cout<<endl; ?
? ? cout<<"---------------------------------"<<endl; ?
? ? cout<<"update the student information:"<<endl; ?
??
? ? index = getArrayIndex(); ?
??
? ? if(NoFind == index) ?
? ? { ?
? ? ? ? cout<<"No find the student!"<<endl; ?
? ? ? ? return NoFind; ?
? ? } ?
??
? ? if(NoOperation == index) return 0; ?
? ? ??
? ? while(1) ?
? ? { ?
? ? ? ? cout<<endl; ?
? ? ? ? cout<<"~~~~~~~~~~~~~~~~~~~~"<<endl; ?
? ? ? ? cout<<" ? 1 ?Id ? ? ? ? ? ?"<<endl; ?
? ? ? ? cout<<" ? 2 ?Name ? ? ? ? ?"<<endl; ?
? ? ? ? cout<<" ? 3 ?Age ? ? ? ? ? "<<endl; ?
? ? ? ? cout<<" ? 4 ?Sex ? ? ? ? ? "<<endl; ?
? ? ? ? cout<<" ? 5 ?Addr ? ? ? ? ?"<<endl; ?
? ? ? ? cout<<" ? 6 ?Phone ? ? ? ? "<<endl; ?
? ? ? ? cout<<" ? 7 ?Room ? ? ? ? ?"<<endl; ?
? ? ? ? cout<<" ? 8 ?Break ? ? ? ? "<<endl; ?
? ? ? ? cout<<" ? 9 ?Exit ? ? ? ? ?"<<endl; ?
? ? ? ? cout<<"~~~~~~~~~~~~~~~~~~~~"<<endl; ?
? ? ? ? cout<<endl; ?
? ? ? ? cout<<"please select update informaton:"<<endl; ?
? ? ? ? cin>>select; ?
??
? ? ? ? switch(select) ?
? ? ? ? { ?
? ? ? ? ? ? case 1://下面循環要判斷id(學號)是否重合,如果重合就再選擇一個學號,直到沒有重合的 ?
? ? ? ? ? ? ? ? while((count-1)) ?
? ? ? ? ? ? ? ? { ?
? ? ? ? ? ? ? ? ? ? buff[index].s.setId(); ?
? ? ? ? ? ? ? ? ? ? count = isExist(buff[index].s.getId()); ?
? ? ? ? ? ? ? ? ? ? if(count >= 2) cout<<"id is exist!"<<endl; ?
? ? ? ? ? ? ? ? } ?
? ? ? ? ? ? ? ? break; ?
??
? ? ? ? ? ? case 2: buff[index].s.setName();break; ?
? ? ? ? ? ? case 3: buff[index].s.setAge();break; ?
? ? ? ? ? ? case 4: buff[index].s.setSex();break; ?
? ? ? ? ? ? case 5: buff[index].s.setAddr();break; ?
? ? ? ? ? ? case 6: buff[index].s.setPhone();break; ?
? ? ? ? ? ? case 7: buff[index].s.setRoom();break; ?
? ? ? ? ? ? case 8: return NoOperation; ?
? ? ? ? ? ? case 9: cout<<"exit process!"<<endl;exit(0); ?
? ? ? ? ? ? default:return NoOperation; ?
? ? ? ? } ?
? ? } ?
? ? return 0; ?
} ?
??
//統計通訊錄中有多少個學生 ?
int accoutStudent() // accout Student number ?
{ ?
? ?cout<<endl; ?
? ?cout<<"---------------------------------"<<endl; ?
? ?cout<<"student number:"<<studentNum<<endl; ?
? ?return 0; ?
} ?
??
//打印指定學生信息 ?
void printStudentInfo() ?
{ ?
? ? int index; ?
? ? ??
? ? index = getArrayIndex(); ?
??
? ? if(NoFind == index) ?
? ? { ?
? ? ? ? cout<<"No find the student!"<<endl; ?
? ? ? ? return; ?
? ? } ?
??
? ? if(NoOperation == index) return; ?
??
? ? cout<<endl; ?
? ? cout<<"---------------------------------"<<endl; ?
? ? buff[index].s.printStudent(); ?
??
? ? return; ?
? ? ??
} ?
??
//打印所有學生的信息 ?
void showAllStudentInfo() ?
{ ?
? ? int i; ?
? ? cout<<endl; ?
? ? cout<<"show all stduent information:"<<endl; ?
? ? for(i = 0; i < LEN; i++) ?
? ? { ?
? ? ? ? if(1 == buff[i].flag) ?
? ? ? ? ? ? buff[i].s.printStudent(); ?
? ? } ?
? ? return; ?
} ?
??
//根據菜單選擇調用對應函數 ?
void select(int number) ?
{ ?
? ?switch(number) ?
? ?{ ?
? ? ?case 1: ?
? ? ? ? ? ?addStudentInfo();break; ?
? ? ?case 2: ?
? ? ? ? ? ?delStudentInfo();break; ?
? ? ?case 3: ?
? ? ? ? ? ?updateStudentInfo();break; ?
? ? ?case 4: ?
? ? ? ? ? ?accoutStudent();break; ?
? ? ?case 5: ?
? ? ? ? ? ?printStudentInfo();break; ?
? ? ?case 6: ?
? ? ? ? ? ?showAllStudentInfo();break; ?
? ? ?default: ?
? ? ? ? ? ?cout<<"error"<<endl;return; ?
? ?} ? ?
} ?
??
//選擇菜單函數 ?
void menu() ?
{ ?
? ? unsigned int number = 7; ?
? ? while(1) ?
? ? ?{ ? ? ? ?
? ? ? ? cout<<endl; ?
? ? ? ? cout<<"**********************************"<<endl; ?
? ? ? ? cout<<" ? ?1 Add student information" ? ? <<endl; ?
? ? ? ? cout<<" ? ?2 Del student information" ? ? <<endl; ?
? ? ? ? cout<<" ? ?3 Update student information" ?<<endl; ?
? ? ? ? cout<<" ? ?4 Accout student number" ? ? ? <<endl; ?
? ? ? ? cout<<" ? ?5 Printf a student information"<<endl; ?
? ? ? ? cout<<" ? ?6 Show all student information"<<endl; ?
? ? ? ? cout<<" ? ?7 Exit ? ? ? ? ? ? ? ? ? ? ? ?"<<endl; ?
? ? ? ? cout<<"**********************************"<<endl; ?
? ? ? ? cout<<endl; ?
? ? ? ? cout<<"please enter the number:<1~7>"<<endl; ?
??
? ? ? ? cin>>number; ?
? ? ? ?
? ? ? ? if(7 == number) ??
? ? ? ? ? return; ? ??
? ? ? ? if((1 <= number)&&(7 > number)) ?
? ? ? ? ? ? select(number); ?
? ? ? ? sleep(1); ?
? ? ?} ?
} ?
??
??
int main(int argc, char **argv) ?
{ ?
? ?menu(); ?
? ?return 0; ?
} ?
? ? ? ? 程序基本就是這樣的,在Linux系統上測試通過,沒問題。在其他系統上應該沒有大問題,如果有的話就是頭文件的問題(聽室友說在mac上sleep()函數是沒有的,Windows下要添加一個頭文件,具體什么頭文件需要的可以百度下);
? ? ? ? 程序還有個問題,就是在menu()函數中輸入值給number時,如果你輸入字符就會出現死循環(這個死循環不是因為while(1)造成的,如果正常死循環,每循環一次就會等待用戶輸入一個值),在我預計中不會出現這個問題的,因為輸入字符也是轉化成ascii碼,然后也會被剔除的。可惜,不是這樣,我打印了下number(當輸入字符A時),結果卻是0,而不是64;還有當輸入學生信息時,如果在id輸入時,不小心輸入名字時(其實就是字符串)也會出現未知的錯誤。查了資料說是c++中類型不能混用,本應該要有防止這種失誤操作的處理方法,但我實驗了下,沒成功,如果誰有好的辦法希望可以告訴我一聲,共同學習嘛。謝謝!
? ? ? ? ?轉載請注明作者和原文出處,原文地址:http://blog.csdn.net/yuzhihui_no1/article/details/43530445
========
C++ 通訊錄實現
http://blog.csdn.net/ky_heart/article/details/55045996昨晚實現了用C++編寫通訊錄,深刻的感受到了封裝的便利性啊,vector真是太方便了!!!
代碼如下:
info.h
[cpp] view plain copy print?
#ifndef _PERSON_H_ ?
#define _PERSON_H_ ?
??
#include <iostream> ?
#include <vector> ?
#include <string> ?
using namespace std; ?
??
class Info ?
{ ?
private: ?
? ? int id; ?
? ? string name; ?
? ? string tel; ?
? ? string addr; ?
public: ?
? ? Info(); ?
? ? ~Info(); ?
? ? static int count; //記錄通訊錄中的人數 ?
? ? int GetId(); ?
? ? void SetName(); ?
? ? string GetName() const; ?
? ? void SetTel(); ?
? ? string GetTel() const; ?
? ? void SetAddr(); ?
? ? string GetAddr() const; ?
? ? void choose(); ?
? ? void insert(); ?
? ? void show(); ?
? ? void search(); ?
? ? void interface(); ?
? ? void delete_info(); ?
? ? void exit_info(); ?
? ? void modify(); ?
}; ?
??
#endif ?
info.cpp
[cpp] view plain copy print?
#include "info.h" ?
??
??
vector<Info> per; ?
int Info::count = 0; ?
??
??
int Info::GetId() ?
{ ?
? ? return id; ?
} ?
??
??
void Info::SetName() ?
{ ?
? ? cout << "姓名:"; ??
? ? cin >> name; ?
} ?
string Info::GetName() const ?
{ ?
? ? string tmp = name; ?
? ? return tmp; ?
} ?
??
??
void Info::SetTel() ?
{ ?
? ? cout << "電話:"; ?
? ? cin >> tel; ?
} ?
string Info::GetTel() const ?
{ ?
? ? string tmp = tel; ?
? ? return tmp; ?
} ?
??
??
void Info::SetAddr() ?
{ ?
? ? cout << "地址:"; ?
? ? cin >> addr; ?
} ?
string Info::GetAddr() const ?
{ ?
? ? string tmp = addr; ?
? ? return tmp; ?
} ?
??
??
Info::Info() ?
{ ?
??
??
} ?
??
??
Info::~Info() ?
{ ?
??
??
} ?
??
??
void Info::insert() ?
{ ?
? ? Info tmp; ?
? ? vector<Info>::iterator it; ?
loop: ?
? ? count++; ?
? ? cout << "ID: " << count << endl; ?
? ? tmp.SetName(); ?
? ? for(it = per.begin(); it != per.end(); ++it) ?
? ? { ?
? ? ? ? if(!((it->GetName()).compare(tmp.GetName()))) ?
{ ?
? ?cout << "與已有聯系人重名,請重新輸入。" << endl; ?
? ?count--; ?
? ?goto loop; ?
} ?
? ? } ?
? ? tmp.SetTel(); ?
? ? tmp.SetAddr(); ?
? ? tmp.id = count; ?
? ? ??
? ? per.push_back(tmp); ?
??
??
? ? cout << "是否繼續添加聯系人 y/n :"; ?
? ? char ch; ?
? ? cin >> ch; ?
? ? if('y' == ch || 'Y' == ch) ?
? ? { ?
? ? ? ? goto loop; ?
? ? } ?
? ? ?
} ?
??
??
void Info::show() ?
{ ?
? ? vector<Info>::iterator it; ?
? ? if(per.empty()) ?
? ? { ?
? ? ? ? cout << "通訊錄暫無聯系人!" << endl; ?
? ? } ?
? ? else ?
? ? { ?
? ? ? ? for(it = per.begin(); it != per.end(); ++it) ?
{ ?
? ?cout << "ID: " << it->GetId() << endl; ?
? ?cout << "姓名:" << it->GetName() << endl; ?
? ?cout << "電話:" << it->GetTel() << endl; ?
? ?cout << "地址:" << it->GetAddr() << endl; ?
} ?
cout << "請按任意鍵退出" << endl; ?
char ch; ?
cin >> ch; ?
? ? } ?
} ?
??
??
void Info::search() ?
{ ?
? ? vector<Info>::iterator it; ?
? ? if(per.empty()) ?
? ? { ?
? ? ? ? cout << "通訊錄暫無聯系人!" << endl; ?
? ? } ?
? ? else ?
? ? { ?
? ? search_loop: ?
int tp = ?0; //查詢方式選擇位 ?
int num = 0; //查找的ID ?
string tn; //查找的姓名 ?
int flag = 0; //查找成功與否標志位 ?
? ? ? ? cout << "查找方式:1.ID 2.姓名" << endl; ?
? ? ? ? cin >> tp; ?
? ? ? ? if(1 == tp) ?
{ ?
? ?cout << "請輸入查找的ID:"; ?
? ?cin >> num; ?
? ? ? ? ? ? for(it = per.begin(); it != per.end(); ++it) ?
? ?{ ?
? ? ? ?if(it->GetId() == num) ?
{ ?
? ? ? ? ? ? ? ? ? ? flag = 1; ?
? ?cout << "你要找的聯系人為:" <<endl; ?
? ? ? ? ? ?cout << "ID: " << it->GetId() << endl; ?
? ? ? ? ? ?cout << "姓名:" << it->GetName() << endl; ?
? ? ? ? ? ?cout << "電話:" << it->GetTel() << endl; ?
? ? ? ? ? ?cout << "地址:" << it->GetAddr() << endl; ?
} ?
? ?} ?
} ?
else if(2 == tp) ?
{ ?
? ?cout << "請輸入查找的姓名:"; ?
? ? ? ? ? ? cin >> tn; ?
? ? ? ? ? ? for(it = per.begin(); it != per.end(); ++it) ?
? ?{ ?
? ? ? ?if(!((it->GetName()).compare(tn))) ?
{ ?
? ? ? ? ? ? ? ? ? ? flag = 1; ?
? ?cout << "你要找的聯系人為:" <<endl; ?
? ? ? ? ? ?cout << "ID: " << it->GetId() << endl; ?
? ? ? ? ? ?cout << "姓名:" << it->GetName() << endl; ?
? ? ? ? ? ?cout << "電話:" << it->GetTel() << endl; ?
? ? ? ? ? ?cout << "地址:" << it->GetAddr() << endl; ?
} ?
? ?} ?
} ?
else ?
{ ?
? ?cout << "查找方式選擇錯誤,請重新選擇。" << endl; ?
? ?goto search_loop; ?
} ?
??
??
if(0 == flag) ?
{ ?
? ?cout << "無找到此聯系人" << endl; ?
} ?
else ?
{ ?
? ?cout << "查找成功" ?<< endl; ?
} ?
cout << "請按任意鍵退出" << endl; ?
char ch; ?
cin >> ch; ?
? ? } ?
? ? ??
} ?
??
??
void Info::delete_info() ?
{ ?
? ? vector<Info>::iterator it; ?
? ? if(per.empty()) ?
? ? { ?
? ? ? ? cout << "通訊錄暫無聯系人!" << endl; ?
? ? } ?
? ? else ?
? ? { ?
? ? delete_loop: ?
int tp = ?0; //刪除方式選擇位 ?
int num = 0; //刪除的ID ?
string tn; //刪除的姓名 ?
int flag = 0; //刪除成功與否標志位 ?
? ? ? ? cout << "刪除方式:1.ID 2.姓名" << endl; ?
? ? ? ? cin >> tp; ?
? ? ? ? if(1 == tp) ?
{ ?
? ?cout << "請輸入刪除的ID:"; ?
? ?cin >> num; ?
? ? ? ? ? ? for(it = per.begin(); it != per.end();) ?
? ?{ ?
? ? ? ?if(it->GetId() == num) ?
{ ?
? ? ? ? ? ? ? ? ? ? flag = 1; ?
? ?cout << "你要刪除的聯系人為:" <<endl; ?
? ? ? ? ? ?cout << "ID: " << it->GetId() << endl; ?
? ? ? ? ? ?cout << "姓名:" << it->GetName() << endl; ?
? ? ? ? ? ?cout << "電話:" << it->GetTel() << endl; ?
? ? ? ? ? ?cout << "地址:" << it->GetAddr() << endl; ?
? ? ? ? ? ? ?
? ?cout << "確定刪除此聯系人嗎?y/n : "; ?
? ? ? ? ? ?char ch1; ?
? ? ? ? ? ?cin >> ch1; ?
? ? ? ? ? ?if (ch1 == 'y' || ch1 == 'Y') ?
? ? ? ? ? ?{ ?
? ? ? ? ? ? ? ?it = per.erase(it); ?
? ? ? ? ? ?} ?
} ?
else ?
{ ?
? ?++it; ?
} ?
? ?} ?
} ?
else if(2 == tp) ?
{ ?
? ?cout << "請輸入刪除的姓名:"; ?
? ? ? ? ? ? cin >> tn; ?
? ? ? ? ? ? for(it = per.begin(); it != per.end(); ) ?
? ?{ ?
? ? ? ?if(!((it->GetName()).compare(tn))) ?
{ ?
? ? ? ? ? ? ? ? ? ? flag = 1; ?
? ?cout << "你要刪除的聯系人為:" <<endl; ?
? ? ? ? ? ?cout << "ID: " << it->GetId() << endl; ?
? ? ? ? ? ?cout << "姓名:" << it->GetName() << endl; ?
? ? ? ? ? ?cout << "電話:" << it->GetTel() << endl; ?
? ? ? ? ? ?cout << "地址:" << it->GetAddr() << endl; ?
? ? ?
? ?cout << "確定刪除此聯系人嗎?y/n : "; ?
? ? ? ? ? ?char ch1; ?
? ? ? ? ? ?cin >> ch1; ?
? ? ? ? ? ?if (ch1 == 'y' || ch1 == 'Y') ?
? ? ? ? ? ?{ ?
? ? ? ? ? ? ? ?it = per.erase(it); ?
? ? ? ? ? ?} ?
} ?
else ?
{ ?
? ?++it; ?
} ?
? ?} ?
} ?
else ?
{ ?
? ?cout << "刪除方式選擇錯誤,請重新選擇。" << endl; ?
? ?goto delete_loop; ?
} ?
??
??
if(0 == flag) ?
{ ?
? ?cout << "沒有找到此聯系人" << endl; ?
? ? ? ? } ?
else ?
{ ?
? ?cout << "刪除成功" << endl; ?
} ?
cout << "請按任意鍵退出" << endl; ?
char ch; ?
cin >> ch; ?
? ? } ?
? ? ??
} ?
void Info::modify() ?
{ ?
? ? vector<Info>::iterator it; ?
? ? if(per.empty()) ?
? ? { ?
? ? ? ? cout << "通訊錄暫無聯系人!" << endl; ?
? ? } ?
? ? else ?
? ? { ?
? ? modify_loop: ?
? ? ? ? string tn; ?
int flag2 = 0; ?
int flag = 0; //修改對象查找成功與否標志位 ?
? ? ? ? cout << "請輸入你要編輯的人的姓名:" ; ?
? ? ? ? cin >> tn; ?
? ? ? ? for(it = per.begin(); it != per.end(); ) ?
{ ?
? ?if(!((it->GetName()).compare(tn))) ?
? ? ? ? ? ? { ?
? ? ? ?flag = 1; ?
cout << "你要修改的聯系人為:" <<endl; ?
? ? ? ?cout << "ID: " << it->GetId() << endl; ?
? ? ? ?cout << "姓名:" << it->GetName() << endl; ?
? ? ? ?cout << "電話:" << it->GetTel() << endl; ?
? ? ? ?cout << "地址:" << it->GetAddr() << endl; ?
? ? ?
cout << "確定修改此聯系人嗎?y/n : "; ?
? ? ? ?char ch1; ?
? ? ? ?cin >> ch1; ?
char ch2; ?
? ? ? ?if (ch1 == 'y' || ch1 == 'Y') ?
? ? ? ?{ ?
? ? ? ? ? ?cout << "你要修改的是:1.姓名 2.電話 3.地址:"; ?
? ?cin >> ch2; ?
? ? ? ? ? ? ? ? ? ? switch(ch2) ?
? ?{ ?
? ? ? ?case '1': ?
{ ?
? ? ? ? ? ? ? ? ? ? ? ? ? ? it->SetName(); ?
? ?cout << "修改成功!該聯系人信息改為:" << endl; ?
? ? ? ? ? ? ? ? ? ?cout << "ID: " << it->GetId() << endl; ?
? ? ? ? ? ? ? ? ? ?cout << "姓名:" << it->GetName() << endl; ?
? ? ? ? ? ? ? ? ? ?cout << "電話:" << it->GetTel() << endl; ?
? ? ? ? ? ? ? ? ? ?cout << "地址:" << it->GetAddr() << endl; ?
? ?flag2 = 1; ?
? ?break; ?
} ?
case '2': ?
{ ?
? ?it->SetTel(); ?
? ?cout << "修改成功!該聯系人信息改為:" << endl; ?
? ? ? ? ? ? ? ? ? ?cout << "ID: " << it->GetId() << endl; ?
? ? ? ? ? ? ? ? ? ?cout << "姓名:" << it->GetName() << endl; ?
? ? ? ? ? ? ? ? ? ?cout << "電話:" << it->GetTel() << endl; ?
? ? ? ? ? ? ? ? ? ?cout << "地址:" << it->GetAddr() << endl; ?
? ?flag2 = 1; ?
? ?break; ?
} ?
case '3': ?
{ ?
? ?it->SetAddr(); ?
? ?cout << "修改成功!該聯系人信息改為:" << endl; ?
? ? ? ? ? ? ? ? ? ?cout << "ID: " << it->GetId() << endl; ?
? ? ? ? ? ? ? ? ? ?cout << "姓名:" << it->GetName() << endl; ?
? ? ? ? ? ? ? ? ? ?cout << "電話:" << it->GetTel() << endl; ?
? ? ? ? ? ? ? ? ? ?cout << "地址:" << it->GetAddr() << endl; ?
? ?flag2 = 1; ?
? ?break; ?
} ?
default : ?
{ ?
? ?cout << "指令輸入錯誤!" << endl; ?
? ?break; ?
} ?
? ?} ?
? ? ? ?} ?
else ?
{ ?
? ?break; ?
} ?
? ? ? ? ? ? } ?
? ? ? ? ? ? else ?
? ?{ ?
++it; ?
? ? ? ? ? ? } ?
} ?
if(0 == flag) ?
{ ?
? ?cout << "沒有找到此聯系人" << endl; ?
} ?
if(1 == flag2) ?
{ ?
? ?cout << "修改成功!" << endl; ?
} ? ? ?
cout << "請按任意鍵退出" << endl; ?
char ch3; ?
cin >> ch3; ?
? ? } ? ? ?
} ?
??
??
??
??
void Info::exit_info() ?
{ ?
? ? cout << "確定退出此通訊錄嗎?y/n: " ; ?
? ? char ch1; ?
? ? cin >> ch1; ?
? ? if (ch1 == 'y' || ch1 == 'Y') ?
? ? { ?
? ? ? ? exit(1); ?
? ? } ?
??
??
} ?
??
??
void Info::choose() ?
{ ?
? ? char action; ?
? ? Info tmp; ?
? ? cout << "請輸入你要實現的功能(0-4):" ; ?
? ? cin >> action; ?
??
??
? ? switch(action) ?
? ? { ?
? ? ? ? case '1': ?
{ ?
? ?tmp.insert(); ?
? ?tmp.interface(); ?
? ? ? ? ? ? choose(); ?
? ?break; ??
} ?
case '2': ?
{ ?
? ?tmp.show(); ?
? ?tmp.interface(); ?
? ?choose(); ?
? ?break; ?
} ?
case '3': ?
{ ?
? ? ? ? ? ? tmp.delete_info(); ?
? ?tmp.interface(); ?
? ?choose(); ?
} ?
case '4': ?
{ ?
? ?tmp.search(); ?
? ?tmp.interface(); ?
? ?choose(); ?
? ?break; ?
} ?
case '5': ?
{ ?
? ?tmp.modify(); ?
? ?tmp.interface(); ?
? ?choose(); ?
? ?break; ?
} ?
case '6': ?
{ ?
? ?tmp.exit_info(); ?
? ?break; ?
} ?
default: ?
{ ?
? ?cout << "輸入指令有誤,請重新輸入!" << endl; ?
? ?choose(); ?
? ?break; ?
} ?
? ? } ?
} ?
??
??
??
??
void Info::interface() ?
{ ?
? ? system("clear"); ?
??
??
? ? printf("\n"); ?
? ? printf("\n"); ?
? ? printf(" ? ? ? ? ?|***********************************************|\n"); ?
? ? printf(" ? ? ? ? ?|* ? ? ? ? ?多 功 能 電 子 通 訊 錄 ? ? ? ? ? ?*|\n"); ?
? ? printf(" ? ? ? ? ?|* ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? *|\n"); ?
? ? printf(" ? ? ? ? ?|* ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?版本號:V_1.0.0*|\n"); ?
? ? printf(" ? ? ? ? ?|***********************************************|\n"); ?
? ? printf(" ? ? ? ? ?|* ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? *|\n"); ?
? ? printf(" ? ? ? ? ?|* ? ? ?功能選擇: ? 1. 添加好友信息 ? ? ? ? ? ?*|\n"); ?
? ? printf(" ? ? ? ? ?|* ? ? ? ? ? ? ? ? ?2. 查看好友信息 ? ? ? ? ? ?*|\n"); ?
? ? printf(" ? ? ? ? ?|* ? ? ? ? ? ? ? ? ?3. 刪除好友信息 ? ? ? ? ? ?*|\n"); ?
? ? printf(" ? ? ? ? ?|* ? ? ? ? ? ? ? ? ?4. 搜索好友信息 ? ? ? ? ? ?*|\n"); ?
? ? printf(" ? ? ? ? ?|* ? ? ? ? ? ? ? ? ?5. 修改好友信息 ? ? ? ? ? ?*|\n"); ?
? ? printf(" ? ? ? ? ?|***********************************************|\n"); ?
? ? printf(" ? ? ? ? ?|* ? ? ?請輸入你想要實現的功能: ? ? ? ? ? ? ? *|\n"); ?
? ? printf(" ? ? ? ? ?|* ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? *|\n"); ?
? ? printf(" ? ? ? ? ?|* ? ?1添加 2查看 3刪除 4搜索 5修改 6退出 ? ? ?*|\n"); ?
? ? printf(" ? ? ? ? ?|***********************************************|\n"); ?
??
??
} ?
main.cpp
[cpp] view plain copy print?
#include "info.h" ?
??
??
int main() ?
{ ?
? ? Info s; ?
? ? s.interface(); ?
? ? s.choose(); ?
??
??
? ? return 0; ?
}?
========
C++通訊錄
http://www.cnblogs.com/orangebook/p/3514452.htmlC++通訊錄1.0
歷時一天,終于把通訊錄寫好了。
項目要求:
復制代碼
編寫一個通訊錄管理程序。
有一已存在的通訊錄文件,數據內容為各聯系人信息。
每個聯系人信息的組成部分為:
? ? 姓名、電話號碼和住址
? ? ? ? ? ? ? ? ? ? ? ? ? ? 等個人基本信息,
? ? ? ? ? ? ? ? ? ? ? ? ? ? 并假設已有兩個聯系人。
? ? ? ? ? ? ? ? ? ? ? ? ? ? 并假設已有兩個聯系人。
(1)輸出聯系人:打開通訊錄文件并顯示其中的數據;
(2)添加聯系人;
(3)查找聯系人:利用字符串函數,按“姓名”查找;
(4)修改聯系人:可以修改該聯系人的任一個信息;
(5)保存到文件:將操作結果保存到已存在的通訊錄文件;
(6)用子函數實現各個子功能。
復制代碼
?
通訊錄的 ? ? ? ?
核心類:VAdressBook
數據庫:SQLite
編程語言:C++
? ? ? ? ? ? ? ? ?常用函數:sprintf
? ? ? ? ? ? ? ? ?常用SQLiteAPI函數:sqlite3_exec
還存在技術問題:重命問題(在翻譯完SQLite高級教程后可解決)
? ? ? ? ? ? ? ? 未使用UI(計劃使用wxWidgets或Java的圖形庫 或 SDL)
還存在的程序設計問題:使用了簡單工廠設計模式,擴展性不佳,維護性不佳。(重構代碼)
現在的皺形效果圖為:
復制代碼
? 1 #include"sqlite3.h"
? 2 #include<cstdlib>
? 3 #include<cstdio>
? 4 #include<iostream>
? 5 #include <cstdio>
? 6?
? 7 using namespace std;
? 8 /*
? 9 格式化輸出命令
?10 sqlite>.header on
?11 sqlite>.mode column
?12 sqlite>.timer on
?13 */
?14?
?15 static int callback(void *data, int argc, char **argv, char **azColName){
?16 ? ?int i;
?17 ? ?for(i=0; i<argc; i++){
?18 ? ? ? printf("%s = %s\n", azColName[i], argv[i] ? argv[i] : "NULL");
?19 ? ?}
?20 ? ?printf("\n");
?21 ? ?return 0;
?22 }
?23?
?24 class VAdressBook
?25 {
?26 ? ? public:
?27 ? ? ? ? virtual bool Display_ContactPerson()=0;
?28 ? ? ? ? virtual bool Add_ContactPerson()=0;
?29 ? ? ? ? virtual bool Find_ContactPerson()=0;
?30 ? ? ? ? virtual bool Change_ContactPerson()=0;
?31 ? ? ? ? //virtual bool SaveToText_ContactPerson()=0;
?32 ? ? ? ? //virtual VAdressBook(){};
?33 };
?34?
?35 class AdressBook :public VAdressBook
?36 {
?37 ? ? private:
?38 ? ? ? ? sqlite3 *db;
?39 ? ? ? ? int rc;
?40 ? ? ? ? char *ErrorMsg;
?41 ? ? ? ? string sql;
?42 ? ? ? ? string m_strName;
?43 ? ? ? ? string m_strAdress;
?44 ? ? ? ? int m_iTelNum;
?45?
?46 ? ? public:
?47 ? ? ? ? AdressBook();
?48 ? ? ? ? bool Display_ContactPerson();
?49 ? ? ? ? bool Add_ContactPerson();
?50 ? ? ? ? bool Find_ContactPerson();
?51 ? ? ? ? bool Change_ContactPerson();
?52 ? ? ? ? //bool SaveToText_ContactPerson();
?53 ? ? ? ? virtual ~AdressBook()
?54 ? ? ? ? {
?55 ? ? ? ? ? ? sqlite3_close(db);
?56 ? ? ? ? }
?57 };
?58 AdressBook::AdressBook()
?59 {
?60 ? ? ErrorMsg=0;
?61 ? ? rc = sqlite3_open("adressbook.db", &db);
?62 ? ? if( rc )
?63 ? ? {
?64 ? ? ? fprintf(stderr, "Can't open database: %s\n", sqlite3_errmsg(db));
?65 ? ? ? exit(0);
?66 ? ? }
?67 ? ? else
?68 ? ? {
?69 ? ? ? cout<<"Opened database successfully\n"<<endl;;
?70 ? ? }
?71 ? ? ?/*** Create SQL statement ***/
?72 ? ? ?/*** ? ?姓名、電話號碼和住址 ? ?***/
?73 ? ? sql = ? ? "CREATE TABLE adress(" ?\
?74 ? ? ? ? ? ? "NAME ? ? ? ? ? TEXT ? ?NOT NULL," \
?75 ? ? ? ? ? ? "TELNUM ? ? ? ? INT ? ? NOT NULL," \
?76 ? ? ? ? ? ? "ADRESS ? ? ? ?CHAR(100) );";
?77?
?78 ? ?/* Execute SQL statement */
?79 ? ? rc = sqlite3_exec(db, sql.c_str(), 0, 0, &ErrorMsg);
?80 ? ? if( rc != SQLITE_OK )
?81 ? ? {
?82 ? ? ? ? fprintf(stderr, "SQL error: %s\n", ErrorMsg);
?83 ? ? }
?84 ? ? else
?85 ? ? {
?86 ? ? ? cout<<"Table created successfully\n"<<endl;
?87 ? ? }
?88?
?89 ? ?/* Create SQL statement */
?90 ? ?sql = "INSERT INTO adress (NAME,TELNUM,ADRESS)" \
?91 ? ? ? ? ?"VALUES ('WANGCHENG',18061623491,'081101-3-4');" \
?92 ? ? ? ? ?"INSERT INTO adress (NAME,TELNUM,ADRESS)" \
?93 ? ? ? ? ?"VALUES ('LIYUAN',18061623492,'081101-3-3');";
?94?
?95 ? ?/* Execute SQL statement */
?96 ? ?rc = sqlite3_exec(db, sql.c_str(),0, 0, &ErrorMsg);
?97 ? ?if( rc != SQLITE_OK )
?98 ? ?{
?99 ? ? ? fprintf(stderr, "SQL error: %s\n", ErrorMsg);
100 ? ? ? sqlite3_free(ErrorMsg);
101 ? ?}else
102 ? ?{
103 ? ? ? cout<<"Records created successfully\n"<<endl;;
104 ? ?}
105 }
106?
107 bool AdressBook::Display_ContactPerson()
108 {
109?
110 ? ? ? ?/* Create SQL statement */
111 ? ?sql = "SELECT * FROM adress";
112 ? ?/* Execute SQL statement */
113 ? ?rc = sqlite3_exec(db, sql.c_str(), callback, 0, &ErrorMsg);
114 ? ?if( rc != SQLITE_OK )
115 ? ?{
116 ? ? ? fprintf(stderr, "SQL error: %s\n", ErrorMsg);
117 ? ? ? sqlite3_free(ErrorMsg);
118 ? ? ? return false;
119 ? ?}
120 ? ?else
121 ? ?{
122 ? ? ? cout<<"Operation done successfully\n"<<endl;;
123 ? ? ? return true;
124 ? ?}
125 }
126 bool AdressBook::Add_ContactPerson()
127 {
128?
129 ? ? cout<<"please input Name,Contact phone number,Adress"<<endl;
130 ? ? cin>>m_strName>>m_iTelNum>>m_strAdress;
131 ? ? sprintf((char *)sql.data(),"INSERT INTO adress VALUES(\'%s\',%d,\'%s\');",(const char *)m_strName.c_str(),m_iTelNum,(const char *)m_strAdress.c_str());
132?
133 ? ? rc = sqlite3_exec(db, sql.c_str(),0, 0, &ErrorMsg);
134 ? ? if( rc != SQLITE_OK )
135 ? ? {
136 ? ? ? fprintf(stderr, "SQL error: %s\n", ErrorMsg);
137 ? ? ? sqlite3_free(ErrorMsg);
138 ? ? ? return false;
139 ? ? }else
140 ? ? {
141 ? ? ? cout<<"Records created successfully\n"<<endl;
142 ? ? ? return true;
143 ? ? }
144 }
145?
146 bool AdressBook::Find_ContactPerson()
147 {
148 ? ? cout<<"please input Name you want find"<<endl;
149 ? ? cin>>m_strName;
150 ? ? sprintf((char *)sql.data(),"SELECT * FROM adress WHERE NAME Like \'%%%s%%\';",(const char *)m_strName.c_str());
151 ? ? rc=sqlite3_exec(db,sql.c_str(),callback,0,&ErrorMsg);
152 ? ? if( rc != SQLITE_OK )
153 ? ? {
154 ? ? ? fprintf(stderr, "SQL error: %s\n", ErrorMsg);
155 ? ? ? sqlite3_free(ErrorMsg);
156 ? ? ? return false;
157 ? ? }
158 ? ? else
159 ? ? {
160 ? ? ? cout<<"Operation done successfully\n"<<endl;
161 ? ? ? return true;
162 ? ? }
163 }
164 bool AdressBook::Change_ContactPerson()
165 {
166 ? ? int flag=0;
167 ? ? string strTemp;
168 ? ? cout<<"please input name to To change Information"<<endl;
169 ? ? cin>>m_strName;
170 ? ? do
171 ? ? {
172 ? ? ? ? cout<<"please input 一個數字:\n"\
173 ? ? ? ? ? ?"1:Name\n" \
174 ? ? ? ? ? ?"2:TelNum\n"\
175 ? ? ? ? ? ?"3:Adress"<<endl;
176 ? ? ? ? cin>>flag;
177 ? ? }
178 ? ? while(1>flag||flag>3);
179?
180 ? ? switch(flag)
181 ? ? {
182 ? ? ? ? case 1:
183 ? ? ? ? cin>>strTemp;
184 ? ? ? ? sprintf((char *)sql.data(),"UPDATE adress set NAME=\'%s\' WHERE NAME LIKE \'%%%s%%\';",(const char *)strTemp.c_str(),(const char *)m_strName.c_str());
185 ? ? ? ? break;
186 ? ? ? ? case 2:
187 ? ? ? ? cin>>m_iTelNum;
188 ? ? ? ? sprintf((char *)sql.data(),"UPDATE adress set TELNUM=\'%d\' WHERE NAME LIKE \'%%%s%%\';",m_iTelNum,(const char *)m_strName.c_str());
189 ? ? ? ? break;
190 ? ? ? ? case 3:
191 ? ? ? ? cin>>strTemp;
192 ? ? ? ? sprintf((char *)sql.data(),"UPDATE adress set ADRESS=\'%s\' WHERE NAME LIKE \'%%%s%%\';",(const char *)strTemp.c_str(),(const char *)m_strName.c_str());
193 ? ? ? ? break;
194 ? ? ? ? default:cout<<"input error,please restart input"<<endl;
195?
196 ? ? }
197 ? ? rc=sqlite3_exec(db,sql.c_str(),callback,0,&ErrorMsg);
198 ? ? if( rc != SQLITE_OK )
199 ? ? {
200 ? ? ? fprintf(stderr, "SQL error: %s\n", ErrorMsg);
201 ? ? ? sqlite3_free(ErrorMsg);
202 ? ? ? return false;
203 ? ? }
204 ? ? else
205 ? ? {
206 ? ? ? cout<<"Operation done successfully\n"<<endl;
207 ? ? ? return true;
208 ? ? }
209 ? ? return true;
210?
211 }
復制代碼
復制代碼
?1 #include "VAdressBook.h"
?2?
?3 int main()
?4 {
?5 ? ? AdressBook test;
?6 ? ? int userchoice;
?7 ? ? while(true)
?8 ? ? {
?9 ? ? do{
10 ? ? ? ? cout<<"****************WELCOME USE FDA 通錄訊***************"<<endl;
11 ? ? ? ? cout<<"* ? ? ? ? ? ? ? ? ?1. Display ? ? All ? ? ? ? ? ? ? *"<<endl;
12 ? ? ? ? cout<<"* ? ? ? ? ? ? ? ? ?2. Add ? ? Contact ? ? ? ? ? ? ? *"<<endl;
13 ? ? ? ? cout<<"* ? ? ? ? ? ? ? ? ?3. Change ?Contact ? ? ? ? ? ? ? *"<<endl;
14 ? ? ? ? cout<<"* ? ? ? ? ? ? ? ? ?4 ?Find ? ?Contact ? ? ? ? ? ? ? *"<<endl;
15 ? ? ? ? cout<<"*****************************************************"<<endl;
16 ? ? ? ? cin>>userchoice;
17 ? ? ? ? }while(userchoice<1||userchoice>4);
18 ? ? ? ? switch(userchoice)
19 ? ? ? ? {
20 ? ? ? ? ? ? case 1:test.Display_ContactPerson();
21 ? ? ? ? ? ? break;
22 ? ? ? ? ? ? case 2:test.Add_ContactPerson();
23 ? ? ? ? ? ? break;
24 ? ? ? ? ? ? case 3:test.Change_ContactPerson();
25 ? ? ? ? ? ? break;
26 ? ? ? ? ? ? case 4:test.Find_ContactPerson();
27 ? ? ? ? ? ? break;
28 ? ? ? ? }
29 ? ? }
30 ? ? return 0;
31 }
復制代碼
想改進這個項目的,請持續關注FDA—orangebook.
========
c/c++通訊錄
http://blog.csdn.net/rushierer/article/details/54633233第二個程序是C語言實訓的程序
其實和第一個學生成績管理系統程序差不多,只是多了文件功能!
[cpp] view plain copy
<span style="color: rgb(51, 51, 51); font-family: "Source Code Pro", monospace; font-size: 14px; white-space: pre; background-color: rgba(128, 128, 128, 0.0470588);">Copyright ?Rushierer</span> ?
[cpp] view plain copy
#include <stdio.h> ?
#include <string.h> ?
#include <stdlib.h> ?
#define N 60 ?
typedef struct s_teleBook ?
{ ?
? ? int number; ? ? ? ? /*編號*/ ?
? ? char name[15]; ? ? ?/*姓名*/ ?
? ? char phone[12]; ? ? /*手機*/ ?
? ? char qq[15]; ? ? ? ?/*QQ號*/ ?
}TELE; ?
typedef struct date ?
{ ?
? ? int infoCount; ? ? ?/*統計數據個數*/ ?
}DATE; ?
??
void showMenu(); ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?/*顯示菜單*/ ?
void mainMenu(TELE myBook[],int n,DATE date[]); ? ? ? /*顯示首頁*/ ?
void showdate(TELE myBook[],int n,DATE date[]); ? ? ? /*只顯示數據*/ ?
void input(TELE myBook[],int n,DATE date[]); ? ? ? ? ?/*從鍵盤輸入數據*/ ?
void searchR(TELE myBook[],int n,DATE date[]); ? ? ? ?/*查詢信息*/ ?
void searchByNumber(TELE myBook[],int n,DATE date[]); /*按編號查詢信息*/ ?
void searchByName(TELE myBook[],int n,DATE date[]); ? /*按名字查詢信息*/ ?
void deleteR(TELE myBook[],int n,DATE date[]); ? ? ? ?/*刪除信息*/ ?
void insertR(TELE myBook[],int n,DATE date[]); ? ? ? ?/*插入信息*/ ?
void modify(TELE myBook[],int n,DATE date[]); ? ? ? ? /*修改信息*/ ?
void sortR(TELE myBook[],int n,DATE date[]); ? ? ? ? ?/*排序信息*/ ?
void sortByName(TELE myBook[],int n,DATE date[]); ? ? /*按名字排序*/ ?
void sortByNumber(TELE myBook[],int n,DATE date[]); ? /*按編號排序*/ ?
void save(TELE myBook[],int n,DATE date[]); ? ? ? ? ? /*保存數據到文件*/ ?
void display(TELE myBook[],int n,DATE date[]); ? ? ? ?/*顯示數據*/ ?
void read(TELE myBook[],int n,DATE date[]); ? ? ? ? ? /*從文件讀取數據*/ ?
??
int main() ?
{ ?
? ? int choice; ?
? ? TELE myBook[N]; ?
? ? DATE date[1]; ?
? ? showMenu(); ?
? ? printf("\n"); ?
? ? printf("歡迎使用通訊錄!\n"); ?
? ? printf("\n"); ?
? ? printf("首次使用通訊錄,請輸入要添加聯系人的個數:"); ?
? ? scanf("%d",&date[0].infoCount); ?
? ? printf("\n"); ?
? ? printf("1進行錄入數據 ?0退出:"); ?
? ? scanf("%d",&choice); ?
? ? switch(choice) ?
? ? { ?
? ? ? ? case 1: ?
? ? ? ? ? ? system("cls"); ?
? ? ? ? ? ? input(myBook,date[0].infoCount,date);break; ?
? ? ? ? case 0: ?
? ? ? ? ? ? exit(0);break; ?
? ? } ?
? ? return 0; ?
??
} ?
??
??
/*顯示選項Menu*/ ?
void showMenu() ?
{ ?
? ? printf(" ? ? ? ? ? ? ? ? ?通訊錄管理系統 ? ? ? ? ? ? \n"); ?
? ? printf(" ?*******************************************\n"); ?
? ? printf(" ?* ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? *\n"); ?
? ? printf(" ?* ?1 input ? record ? ? 2 search ?record ?*\n"); ?
? ? printf(" ?* ?3 delete ?record ? ? 4 insert ?record ?*\n"); ?
? ? printf(" ?* ?5 modify ?record ? ? 6 sort ? ?record ?*\n"); ?
? ? printf(" ?* ?7 save ? ?record ? ? 8 display record ?*\n"); ?
? ? printf(" ?* ?9 read ? ?record ? ? 0 quit ? ?system ?*\n"); ?
? ? printf(" ?* ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? *\n"); ?
? ? printf(" ?*******************************************\n"); ?
} ?
??
??
/*顯示主菜單*/ ?
void mainMenu(TELE myBook[],int n,DATE date[]) ?
{ ?
? ? int choice; ?
? ? showMenu(); ?
? ? printf("請輸入選項(0~9):"); ?
? ? scanf("%d",&choice); ?
? ? switch(choice) ?
? ? { ?
? ? ? ? case 1: ?
? ? ? ? ? ? system("cls"); ?
? ? ? ? ? ? input(myBook,n,date);break; ?
? ? ? ? case 2: ?
? ? ? ? ? ? system("cls"); ?
? ? ? ? ? ? searchR(myBook,n,date);break; ?
? ? ? ? case 3: ?
? ? ? ? ? ? system("cls"); ?
? ? ? ? ? ? deleteR(myBook,n,date);break; ?
? ? ? ? case 4: ?
? ? ? ? ? ? system("cls"); ?
? ? ? ? ? ? insertR(myBook,n,date);break; ?
? ? ? ? case 5: ?
? ? ? ? ? ? system("cls"); ?
? ? ? ? ? ? modify(myBook,n,date);break; ?
? ? ? ? case 6: ?
? ? ? ? ? ? system("cls"); ?
? ? ? ? ? ? sortR(myBook,n,date);break; ?
? ? ? ? case 7: ?
? ? ? ? ? ? system("cls"); ?
? ? ? ? ? ? save(myBook,n,date);break; ?
? ? ? ? case 8: ?
? ? ? ? ? ? system("cls"); ?
? ? ? ? ? ? display(myBook,n,date);break; ?
? ? ? ? case 9: ?
? ? ? ? ? ? system("cls"); ?
? ? ? ? ? ? read(myBook,n,date);break; ?
? ? ? ? case 0: ?
? ? ? ? ? ? exit(0);break; ?
??
? ? } ?
} ?
??
??
/*從鍵盤輸入聯系人信息*/ ?
void input(TELE myBook[],int n,DATE date[]) ?
{ ?
? ? int j,i; ?
? ? printf(" ? ? ? ? ? ? ? ? ? Input record ? ? ? ? ? ? ? ? ? ? ?\n"); ?
? ? printf("*****************************************************\n"); ?
? ? printf("\n"); ?
? ? printf("數據錄入格式提示:\n"); ?
? ? printf("1.數據內容:編號、姓名、手機號碼、QQ號\n"); ?
? ? printf("2.數據間以空格做間隔,最后回車錄入數據結束!\n"); ?
? ? printf("\n"); ?
? ? for(i=0;i<n;i++) ?
? ? { ?
? ? ? ? printf("請輸入第%d個聯系人的信息:",i+1); ?
? ? ? ? scanf(" %d",&myBook[i].number); ?
? ? ? ? scanf("%s",myBook[i].name); ?
? ? ? ? scanf("%s",myBook[i].phone); ?
? ? ? ? scanf("%s",myBook[i].qq); ?
? ? } ?
? ? printf("\n"); ?
? ? printf("數據錄入完成!\n"); ?
? ? printf("\n"); ?
? ? printf("1顯示錄入的信息 ?2返回主菜單 ?0退出:"); ?
? ? scanf("%d",&j); ?
? ? if(j==1) ?
? ? { ?
? ? ? ? printf("\n"); ?
? ? ? ? display(myBook,n,date); ?
? ? } ?
? ? else if(j==2) ?
? ? { ?
? ? ? ? system("cls"); ?
? ? ? ? mainMenu(myBook,n,date); ?
? ? } ?
? ? else ?
? ? ? ? exit(0); ?
} ?
??
??
/*顯示所有信息*/ ?
void display(TELE myBook[],int n,DATE date[]) ?
{ ?
? ? int i,j; ?
? ? printf("所有信息:\n"); ?
? ? printf("\n"); ?
? ? printf("編號 ? 姓名 ? ? ? ? ?電話號碼 ? ? ? ?QQ號 ? ?\n"); ?
? ? for(i=0;i<n;i++) ?
? ? { ?
? ? ? ? printf("%-6d%-15s%-15s%-15s\n",myBook[i].number, ?
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? myBook[i].name, ?
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? myBook[i].phone, ?
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? myBook[i].qq); ?
??
? ? } ?
? ? printf("\n"); ?
? ? printf("1返回主菜單 ?0退出:"); ?
? ? scanf("%d",&j); ?
? ? if(j==1) ?
? ? { ?
? ? ? ? system("cls"); ?
? ? ? ? mainMenu(myBook,n,date); ?
? ? } ?
? ? else ?
? ? ? ? exit(0); ?
??
} ?
??
/*只顯示數據*/ ?
void showdate(TELE myBook[],int n,DATE date[]) ?
{ ?
? ? int i; ?
? ? printf("已錄入的信息:\n"); ?
? ? printf("\n"); ?
? ? printf("編號 ? 姓名 ? ? ? ? ?電話號碼 ? ? ? ?QQ號 ? ?\n"); ?
? ? for(i=0;i<n;i++) ?
? ? { ?
? ? ? ? printf("%-6d%-15s%-15s%-15s\n",myBook[i].number, ?
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?myBook[i].name, ?
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?myBook[i].phone, ?
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?myBook[i].qq); ?
??
? ? } ?
} ?
??
/*查找數據*/ ?
void searchR(TELE myBook[],int n,DATE date[]) ?
{ ?
? ? int k; ?
? ? printf(" ? ? ? ? ? ? ? ? ? Search record ? ? ? ? ? ? ? ? ? ? \n"); ?
? ? printf("*****************************************************\n"); ?
? ? printf("\n"); ?
? ? printf("查找方式:1.按編號查找 2.按姓名查找\n"); ?
? ? printf("請選擇查找方式(1/2):"); ?
? ? scanf("%d",&k); ?
? ? printf("\n"); ?
? ? if(k==1) ?
? ? { ?
? ? ? ? searchByNumber(myBook,n,date); ?
? ? } ?
? ? else ?
? ? { ?
? ? ? ? searchByName(myBook,n,date); ?
? ? } ?
} ?
??
??
/*按編號查詢*/ ?
void searchByNumber(TELE myBook[],int n,DATE date[]) ?
{ ?
? ? int i,j,k=0; ?
? ? int number1; ?
? ? printf("請輸入想查找的編號:"); ?
? ? scanf("%d",&number1); ?
? ? printf("\n"); ?
? ? printf("查找結果:"); ?
? ? printf("\n"); ?
? ? for(i=0;i<n;i++) ?
? ? { ?
? ? ? ? if(myBook[i].number==number1) ?
? ? ? ? { ?
? ? ? ? ? ? printf("%-6d%-15s%-15s%-15s\n",myBook[i].number, ?
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? myBook[i].name, ?
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? myBook[i].phone, ?
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? myBook[i].qq); ?
??
? ? ? ? ? ? k+=1; ?
? ? ? ? } ?
? ? } ?
? ? if(k==0) ?
? ? ? ? printf("輸入的編號不存在或輸入格式不對!\n"); ?
? ? printf("\n"); ?
? ? printf("1重新查詢 ?2返回主菜單 ?0退出:"); ?
? ? scanf("%d",&j); ?
? ? switch(j) ?
? ? { ?
? ? ? ? case 1: ?
? ? ? ? ? ? system("cls"); ?
? ? ? ? ? ? searchR(myBook,n,date);break; ?
? ? ? ? case 2: ?
? ? ? ? ? ? system("cls"); ?
? ? ? ? ? ? mainMenu(myBook,n,date);break; ?
? ? ? ? case 0: ?
? ? ? ? ? ? exit(0);break; ?
? ? } ?
} ?
??
??
/*按姓名查詢*/ ?
void searchByName(TELE myBook[],int n,DATE date[]) ?
{ ?
? ? int i,j,k=0; ?
? ? char name1[15]; ?
? ? printf("請輸入想查找的姓名:"); ?
? ? scanf("%s",name1); ?
? ? printf("\n"); ?
? ? printf("查找結果:"); ?
? ? printf("\n"); ?
? ? for(i=0;i<n;i++) ?
? ? { ?
? ? ? ? if(strcmp(myBook[i].name,name1)==0) ?
? ? ? ? { ?
? ? ? ? ? ? printf("%-6d%-15s%-15s%-15s\n",myBook[i].number, ?
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? myBook[i].name, ?
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? myBook[i].phone, ?
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? myBook[i].qq); ?
??
? ? ? ? ? ? k+=1; ?
? ? ? ? } ?
? ? } ?
? ? if(k==0) ?
? ? ? ? printf("輸入的姓名不存在或輸入格式不對!\n"); ?
? ? printf("\n"); ?
? ? printf("1重新查詢 ?2返回主菜單 ?0退出:"); ?
? ? scanf("%d",&j); ?
? ? switch(j) ?
? ? { ?
? ? ? ? case 1: ?
? ? ? ? ? ? system("cls"); ?
? ? ? ? ? ? searchR(myBook,n,date);break; ?
? ? ? ? case 2: ?
? ? ? ? ? ? system("cls"); ?
? ? ? ? ? ? mainMenu(myBook,n,date);break; ?
? ? ? ? case 0: ?
? ? ? ? ? ? exit(0);break; ?
? ? } ?
} ?
??
/*刪除信息*/ ?
void deleteR(TELE myBook[],int n,DATE date[]) ?
{ ?
? ? int i,k,j; ?
? ? printf(" ? ? ? ? ? ? ? ? ? Delete record ? ? ? ? ? ? ? ? ? ? \n"); ?
? ? printf("*****************************************************\n"); ?
? ? printf("\n"); ?
? ? showdate(myBook,n,date); ?
? ? printf("已經錄入%d個聯系人的信息,你想刪除第幾聯系人個的信息:",n); ?
? ? scanf("%d",&k); ?
? ? i=k-1; ?
? ? for(;i<=n-2;i++) ?
? ? { ?
? ? ? ? strcpy(myBook[i].name,myBook[i+1].name); ?
? ? ? ? strcpy(myBook[i].phone,myBook[i+1].phone); ?
? ? ? ? strcpy(myBook[i].qq,myBook[i+1].qq); ?
? ? ? ? myBook[i].number=myBook[i+1].number; ?
? ? } ?
? ? printf("\n"); ?
? ? printf("已成功刪除!\n"); ?
? ? date[0].infoCount=date[0].infoCount-1; ?
? ? n=n-1; ?
? ? printf("\n"); ?
? ? printf("1顯示修改后的信息 ?2返回主菜單 ?0退出:"); ?
? ? scanf("%d",&j); ?
? ? switch(j) ?
? ? { ?
? ? ? ?case 1: ?
? ? ? ? ? ? printf("\n"); ?
? ? ? ? ? ? display(myBook,n,date);break; ?
? ? ? ? case 2: ?
? ? ? ? ? ? system("cls"); ?
? ? ? ? ? ? mainMenu(myBook,n,date);break; ?
? ? ? ? case 0: ?
? ? ? ? ? ? exit(0);break; ?
? ? } ?
} ?
??
/*插入信息*/ ?
void insertR(TELE myBook[],int n,DATE date[]) ?
{ ?
? ? int i,k,j; ?
? ? int number; ?
? ? char name[15]; ? ? ?/*姓名*/ ?
? ? char phone[12]; ? ? /*電話*/ ?
? ? char qq[15]; ? ? ? ?/*QQ號*/ ?
? ? printf(" ? ? ? ? ? ? ? ? ? Insert record ? ? ? ? ? ? ? ? ? ? \n"); ?
? ? printf("*****************************************************\n"); ?
? ? printf("\n"); ?
? ? showdate(myBook,n,date); ?
? ? printf("\n"); ?
? ? printf("你想在第幾個數據之后插入數據:"); ?
? ? scanf("%d",&k); ?
? ? if(k>n||k<=0) ?
? ? { ?
? ? ? ? printf("\n"); ?
? ? ? ? printf("輸入錯誤!\n"); ?
? ? ? ? printf("請重新輸入你想在第幾個數據之后插入數據:"); ?
? ? ? ? scanf("%d",&i); ?
? ? ? ? k=i; ?
? ? } ?
? ? printf("\n"); ?
? ? printf("請輸入插入的信息:"); ?
? ? scanf("%d",&number); ?
? ? scanf("%s",name); ?
? ? scanf("%s",phone); ?
? ? scanf("%s",qq); ?
? ? for(i=n;k+1<=i;i--); ?
? ? { ?
? ? ? ? strcpy(myBook[i].name,myBook[i-1].name); ?
? ? ? ? strcpy(myBook[i].phone,myBook[i-1].phone); ?
? ? ? ? strcpy(myBook[i].qq,myBook[i-1].qq); ?
? ? ? ? myBook[i].number=myBook[i-1].number; ?
? ? } ?
? ? strcpy(myBook[k].name,name); ?
? ? strcpy(myBook[k].phone,phone); ?
? ? strcpy(myBook[k].qq,qq); ?
? ? myBook[k].number=number; ?
? ? date[0].infoCount=date[0].infoCount+1; ?
? ? n=n+1; ?
? ? printf("\n"); ?
? ? printf("插入完成!\n"); ?
? ? printf("\n"); ?
? ? printf("1顯示修改后的信息輸入 ?2返回主菜單 ?0退出:"); ?
? ? scanf("%d",&j); ?
? ? switch(j) ?
? ? { ?
? ? ? ?case 1: ?
? ? ? ? ? ? printf("\n"); ?
? ? ? ? ? ? display(myBook,n,date);break; ?
? ? ? ? case 2: ?
? ? ? ? ? ? system("cls"); ?
? ? ? ? ? ? mainMenu(myBook,n,date);break; ?
? ? ? ? case 0: ?
? ? ? ? ? ? exit(0);break; ?
? ? } ?
??
} ?
??
??
/*修改信息*/ ?
void modify(TELE myBook[],int n,DATE date[]) ?
{ ?
? ? int i,k,j; ?
? ? int number; ?
? ? char name[15]; ? ? ?/*姓名*/ ?
? ? char phone[12]; ? ? /*電話*/ ?
? ? char qq[15]; ? ? ? ?/*QQ號*/ ?
? ? printf(" ? ? ? ? ? ? ? ? ? Modify record ? ? ? ? ? ? ? ? ? ? \n"); ?
? ? printf("*****************************************************\n"); ?
? ? printf("\n"); ?
? ? showdate(myBook,n,date); ?
? ? printf("\n"); ?
? ? printf("已經錄入%d個聯系人的信息,你想修改第幾個聯系人的記錄:",n); ?
? ? scanf("%d",&k); ?
? ? if(k>n||k<=0) ?
? ? { ?
? ? ? ? printf("\n"); ?
? ? ? ? printf("輸入錯誤,無這條記錄!\n"); ?
? ? ? ? printf("請重新輸入你想修改第幾個聯系人的記錄:"); ?
? ? ? ? scanf("%d",&i); ?
? ? ? ? k=i; ?
? ? } ?
? ? printf("\n"); ?
? ? printf("請輸入修改后的信息:"); ?
? ? scanf("%d",&number); ?
? ? scanf("%s",name); ?
? ? scanf("%s",phone); ?
? ? scanf("%s",qq); ?
? ? strcpy(myBook[k-1].name,name); ?
? ? strcpy(myBook[k-1].phone,phone); ?
? ? strcpy(myBook[k-1].qq,qq); ?
? ? myBook[k-1].number=number; ?
? ? printf("\n"); ?
? ? printf("修改完成!\n"); ?
? ? printf("\n"); ?
? ? printf("1顯示修改后的信息 ?2返回主菜單 ?0退出:"); ?
? ? scanf("%d",&j); ?
? ? switch(j) ?
? ? { ?
? ? ? ?case 1: ?
? ? ? ? ? ? printf("\n"); ?
? ? ? ? ? ? display(myBook,n,date);break; ?
? ? ? ? case 2: ?
? ? ? ? ? ? system("cls"); ?
? ? ? ? ? ? mainMenu(myBook,n,date);break; ?
? ? ? ? case 0: ?
? ? ? ? ? ? exit(0);break; ?
? ? } ?
} ?
??
??
/*排序信息*/ ?
void sortR(TELE myBook[],int n,DATE date[]) ?
{ ?
? ? int k; ?
? ? printf(" ? ? ? ? ? ? ? ? ? ? Sort record ? ? ? ? ? ? ? ? ? ? \n"); ?
? ? printf("*****************************************************\n"); ?
? ? printf("\n"); ?
? ? printf("排序方式:1.按編號排序 ?2.按姓名排序\n"); ?
? ? printf("請選擇排序方式(1/2):"); ?
? ? scanf("%d",&k); ?
? ? printf("\n"); ?
? ? if(k==1) ?
? ? { ?
? ? ? ? sortByNumber(myBook,n,date); ?
? ? } ?
? ? else ?
? ? { ?
? ? ? ? sortByName(myBook,n,date); ?
? ? } ?
} ?
??
??
/*按姓名排序信息*/ ?
void sortByName(TELE myBook[],int n,DATE date[]) ?
{ ?
? ? int i,j; ?
? ? TELE temp; ?
? ? for(j=1;j<n;j++) ?
? ? ? ? for(i=0;i<n-j;i++) ?
? ? ? ? if(strcmp(myBook[i].name,myBook[i+1].name)>0) ?
? ? ? ? { ?
? ? ? ? ? ? temp=myBook[i]; ?
? ? ? ? ? ? myBook[i]=myBook[i+1]; ?
? ? ? ? ? ? myBook[i+1]=temp; ?
? ? ? ? } ?
? ? printf("排序后的信息:\n"); ?
? ? printf("\n"); ?
? ? printf(" 姓名 ? ? ? ?編號 ? ?電話號碼 ? ? ? ?QQ號 ? ?\n"); ?
? ? for(i=0;i<n;i++) ?
? ? { ?
? ? ? ? printf("%-15s%-6d%-15s%-15s\n",myBook[i].name, ?
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?myBook[i].number, ?
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?myBook[i].phone, ?
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?myBook[i].qq); ?
? ? } ?
? ? printf("\n"); ?
? ? printf("1重新排序 ?2返回主菜單 ?0退出:"); ?
? ? scanf("%d",&j); ?
? ? switch(j) ?
? ? { ?
? ? ? ? case 1: ?
? ? ? ? ? ? system("cls"); ?
? ? ? ? ? ? sortR(myBook,n,date);break; ?
? ? ? ? case 2: ?
? ? ? ? ? ? system("cls"); ?
? ? ? ? ? ? mainMenu(myBook,n,date);break; ?
? ? ? ? case 0: ?
? ? ? ? ? ? exit(0);break; ?
? ? } ?
} ?
??
/*按編號排序信息*/ ?
void sortByNumber(TELE myBook[],int n,DATE date[]) ?
{ ?
? ? int i,j; ?
? ? TELE temp; ?
? ? for(j=1;j<n;j++) ?
? ? ? ? for(i=0;i<n-j;i++) ?
? ? ? ? if(myBook[i].number>myBook[i+1].number) ?
? ? ? ? { ?
? ? ? ? ? ? temp=myBooki[]; ?
? ? ? ? ? ? myBook[i]=myBook[i+1]; ?
? ? ? ? ? ? myBook[i+1]=temp; ?
? ? ? ? } ?
? ? printf("排序后的信息:\n"); ?
? ? printf("\n"); ?
? ? printf("編號 ? 姓名 ? ? ? ? ?電話號碼 ? ? ? ?QQ號 ? ?\n"); ?
? ? for(i=0;i<n;i++) ?
? ? { ?
? ? ? ? printf("%-6d%-15s%-15s%-15s\n",myBook[i].number, ?
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?myBook[i].name, ?
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?myBook[i].phone, ?
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?myBook[i].qq); ?
? ? } ?
? ? printf("\n"); ?
? ? printf("1重新排序 ?2返回主菜單 ?0退出:"); ?
? ? scanf("%d",&j); ?
? ? switch(j) ?
? ? { ?
? ? ? ? case 1: ?
? ? ? ? ? ? system("cls"); ?
? ? ? ? ? ? sortR(myBook,n,date);break; ?
? ? ? ? case 2: ?
? ? ? ? ? ? system("cls"); ?
? ? ? ? ? ? mainMenu(myBook,n,date);break; ?
? ? ? ? case 0: ?
? ? ? ? ? ? exit(0);break; ?
? ? } ?
} ?
??
??
/*保存信息*/ ?
void save(TELE myBook[],int n,DATE date[]) ?
{ ?
? ? FILE *fp; ?
? ? int i,j; ?
? ? TELE temp; ?
? ? printf(" ? ? ? ? ? ? ? ? ? ? Save record ? ? ? ? ? ? ? ? ? ? \n"); ?
? ? printf("*****************************************************\n"); ?
? ? if((fp=fopen("teleBook.txt","w"))==NULL) ? ? ? ?/*以寫方式打開文本文件*/ ?
? ? { ?
? ? ? ? printf("Failure to open teleBook.txt!\n"); ?
? ? ? ? printf("1返回主菜單 ?0退出:"); ?
? ? ? ? scanf("%d",&j); ?
? ? ? ? if(j==1) ?
? ? ? ? { ?
? ? ? ? ? ? system("cls"); ?
? ? ? ? ? ? mainMenu(myBook,n,date); ?
? ? ? ? } ?
? ? ? ? else ?
? ? ? ? ? ? exit(0); ?
? ? } ?
? ? for(j=1;j<n;j++) ?//對全部信息按序號排序后再保存到文件 ?
? ? ? ? for(i=0;i<n-j;i++) ?
? ? ? ? if(myBook[i].number>myBook[i+1].number) ?
? ? ? ? { ?
? ? ? ? ? ? temp=myBook[i]; ?
? ? ? ? ? ? myBook[i]=myBook[i+1]; ?
? ? ? ? ? ? myBook[i+1]=temp; ?
? ? ? ? } ?
? ? for(i=0;i<n;i++) ?
? ? { ?
? ? ? ? fprintf(fp," %-6d%-15s%-15s%-15s",myBook[i].number, ?
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?myBook[i].name, ?
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?myBook[i].phone, ?
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?myBook[i].qq); ?
? ? ? ? fprintf(fp,"\n"); ?
? ? } ?
? ? fclose(fp); ?
? ? printf("\n"); ?
? ? printf("保存通訊錄信息成功!\n"); ?
? ? printf("\n"); ?
? ? printf("可在文件目錄查看teleBook.txt文件!\n"); ?
? ? printf("\n"); ?
? ? printf("1返回主菜單 ?0退出:"); ?
? ? scanf("%d",&j); ?
? ? if(j==1) ?
? ? { ?
? ? ? ? system("cls"); ?
? ? ? ? mainMenu(myBook,n,date); ?
? ? } ?
? ? else ?
? ? ? ? exit(0); ?
} ?
??
??
/*從文件讀取數據*/ ?
void read(TELE myBook[],int n,DATE date[]) ?
{ ?
? ? FILE *fp; ?
? ? int i,j; ?
? ? TELE myBook1[N]; ?
? ? printf(" ? ? ? ? ? ? ? ? ? ? ?Read record ? ? ? ? ? ? ? ? ? ?\n"); ?
? ? printf("*****************************************************\n"); ?
? ? printf("\n"); ?
? ? printf("讀取信息結果:\n"); ?
? ? printf("\n"); ?
? ? if((fp=fopen("teleBook.txt","r"))==NULL) ? ? ? ?/*以讀方式打開文本文件*/ ?
? ? { ?
? ? ? ? printf("Failure to open teleBook.txt!\n"); ?
? ? ? ? printf("可能沒有保存數據,可以返回主菜單先保存數據!\n"); ?
? ? ? ? printf("1返回主菜單 ?0退出:"); ?
? ? ? ? scanf("%d",&j); ?
? ? ? ? if(j==1) ?
? ? ? ? { ?
? ? ? ? ? ? system("cls"); ?
? ? ? ? ? ? mainMenu(myBook,n,date); ?
? ? ? ? } ?
? ? ? ? else ?
? ? ? ? ? ? exit(0); ?
? ? } ?
? ? for(i=0;!feof(fp);i++) ? ? ? ? ? ? ?/*若未讀到文件末尾,則繼續讀*/ ?
? ? { ?
? ? ? ? fscanf(fp," %6d",&myBook1[i].number); ?
? ? ? ? fscanf(fp,"%15s",myBook1[i].name); ?
? ? ? ? fscanf(fp,"%15s",myBook1[i].phone); ?
? ? ? ? fscanf(fp,"%15s",myBook1[i].qq); ?
? ? } ?
? ? fclose(fp); ?
? ? printf("編號 ? 姓名 ? ? ? ? ?電話號碼 ? ? ? ?QQ號 ? ?\n"); ?
? ? for(i=0;i<n;i++) ?
? ? { ?
? ? ? ? printf("%-6d%-15s%-15s%-15s",myBook1[i].number, ?
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?myBook1[i].name, ?
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?myBook1[i].phone, ?
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?myBook1[i].qq); ?
? ? ? ? printf("\n"); ?
? ? } ?
? ? printf("\n"); ?
? ? printf("讀取通訊錄文件信息成功!\n"); ?
? ? printf("\n"); ?
? ? printf("1返回主菜單 ?0退出:"); ?
? ? scanf("%d",&j); ?
? ? if(j==1) ?
? ? { ?
? ? ? ? system("cls"); ?
? ? ? ? mainMenu(myBook,n,date); ?
? ? } ?
? ? else ?
? ? ? ? exit(0); ?
}?
========
總結
以上是生活随笔為你收集整理的C++ 通讯录学习总结的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: C++内联函数学习总结
- 下一篇: MySQL优化学习总结