C++ 通讯录学习总结
生活随笔
收集整理的這篇文章主要介紹了
C++ 通讯录学习总结
小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.
用c++寫的簡(jiǎn)易學(xué)生通訊錄
http://blog.csdn.net/yuzhihui_no1/article/details/43530445 ?? ? ? ? 昨天我一大學(xué)室友找到我說他親戚要個(gè)學(xué)生通訊錄程序,一定要c++來(lái)寫,而他是學(xué)Java的,對(duì)c++的基本語(yǔ)法都忘干凈了。因?yàn)槲沂亲鯿方面開發(fā)的,所以問我能不能搞定。雖然我也半年多沒用c++寫過東西了,但作為室友怎么能拒絕呢,再個(gè)看了下他的需求感覺挺簡(jiǎn)單的(沒涉及到數(shù)據(jù)庫(kù)操作),于是就應(yīng)承下來(lái)了。
? ? ? ? 他給我的需求是這樣的:
? ? ? ? 學(xué)生通訊錄系統(tǒng)
? ? ? ? 學(xué)生通信錄信息包括:姓名、學(xué)號(hào)、年齡、性別、家庭住址、聯(lián)系電話、寢室號(hào)等信息。
? ? ? ? 系統(tǒng)以菜單方式工作,使之能提供以下功能::
? ? ? ? 學(xué)生通信錄信息的輸入
? ? ? ? 學(xué)生的通信錄信息刪除和修改
? ? ? ? 學(xué)生的通信錄信息查詢和統(tǒng)計(jì)功能
? ? ? ? 學(xué)生的通信錄信息輸出顯示?
? ? ? ? 而且數(shù)據(jù)都保存在內(nèi)存中;
? ? ? ? 我在Linux下用了半上午時(shí)間給他寫了個(gè)簡(jiǎn)易的通訊錄程序,下面我把代碼貼出來(lái),希望可以給那些大一大二想寫這個(gè)程序的同學(xué)一點(diǎn)思路。其中不規(guī)范的地方還望大家指出了(因?yàn)闉榱吮M快完成功能,所以一些規(guī)范就沒太注意),謝謝!
[cpp] view plain copy
#include<iostream> ?
#include<cstdlib> //主要是用到exit()退出進(jìn)程函數(shù) ??
#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; ?
} ?
//上面都是學(xué)生類,以及類的屬性設(shè)置和獲取函數(shù) ?
?
??
#define LEN 1024 ?
??
//定義一個(gè)結(jié)構(gòu)體,保存類對(duì)象和數(shù)據(jù)有效性標(biāo)志 ?
typedef struct Node{ ?
? ? ?student s; ?
? ? ?int flag;//如果為0 表示該結(jié)構(gòu)體中的對(duì)象無(wú)效 ?
}Node; ?
??
static int studentNum = 0; ?
static Node buff[LEN] = {};//用來(lái)存放上面結(jié)構(gòu)體對(duì)象的,一個(gè)對(duì)象表示一個(gè)同學(xué)的信息 ?
??
//在數(shù)組中得到一個(gè)空閑的元素,返回?cái)?shù)組小標(biāo);是否空閑可以查看flag標(biāo)志位 ?
int getArrayFree() ?
{ ?
? ? int i; ?
? ? for(i = 0; i < LEN; i++) ?
? ? { ?
? ? ? ? if(0 == buff[i].flag) ?
? ? ? ? ? ? return i; ?
? ? } ?
? ? return Fill; ?
} ?
??
//在數(shù)組中查找指定學(xué)生的信息,該函數(shù)被刪除和修改函數(shù)調(diào)用 ?
//用學(xué)號(hào)查詢和姓名查詢兩種方式,返回查找到的學(xué)生在數(shù)組中下標(biāo)號(hào) ?
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是否存在,姓名可以相同,但學(xué)號(hào)一定不能相同 ?
int isExist(int id) ?
{ ?
? ? int i; ?
? ? int count = 0; ?
? ? for(i = 0; i < LEN; i++) ?
? ? { ?
? ? ? ? if(id == buff[i].s.getId()) ?
? ? ? ? ? ?count++; ?
? ? } ?
? ? return count; ?
} ?
??
//增加一個(gè)學(xué)生的信息到數(shù)組中,也即是通訊錄中增加一條通訊錄 ?
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; ?
??
} ?
??
//刪除指定學(xué)生的通訊錄信息,只要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; ? ??
} ?
??
//修改指定學(xué)生的信息 ?
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://下面循環(huán)要判斷id(學(xué)號(hào))是否重合,如果重合就再選擇一個(gè)學(xué)號(hào),直到?jīng)]有重合的 ?
? ? ? ? ? ? ? ? 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; ?
} ?
??
//統(tǒng)計(jì)通訊錄中有多少個(gè)學(xué)生 ?
int accoutStudent() // accout Student number ?
{ ?
? ?cout<<endl; ?
? ?cout<<"---------------------------------"<<endl; ?
? ?cout<<"student number:"<<studentNum<<endl; ?
? ?return 0; ?
} ?
??
//打印指定學(xué)生信息 ?
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; ?
? ? ??
} ?
??
//打印所有學(xué)生的信息 ?
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; ?
} ?
??
//根據(jù)菜單選擇調(diào)用對(duì)應(yīng)函數(shù) ?
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; ?
? ?} ? ?
} ?
??
//選擇菜單函數(shù) ?
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系統(tǒng)上測(cè)試通過,沒問題。在其他系統(tǒng)上應(yīng)該沒有大問題,如果有的話就是頭文件的問題(聽室友說在mac上sleep()函數(shù)是沒有的,Windows下要添加一個(gè)頭文件,具體什么頭文件需要的可以百度下);
? ? ? ? 程序還有個(gè)問題,就是在menu()函數(shù)中輸入值給number時(shí),如果你輸入字符就會(huì)出現(xiàn)死循環(huán)(這個(gè)死循環(huán)不是因?yàn)閣hile(1)造成的,如果正常死循環(huán),每循環(huán)一次就會(huì)等待用戶輸入一個(gè)值),在我預(yù)計(jì)中不會(huì)出現(xiàn)這個(gè)問題的,因?yàn)檩斎胱址彩寝D(zhuǎn)化成ascii碼,然后也會(huì)被剔除的。可惜,不是這樣,我打印了下number(當(dāng)輸入字符A時(shí)),結(jié)果卻是0,而不是64;還有當(dāng)輸入學(xué)生信息時(shí),如果在id輸入時(shí),不小心輸入名字時(shí)(其實(shí)就是字符串)也會(huì)出現(xiàn)未知的錯(cuò)誤。查了資料說是c++中類型不能混用,本應(yīng)該要有防止這種失誤操作的處理方法,但我實(shí)驗(yàn)了下,沒成功,如果誰(shuí)有好的辦法希望可以告訴我一聲,共同學(xué)習(xí)嘛。謝謝!
? ? ? ? ?轉(zhuǎn)載請(qǐng)注明作者和原文出處,原文地址:http://blog.csdn.net/yuzhihui_no1/article/details/43530445
========
C++ 通訊錄實(shí)現(xiàn)
http://blog.csdn.net/ky_heart/article/details/55045996昨晚實(shí)現(xiàn)了用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; //記錄通訊錄中的人數(shù) ?
? ? 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 << "與已有聯(lián)系人重名,請(qǐng)重新輸入。" << endl; ?
? ?count--; ?
? ?goto loop; ?
} ?
? ? } ?
? ? tmp.SetTel(); ?
? ? tmp.SetAddr(); ?
? ? tmp.id = count; ?
? ? ??
? ? per.push_back(tmp); ?
??
??
? ? cout << "是否繼續(xù)添加聯(lián)系人 y/n :"; ?
? ? char ch; ?
? ? cin >> ch; ?
? ? if('y' == ch || 'Y' == ch) ?
? ? { ?
? ? ? ? goto loop; ?
? ? } ?
? ? ?
} ?
??
??
void Info::show() ?
{ ?
? ? vector<Info>::iterator it; ?
? ? if(per.empty()) ?
? ? { ?
? ? ? ? cout << "通訊錄暫無(wú)聯(lián)系人!" << 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 << "請(qǐng)按任意鍵退出" << endl; ?
char ch; ?
cin >> ch; ?
? ? } ?
} ?
??
??
void Info::search() ?
{ ?
? ? vector<Info>::iterator it; ?
? ? if(per.empty()) ?
? ? { ?
? ? ? ? cout << "通訊錄暫無(wú)聯(lián)系人!" << endl; ?
? ? } ?
? ? else ?
? ? { ?
? ? search_loop: ?
int tp = ?0; //查詢方式選擇位 ?
int num = 0; //查找的ID ?
string tn; //查找的姓名 ?
int flag = 0; //查找成功與否標(biāo)志位 ?
? ? ? ? cout << "查找方式:1.ID 2.姓名" << endl; ?
? ? ? ? cin >> tp; ?
? ? ? ? if(1 == tp) ?
{ ?
? ?cout << "請(qǐng)輸入查找的ID:"; ?
? ?cin >> num; ?
? ? ? ? ? ? for(it = per.begin(); it != per.end(); ++it) ?
? ?{ ?
? ? ? ?if(it->GetId() == num) ?
{ ?
? ? ? ? ? ? ? ? ? ? flag = 1; ?
? ?cout << "你要找的聯(lián)系人為:" <<endl; ?
? ? ? ? ? ?cout << "ID: " << it->GetId() << endl; ?
? ? ? ? ? ?cout << "姓名:" << it->GetName() << endl; ?
? ? ? ? ? ?cout << "電話:" << it->GetTel() << endl; ?
? ? ? ? ? ?cout << "地址:" << it->GetAddr() << endl; ?
} ?
? ?} ?
} ?
else if(2 == tp) ?
{ ?
? ?cout << "請(qǐng)輸入查找的姓名:"; ?
? ? ? ? ? ? cin >> tn; ?
? ? ? ? ? ? for(it = per.begin(); it != per.end(); ++it) ?
? ?{ ?
? ? ? ?if(!((it->GetName()).compare(tn))) ?
{ ?
? ? ? ? ? ? ? ? ? ? flag = 1; ?
? ?cout << "你要找的聯(lián)系人為:" <<endl; ?
? ? ? ? ? ?cout << "ID: " << it->GetId() << endl; ?
? ? ? ? ? ?cout << "姓名:" << it->GetName() << endl; ?
? ? ? ? ? ?cout << "電話:" << it->GetTel() << endl; ?
? ? ? ? ? ?cout << "地址:" << it->GetAddr() << endl; ?
} ?
? ?} ?
} ?
else ?
{ ?
? ?cout << "查找方式選擇錯(cuò)誤,請(qǐng)重新選擇。" << endl; ?
? ?goto search_loop; ?
} ?
??
??
if(0 == flag) ?
{ ?
? ?cout << "無(wú)找到此聯(lián)系人" << endl; ?
} ?
else ?
{ ?
? ?cout << "查找成功" ?<< endl; ?
} ?
cout << "請(qǐng)按任意鍵退出" << endl; ?
char ch; ?
cin >> ch; ?
? ? } ?
? ? ??
} ?
??
??
void Info::delete_info() ?
{ ?
? ? vector<Info>::iterator it; ?
? ? if(per.empty()) ?
? ? { ?
? ? ? ? cout << "通訊錄暫無(wú)聯(lián)系人!" << endl; ?
? ? } ?
? ? else ?
? ? { ?
? ? delete_loop: ?
int tp = ?0; //刪除方式選擇位 ?
int num = 0; //刪除的ID ?
string tn; //刪除的姓名 ?
int flag = 0; //刪除成功與否標(biāo)志位 ?
? ? ? ? cout << "刪除方式:1.ID 2.姓名" << endl; ?
? ? ? ? cin >> tp; ?
? ? ? ? if(1 == tp) ?
{ ?
? ?cout << "請(qǐng)輸入刪除的ID:"; ?
? ?cin >> num; ?
? ? ? ? ? ? for(it = per.begin(); it != per.end();) ?
? ?{ ?
? ? ? ?if(it->GetId() == num) ?
{ ?
? ? ? ? ? ? ? ? ? ? flag = 1; ?
? ?cout << "你要?jiǎng)h除的聯(lián)系人為:" <<endl; ?
? ? ? ? ? ?cout << "ID: " << it->GetId() << endl; ?
? ? ? ? ? ?cout << "姓名:" << it->GetName() << endl; ?
? ? ? ? ? ?cout << "電話:" << it->GetTel() << endl; ?
? ? ? ? ? ?cout << "地址:" << it->GetAddr() << endl; ?
? ? ? ? ? ? ?
? ?cout << "確定刪除此聯(lián)系人嗎?y/n : "; ?
? ? ? ? ? ?char ch1; ?
? ? ? ? ? ?cin >> ch1; ?
? ? ? ? ? ?if (ch1 == 'y' || ch1 == 'Y') ?
? ? ? ? ? ?{ ?
? ? ? ? ? ? ? ?it = per.erase(it); ?
? ? ? ? ? ?} ?
} ?
else ?
{ ?
? ?++it; ?
} ?
? ?} ?
} ?
else if(2 == tp) ?
{ ?
? ?cout << "請(qǐng)輸入刪除的姓名:"; ?
? ? ? ? ? ? cin >> tn; ?
? ? ? ? ? ? for(it = per.begin(); it != per.end(); ) ?
? ?{ ?
? ? ? ?if(!((it->GetName()).compare(tn))) ?
{ ?
? ? ? ? ? ? ? ? ? ? flag = 1; ?
? ?cout << "你要?jiǎng)h除的聯(lián)系人為:" <<endl; ?
? ? ? ? ? ?cout << "ID: " << it->GetId() << endl; ?
? ? ? ? ? ?cout << "姓名:" << it->GetName() << endl; ?
? ? ? ? ? ?cout << "電話:" << it->GetTel() << endl; ?
? ? ? ? ? ?cout << "地址:" << it->GetAddr() << endl; ?
? ? ?
? ?cout << "確定刪除此聯(lián)系人嗎?y/n : "; ?
? ? ? ? ? ?char ch1; ?
? ? ? ? ? ?cin >> ch1; ?
? ? ? ? ? ?if (ch1 == 'y' || ch1 == 'Y') ?
? ? ? ? ? ?{ ?
? ? ? ? ? ? ? ?it = per.erase(it); ?
? ? ? ? ? ?} ?
} ?
else ?
{ ?
? ?++it; ?
} ?
? ?} ?
} ?
else ?
{ ?
? ?cout << "刪除方式選擇錯(cuò)誤,請(qǐng)重新選擇。" << endl; ?
? ?goto delete_loop; ?
} ?
??
??
if(0 == flag) ?
{ ?
? ?cout << "沒有找到此聯(lián)系人" << endl; ?
? ? ? ? } ?
else ?
{ ?
? ?cout << "刪除成功" << endl; ?
} ?
cout << "請(qǐng)按任意鍵退出" << endl; ?
char ch; ?
cin >> ch; ?
? ? } ?
? ? ??
} ?
void Info::modify() ?
{ ?
? ? vector<Info>::iterator it; ?
? ? if(per.empty()) ?
? ? { ?
? ? ? ? cout << "通訊錄暫無(wú)聯(lián)系人!" << endl; ?
? ? } ?
? ? else ?
? ? { ?
? ? modify_loop: ?
? ? ? ? string tn; ?
int flag2 = 0; ?
int flag = 0; //修改對(duì)象查找成功與否標(biāo)志位 ?
? ? ? ? cout << "請(qǐng)輸入你要編輯的人的姓名:" ; ?
? ? ? ? cin >> tn; ?
? ? ? ? for(it = per.begin(); it != per.end(); ) ?
{ ?
? ?if(!((it->GetName()).compare(tn))) ?
? ? ? ? ? ? { ?
? ? ? ?flag = 1; ?
cout << "你要修改的聯(lián)系人為:" <<endl; ?
? ? ? ?cout << "ID: " << it->GetId() << endl; ?
? ? ? ?cout << "姓名:" << it->GetName() << endl; ?
? ? ? ?cout << "電話:" << it->GetTel() << endl; ?
? ? ? ?cout << "地址:" << it->GetAddr() << endl; ?
? ? ?
cout << "確定修改此聯(lián)系人嗎?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 << "修改成功!該聯(lián)系人信息改為:" << 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 << "修改成功!該聯(lián)系人信息改為:" << 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 << "修改成功!該聯(lián)系人信息改為:" << endl; ?
? ? ? ? ? ? ? ? ? ?cout << "ID: " << it->GetId() << endl; ?
? ? ? ? ? ? ? ? ? ?cout << "姓名:" << it->GetName() << endl; ?
? ? ? ? ? ? ? ? ? ?cout << "電話:" << it->GetTel() << endl; ?
? ? ? ? ? ? ? ? ? ?cout << "地址:" << it->GetAddr() << endl; ?
? ?flag2 = 1; ?
? ?break; ?
} ?
default : ?
{ ?
? ?cout << "指令輸入錯(cuò)誤!" << endl; ?
? ?break; ?
} ?
? ?} ?
? ? ? ?} ?
else ?
{ ?
? ?break; ?
} ?
? ? ? ? ? ? } ?
? ? ? ? ? ? else ?
? ?{ ?
++it; ?
? ? ? ? ? ? } ?
} ?
if(0 == flag) ?
{ ?
? ?cout << "沒有找到此聯(lián)系人" << endl; ?
} ?
if(1 == flag2) ?
{ ?
? ?cout << "修改成功!" << endl; ?
} ? ? ?
cout << "請(qǐng)按任意鍵退出" << 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 << "請(qǐng)輸入你要實(shí)現(xiàn)的功能(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 << "輸入指令有誤,請(qǐng)重新輸入!" << endl; ?
? ?choose(); ?
? ?break; ?
} ?
? ? } ?
} ?
??
??
??
??
void Info::interface() ?
{ ?
? ? system("clear"); ?
??
??
? ? printf("\n"); ?
? ? printf("\n"); ?
? ? printf(" ? ? ? ? ?|***********************************************|\n"); ?
? ? printf(" ? ? ? ? ?|* ? ? ? ? ?多 功 能 電 子 通 訊 錄 ? ? ? ? ? ?*|\n"); ?
? ? printf(" ? ? ? ? ?|* ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? *|\n"); ?
? ? printf(" ? ? ? ? ?|* ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?版本號(hào):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(" ? ? ? ? ?|* ? ? ?請(qǐng)輸入你想要實(shí)現(xiàn)的功能: ? ? ? ? ? ? ? *|\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
歷時(shí)一天,終于把通訊錄寫好了。
項(xiàng)目要求:
復(fù)制代碼
編寫一個(gè)通訊錄管理程序。
有一已存在的通訊錄文件,數(shù)據(jù)內(nèi)容為各聯(lián)系人信息。
每個(gè)聯(lián)系人信息的組成部分為:
? ? 姓名、電話號(hào)碼和住址
? ? ? ? ? ? ? ? ? ? ? ? ? ? 等個(gè)人基本信息,
? ? ? ? ? ? ? ? ? ? ? ? ? ? 并假設(shè)已有兩個(gè)聯(lián)系人。
? ? ? ? ? ? ? ? ? ? ? ? ? ? 并假設(shè)已有兩個(gè)聯(lián)系人。
(1)輸出聯(lián)系人:打開通訊錄文件并顯示其中的數(shù)據(jù);
(2)添加聯(lián)系人;
(3)查找聯(lián)系人:利用字符串函數(shù),按“姓名”查找;
(4)修改聯(lián)系人:可以修改該聯(lián)系人的任一個(gè)信息;
(5)保存到文件:將操作結(jié)果保存到已存在的通訊錄文件;
(6)用子函數(shù)實(shí)現(xiàn)各個(gè)子功能。
復(fù)制代碼
?
通訊錄的 ? ? ? ?
核心類:VAdressBook
數(shù)據(jù)庫(kù):SQLite
編程語(yǔ)言:C++
? ? ? ? ? ? ? ? ?常用函數(shù):sprintf
? ? ? ? ? ? ? ? ?常用SQLiteAPI函數(shù):sqlite3_exec
還存在技術(shù)問題:重命問題(在翻譯完SQLite高級(jí)教程后可解決)
? ? ? ? ? ? ? ? 未使用UI(計(jì)劃使用wxWidgets或Java的圖形庫(kù) 或 SDL)
還存在的程序設(shè)計(jì)問題:使用了簡(jiǎn)單工廠設(shè)計(jì)模式,擴(kuò)展性不佳,維護(hù)性不佳。(重構(gòu)代碼)
現(xiàn)在的皺形效果圖為:
復(fù)制代碼
? 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 ? ? ?/*** ? ?姓名、電話號(hào)碼和住址 ? ?***/
?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 一個(gè)數(shù)字:\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 }
復(fù)制代碼
復(fù)制代碼
?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 }
復(fù)制代碼
想改進(jìn)這個(gè)項(xiàng)目的,請(qǐng)持續(xù)關(guān)注FDA—orangebook.
========
c/c++通訊錄
http://blog.csdn.net/rushierer/article/details/54633233第二個(gè)程序是C語(yǔ)言實(shí)訓(xùn)的程序
其實(shí)和第一個(gè)學(xué)生成績(jī)管理系統(tǒng)程序差不多,只是多了文件功能!
[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; ? ? ? ? /*編號(hào)*/ ?
? ? char name[15]; ? ? ?/*姓名*/ ?
? ? char phone[12]; ? ? /*手機(jī)*/ ?
? ? char qq[15]; ? ? ? ?/*QQ號(hào)*/ ?
}TELE; ?
typedef struct date ?
{ ?
? ? int infoCount; ? ? ?/*統(tǒng)計(jì)數(shù)據(jù)個(gè)數(shù)*/ ?
}DATE; ?
??
void showMenu(); ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?/*顯示菜單*/ ?
void mainMenu(TELE myBook[],int n,DATE date[]); ? ? ? /*顯示首頁(yè)*/ ?
void showdate(TELE myBook[],int n,DATE date[]); ? ? ? /*只顯示數(shù)據(jù)*/ ?
void input(TELE myBook[],int n,DATE date[]); ? ? ? ? ?/*從鍵盤輸入數(shù)據(jù)*/ ?
void searchR(TELE myBook[],int n,DATE date[]); ? ? ? ?/*查詢信息*/ ?
void searchByNumber(TELE myBook[],int n,DATE date[]); /*按編號(hào)查詢信息*/ ?
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[]); ? /*按編號(hào)排序*/ ?
void save(TELE myBook[],int n,DATE date[]); ? ? ? ? ? /*保存數(shù)據(jù)到文件*/ ?
void display(TELE myBook[],int n,DATE date[]); ? ? ? ?/*顯示數(shù)據(jù)*/ ?
void read(TELE myBook[],int n,DATE date[]); ? ? ? ? ? /*從文件讀取數(shù)據(jù)*/ ?
??
int main() ?
{ ?
? ? int choice; ?
? ? TELE myBook[N]; ?
? ? DATE date[1]; ?
? ? showMenu(); ?
? ? printf("\n"); ?
? ? printf("歡迎使用通訊錄!\n"); ?
? ? printf("\n"); ?
? ? printf("首次使用通訊錄,請(qǐng)輸入要添加聯(lián)系人的個(gè)數(shù):"); ?
? ? scanf("%d",&date[0].infoCount); ?
? ? printf("\n"); ?
? ? printf("1進(jìn)行錄入數(shù)據(jù) ?0退出:"); ?
? ? scanf("%d",&choice); ?
? ? switch(choice) ?
? ? { ?
? ? ? ? case 1: ?
? ? ? ? ? ? system("cls"); ?
? ? ? ? ? ? input(myBook,date[0].infoCount,date);break; ?
? ? ? ? case 0: ?
? ? ? ? ? ? exit(0);break; ?
? ? } ?
? ? return 0; ?
??
} ?
??
??
/*顯示選項(xiàng)Menu*/ ?
void showMenu() ?
{ ?
? ? printf(" ? ? ? ? ? ? ? ? ?通訊錄管理系統(tǒng) ? ? ? ? ? ? \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("請(qǐng)輸入選項(xiàng)(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; ?
??
? ? } ?
} ?
??
??
/*從鍵盤輸入聯(lián)系人信息*/ ?
void input(TELE myBook[],int n,DATE date[]) ?
{ ?
? ? int j,i; ?
? ? printf(" ? ? ? ? ? ? ? ? ? Input record ? ? ? ? ? ? ? ? ? ? ?\n"); ?
? ? printf("*****************************************************\n"); ?
? ? printf("\n"); ?
? ? printf("數(shù)據(jù)錄入格式提示:\n"); ?
? ? printf("1.數(shù)據(jù)內(nèi)容:編號(hào)、姓名、手機(jī)號(hào)碼、QQ號(hào)\n"); ?
? ? printf("2.數(shù)據(jù)間以空格做間隔,最后回車錄入數(shù)據(jù)結(jié)束!\n"); ?
? ? printf("\n"); ?
? ? for(i=0;i<n;i++) ?
? ? { ?
? ? ? ? printf("請(qǐng)輸入第%d個(gè)聯(lián)系人的信息:",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("數(shù)據(jù)錄入完成!\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("編號(hào) ? 姓名 ? ? ? ? ?電話號(hào)碼 ? ? ? ?QQ號(hào) ? ?\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); ?
??
} ?
??
/*只顯示數(shù)據(jù)*/ ?
void showdate(TELE myBook[],int n,DATE date[]) ?
{ ?
? ? int i; ?
? ? printf("已錄入的信息:\n"); ?
? ? printf("\n"); ?
? ? printf("編號(hào) ? 姓名 ? ? ? ? ?電話號(hào)碼 ? ? ? ?QQ號(hào) ? ?\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); ?
??
? ? } ?
} ?
??
/*查找數(shù)據(jù)*/ ?
void searchR(TELE myBook[],int n,DATE date[]) ?
{ ?
? ? int k; ?
? ? printf(" ? ? ? ? ? ? ? ? ? Search record ? ? ? ? ? ? ? ? ? ? \n"); ?
? ? printf("*****************************************************\n"); ?
? ? printf("\n"); ?
? ? printf("查找方式:1.按編號(hào)查找 2.按姓名查找\n"); ?
? ? printf("請(qǐng)選擇查找方式(1/2):"); ?
? ? scanf("%d",&k); ?
? ? printf("\n"); ?
? ? if(k==1) ?
? ? { ?
? ? ? ? searchByNumber(myBook,n,date); ?
? ? } ?
? ? else ?
? ? { ?
? ? ? ? searchByName(myBook,n,date); ?
? ? } ?
} ?
??
??
/*按編號(hào)查詢*/ ?
void searchByNumber(TELE myBook[],int n,DATE date[]) ?
{ ?
? ? int i,j,k=0; ?
? ? int number1; ?
? ? printf("請(qǐng)輸入想查找的編號(hào):"); ?
? ? scanf("%d",&number1); ?
? ? printf("\n"); ?
? ? printf("查找結(jié)果:"); ?
? ? 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("輸入的編號(hào)不存在或輸入格式不對(duì)!\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("請(qǐng)輸入想查找的姓名:"); ?
? ? scanf("%s",name1); ?
? ? printf("\n"); ?
? ? printf("查找結(jié)果:"); ?
? ? 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("輸入的姓名不存在或輸入格式不對(duì)!\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("已經(jīng)錄入%d個(gè)聯(lián)系人的信息,你想刪除第幾聯(lián)系人個(gè)的信息:",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號(hào)*/ ?
? ? printf(" ? ? ? ? ? ? ? ? ? Insert record ? ? ? ? ? ? ? ? ? ? \n"); ?
? ? printf("*****************************************************\n"); ?
? ? printf("\n"); ?
? ? showdate(myBook,n,date); ?
? ? printf("\n"); ?
? ? printf("你想在第幾個(gè)數(shù)據(jù)之后插入數(shù)據(jù):"); ?
? ? scanf("%d",&k); ?
? ? if(k>n||k<=0) ?
? ? { ?
? ? ? ? printf("\n"); ?
? ? ? ? printf("輸入錯(cuò)誤!\n"); ?
? ? ? ? printf("請(qǐng)重新輸入你想在第幾個(gè)數(shù)據(jù)之后插入數(shù)據(jù):"); ?
? ? ? ? scanf("%d",&i); ?
? ? ? ? k=i; ?
? ? } ?
? ? printf("\n"); ?
? ? printf("請(qǐng)輸入插入的信息:"); ?
? ? 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號(hào)*/ ?
? ? printf(" ? ? ? ? ? ? ? ? ? Modify record ? ? ? ? ? ? ? ? ? ? \n"); ?
? ? printf("*****************************************************\n"); ?
? ? printf("\n"); ?
? ? showdate(myBook,n,date); ?
? ? printf("\n"); ?
? ? printf("已經(jīng)錄入%d個(gè)聯(lián)系人的信息,你想修改第幾個(gè)聯(lián)系人的記錄:",n); ?
? ? scanf("%d",&k); ?
? ? if(k>n||k<=0) ?
? ? { ?
? ? ? ? printf("\n"); ?
? ? ? ? printf("輸入錯(cuò)誤,無(wú)這條記錄!\n"); ?
? ? ? ? printf("請(qǐng)重新輸入你想修改第幾個(gè)聯(lián)系人的記錄:"); ?
? ? ? ? scanf("%d",&i); ?
? ? ? ? k=i; ?
? ? } ?
? ? printf("\n"); ?
? ? printf("請(qǐng)輸入修改后的信息:"); ?
? ? 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.按編號(hào)排序 ?2.按姓名排序\n"); ?
? ? printf("請(qǐng)選擇排序方式(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(" 姓名 ? ? ? ?編號(hào) ? ?電話號(hào)碼 ? ? ? ?QQ號(hào) ? ?\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; ?
? ? } ?
} ?
??
/*按編號(hào)排序信息*/ ?
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("編號(hào) ? 姓名 ? ? ? ? ?電話號(hào)碼 ? ? ? ?QQ號(hào) ? ?\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++) ?//對(duì)全部信息按序號(hào)排序后再保存到文件 ?
? ? ? ? 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); ?
} ?
??
??
/*從文件讀取數(shù)據(jù)*/ ?
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("讀取信息結(jié)果:\n"); ?
? ? printf("\n"); ?
? ? if((fp=fopen("teleBook.txt","r"))==NULL) ? ? ? ?/*以讀方式打開文本文件*/ ?
? ? { ?
? ? ? ? printf("Failure to open teleBook.txt!\n"); ?
? ? ? ? printf("可能沒有保存數(shù)據(jù),可以返回主菜單先保存數(shù)據(jù)!\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++) ? ? ? ? ? ? ?/*若未讀到文件末尾,則繼續(xù)讀*/ ?
? ? { ?
? ? ? ? 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("編號(hào) ? 姓名 ? ? ? ? ?電話號(hào)碼 ? ? ? ?QQ號(hào) ? ?\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); ?
}?
========
總結(jié)
以上是生活随笔為你收集整理的C++ 通讯录学习总结的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: C++内联函数学习总结
- 下一篇: VC++ .Net 实例学习