一个c++的例子-通讯录
参考B站黑马c++视频教程,这里推荐vscode一个extension,koroFileHeader
可以自动写头注释,非常方便
为啥用英语?因为code runner运行的时候中文输出到terminal为乱码,懒得搞
注意,在vsocode中在struct指针后输入.之后也会自动边为->,我一开始都手动输入的。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 | /* * @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 ; } } } |
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· AI与.NET技术实操系列:基于图像分类模型对图像进行分类
· go语言实现终端里的倒计时
· 如何编写易于单元测试的代码
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
· 分享一个免费、快速、无限量使用的满血 DeepSeek R1 模型,支持深度思考和联网搜索!
· 基于 Docker 搭建 FRP 内网穿透开源项目(很简单哒)
· ollama系列01:轻松3步本地部署deepseek,普通电脑可用
· 25岁的心里话
· 按钮权限的设计及实现