请教各位版主大大和高手,如何高效的设定自定义函数
这不是一个问题求教贴,只是想寻求点编程经验,以下是两个代码,功能都是一样的,第一个是我写的,第二个是答案,很简单就是利用结构体存储人名电话,然后利用名和姓查找电话,本来自己写出程序满心欢喜,运行后没有错误而且结果完全满足题目要求,但是一个答案对比,我心一下凉了一半,看着答案那短小精悍的自定义函数,目的和功能性相当强,再看我的简直惨不忍睹,函数冗长功能多,真心在这里求教,在解决实际问题的时候,如何高效设置自定义函数
程序代码:#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');
}
#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');
}









