C++通讯录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.