| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 2125 人关注过本帖
标题:请教各位版主大大和高手,如何高效的设定自定义函数
只看楼主 加入收藏
foxeer
Rank: 3Rank: 3
等 级:论坛游侠
帖 子:107
专家分:136
注 册:2015-12-29
结帖率:90%
收藏
已结贴  问题点数:20 回复次数:4 
请教各位版主大大和高手,如何高效的设定自定义函数
这不是一个问题求教贴,只是想寻求点编程经验,以下是两个代码,功能都是一样的,第一个是我写的,第二个是答案,很简单就是利用结构体存储人名电话,然后利用名和姓查找电话,本来自己写出程序满心欢喜,运行后没有错误而且结果完全满足题目要求,但是一个答案对比,我心一下凉了一半,看着答案那短小精悍的自定义函数,目的和功能性相当强,再看我的简直惨不忍睹,函数冗长功能多,真心在这里求教,在解决实际问题的时候,如何高效设置自定义函数
程序代码:
#define __STDC_WANT_LIB_EXT1__ 1
#include <stdio.h>
#include <ctype.h>
#include <string.h>
#include <stdlib.h>
#include <stdbool.h>

typedef struct information information;
typedef struct Name Name;

void show_all_storage(information* info[], int count);
void show_you_want(information* info[], int count);
void free_memory(information* info[], int count);

struct Name
{
    char first_name[20];
    char second_name[20];
};

struct information
{
    struct Name name;
    char phone_number[20];
};

int main(void)
{
    information* personal_info[10];
    size_t count = 0;
    char choice = '\0';
    for (count = 0; count < sizeof(personal_info) / sizeof(information*); count++)
    {
        printf_s("Do you want to enter details of a%spersonal informatiaon (Y or N)? ", count == 0 ? " " : "nother ");
        scanf_s(" %c", &choice, sizeof(choice));
        if (tolower(choice) == 'n')
            break;
        if (!(personal_info[count] = (information*)malloc(sizeof(information))))
        {
            printf_s("No memory is here!\n");
            return 1;
        }

        printf("Please enter the first name of you want to storage: ");
        scanf_s("%s", personal_info[count]->name.first_name, sizeof(personal_info[count]->name.first_name));

        printf("Please enter the second name of %s. ", personal_info[count]->name.first_name);
        scanf_s("%s", personal_info[count]->name.second_name, sizeof(personal_info[count]->name.second_name));

        printf("Please enter %s %s's phone number: ", personal_info[count]->name.first_name, personal_info[count]->name.second_name);
        scanf_s("%s", personal_info[count]->phone_number, sizeof(personal_info[count]->phone_number));
    }
    printf_s("\n");
there:
    printf_s("Do you want to show these storage:\n1.show all of the names and number\n2.show you want to check ? ");
    printf_s("\n3.You don't want to check.");
    scanf_s(" %c", &choice, sizeof(choice));
    switch (choice)
    {
    case '1':
        show_all_storage(personal_info, count);
        break;
    case '2':
        show_you_want(personal_info, count);
        break;
    case '3':
        printf_s("Thanks,Goodbye.\n");
        break;
    default:
        printf_s("\nEnter wrong! Please enter again!\n");
        break;
    }
    if (choice != '1'&&choice != '2'&&choice != '3')
    {
        goto there;
    }
    free_memory(personal_info, count);
    return 0;
}

void show_all_storage(information* info[], int count)
{
    for (int i = 0; i < count; i++)
    {
        printf_s("name: %s %s  phone number: %s\n", info[i]->name.first_name, info[i]->name.second_name, info[i]->phone_number);
    }
}

void show_you_want(information* info[], int count)
{
    char fname[20] = { '0' };
    char sname[20] = { '0' };
    int record = 0;
    getchar();
    printf_s("Please enter the first name: ");
    gets_s(fname, sizeof(fname));
    printf_s("Please enter the second name: ");
    gets_s(sname, sizeof(sname));

    for (int i = 0; i < count; i++)
    {
        if (strcmp(fname, info[i]->name.first_name) == 0)
        {
            if (strcmp(sname, info[i]->name.second_name) == 0)
            {
                printf_s("%s %s's phone number is %s\n", fname, sname, info[i]->phone_number);
                record++;
            }
            else
            {
                printf_s("%s %s's phone number is %s\n", fname, info[i]->name.second_name, info[i]->phone_number);
                record++;
            }
        }
        else
        {
            if (strcmp(sname, info[i]->name.second_name) == 0)
            {
                printf_s("%s %s's phone number is %s\n", info[i]->name.first_name, sname, info[i]->phone_number);
                record++;
            }
        }
    }
    if (record == 0)
    {
        printf_s("Sorry, there is no phone number you found!\n");
    }
}

void free_memory(information* info[], int count)
{
    for (int i = 0; i < count; i++)
    {
        free(info[i]);
    }
}



#define __STDC_WANT_LIB_EXT1__ 1
#include <stdio.h>
#include <string.h>
#include <ctype.h>
#include <stdbool.h>

#define FIRST_NAME_LEN  31
#define SECOND_NAME_LEN 51
#define NUMBER_LEN      16
#define MAX_NUMBERS     50


// Structure defining a name
typedef struct Name
{
  char firstname[FIRST_NAME_LEN];
  char secondname[SECOND_NAME_LEN];
} Name;

// Structure defining a phone record
typedef struct PhoneRecord
{
  struct Name name;
  char number[NUMBER_LEN];
} PhoneRecord;

