(day18 )scanf函数返回值问题,以及通讯录程序例题

scanf 可以用int 来接收 ,接收值是正确接收的个数返回值

通讯录

test01

#include"contact.h"

void menu()
{
    printf("******1.add     2.del********\n");
    printf("******3.search  4.show********\n");
    printf("******5.modify  ***************\n");
    printf("******0.exit*******************\n");
}
 
enum Menu {
    EXIT,
    ADD,
    DEL,
    SEARCH,
    SHOW,
    MODIFY,
};
int main()
{
    struct contact p;
    initfun(&p);// 初始化通讯录p;


    int input = 0;
    do
    {
        menu();
        printf("input choose\n");
        scanf("%d", &input);
        switch (input)
        {
        case ADD:
            addfun(&p);
            break;
        case DEL:
            delfun(&p);
            break;
        case SEARCH:
            searchfun(&p);
            break;
        case SHOW:
            showfun(&p);
            break;
        case MODIFY:
            modifyfun(&p);
            break;
        case EXIT:
            printf("****exit***\n");
            clearfun(&p);
            break;
            default;
            printf("input error\n");
            break;

        }

    } while (input);
    system("pause");
    return 0;
}
 

contact.c

#include"contact.h"


void initfun(struct contact* p)
{
    /*p->sz = 0;

    memset(p->body, 0, MAX*sizeof(struct person));*/

    p->sz = 0;
    p->body = (struct person*)malloc(default * sizeof(struct person));
    p->capacity = default;

}

void addfun(struct contact* p)
{
    if (p->sz == p->capacity)  //增容
    {
        struct person* ptr = (struct person*)realloc(p->body, (p->capacity + 2) * sizeof(struct person));
        if (ptr != NULL)
        {
            p->body = ptr;
        }
        p->capacity += 2;
        printf("increase capacity successful\n");
    }
    printf("input information\n");
    printf("input name\n");
    scanf("%s", p->body[p->sz].name);
    printf("input sex\n");
    scanf("%s", p->body[p->sz].sex);

    printf("input age\n");
    scanf("%d", &(p->body[p->sz].age));
    printf("input tele\n");
    scanf("%s", p->body[p->sz].tele);

    printf("input address\n");
    scanf("%s", p->body[p->sz].addr);

    p->sz++;
    printf("input successful\n");
}

void showfun(struct contact* p)
{
    printf("%15s\t%5s\t%8s\t%15s\t%30s\t\n", "name", "age", "sex", "tele", "addr");
    for (int i = 0; i < p->sz; i++)
    {
        printf("%15s\t%5d\t%8s\t%15s\t%30s\t",
            p->body[i].name,
            p->body[i].age,
            p->body[i].sex,
            p->body[i].tele,
            p->body[i].addr

        );
        printf("\n");
    }

}

int findbodybyname(char* name, struct contact* p)
{
    int i = 0;
    for (i = 0; i <p->sz; i++)
    {
        int ret = strcmp(name, p->body[i].name);
        if (ret == 0)
        {
            return i;
        }
    }
    return -1;
}
void delfun(struct contact* p)
{
    printf("input delete person name\n");
    char name[20] = { 0 };
    scanf("%s", name);
    int ret=findbodybyname(name,p);
    if (ret == -1)
    {
        printf("no this person\n");
    }
    else
    {
        int pos = 1;
        for (pos = ret; pos < p->sz; pos++)
        {
            p->body[pos] = p->body[pos + 1];

        }

        printf("delete succseeful\n");
    }
}
void searchfun(struct contact* p)
{
    printf("input person name\n");
    char name[20] = { 0 };
    scanf("%s", name);
    int ret = findbodybyname(name, p);
    if (ret != -1)
    {
        printf("find this people\n");
        printf("%15s\t%5d\t%8s\t%15s\t%30s\t",
            p->body[ret].name,
            p->body[ret].age,
            p->body[ret].sex,
            p->body[ret].tele,
            p->body[ret].addr
            
        );
        printf("\n");
    }
    else
    {
        printf("no this people\n");
    }

}
void modifyfun(struct contact* p)
{
    printf("input person name\n");
    char name[20] = { 0 };
    scanf("%s", name);
    int ret = findbodybyname(name, p);
    if (ret != -1)
    {
        printf("precious information\n");
        printf("%15s\t%5d\t%8s\t%15s\t%30s\n",
            p->body[ret].name,
            p->body[ret].age,
            p->body[ret].sex,
            p->body[ret].tele,
            p->body[ret].addr

        );
        printf("input new name\n");
        scanf("%s", p->body[ret].name);
        printf("intpur new sex\n");
        scanf("%s", p->body[ret].sex);
        printf("input new age\n");
        scanf("%d", &(p->body[ret].age));
        printf("input new tele\n");
        scanf("%s", p->body[ret].tele);
        printf("input new addr\n");
        scanf("%s", p->body[ret].addr);
        printf("input successful\n");
    }
    

}
void clearfun(struct contact* p)
{
    free(p->body);
    p->body = NULL;
    p->capacity = 0;
    p->sz = 0;
}

contact.h

#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
#include<stdlib.h>
#include<string.h>

#define name_max 20
#define sex_max 3
#define    tele_max 30
#define addr_max 30
#define MAX 50
#define default 3

struct person {

    char name[name_max];
    char sex[sex_max];
    char tele[tele_max];
    char addr[addr_max];
    int age;
    
};
//静态版本
//struct contact {
//
//    struct person body[MAX];
//    int sz;
//};
//动态版本
struct contact
{
    struct person* body;
    int sz;
    int capacity;//通讯录当前最大容量

};

void initfun(struct contact* p);  //初始化通讯录
void addfun(struct contact* p);   //往通讯录中添加信息:
void showfun(struct contact* p); //显示通讯录的信息;
void delfun(struct contact* p);
void searchfun(struct contact* p);
void modifyfun(struct contact* p);
void clearfun(struct contact* p);

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值