一个c++的例子-通讯录
参考B站黑马c++视频教程,这里推荐vscode一个extension,koroFileHeader
可以自动写头注释,非常方便
为啥用英语?因为code runner运行的时候中文输出到terminal为乱码,懒得搞
注意,在vsocode中在struct指针后输入.之后也会自动边为->,我一开始都手动输入的。
/* * @Author: your name * @Date: 2021-08-05 11:27:35 * @LastEditTime: 2021-08-06 10:06:16 * @LastEditors: Please set LastEditors * @Description: In User Settings Edit * @FilePath: \c++\tongxunlu\main.cpp */ #include <iostream> #include <string> using namespace std; #define MAX 1000 struct Person { string m_name; int m_sex; int m_age; string m_phone; string m_address; }; struct AddressBook { struct Person personArr[MAX]; int m_size; }; void showMenu() { cout << "*************************" << endl; cout << "**** 1 Add *******" << endl; cout << "**** 2 Display *******" << endl; cout << "**** 3 Delete *******" << endl; cout << "**** 4 Find *******" << endl; cout << "**** 5 Modify *******" << endl; cout << "**** 6 Clear *******" << endl; cout << "**** 0 Quit *******" << endl; cout << "*************************" << endl; } void getOnePerson(AddressBook *book, int index) { cout << "name: "; cin >> book->personArr[index].m_name; // cout << "your name is: " << book->personArr[size].m_name << endl; int sex; while (true) { cout << "sex (1-male,2-female):"; cin >> sex; if (sex == 1 || sex == 2) { book->personArr[index].m_sex = sex; break; } } int age; while (true) { cout << "age (from 0 to 150):"; cin >> age; if (age >= 0 && age <= 150) { book->personArr[index].m_age = age; break; } } } void add(AddressBook *book) { // string name; // int sex; // int age; // string phone; // string address; if (book->m_size == MAX) { cout << "full!" << endl; return; } else { int size = book->m_size; getOnePerson(book, size); book->m_size += 1; cout << "one person added!" << endl; system("pause"); system("cls"); } } void displayOnePerson(AddressBook *book, int index) { string name = book->personArr[index].m_name; int sex = book->personArr[index].m_sex; int age = book->personArr[index].m_age; cout << name << "\t" << (sex == 1 ? "male" : "female") << "\t" << age << endl; } void display(AddressBook *book) { int size = book->m_size; if (size == 0) { cout << "the book is empty!" << endl; } else { cout << "name\tsex\tage" << endl; for (int i = 0; i < size; i++) { displayOnePerson(book, i); } } system("pause"); system("cls"); } int isExist(AddressBook *book, string name) { for (int i = 0; i < book->m_size; i++) { if (book->personArr[i].m_name == name) { return i; } } return -1; } void deletePerson(AddressBook *book) { string name; cout << "the name to delete: "; cin >> name; int is_exist; is_exist = isExist(book, name); if (is_exist == -1) { cout << "not exist" << endl; } else { cout << "exist" << endl; cout << "deleted" << endl; } } void find(AddressBook *book) { string name; cout << "the name to find: "; cin >> name; int is_exist; is_exist = isExist(book, name); int index; index = is_exist; if (is_exist != -1) { cout << "finded" << endl; cout << "name\tsex\tage" << endl; displayOnePerson(book, index); } else { cout << "not exist" << endl; } } void modify(AddressBook *book) { string name; cout << "the name to modify: "; cin >> name; int is_exist; is_exist = isExist(book, name); int index; index = is_exist; if (is_exist != -1) { cout << "the person finded" << endl; getOnePerson(book, index); cout << "modified" << endl; } else { cout << "not exist" << endl; } } void clear(AddressBook *book) { book->m_size = 0; cout << "cleared" << endl; } int main() { AddressBook book; book.m_size = 0; int select = 0; while (true) { showMenu(); cin >> select; switch (select) { case 1: //add add(&book); break; case 2: //display display(&book); break; case 3: //delete deletePerson(&book); break; case 4: //find find(&book); break; case 5: //modify modify(&book); break; case 6: //clear clear(&book); break; case 0: //quit cout << "quited" << endl; system("pause"); return 0; break; default: break; } } }