// Function prototypes
size_t get_records(PhoneRecord records[], size_t size);                                  // Read phone records
void search_records(PhoneRecord records[], size_t count);                                // Search the phone records
int find_record(PhoneRecord records[], size_t start, size_t count, const Name *pName);   // Find the record for a name
void show(const PhoneRecord *pRecord);                                                     // Output a phone record
bool has_name(const PhoneRecord *pRecord, const Name *pName);                            // Test for a name
Name read_name();                                                                        // Read a name from the keyboard

int main(void)
{
  char answer = 'n';
  PhoneRecord records[MAX_NUMBERS];                                                      // Array of phone records
//  Name aName;                                                                          // Stores a name
  size_t count = get_records(records, MAX_NUMBERS);                                      // Number of phone records

  printf_s("\nDo you want to search the phone records (y or n)? ");
  scanf_s(" %c" , &answer, sizeof(answer));
  if(tolower(answer) == 'y')
    search_records(records, count);

  printf_s("\nThe records we have are:\n");
  for(size_t i = 0 ; i < count ; ++i)
    show(records[i]);
  printf_s("\n");
  return 0;
}

// Function to read an arbitrary number of phone records from the keyboard
size_t get_records(PhoneRecord records[], size_t size)
{
  size_t count = 0;
  char answer = 'y';
  do
  {
    records[count].name = read_name();                                        // Read the name
    printf_s("Enter the number for this name: ");
    scanf_s(" %[ 0123456789]",records[count++].number, (rsize_t)NUMBER_LEN);  // Read the number - including spaces

    printf_s("Do you want to enter another(y or n)?: ");
    scanf_s(" %c", &answer, sizeof(answer));
  }while(count <= size && tolower(answer) == 'y');
  return count;
}

// Function to output a record
void show(const PhoneRecord *pRecord)
{
  printf_s("\n%s %s   %s", pRecord->name.firstname,pRecord->name.secondname, pRecord->number);
}

// Function to test whether the name is the same as in a record
bool has_name(const PhoneRecord *pRecord, const Name *pName)
{
  return (strcmp(pName->firstname, pRecord->name.firstname) == 0 &&
                  strcmp(pName->secondname, pRecord->name.secondname) == 0);
}

// Function to read a name and store it in a Name structure
Name read_name(void)
{
  Name name;
  printf_s("Enter a first name: ");
  scanf_s(" %s", name.firstname, (rsize_t)FIRST_NAME_LEN);
  printf_s("Enter a second name: ");
  scanf_s(" %s", name.secondname, (rsize_t)SECOND_NAME_LEN);
  return name;
}

// Function to find the record for a name in records starting at index start.
// count is the number of elements in records.
// The index to the record is returned or -1 if it doesn't exist.
int find_record(PhoneRecord records[], size_t start, size_t count, const Name *pName)
{
  for(size_t i = start ; i < count ; ++i)
  {
    if(has_name(&records[i], pName))                              // Test for the name
      return i;
  }
  return -1;
}

// Search the array of phone records for a number
void search_records(PhoneRecord records[], size_t count)
{
  Name name;
  char answer = 'n';
  do
  {
    name = read_name();
    int index = 0;
    bool first = true;
    while((index = find_record(records, index, count, &name)) >= 0)
    {
      if(first)
      {
        printf_s("The numbers for %s %s are:\n", name.firstname, name.secondname);
        first = false;   
      }
      printf_s("%s\n", records[index++].number);
      if(index >= count)
        break;  
    }
    if(first)
      printf_s("No numbers found for %s %s.\n", name.firstname, name.secondname);

    printf_s("\nDo you want to search for another name (y or n)? ");
    scanf_s(" %c" , &answer, sizeof(answer));
  }while(tolower(answer) == 'y');
}
搜索更多相关主题的帖子: 功能性 结构体 如何 电话 
2016-04-02 08:57
诸葛欧阳
Rank: 19Rank: 19Rank: 19Rank: 19Rank: 19Rank: 19
来 自:流年
等 级:贵宾
威 望:82
帖 子:2790
专家分:14619
注 册:2014-10-16
收藏
得分:10 
把功能细化全部放到不同的函数里,主函数里的内容越少越好(✪▽✪)

一片落叶掉进了回忆的流年。
2016-04-02 09:01
alice_usnet
Rank: 11Rank: 11Rank: 11Rank: 11
等 级:贵宾
威 望:18
帖 子:370
专家分:2020
注 册:2016-3-7
收藏
得分:10 
C语言本来就是一门自顶向下,逐步求精,模块化的编程语言。你说的高效只是没有整体规划,没有把各个问题细化罢了。要做到真正的高效其实是涉及到很多方面的问题的,包括代码和算法的优化,特别是指针,连有多年丰富经验的编程高手都不能保证对它运用自如、得心应手;所以越追求高效,就要面临越严峻的安全问题,你始终要在安全与高效之间权横利弊,找到一个平衡点。

未佩好剑,转身便已是江湖
2016-04-02 09:25
foxeer
Rank: 3Rank: 3
等 级:论坛游侠
帖 子:107
专家分:136
注 册:2015-12-29
收藏
得分:0 
回复 2楼 诸葛欧阳
谢谢
2016-04-02 09:32
foxeer
Rank: 3Rank: 3
等 级:论坛游侠
帖 子:107
专家分:136
注 册:2015-12-29
收藏
得分:0 
回复 3楼 alice_usnet
谢谢
2016-04-02 09:33
快速回复:请教各位版主大大和高手,如何高效的设定自定义函数
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.018295 second(s), 8 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved