gpt4 book ai didi

c - 从 C 中的 typedef 结构对数组进行排序

转载 作者:太空狗 更新时间:2023-10-29 16:45:01 25 4
gpt4 key购买 nike

问题:尝试对来 self 创建的 typedef 结构(电话簿)的数组进行排序。

目标:尝试构建一个允许用户添加、删除、排序和打印电话簿的电话簿。

我现在的处境:我的一切都正常,除了排序。我通过阅读各种网络论坛/示例拼凑了一个排序功能,但无法使其正常工作。

我遇到的问题:添加条目后(工作正常),如果您尝试对条目进行排序,该函数会将这些条目的值清零,当您打印电话簿时,它会显示所有条目为空白。它应该按姓氏的字母顺序对它们进行排序。

这是我使用的排序算法:

void Sort (phone phonebook[])
{
phone temp;
int i; int j;

for (i=0; i<19; i++)
{
for (j=i+1; j<19; j++)
{
if (strcmp(phonebook[i].Surname, phonebook[j].Surname) > 0)
{
temp=phonebook[i];
phonebook[i]=phonebook[j];
phonebook[j]=temp;

}
}
}
}

有什么想法吗?


完整代码在这里:

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

//typedef struct to define what's in the phonebook
typedef struct PhoneBookContacts
{
char Name[20];
char Surname[20];
char PhoneNumber[20];
} phone;

//Function prototypes
void AddEntry (phone[]);
void DeleteEntry (phone[]);
void PrintEntry (phone[]);
void Sort (phone[]);
int counter = 0; //Global counter variable used to keep track of number of contacts

//Begin main function
int main (void)
{
phone phonebook[20]; //Phonebook instance
char userChoice; //Variable to use to select menu choice

while (userChoice != 'Q') {
printf ("***************\n");
printf ("Please enter a command:\n");
printf("'A': Add an entry\n");
printf("'D': Delete an entry\n");
printf("'S': Sort entries\n");
printf("'P': Print the phonebook\n");
printf("'Q': Quit\n");
printf ("***************\n");

scanf("%s", &userChoice); //Stores menu choice into variable userChoice

// Add Contact
if (userChoice == 'A')
AddEntry(phonebook);

//Remove Contact
if (userChoice == 'D')
DeleteEntry (phonebook);

//Print Contacts
if (userChoice == 'P')
PrintEntry(phonebook);

//Sort Contacts
if (userChoice == 'S')
Sort(phonebook);

//Quit
if (userChoice == 'Q') {
printf("Phonebook will now quit.");
return 0;
}
}
}

//Function Definition to Add Contacts to the Phonebook
void AddEntry (phone phonebook[]) {
counter++; //global counter increase

printf("\nFirst Name: ");
scanf("%s", phonebook[counter-1].Name); //counter-1 b/c arrays start at 0

printf("Last Name: ");
scanf("%s", phonebook[counter-1].Surname);

printf("Phone Number (XXX-XXX-XXXX): ");
scanf("%s", phonebook[counter-1].PhoneNumber);

printf("\n%s added to phonebook\n", phonebook[counter-1].Name); //tell user friend added
}

void DeleteEntry (phone phonebook[])
{
int x = 0;
char deleteName[20]; // Temp string to compare to existing phonebook
char deleteSurname[20]; //temp string
char nullStr[20] = {"\0"}; // empty string to remove phonenumber

printf("\nEnter name: ");
scanf("%s", deleteName); //place into temp string
printf("Enter Surname: ");
scanf("%s", deleteSurname); //place into temp string

for (x = 0; x < counter; x++)
{
if (strcmp(deleteName, phonebook[x].Name) == 0) //compare deleteName to phonebook.Name
{
for (x = 0; x < counter; x++)
{
if (strcmp(deleteSurname, phonebook[x].Surname) == 0) //If deleteSurname matches phonebook.Surname
{
strcpy(phonebook[x].Name, nullStr); //Put null into Name
strcpy(phonebook[x].Surname, nullStr); //Null into Surname
strcpy(phonebook[x].PhoneNumber, nullStr); //Null into PhoneNumber
printf("Contact removed from phonebook.\n");
counter--;
break;
}
}

}
else printf("Invalid entry--try again.\n");
}
}

// Function def to print contacts
void PrintEntry (phone phonebook[]) {
int x = 0;
printf("\nPhonebook entries:\n");

for ( x = 0; x < counter; x++) {
printf("\n(%d)\n", x+1); //Show contact number
printf("Name: %s %s\n", phonebook[x].Name, phonebook[x].Surname); //Name
printf("Number: %s\n", phonebook[x].PhoneNumber); //Number
}
}

void Sort (phone phonebook[]) {
phone temp;
int i; int j;

for (i=0; i<19; i++) {
for (j=i+1; j<19; j++) {
if (strcmp(phonebook[i].Surname, phonebook[j].Surname) > 0) {
temp=phonebook[i];
phonebook[i]=phonebook[j];
phonebook[j]=temp;
}
}
}
}

最佳答案

您可以使用 stdlib.h 中已实现的排序函数 qsort 函数:

int SortFunc(void* a, void* b) {
phone *p1 = (phone*)a;
phone *p2 = (phone*)b;

return strcmp(p1->Surname, p2->Surname);
}

void Sort (phone phonebook[]) {
qsort(phonebook, counter, sizeof(phone), &SortFunc);
}

函数通常是Quicksort ,但这取决于 C 库的实现。

更新:

空白列表是因为排序是倒过来的,总是把电话簿的19条全部排序,空的和真的比较。如果电话簿中的条目少于 19 个,则实际数据将出现在电话簿数组的末尾

您原来的排序功能总是几乎正常。只需更改两个 for 的结束条件即可。

void Sort (phone phonebook[]) {
phone temp;
int i; int j;

for (i=0; i<counter; i++) {
for (j=i+1; j<counter; j++) {
if (strcmp(phonebook[i].Surname, phonebook[j].Surname) > 0) {
temp=phonebook[i];
phonebook[i]=phonebook[j];
phonebook[j]=temp;
}
}
}
}

我还更新了上面的排序

关于c - 从 C 中的 typedef 结构对数组进行排序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7911919/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com