摘要:本文实现了使用C++程序代码模拟手机通讯录功能,有添加,删除,修改联系人等功能,这是一个入门C++时写的的命令行模拟程序,并非界面程序实现!
代码如下:
- 主函数
//main.cpp #include"Edit_contect.h" using namespace std; int main() { //Part 1:main Interface for (int i = 0; i < 30; i++){ cout << " " << "*"; }cout << endl; cout << " * Contects *" << endl; cout << " * *" << endl; cout << " * 1.Add_contect 2.View_contectlist *" << endl; cout << " * *" << endl; cout << " * 3.Delete_contect 4.Save_contect *" << endl; cout << " * *" << endl; cout << " * 5.Edit_contect 6.Close_contect *" << endl; cout << " * *" << endl; cout << " * *" << endl; cout << " * *" << endl; cout << " * Attention:You have to ADD CONTECTS first for *" << endl; cout << " * *" << endl; cout << " * using other functions correctly! *" << endl; cout << " * *" << endl; cout << " * You can add 15 contects at most. *" << endl; for (int i = 0; i < 2; i++){ cout << " " << "*" << " " << "*" << endl; } for (int i = 0; i < 30; i++){ cout << " " << "*"; }cout << endl; //Part 2: functions int Noun = 1; cout << "Please choose the function you need: "; cin >> Noun; while (Noun != 6)//loop structure { if (Noun == 1) { Add_contect(); cout << "Please choose the function you need: "; cin >> Noun; }//this funcion for adding contect else if (Noun == 2){ View_contect(); cout << "Please choose the function you need: "; cin >> Noun; }//this funcion for viewing contect else if (Noun == 3){ Delete_contect(); cout << "Please choose the function you need: "; cin >> Noun; }//this funcion for deleting contect else if (Noun == 4){ Save_contect(); cout << "Please choose the function you need: "; cin >> Noun; }//this funcion for showing all contects else if (Noun == 5){ Edit_contect(); cout << "Please choose the function you need: "; cin >> Noun; }//this funcion for editting contect } if (Noun == 6){ cout << "Successfully to stop!" << endl; } //Part 3: Over system("pause"); return 0; }
- 添加联系人
//Add_contect.h: //this part for adding contects #include #include #include using namespace std; string NAME[15]; string NUMBER[15]; string CATEGORY[15]; string EMAIL[15]; string info[15]; void Add_contect() { int GOON = 1; cout << "Input contect's NAME TELEPHONE_NUMBER CATEGORY EMAIL" << endl; cout << "Attention:Input ONE item at a time!" << endl; for (int i = 1; i < 20; i++) { if (i <= 15) { cout << "NAME " << i << " : "; cin >> NAME[i]; if (i >= 2) { for (int k = 1; k <= 15; k++) { if (NAME[i] == NAME[k]){ cout << "Contact already exists!Please enter another contect: "; i = i - 1; break; } else { cout << "TELEPHONE_NUMBER " << i << " : "; cin >> NUMBER[i]; cout << "CATEGORY " << i << " : "; cin >> CATEGORY[i]; cout << "EMAIL " << i << " : "; cin >> EMAIL[i]; break; } } } cout << "GO ON? 1.YES 2.NO" << endl; cin >> GOON; if (GOON == 2)break; else if (GOON == 1)continue; } else cout << "There is no extra space!" << endl; } }
- 删除联系人
//Delete_contect.h //this part for deleting contects #include"View_contect.h" void Delete_contect() { string NAME1; int N; for (int i = 1; i <= 15; i++) { cout << "Input the name you want to delete(ONE NAME AT A TIME): " << endl; cin >> NAME1; if (NAME1 == NAME[i]) { cout << "Are you sure to delete the infomation of " << NAME1 << ". 1.YES 2.NO " << endl; cin >> N; if (N == 1) { NAME[i] = NAME[i + 1]; NUMBER[i] = NUMBER[i + 1]; CATEGORY[i] = CATEGORY[i + 1]; EMAIL[i] = EMAIL[i + 1]; cout << "Deleting finished!" << endl; } else break; cout << "1.Go on 2.Return "; cin >> N; if (N == 2)break; else if (N == 1)continue; else { cout << "ERROR!"; break; } } } }
- 保存联系人
//Save_contect.h //this part for saving contects #include #include #include"Delete_contect.h" void Save_contect() { ofstream Save_file; Save_file.open("E:\contect.txt"); if (Save_file.is_open()) { for (int i = 1; i <= 15; i++) { if (!NAME[i].empty()) { Save_file << i << NAME[i] << NUMBER[i] << CATEGORY[i] << EMAIL[i]; } Save_file.close(); Save_file.close(); } cout << "Saving contects finished!" << endl; } else cout << "Can't create file contect.txt" << endl; }
- 编辑联系人
//Edit_contect.h //this part for editing contects #include #include"Save_contect.h" void Edit_contect() { string name2; int N, M; for (int i = 1; i <= 15; i++) { cout << "Input the name you want to edit: "; cin >> name2; if (name2 == NAME[i]) { cout << "which item do you want to edit? 1." << NAME[i] << " 2." << NUMBER[i] << " 3." << CATEGORY[i] << " 4." << EMAIL[i] << endl; cin >> N; if (N == 1){ cout << "Input new name: "; cin >> NAME[i]; } else if (N == 2){ cout << "Input new number: "; cin >> NUMBER[i]; } else if (N == 3){ cout << "Input new category: "; cin >> CATEGORY[i]; } else if (N == 4){ cout << "Input new email: "; cin >> EMAIL[i]; } else { cout << "ERROR!"; break; } cout << "new item has been saved!" << endl; } else { cout << "ERROR!"; break; } cout << "Go on? 1.Yes 2.No "; cin >> M; if (M == 1)continue; else if (M == 2)break; else cout << "ERROR!"; break; } }
- 查看联系人
//View_contect.h // this part for viewing contects #include #include"Add_contect.h" void View_contect() { char category; cout << "Please choose category(A、B or C)(A.办公类 B.个人类 C.商务类): "; cin >> category; if (category == 'A') { int N = 1; for (int i = 1;; i++) { if (CATEGORY[i] == "办公类"){ cout << N << " " << NAME[i] << " " << NUMBER[i] << endl; N++; } else if (i > 15) break; } } else if (category == 'B') { int N = 1; for (int i = 1;; i++) { if (CATEGORY[i] == "个人类"){ cout << N << " " << NAME[i] << " " << NUMBER[i] << endl; N++; } else if (i > 15)break; } } else if (category == 'C') { int N = 1; for (int i = 1;; i++) { if (CATEGORY[i] == "商务类"){ cout << N << " " << NAME[i] << " " << NUMBER[i] << endl; N++; } else if (i > 15)break; } } }