Address Book--Course Design



#include<stdio.h>
#include<stdlib.h>
#include<string.h>
struct Person
{
    char name[50]; /*name*/
    char classes[15];/*classes*/
    int score;/ *student number*/
    char sex[5]; /*gender*/
    int age; /*age*/
    char num[15]; /*number*/
    char adds[20]; /*address*/
} Info[50000 ],*q;


FILE* fin;


//Login function, no input and output, display program name, author, version and other information
void logon()
{
    printf("\n\n\n\t\t\t Address book management System\n\n");
    printf("\t\t\t Version number: 1.0\n\n");
    printf("\n\n\n\n\n\t\t\t April 2018 20th of the month\n\n");
    printf("\t\t\t Count 153-2 Zhang Feng\n");
    system("pause");
}
//Function selection directory, input character C, select corresponding operation
int menu_select()
{
    char c;
    do
    {
        system("cls");
        printf("\t\t *****Address Book***** \n");
        printf("\t\t┌─ ──────┐\n");
        printf("\t\t│ 1. Add record│\n");
        printf("\t\t│ 2. Delete record│\n");
        printf( "\t\t│ 3. Query record│\n");
        printf("\t\t│ 0. Exit program│\n");
        printf("\t\t└───────┘ \n");
        printf("\t\tPlease choose (0-3):");
        c=getchar();
    }
    while(c<'0'||c>'4');
    return(c -'0');
}
//Add record, input contact information, and output add completion
void Input()
{
 if((fin=fopen("document.txt","a+"))==NULL)
    {
        printf("Failed to open\n");
        exit(-1);
    }
 int cnt=0;
     q=&Info[cnt];
        printf("\tClass:");
        scanf("\t%s",q- >classes);
        printf("\tName:");
        scanf("\t%s",q->name);
        printf("\tStudent ID:");
        scanf("\t%d",&q ->score);
        printf("\tSex:");
        scanf("\t%s",q->sex);
        printf("\tAge:");
        scanf("\t%d",&q ->age);
        printf("\tPhone number:");
        scanf("\t%s",q->num);
        printf("\tAddress:");
        scanf("\t%s",q->adds);
      int i=0;
          fprintf(fin,"%s %s %d %s %d %s %s\n",Info[i].classes,
        Info[i].name,Info[i].score,Info[i].sex,
     Info[i].age,Info[i].num,Info[i].adds);


        printf("\tAdd complete .\n");


        fclose(fin);


        system("pause");
}


//Delete the record, enter the name of the person to delete, and output the contact has been deleted
void Delete()
{
    if((fin=fopen( "document.txt","r"))==NULL)
    {
        printf("Failed to open\n");
        exit(-1);
    }
    char name[10]; //First find and delete
    int cnt=0;
    while (fscanf(fin,"%s %s %d %s %d %s %s",Info[cnt].classes,
        Info[cnt].name,&Info[cnt].score,Info[cnt].sex,
     &Info[cnt].age,Info[cnt].num,Info[cnt]. adds)!=EOF)cnt++;
    if (cnt == 0)
    {
        printf("The address book is empty\n");
        return;
    }
fclose(fin);
if((fin=fopen("document.txt","w"))==NULL)
    {
        printf("Failed to open\n");
        exit(-1);
    }
    printf("Please enter the name of the record you want to delete: \n");
    scanf("%s",name);
    int i=0,j ;
    while ((strcmp(Info[i].name,name)!=0 )&& i<cnt)i++;




    if (strcmp(name,Info[i].name)==0) //delete information
    {
       for( j=0;j<cnt;j++)
        if(i==j)continue;
       else fprintf(fin,"%s %s %d %s %d %s %s\n",Info[j].classes,
        Info[j].name, Info[j].score,Info[j].sex,
     Info[j].age,Info[j].num,Info[j].adds);
    }
    else
    {
        printf("The friend does not exist!\n");
        return;
    }
    printf("Information deleted!\n ");
fclose(fin);
}
//Query records, enter the name of the contact to be queried, or the initials of the surname, and output the contact information
void Query()
{
    if((fin=fopen("document.txt", "r"))==NULL)
    {
        printf("Failed to open\n");
        exit(-1);
    }


    int m = 0;
    printf("\t\nPlease select the query method:\n");
    printf ("\t┌──────┐\n");
    printf("\t│1------name│\n");
    printf("\t│2------ Last name│\n");
    printf(" \t│3------return│\n");
    printf("\t└──────┘\n");
    printf("Please choose:");
    scanf("%d",&m);
    while(m!=1&&m!=2)
    {
        printf("Incorrect input, please choose again:");
        scanf("%d", &m);
    }
    if(m == 1)
    {
        char name[50];
        int cnt=0;
    while(fscanf(fin,"%s %s %d %s %d %s %s",Info[cnt] .classes,
        Info[cnt].name,&Info[cnt].score,Info[cnt].sex,
     &Info[cnt].age,Info[cnt].num,Info[cnt].adds)!=EOF)cnt++ ;
        if(cnt==0) // If it is an empty linked list;
        {
            printf("The address book is empty!\n");
            return;
        }
        printf("\tPlease enter the name you want to query:");
        scanf("%s",name);
        int i=0;
        for(i=0;i<cnt;i++) //loop through the nodes in the linked list to find the smallest node at this time
        {


            if(strcmp(Info[i].name,name)==0) //comparison input name
            {
                printf("Class\tName\tStudent ID\tSex\tAge\tNumber\tAddress\n");
                printf("%s\t%s\t%d\t%s\t %d\t%s\t%s\n",Info[i].classes,Info[i].name,Info[i].score,Info[i].sex,Info[i].age,Info [i].num,Info[i].adds);
            break;
            }
        }
        if(i==cnt) printf("\tThis contact does not exist\n");
    }
    if(m==2)
    {
        char name[50];
        int cnt=0;
    while(fscanf(fin,"%s %s %d %s %d %s %s",Info[cnt].classes,
        Info[cnt].name,&Info[cnt ].score,Info[cnt].sex,
     &Info[cnt].age,Info[cnt].num,Info[cnt].adds)!=EOF)cnt++;
        if(cnt==0) // If it is an empty list;
        {
            printf("The address book is empty! \n");
            return;
        }
        printf("\tPlease enter the name you want to query:");
        scanf("%s",name);
        int i=0;
        for(i=0;i<cnt;i++) // Loop through the nodes in the linked list to find the smallest node at this time
        {


            if(Info[i].name[0]==name[0]) //Compare the input names
            {
                printf("Class\tName\ tstudent number\tgender\tage\tnumber\taddress\n");
                printf("%s\t%s\t%d\t%s\t%d\t%s\t%s\ n",Info[i].classes,Info[i].name,Info[i].score,Info[i].sex,Info[i].age,Info[i].num,Info[i]. adds);
            break;
            }
        }
         if(i==cnt) printf("\tThis contact does not exist\n");
    }
    fclose(fin);
}




int main()
{


    logon();
    for(;;)
    {
        switch(menu_select() )
        {
        case 1:
            printf("\n\tAdd record to address book\n");
            Input();
            break;
        case 2:
            printf("\n\tDelete record from address book\n");
            Delete ();
            printf("\t");
            system("pause");
            break;
        case 3:
            printf("\n\tFind record in address book\n");
            Query();
            printf("\t");
            system("pause");
            break;
        case 0:
            printf("\n\t\t谢谢使用,再见!\n");
            printf("\n\t\t");
            system("pause");
            exit(0);
        }
    }
    return 0;

}







Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324814428&siteId=291194637