【软件开发综合实验】电话号码本
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 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 | #include<cstdio> #include<iostream> #include<string> #include<cstring> #include<list> #include<conio.h> #include<windows.h> using namespace std; typedef unsigned long long ull; const int MOD=1009; const ull base=131; struct data{ string name,tel,add; data( const string &name, const string &tel, const string &add){ this ->name=name; this ->tel=tel; this ->add=add; } data( const char * name, const char * tel, const char * add){ this ->name=name; this ->tel=tel; this ->add=add; } data(){} }; typedef list<data>::iterator ITER; bool type; list<data>L[2][MOD]; data H[2][MOD]; bool Full[2][MOD]; int sz; ull GetHkey( const string &s); int SearchHTable_name( const string &name); //返回所查找姓名在哈希表中的下标,如该姓名不存在,返回-1 int SearchHTable_tel( const string &tel); //返回所查找电话号码在哈希表中的下标,如该电话号码不存在,返回-1 bool InsertHTable( const data &man); bool DeleteHTable( const string &name); void CreateHTable( const bool &op); //op为0 链地址法; op为1 开放地址法+线性探测再散列 void DisplayHTable(); int main(){ int opt; printf ( "\n\n\t\t欢迎使用电话簿!请您先选择合适的哈希方式!\n" ); printf ( "\n\t\t请输入选项前的数字进行选择\n" ); printf ( "\n\t\t1. 链地址法\n" ); printf ( "\n\t\t2. 开放地址法 + 线性探测再散列\n" ); printf ( "\n\t\t" ); scanf ( "%d" ,&opt); CreateHTable(( bool )(opt-1)); while (1){ system ( "cls" ); printf ( "\n\n\t\t您正在使用基于" ); if (!type){ printf ( " 链地址法 " ); } else { printf ( " 开放地址法 + 线性探测再散列 " ); } printf ( "的电话簿,请选择您想使用的功能!\n" ); printf ( "\n\t\t1. 向电话簿中插入一个人的信息\n" ); printf ( "\n\t\t2. 根据输入的姓名查询其完整信息\n" ); printf ( "\n\t\t3. 根据输入的电话号码查询其完整信息\n" ); printf ( "\n\t\t4. 删除一个已存在的人的信息\n" ); printf ( "\n\t\t5. 展示完整的电话簿\n" ); printf ( "\n\t\t6. 将当前电话簿保存到文件\n" ); printf ( "\n\t\t7. 退出\n" ); printf ( "\n\t\t" ); scanf ( "%d" ,&opt); if (opt==1){ string Name,Tel,Add; system ( "cls" ); printf ( "\n\n\t\t请依次键入姓名 电话号码 住址(以空格分隔)!\n\n\t\t" ); cin>>Name>>Tel>>Add; if (Name.length()>20 || Tel.length()>15 || Add.length()>50){ printf ( "\n\n\t\t输入不合法,插入失败!" ); } else if (InsertHTable(data(Name,Tel,Add))){ printf ( "\n\n\t\t插入成功!" ); } else { if (sz==MOD){ printf ( "\n\n\t\t电话簿已满,插入失败!" ); } else { printf ( "\n\n\t\t已存在,插入失败!" ); } } } else if (opt==2){ string Name; system ( "cls" ); printf ( "\n\n\t\t请键入姓名!\n\n\t\t" ); cin>>Name; int p; if ((p=SearchHTable_name(Name))!=-1){ printf ( "\n\t\t姓名 电话号码 住址" ); if (!type){ for (ITER it=L[0][p].begin();it!=L[0][p].end();++it){ if (it->name==Name){ cout<< "\n\n\t\t" <<it->name<< ' ' <<it->tel<< ' ' <<it->add; break ; } } } else { cout<< "\n\n\t\t" <<H[0][p].name<< ' ' <<H[0][p].tel<< ' ' <<H[0][p].add; } } else { printf ( "\n\n\t\t您要查找的姓名不存在!" ); } } else if (opt==3){ string Tel; system ( "cls" ); printf ( "\n\n\t\t请键入电话号码!\n\n\t\t" ); cin>>Tel; int p; if ((p=SearchHTable_tel(Tel))!=-1){ printf ( "\n\t\t姓名 电话号码 住址" ); if (!type){ for (ITER it=L[1][p].begin();it!=L[1][p].end();++it){ if (it->tel==Tel){ cout<< "\n\n\t\t" <<it->name<< ' ' <<it->tel<< ' ' <<it->add; break ; } } } else { cout<< "\n\n\t\t" <<H[1][p].name<< ' ' <<H[1][p].tel<< ' ' <<H[1][p].add; } } else { printf ( "\n\n\t\t您要查找的电话号码不存在!" ); } } else if (opt==4){ string Name; system ( "cls" ); printf ( "\n\n\t\t请键入要删除的人的姓名!\n\n\t\t" ); cin>>Name; int p; if (DeleteHTable(Name)){ printf ( "\n\n\t\t删除成功!" ); } else { printf ( "\n\n\t\t您要删除的姓名不存在!" ); } } else if (opt==5){ DisplayHTable(); } else if (opt==6){ system ( "cls" ); printf ( "\n\n\t\t保存成功!" ); FILE *fp= fopen ( "TELETABLE.txt" , "w" ); if (!type){ for ( int i=0;i<MOD;++i){ for (ITER it=L[0][i].begin();it!=L[0][i].end();++it){ fprintf (fp, "%s %s %s\n" ,(it->name).c_str(),(it->tel).c_str(),(it->add).c_str()); } } } else { for ( int i=0;i<MOD;++i){ if (Full[0][i]){ fprintf (fp, "%s %s %s\n" ,(H[0][i].name).c_str(),(H[0][i].tel).c_str(),(H[0][i].add).c_str()); } } } fclose (fp); } else { return 0; } printf ( "\n\n\t\t按任意键返回主菜单!" ); while (!kbhit()); getch(); } return 0; } ull GetHkey( const string &s){ int len=s.length(); ull hs=0; for ( int i=0;i<len;++i){ hs=hs*base+s[i]; } return hs; } int SearchHTable_name( const string &name){ ull hs=GetHkey(name); int U=( int )(hs%(ull)MOD); if (!type){ for (ITER it=L[0][U].begin();it!=L[0][U].end();++it){ if (it->name==name){ return U; } } return -1; } else { for ( int i=U;i!=(U-1+MOD)%MOD;i=(i+1)%MOD){ if (!Full[0][i]){ return -1; } if (H[0][i].name==name){ return i; } } return -1; } } int SearchHTable_tel( const string &tel){ ull hs=GetHkey(tel); int U=( int )(hs%(ull)MOD); if (!type){ for (ITER it=L[1][U].begin();it!=L[1][U].end();++it){ if (it->tel==tel){ return U; } } return -1; } else { for ( int i=U;i!=(U-1+MOD)%MOD;i=(i+1)%MOD){ if (!Full[1][i]){ return -1; } if (H[1][i].tel==tel){ return i; } } return -1; } } bool InsertHTable( const data &man){ if (sz==MOD || SearchHTable_name(man.name)!=-1){ return false ; } ++sz; int U[2]; U[0]=( int )(GetHkey(man.name)%(ull)MOD); U[1]=( int )(GetHkey(man.tel)%(ull)MOD); if (!type){ for ( int i=0;i<=1;++i){ L[i][U[i]].push_back(man); } } else { for ( int i=0;i<=1;++i){ for ( int j=U[i];j!=(U[i]-1+MOD)%MOD;j=(j+1)%MOD){ if (!Full[i][j]){ H[i][j]=man; Full[i][j]= true ; break ; } } } } return true ; } bool DeleteHTable( const string &name){ int p[2]; p[0]=SearchHTable_name(name); if (p[0]==-1){ return false ; } string tel; if (!type){ for (ITER it=L[0][p[0]].begin();it!=L[0][p[0]].end();++it){ if (it->name==name){ tel=it->tel; break ; } } } else { tel=H[0][p[0]].tel; } p[1]=SearchHTable_tel(tel); if (!type){ for ( int i=0;i<=1;++i){ for (ITER it=L[i][p[i]].begin();it!=L[i][p[i]].end();++it){ if (it->name==name){ L[i][p[i]].erase(it); break ; } } } } else { Full[0][p[0]]=Full[1][p[1]]= false ; } return true ; } void CreateHTable( const bool &op){ type=op; sz=0; if (!op){ for ( int i=0;i<=1;++i){ for ( int j=0;j<MOD;++j){ L[i][j].clear(); } } } else { memset (Full,0, sizeof (Full)); } FILE *fp= fopen ( "TELETABLE.txt" , "r" ); char tname[21],ttel[16],tadd[51]; while ( fscanf (fp, "%s%s%s" ,tname,ttel,tadd)!=EOF){ InsertHTable(data(tname,ttel,tadd)); } fclose (fp); } void DisplayHTable(){ system ( "cls" ); printf ( "\n\t\t姓名 电话号码 住址\n" ); if (!type){ for ( int i=0;i<MOD;++i){ for (ITER it=L[0][i].begin();it!=L[0][i].end();++it){ cout<< "\n\t\t" <<it->name<< ' ' <<it->tel<< ' ' <<it->add<<endl; } } } else { for ( int i=0;i<MOD;++i){ if (Full[0][i]){ cout<< "\n\t\t" <<H[0][i].name<< ' ' <<H[0][i].tel<< ' ' <<H[0][i].add<<endl; } } } } |
——The Solution By AutSky_JadeK From UESTC
转载请注明出处:http://www.cnblogs.com/autsky-jadek/
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步