C language: write an address book

#define  _CRT_SECURE_NO_WARNINGS

#define MAX 100
#include<stdio.h>
#include<string.h>
#include<stdlib.h>

enum MyEnum
{
    OUT,
    Incre,
    Delete,
    Lookup,
    Modify,
    Print,
    Sort,
};

void meun()
​​{     printf("\n");     printf("******************************\n") ;     printf("*****1.Incre 2.Delete*****\n");     printf("*****3.Lookup 4.Modify*****\n");     printf ("*****5.Print 0.OUT *****\n");     printf("************************ ******\n"); } struct ProInon {     char name[20];//Name     int age; //Age     char sex[10]; //Gender     char Phone[12];//Phone number } ;













struct comact //Contains the maximum capacity of the address book and the current number of recorded address books
{     struct ProInon data[MAX];     int sizo; };



void Initialization(struct comact* ps)
{
    memset(ps->data, 0, sizeof(ps->data));
    ps->sizo = 0;
}


void Increrec(struct comact* ps)
{     if (ps->sizo > MAX)     {         printf("Your address book is full\n");     }     else     {         printf("Please enter your name\n");         scanf(" %s", ps->data[ps->sizo].name);         printf("Please enter your age\n");         scanf("%d", &(ps->data[ps->sizo].age ));         printf("Please enter your gender\n");         scanf("%s", ps->data[ps->sizo].sex);         printf("Please enter your phone number\n");         scanf(" %s", ps->data[ps->sizo].Phone);         rewind(stdin);














        ps->sizo++;
        printf("Added successfully\n");
        
    }

}

void Printrec(const struct comact* ps)
{     if (ps->sizo==0)     {         printf("Sorry, your address book is empty\n");


    }
    else
    {         printf("%-10s\t%-10s\t%-5s\t%-12s\t\n","name","age","gender","phone number"); for         ( int i = 0; i < ps->sizo; i++)         {             printf("%-10s\t%-10d\t%-5s\t%-12s\t\n",                  ps->data[i]. name,                 ps->data[i].age,                 ps->data[i].sex,                 ps->data[i].Phone);         }     }









}


int Find_name(struct comact* ps,char name[20])
{
    int i = 0;

    for (i = 0; i < ps->sizo; i++)
    {
        if (0 == strcmp(ps->data[i].name, name))
        {
            return i;
        }
    }
    return -1;
}


void Deleterec(struct comact* ps)
{     char name[20];     printf("Please enter the name of the contact you want to delete: "");     scanf("%s",name);     //Find name information     int ret = Find_name(ps, name);     if (ret == -1)     {         printf("The information you want to delete does not exist\n");     }     else     {         //Delete information         int j = 0;         for ( j = ret; j < ps->sizo; j++)         {             ps->data[j] = ps->data[j + 1];         }         ps->sizo--;         printf("Deletion successful\n");     }

    


















}


void Lookuprec(struct comact* ps)
{     char name[20];     printf("Please enter the name you are looking for:》");     scanf("%s", name);     //Find name information     int ret = Find_name( ps, name);     if (ret == -1)     {         printf("The contact you are looking for does not exist\n");     }     else     {         //Print information         printf("%-10s\t%-10s\t %-5s\t%-12s\t\n", "name", "age", "gender", "phone number"); printf("%-10s\t%         -10d\t%-5s\t %-12s\t\n",             ps->data[ret].name,             ps->data[ret].age,             ps->data[ret].sex,             ps->data[ret].Phone);     }





    













        


}


void Modifyrec(struct comact* ps)
{     char name[20];     printf("Please enter the name of the contact you want to modify:》");     scanf("%s", name);


    //Find the contact
    int ret = Find_name(ps, name);
    if (ret == -1)
    {         printf("The contact you want to modify does not exist\n");     }     else     {         //Modify the contact         printf( "Please enter your name\n");         scanf("%s", ps->data[ret].name);         printf("Please enter your age\n");         scanf("%d", &(ps-> data[ret].age));         printf("Please enter your gender\n");         scanf("%s", ps->data[ret].sex);         printf("Please enter your phone number\n");         scanf("%s", ps->data[ret].Phone);













        printf("Modification successful\n");
    }

}


int main()
{     int input = 0;     int sizo = 0;//Record the number information of the address book     //Create the address book     struct comact con;     //Initialize the address book     Initialization(&con);     do     {//Print menu         meun ();         printf("Please enter your choice:》");         scanf("%d", &input);         //Make a judgment         switch (input)         {         case Incre: //Add a contact











        



            Increrec(&con);
            break;

        case Delete: //Delete contact

            Deleterec(&con);
            break;

        case Lookup: //Find contacts

            Lookuprec(&con);
            break;

        case Modify: //Modify contact

            Modifyrec(&con);
            break;

        case Print: //Print contact

            Printrec(&con);

        case OUT: //Exit
            printf("Exit address book");
            break;
        default: 
            printf("Incorrect input, please re-enter\n");
            break;
        }

    } while (input);
    
    return 0;
}

Guess you like

Origin blog.csdn.net/weixin_52300845/article/details/123608568