Today I wrote an address book for you to see
#define _CRT_SECURE_NO_WARNINGS 1#include <stdio.h> #include <stdlib.h> #include <string.h>extern meau ();//directory function is not written out, you can write your own typedef struct t//define a struct {char NAME[10];CHAR&NBSP;SEX[4];INT&NBSP;AGE;CHAR&NBSP;TETL[13];CHAR&NBSP;ADD[20];} t;int count = 0;//global variable, used to record phone number Void add (T&NBSP;*P)//Add directory {int falg = 0;do{ printf ("Please enter name: \ n");//The use of a struct pointer, based on the scanf function's scanf ("%s", (* (P + count)), should be noted here.// When entering a string, you do not need to take an address, enter the address printf when shaping ("Please enter gender: \ n"); scanf ("%s", (* (P + count)). Sex);p rintf ("Please enter Age: \ n") ; scanf ("%d", & ((* (P + count)));p rintf ("Please enter phone number: \ n"); scanf ("%s", (* (p + Count)). Tetl);p rintf ("Please enter user address: \ n"), scanf ("%s", (* (P + count)). Add) count++;p rintf ("Input done! Press 0 to exit, press any key to continue typing! \ n "); scanf ("%d ", &falg);} while (FALG);} Void dele (t*p) //delete Address Book letterNumber {int i = 0;int flag = 0;char goal[10] = {0};d o{printf (" Please enter the user name to delete: \ n "); scanf ("%s ", goal);for (i = 0; i < count; i++) {if (!STRCMP (goal, (* (P + i)). Name) {while (i <= count)// Here is an array of elements I element after the element moved forward, to remove the I element to the purpose of a/ //Algorithm {* (p + i) = * (p + i + 1); i++;} count--;flag = 0;} elseflag = 1;} if (flag) {printf ("Input user does not exist, press any key to reenter, press 0 to exit: \ n"); scanf ("%d", &flag);}} while (flag);} Void find (T*P)//Lookup function {int i = 0;int flag = 0;char goal[10] = { 0 };d o{printf ("Please enter the user name to find: \ n"), scanf ("%s", goal);for (i = 0; i < count; i++) {if (!strcmp (goal, (* (P + i)). Name) {printf ("Name:%s\n", &NBSP; (* (P + i)). Name);p rintf ("Gender:%s\n", (* (p + i)). Sex);p rintf ("Age:%d\n", (* (P + i). Age);p rintf ("Phone number:%s\n", (* (P + i)). Tetl);p rintf ("Home Address:%s\n", (* (p + i)). add); flag = 0;} elseflag = 1;} if (flag) {printf ("Input User not present, select 0 exit, press any key to continue to find \ n"); scanf ("%d", &flag);}} while (flag);} Void revise (T*P)//modify function {int i = 0;int flag = 0;char goal[10] = { 0 };d o{printf ("Please enter the user name to find: \ n"), scanf ("%s", goal);for (i = 0; i < count; i++) {if (! ( strcmp (goal, (* (p + i)))) {printf ("Please select what you want to modify: \n1. Name \n2. \n3. Age \n4. Phone number \n5. Home address \ n"); scanf ("%d", &flag);switch (flag) {case 1:{ printf ("Please enter a new name: \ n"); &NBSP;&NBSP;SCANF ("%s" , (* (P + i)). name); break;} case 2:{ printf ("Please enter new gender: \ n"); &NBSP;&NBSP;SCANF ("%s", (* (p + (i)). sex); break;} case 3:{ printf ("Please enter a new Age: \ n"); &NBSP;&NBSP;SCANF ("%d", & ((* (p + i)); age)); break;} case 4:{ printf ("Please enter a new phone number: \ n"); &NBSP;&NBSP;SCANF ("%s", (* (P + i)). Tetl); break;} case 5:{ printf ("Please enter a new user address: \ n"); &NBSP;&NBSP;SCANF ("%s", (* (P + i)). Add); break;} default:{ printf ("The directive is wrong, please re-enter!") \ n "); break;}} flag = 0;} elseflag = 1;} if (flag) {printf ("Input user does not exist, select any key to continue, press 0 key to exit: \ n"); scanf ("%d", &flag);}} while (flag);} Void display (t*p)//Print all phone book functions {int i = 0;if (count) {for (i = 0; i < count; i++) {printf ("Name:%s\n", (* (P + i)). Name);p rintf ("Gender:%s\n", (* (P + i)). Sex);p rintf ("Age:%d\n", (* (P + i)). Ages);p rintf ("Phone number:%s\n", (* (p + i). Tetl);p rintf ("Home Address:%s\n", (* (P&NBsp;+ i)). Add);}} else{printf ("No content in the phone book!") \ n ");}} Void empty (t*p)//Empty function, here I do not empty the contents of the array, but instead of count to 0, so that the phone book refresh {count = 0;} Void dobble (t*p)//bubble sort to name sort {int i = 0;int j = 0;for (i = 0; i < count; i++) {for (j = 0; j < count - i - 1; j++) {if (strcmp ((* (P&NBSP;+&NBSP;J)) .name, (* (p + j+1)). Name) > 0) {* (P + count) = * (P&NBSP;+&NBSP;J); * (P&NBSP;+&NBSP;J) = * (p + &NBSP;J&NBSP;+&NBSP;1); * (p + j + 1) = * (P + count);}}} Int main () {t tel[1000]; T*p = tel;int key = 0;do{meau ();p rintf ("Please select: \ n"); scanf ("%d", &key); switch (key) {Case 1:{ add (P); break;} Case 2:{ dele (P); break;} Case 3:{ find (P); break;} Case 4:{ revise (P); break;} Case 5:{ display (P); break;} Case 6:{ empty (P); break;} Case 7:{ dobble (P); break;} case 0:{ return 0;} default:{ printf ("Input error, please reenter!\n"); break;}}} while (key); System ("pause"); return 0;}
If there is any deficiency, I wish to correct my mistakes
This article is from the "Pawnsir It Road" blog, so be sure to keep this source http://10743407.blog.51cto.com/10733407/1717800
C language with structure write an address book