• 通讯录结构体方法的实现 和VS中存在的一些问题的分析


    实现一个通讯录;
    通讯录能够用来存储1000个人的信息。每一个人的信息包含:
    姓名、性别、年龄、电话、住址


    功能例如以下:
    1.  加入联系人信息
    2.  删除指定联系人信息
    3.  查找指定联系人信息
    4.  改动指定联系人信息
    5.  显示全部联系人信息

    6.  清空全部联系人




    模块化设计:




    头文件 结构体和对应函数的定义,声明

    #include <stdio.h>  
    #include <string.h>  
    #include <stdlib.h>  
    #include <memory.h>  
    
    #define MAX 1000  
    #define NAME_LENGTH 20  
    #define SEX_LENGTH 5 
    #define AGE_LENGTH 3 
    #define TELE_LENGTH 20  
    #define ADDR_LENGTH 30  
    
    /*
    	结构体 用于储存通讯录人员信息
    */
    struct ContactsUser
    {
    	char name[NAME_LENGTH];
    	char sex[SEX_LENGTH];
    	/*
    		VS编译器下scanf_s对于长度有安全保护 因此採用字符数组保存年龄
    	*/
    	char age[AGE_LENGTH];
    	char tele[TELE_LENGTH];
    	char addr[ADDR_LENGTH];
    };
    
    /*
    	结构体 将上一个结构体装起来 同一时候创建变量记录人数
    */
    struct Contacts
    {
    	struct ContactsUser person[MAX];
    	int user_count;
    };
    
    typedef struct Contacts *pContacts;
    
    int add_contacts(pContacts pcon);//加入函数
    int dele_contacts(pContacts pcon);//删除函数
    int clear_contacts(pContacts pcon);//清空函数
    int find_contacts(pContacts pcon);//查找函数
    int modify_contacts(pContacts pcon);//改动函数
    void show_contacts(pContacts pcon);//显示函数
    void menu();//主菜单</span>


    #include "contacts.h"  
    
    /*
    	各个功能函数
    */
    
    
    /*
    菜单
    */
    void menu()
    {
    	printf("         Contacts              
    ");
    	printf("
    ");
    	printf("1.  Add the users_info 
    ");
    	printf("2.  Delete the users_info 
    ");
    	printf("3.  Clean all the users_info	
    ");
    	printf("4.  Find the users_info 
    ");
    	printf("5.  Modify the users_info
    ");
    	printf("6.  Show all the users_info
    ");
    	printf("7.  exit
    ");
    	printf("
    ");
    
    }
    
    /*
    	查询实体函数  用于将输入的用户特征和储存进行比較(strcmp)
    	方便其它功能函数的调用
    */
    int find_entry(pContacts pcon)
    {
    	int i = 0;
    	char name[NAME_LENGTH];
    	printf("please input name:");
    	scanf_s("%s", name,NAME_LENGTH);
    	for (i = 0; i < pcon->user_count; i++)
    	{
    		if (strcmp(pcon->person[i].name, name) == 0) //输入和存储进行比較
    		{
    			return i;
    		}
    	}
    	return -1;
    }
    
    
    /*
    	增添函数
    */
    int add_contacts(pContacts pcon)
    {
    	if (pcon->user_count == MAX)
    	{
    		printf("Telephone book is full!
    ");
    		return -1;
    	}
    	else
    	{
    	printf("Please input name:");
    	/*
    		scanf_s安全函数  应该加入控制长度的參数
    	*/
    	scanf_s("%s", pcon->person[pcon->user_count].name, NAME_LENGTH);
    	/*
    		数组从下标为0到下标为user_count-1 ,则在user_count处操作
    	*/
    	printf("Please input sex:");
    	scanf_s("%s", pcon->person[pcon->user_count].sex, SEX_LENGTH);
    	printf("Please input age:");
    	scanf_s("%s", pcon->person[pcon->user_count].age, AGE_LENGTH);
    	printf("Please input tele:");
    	scanf_s("%s", pcon->person[pcon->user_count].tele,TELE_LENGTH);
    	printf("Please input addr:");
    	scanf_s("%s", pcon->person[pcon->user_count].addr, ADDR_LENGTH);
    
    	pcon->user_count++;//加入结束 人员数目添加1
    	return 1;
    	}
    	
    }
    
    
    
    /*
    	删除函数
    */
    int dele_contacts(pContacts pcon)
    {
    	int i = 0;
    	int ret = find_entry(pcon);//定义ret 接收find_entry的返回位置
    
    	if (ret != -1)
    	{
    		for (i = ret; i < pcon->user_count - 1; i++)
    		{
    			pcon->person[i] = pcon->person[i + 1];
    		}
    		pcon->user_count--;
    		return 1;
    	}
    	else
    	{
    		printf("not exist!
    ");
    		return -1;
    	}
    }
    
    
    
    /*
    	清空函数
    */
    int clear_contacts(pContacts pcon)
    { 
    	memset(pcon->person,0,MAX);
    	/*
    	memset函数 在一段内存中填充给定的值 
    	是对较大结构体或数组清零的最快方法
    	*/
    	pcon->user_count = 0; //count 赋值0  进行清零
    	
    	return 1;
    }
    
    
    /*
    	查找函数
    */
    int find_contacts(pContacts pcon)
    {
    	int ret = find_entry(pcon);//定义ret 接收find_entry的返回位置
    	if (ret != -1)
    	{
    		printf("name:%-5s", pcon->person[ret].name, NAME_LENGTH);
    		printf("sex:%-5s", pcon->person[ret].sex, SEX_LENGTH);
    		printf("age:%-5s", pcon->person[ret].age, AGE_LENGTH);
    		printf("tele:%-5s", pcon->person[ret].tele, TELE_LENGTH);
    		printf("addr:%-5s", pcon->person[ret].addr, ADDR_LENGTH);
    		return 1;
    	}
    	else
    	{
    		printf("not exist!
    ");
    		return -1;
    	}
    }
    
    
    /*
    	改动函数
    */
    int modify_contacts(pContacts pcon)
    {
    	int ret = find_entry(pcon);//定义ret 接收find_entry的返回位置
    	if (ret != -1)
    	{
    		printf("Please input name:");
    		scanf_s("%s", pcon->person[ret].name, NAME_LENGTH);
    		printf("Please input sex:");
    		scanf_s("%s", pcon->person[ret].sex, SEX_LENGTH);
    		printf("Please input age:");
    		scanf_s("%s", pcon->person[ret].age, AGE_LENGTH);
    		printf("Please input tele:");
    		scanf_s("%s", pcon->person[ret].tele, TELE_LENGTH);
    		printf("Please input addr:");
    		scanf_s("%s", pcon->person[ret].addr, ADDR_LENGTH);
    		return 1;
    	}
    	else
    	{
    		printf("not exist!
    ");
    		return -1;
    	}
    }
    
    
    /*
    	显示函数
    */
    void show_contacts(pContacts pcon)
    {
    	int i = 0;
    	printf("	name	sex		age		tele			addr
    ");
    	for (i = 0; i < pcon->user_count; i++)
    	{
    		printf("%10s	", pcon->person[i].name);
    		printf("%5s	", pcon->person[i].sex);
    		printf("%10s	", pcon->person[i].age);
    		printf("%15s	", pcon->person[i].tele);
    		printf("%20s	", pcon->person[i].addr);
    	}
    	printf("
    ");
    }</span>


    #include "contacts.h"  
    
    /*
    	主函数
    */
    int main()
    {
    
    	int input = 1;   //定义一个输入  初始化
    	struct Contacts user;
    	user.user_count = 0;//为user_count进行初始化
    
    	menu(); 
    
    	while (input)
    	{
    	    printf("
      enter you choice(0-7):
    ");
    		scanf_s("%d", &input);
    		switch (input)  //switch-case 使用不同的功能函数
    		{
    		case 1:
    			add_contacts(&user);
    			break;
    		case 2:
    			dele_contacts(&user);
    			break;
    		case 3:
    			clear_contacts(&user);
    			break;
    		case 4:
    			find_contacts(&user);
    			break;
    		case 5:
    			modify_contacts(&user);
    			break;
    		case 6:
    			show_contacts(&user);
    			break;
    		case 7:
    			printf("Thanks for use!
    ");
    			break;
    		default:
    			printf("input error!
    ");
    			break;
    		}
    	}
    
    	return 0;
    }



    执行结果:





  • 相关阅读:
    区分git ,github,github桌面版,gitbash、gitshell
    Andrew Ng机器学习课程笔记(四)之神经网络
    Andrew Ng机器学习课程笔记(三)之正则化
    Andrew Ng机器学习课程笔记(二)之逻辑回归
    Andrew Ng机器学习课程笔记(一)之线性回归
    python机器学习实战(四)
    python机器学习实战(三)
    python机器学习实战(二)
    python机器学习实战(一)
    如何去破解所有的window和offices(超级全面)
  • 原文地址:https://www.cnblogs.com/gavanwanggw/p/7324133.html
Copyright © 2020-2023  润新知