1#include <stdio.h> 2#include <windows.h> 3#include <io.h> 4#include <stdlib.h> 5#include <string.h> 6#include <conio.h> 7#include <ctype.h> 8#define WIDTH 40 9 10struct info { 11 char name[20], mo_tel[20], home_tel[20]; 12 char address[20], text[100], e_mail[30]; 13}; 14 15info person[200]; 16int indexx; 17FILE *F; 18char Name[30]; 19 20void logo(int); 21void tips(int); 22void print_login(); 23void Register(); 24bool login(); 25void print_mulu(); 26void io(info, int, FILE); 27void insert(); 28void query(); 29void choise(); 30void work(); 31void solve(); 32void edit(); 33void search(); 34void show(); 35void off(); 36 37 38void logo(int flag) { 39 system("cls"); 40 for(int i = 0; i < WIDTH; i ++) putchar('='); puts(""); 41 for(int i = 0; i < WIDTH / 4; i ++) putchar(' '); 42 puts("欢迎使用XX名片管理系统"); 43 for(int i = 0; i < WIDTH; i ++) putchar('='); puts(""); 44 if(flag == 1) 45 printf("您现在所登陆的是%s的通讯录\n", Name); 46} 47 48void tips(int flag) { 49 if(flag == 1) { 50 puts("提示:您尚未在通讯录中添加任何信息!"); 51 return; 52 } 53 if(flag == 2) { 54 puts("请输入一张名片的信息:"); 55 puts("姓名 移动电话 固定电话 地址 电子邮件 备注"); 56 return; 57 } 58 if(flag == 3) { 59 printf("用户%s的通讯录记录如下:\n", Name); 60 } 61} 62 63void print_login() { 64 65 puts("1、注册"); 66 puts("2、登陆"); 67 puts("3、注销"); 68 puts("4、退出"); 69 printf("请选择操作:"); 70} 71 72void get_password(char ch[], int flag) { 73 gets(ch); 74 int len = 0; 75 while( (ch[len] = getch()) != '\x0d' ) { 76 if( ch[len] == '\x08' ) { 77 system("cls"); 78 logo(0); 79 if(flag == 0) printf("输入你要创建的账号:%s\n\n", Name); 80 if(flag == 1) printf("输入你的账号:%s\n\n", Name); 81 printf("输入你的密码:"); 82 if(len == 0) continue; 83 len --; 84 for(int i = 0; i < len; i ++) putchar('*'); 85 continue; 86 } 87 len ++; 88 putchar('*'); 89 } 90 ch[len] = 0; 91 puts(""); 92} 93 94void Register() { 95 logo(0); 96 FILE *fp; 97 char name[30], key[30]; 98 int num = 0; 99 printf("输入你要创建的账号:"); scanf("%s", name); puts(""); 100 strcpy(Name, name); 101 printf("输入你的密码:"); get_password(key, 0); 102 char ffile[40]; 103 strcpy(ffile, name); 104 strcat(ffile, ".data"); 105 if(access(ffile,0)) { 106 fp = fopen(ffile, "w"); 107 fputs(name, fp); fputs(key, fp); 108 fclose(fp); 109 puts("创建用户成功"); 110 system("pause"); 111 } else { 112 puts("用户名冲突!"); 113 system("pause"); 114 } 115} 116 117bool login() { 118 FILE *fp; 119 char name[30], key[30], ffile[40];; 120 logo(0); 121 printf("输入你的账号:"); scanf("%s", name); puts(""); 122 strcpy(Name, name); 123 printf("输入你的密码:"); get_password(key, 1); 124 strcpy(ffile, name); 125 strcat(ffile, ".data"); 126 if( access(ffile,0) ) { 127 puts("该用户不存在!"); 128 system("pause"); 129 return false; 130 } else { 131 char tt[60]; 132 fp = fopen(ffile, "r"); 133 fscanf(fp, "%s", tt); 134 strcpy(Name, name); 135 strcat(name, key); 136 if( !strcmp(name, tt) ) { 137 puts("成功登陆!"); 138 system("pause"); 139 fclose(fp); 140 return true; 141 } else { 142 puts("密码错误"); 143 system("pause"); 144 fclose(fp); 145 return false; 146 } 147 } 148} 149 150void off() { 151 FILE *fp; 152 char name[30], key[30], ffile[40];; 153 logo(0); 154 printf("输入你的账号:"); scanf("%s", name); puts(""); 155 strcpy(Name, name); 156 printf("输入你的密码:"); get_password(key, 1); 157 strcpy(ffile, name); 158 strcat(ffile, ".data"); 159 if( access(ffile,0) ) { 160 puts("该用户不存在!"); 161 system("pause"); 162 return; 163 } else { 164 char tt[60]; 165 fp = fopen(ffile, "r"); 166 fscanf(fp, "%s", tt); 167 strcpy(Name, name); 168 strcat(name, key); 169 fclose(fp); 170 if( !strcmp(name, tt) ) { 171 remove(ffile); 172 strcpy(ffile, Name); 173 strcat(ffile, ".dat"); 174 remove(ffile); 175 strcpy(ffile, Name); 176 strcat(ffile, ".txt"); 177 remove(ffile); 178 puts("成功注销!"); 179 system("pause"); 180 return; 181 } else { 182 puts("密码错误"); 183 system("pause"); 184 return; 185 } 186 } 187 188} 189 190void print_mulu() { 191 puts("1、插入一张名片"); 192 puts("2、编辑名片"); 193 puts("3、删除名片"); 194 puts("4、查询所有记录"); 195 puts("5、搜索名片"); 196 puts("6、退出"); 197 printf("请选择您需要的服务:"); 198} 199 200void io(info &temp, int flag, FILE *fp) { // 0->控制台读入 1->写入文件 2->控制台打印 3->从文件读入 201 if(flag == 0) 202 scanf("%s %s %s %s %s %s", temp.name, temp.mo_tel, temp.home_tel, 203 temp.address, temp.e_mail, temp.text); 204 else if(flag == 1) 205 fprintf(fp, "%s %s %s %s %s %s\n", temp.name, temp.mo_tel, temp.home_tel, 206 temp.address, temp.e_mail, temp.text); 207 else if(flag == 2) 208 printf("%s %s %s %s %s %s\n", temp.name, temp.mo_tel, temp.home_tel, 209 temp.address, temp.e_mail, temp.text); 210 else if(flag == 3) 211 fscanf(fp, "%s %s %s %s %s %s", temp.name, temp.mo_tel, temp.home_tel, 212 temp.address, temp.e_mail, temp.text); 213} 214 215void insert() { 216 FILE *fp; 217 logo(1); 218 info temp; 219 tips(2); //logo 220 221 char ffile[40]; 222 strcpy(ffile, Name); 223 strcat(ffile, ".dat"); 224 if( access(ffile, 0) ) { 225 fp = fopen(ffile, "w"); 226 fprintf(fp, "0"); 227 fclose(fp); 228 strcpy(ffile, Name); 229 strcat(ffile, ".txt"); 230 fp = fopen(ffile, "w"); 231 fclose(fp); 232 } //文件不存在则创建文件 233 234 strcpy(ffile, Name); 235 strcat(ffile, ".dat"); 236 fp = fopen(ffile, "r"); 237 int num; 238 fscanf(fp, "%d", &num); 239 num ++; 240 fclose(fp); 241 242 fp = fopen(ffile, "w+"); 243 fprintf(fp, "%d\n", num); 244 fclose(fp); //更新记录条数 245 246 strcpy(ffile, Name); 247 strcat(ffile, ".txt"); 248 249 fp = fopen(ffile, "a+"); 250 io(temp, 0, fp); 251 io(temp, 1, fp); 252 fclose(fp); //从控制台读入记录,存入文件 253} 254 255void query() { 256 FILE *fp; 257 char ffile[40]; 258 logo(1); 259 tips(3); //输出logo 260 261 int num; 262 strcpy(ffile, Name); 263 strcat(ffile, ".dat"); 264 if( access(ffile, 0) ) { 265 puts("无记录!"); 266 system("pause"); 267 return; 268 } 269 fp = fopen(ffile, "r"); 270 fscanf(fp, "%d", &num); 271 fclose(fp); 272 printf("一共%d条记录:\n", num); //读取记录条数 273 274 strcpy(ffile, Name); 275 strcat(ffile, ".txt"); 276 fp = fopen(ffile, "r"); 277 for(int i = 0; i < num; i ++) { 278 info temp; 279 io(temp, 3, fp); 280 printf("%d: ", i); 281 io(temp, 2, fp); 282 } 283 fclose(fp); //读入记录输出至屏幕 284 285 system("pause"); 286} 287 288void change(char ch[], char st[]) { 289 if( strcmp(ch, "NULL") ) { 290 strcpy(st, ch); 291 } 292} 293 294void edit() { 295 logo(1); 296 char ffile[40]; 297 FILE *fp; 298 strcpy(ffile, Name); 299 strcat(ffile, ".dat"); 300 if( access(ffile, 0) ) { 301 puts("无记录可供编辑!"); 302 system("pause"); 303 return; 304 } 305 fp = fopen(ffile, "r"); 306 int num; 307 fscanf(fp, "%d", &num); //读入记录条数 308 fclose(fp); 309 310 printf("请输入您想修改的记录编号 :"); 311 int indexx; 312 scanf("%d", &indexx); 313 if(indexx < 0 || indexx >= num) { 314 puts("对不起,不存在对应信息!"); 315 system("pause"); 316 return; 317 } else { 318 info temp[1000]; 319 strcpy(ffile, Name); 320 strcat(ffile, ".txt"); 321 fp = fopen(ffile, "r"); 322 for(int i = 0; i < num; i ++) { 323 io(temp[i], 3, fp); 324 } 325 fclose(fp); 326 fp = fopen(ffile, "w+"); 327 puts("请修改改信息(如无需修改则对应项输入NULL)"); 328 puts("姓名 移动电话 固定电话 地址 电子邮件 备注"); 329 info t; 330 io(t, 0, fp); 331 change(t.name, temp[indexx].name); 332 change(t.mo_tel, temp[indexx].mo_tel); 333 change(t.home_tel, temp[indexx].home_tel); 334 change(t.address, temp[indexx].address); 335 change(t.text, temp[indexx].text); 336 change(t.e_mail, temp[indexx].e_mail); 337 for(int i = 0; i < num; i ++) { 338 io(temp[i], 1, fp); 339 } 340 puts("修改成功!"); 341 system("pause"); 342 fclose(fp); 343 } 344} 345 346void print_search_type() { 347 puts("1、按姓名搜索"); 348 puts("2、按移动电话搜索"); 349 puts("3、按固定电话搜索"); 350 puts("4、按地址搜索"); 351 puts("5、按电子邮件搜索"); 352 puts("6、返回上一层"); 353} 354 355void search() { 356 logo(1); 357 print_search_type(); 358 printf("请选择您需要的服务:"); 359 int cho; 360 info p[1000]; 361 362 char ffile[40]; 363 FILE *fp; 364 strcpy(ffile, Name); 365 strcat(ffile, ".dat"); 366 if( access(ffile, 0) ) { 367 puts("无记录可供编辑!"); 368 system("pause"); 369 return; 370 } 371 fp = fopen(ffile, "r"); 372 int num; 373 fscanf(fp, "%d", &num); //读入记录条数 374 fclose(fp); 375 376 strcpy(ffile, Name); 377 strcat(ffile, ".txt"); 378 fp = fopen(ffile, "r"); 379 for(int i = 0; i < num; i ++) { 380 io(p[i], 3, fp); 381 } 382 fclose(fp); 383 scanf("%d", &cho); 384 char ch[30]; 385 int cnt = 0; 386 switch(cho) { 387 case 1: 388 printf("请输入姓名"); 389 scanf("%s", ch); 390 for(int i = 0; i < num; i ++) { 391 if( !strcmp(ch, p[i].name) ) { 392 printf("%d: ", cnt++); 393 io(p[i], 2, fp); 394 } 395 } 396 break; 397 case 2: 398 printf("请输入移动电话"); 399 scanf("%s", ch); 400 for(int i = 0; i < num; i ++) { 401 if( !strcmp(ch, p[i].mo_tel) ) { 402 printf("%d: ", cnt++); 403 io(p[i], 2, fp); 404 } 405 } 406 break; 407 case 3: 408 printf("请输入固定电话"); 409 scanf("%s", ch); 410 for(int i = 0; i < num; i ++) { 411 if( !strcmp(ch, p[i].home_tel) ) { 412 printf("%d: ", cnt++); 413 io(p[i], 2, fp); 414 } 415 } 416 break; 417 case 4: 418 printf("请输入地址"); 419 scanf("%s", ch); 420 for(int i = 0; i < num; i ++) { 421 if( !strcmp(ch, p[i].address) ) { 422 printf("%d: ", cnt++); 423 io(p[i], 2, fp); 424 } 425 } 426 break; 427 case 5: 428 printf("请输入地址"); 429 scanf("%s", ch); 430 for(int i = 0; i < num; i ++) { 431 if( !strcmp(ch, p[i].e_mail) ) { 432 printf("%d: ", cnt++); 433 io(p[i], 2, fp); 434 } 435 } 436 break; 437 case 6: 438 return; 439 440 } 441 442 if(cnt == 0) { 443 puts("对不起没找到对应记录!"); 444 } 445 system("pause"); 446 447} 448 449void del() { 450 logo(1); 451 info p[1000]; 452 char ffile[40]; 453 FILE *fp; 454 strcpy(ffile, Name); 455 strcat(ffile, ".dat"); 456 if( access(ffile, 0) ) { 457 puts("尚未插入记录,请插入记录后再编辑!"); 458 system("pause"); 459 return; 460 } 461 fp = fopen(ffile, "r"); 462 int num; 463 fscanf(fp, "%d", &num); //读入记录条数 464 fclose(fp); 465 466 strcpy(ffile, Name); 467 strcat(ffile, ".txt"); 468 fp = fopen(ffile, "r"); 469 for(int i = 0; i < num; i ++) { 470 io(p[i], 3, fp); 471 } 472 fclose(fp); 473 474 printf("请输入您要删除的名片编号"); 475 int indexx; 476 scanf("%d", &indexx); 477 478 if(indexx < 0 || indexx >= num) { 479 puts("对不起对应信息不存在"); 480 system("pause"); 481 return; 482 } else { 483 fp = fopen(ffile, "w+"); 484 for(int i = 0; i < num; i ++) { 485 if(i != indexx) io(p[i], 1, fp); 486 } 487 num --; 488 fclose(fp); 489 strcpy(ffile, Name); 490 strcat(ffile, ".dat"); 491 fp = fopen(ffile, "w+"); 492 fprintf(fp, "%d\n", num); 493 fclose(fp); 494 } 495 puts("成功删除!"); 496 system("pause"); 497} 498 499void choise() { 500 int cho; 501 while(true) { 502 logo(1); 503 print_mulu(); 504 scanf("%d", &cho); 505 switch( cho ) { 506 case 1: 507 insert(); 508 break; 509 case 2: 510 edit(); 511 break; 512 case 3: 513 del(); 514 break; 515 case 4: 516 query(); 517 break; 518 case 5: 519 search(); 520 break; 521 case 6: 522 return; 523 } 524 } 525} 526 527void work() { 528 logo(0); 529 FILE *fp; 530 char ffile[40]; 531 strcpy(ffile, Name); 532 strcat(ffile, ".txt"); 533 logo(1); 534 if( access(ffile, 0) ) { 535 tips(1); 536 } 537 print_mulu(); 538 choise(); 539 540} 541 542void show() { 543 for(int i = 1; i <= 40; i ++) { 544 system("cls");puts("\n\n\n\n\n系统正导入信息"); 545 for(int j = 1; j <= i; j ++) { 546 printf("■"); 547 } 548 } 549} 550 551void solve() { 552 while(true) { 553 logo(0); 554 print_login(); 555 int cho; 556 scanf("%d", &cho); 557 switch(cho) { 558 case 1: 559 Register(); 560 break; 561 case 2: 562 if( login() ) { 563 show(); 564 work(); 565 } 566 break; 567 case 3: 568 off(); 569 break; 570 case 4: 571 return; 572 } 573 } 574} 575 576int main() { 577 solve(); 578} 579
|