通讯录

来源:互联网 发布:人工智能替代人类 编辑:程序博客网 时间:2022/08/11 16:23

写了一晚上,调了调,也没啥优化的,鲁棒性也没怎么考虑

#include<stdio.h>int counts;struct people {    int num;    char name[20];    int phone[20];    int QQ[20];};struct people node[100];void mainMenu() {    printf("           通讯录管理系统\n");    for(int i=0;i<25;i++) printf("*");    printf("Mune");    for(int i=0;i<25;i++) printf("*");    printf("\n");    printf("*   1 input record\t\t2 search record      *\n");    printf("*   3 delete record\t\t4 insert record      *\n");    printf("*   5 modify record\t\t6 sort record        *\n");    printf("*   7 save record\t\t8 display record     *\n");    printf("*   9 read record\t\t0 quit record        *\n");}void input(int n) {    for(int i=counts;i<counts+n;i++) {        printf("Pleaes input the people's number:");        scanf("%d",&node[i].num);        printf("Please input the people's name:");        scanf("%s",&node[i].name);        printf("Please input the people's phone number:");        scanf("%s",&node[i].phone);        printf("Please input the people's QQ number:");        scanf("%s",&node[i].QQ);    }    printf("You input the %d people successful\n",n);    counts+=n;}void delect(int num) {    for(int i=num-1;i<counts;i++) {        node[i].num=node[i+1].num;        strcpy(node[i].name,node[i+1].name);        strcpy(node[i].phone,node[i+1].phone);        strcpy(node[i].QQ,node[i+1].QQ);    }    counts--;    printf("You delect the %d's people successful.\n",num);}void insert(struct people temp) {    node[counts].num=temp.num;    strcpy(node[counts].name,temp.name);    strcpy(node[counts].phone,temp.phone);    strcpy(node[counts].QQ,temp.QQ);    counts++;    printf("You insert the information successful.\n");}int searchByNumber(int num) {    for(int i=0;i<counts;i++) {        if(node[i].num==num) {            printf("The people 's number is :%d\n",node[i].num);            printf("The people 's name is :%s\n",node[i].name);            printf("The people 's phone number is :%s\n",node[i].phone);            printf("The people 's QQ number is :%s\n",node[i].QQ);            return 1;        }    }    return -1;}int searchByName(char *name) {    for(int i=0;i<counts;i++) {        if(strcpy(name,node[i].name)) {            printf("The people 's number is :%d\n",node[i].num);            printf("The people 's name is :%s\n",node[i].name);            printf("The people 's phone number is :%s\n",node[i].phone);            printf("The people 's QQ number is :%s\n",node[i].QQ);            return 1;        }    }    return -1;}void modify(){    printf("Please input the people's name you want to modify:");    char name[20];    scanf("%s",&name);    for(int i=0;i<counts;i++) {        if(strcmp(name,node[i].name)==0) {            printf("Which information you want to change?");            printf("1.num   2.name   3.phone number   4QQ\n");            int n;            scanf("%d",&n) ;            if(n==1)  {                printf("Please input the new number:");                int new_number;                scanf("%d",&new_number);                node[i].num=new_number;            }            else if(i==2) {                printf("Please input the new name:");                int new_name[20];                scanf("%s",&new_name);                strcmp(node[i].name,new_name);            }            else if(i==3) {                printf("Please input the new phone number:");                int new_phone[20];                scanf("%s",&new_phone);                strcmp(node[i].phone,new_phone);            }            else if(i==4) {                printf("Please input the new QQ number:");                int new_QQ[20];                scanf("%s",&new_QQ);                strcmp(node[i].QQ,new_QQ);            }            printf("You modify the information successful.\n");        }    }}void orderByName() {    struct people temp;    for(int i=0;i<counts;i++) {        for(int j=i;j<counts;j++) {            for(int k=0;k<strlen(node[i].name)&&k<strlen(node[j].name);k++)            if(node[i].name[k]!=node[j].name[k]) {                if(node[i].name[k]>node[j].name[k]) {                    temp=node[i];                    node[i]=node[j];                    node[j]=temp;                }                else break;            }        }    }    printf("You have sort the book successful.\n");}void orderByNumber() {    struct people temp;    for(int i=0;i<counts;i++) {        for(int j=i;j<counts;j++) {            if(node[i].num!=node[j].num) {                if(node[i].num>node[j].num) {                    temp=node[i];                    node[i]=node[j];                    node[j]=temp;                }                break;            }        }    }    printf("You have sort the book successful.\n");}void save(int n,FILE *fp) {    for(int i=0;i<n;i++) {        fprintf(fp, "The people's num is %d\n", node[i].num);        fprintf(fp, "The people's name is %s\n", node[i].name);        fprintf(fp, "The people's phone is %s\n", node[i].phone);        fprintf(fp, "The people's QQ is %s\n", node[i].QQ);    }    printf("You save the book successful.\n");}void print(int n) {   // if(counts<n) {printf("You only have %d people's information\n",counts);return;}    for(int i=0;i<n;i++) {        printf("The %d's number is :%d\n",i+1,node[i].num);        printf("The %d's name is :%s\n",i+1,node[i].name);        printf("The %d's phone is :%s\n",i+1,node[i].phone);        printf("The %d's QQ is :%s\n\n\n",i+1,node[i].QQ);    }}void read(FILE *fp) {    int num;    printf("How many people do you want to read:");    scanf("%d",&num);    for(int i=0;i<num;i++) {        fscanf(fp, "The people's num is %d\n", &node[i].num);        fscanf(fp, "The people's name is %s\n", &node[i].name);        fscanf(fp, "The people's phone is %s\n",& node[i].phone);        fscanf(fp, "The people's QQ is %s\n", &node[i].QQ);    }    printf("You read the book successful.\n");}int main(){    mainMenu();    int x;    counts=0;    while(scanf("%d",&x)&&x) {        if(x>9) printf("please input the number from 0 to 9");        else {            if(x==1) {                printf("How many people do you want to input:");                int n;                scanf("%d",&n);                input(n);            }            if(x==2) {                printf("Which way do you want to search?");                printf("1.num     2.name\n");                int n;                scanf("%d",&n);                if(n==1) {                printf("Please input the num you want to search:");                int num;                scanf("%d",&num);                    if(searchByNumber(num)==-1) {                        printf("I can't find this people.\n");                    }                }                else if(n==2) {                printf("Please input the name you want to search:");                char name[20];                scanf("%s",&name);                    if(searchByName(name)==-1) {                        printf("I can't find this people.\n");                    }                }            }            if(x==3) {                printf("Please input the people's number you want do delect:\n");                int num;                scanf("%d",&num);                void delect(num);            }            if(x==4)  {                struct people temp;                printf("Pleaes input the people's number:");                scanf("%d",&temp.num);                printf("Please input the people's name:");                scanf("%s",&temp.name);                printf("Please input the people's phone number:");                scanf("%s",&temp.phone);                printf("Please input the people's QQ number:");                scanf("%s",&temp.QQ);                insert(temp);            }            if(x==5)  {                     modify();            }            if(x==6) {                printf("Which way do you want do sort those people\n");                printf("1.name     2.number\n");                int n;                scanf("%d",&n);                if(n==1) {                    orderByName();                }                else orderByNumber();            }            if(x==7)  {                FILE *fp;                fp=fopen("out.txt","wb");                printf("How many people do you want to save?");                int n;                scanf("%d",&n);                save(n,fp);                fclose(fp);            }            if(x==8) {                int n;                printf("How many people do you want to look?");                scanf("%d",&n);                print(n);            }            if(x==9) {                FILE *fp;                fp=fopen("out.txt" , "rb" );                if ( fp == NULL ) printf("I can't find this file.\n");                else read(fp);                fclose(fp);            }        }    }    printf("You had quit The people's Management System\n");    return 0;}
原创粉丝点击