对于这个问题,我确实需要一些帮助,我们应该调试的程序存在一些我找不到的错误。

该程序具有一个名称数组,应该对名称按字母顺序进行冒泡排序,然后让用户搜索名称。

冒泡排序似乎没有任何作用,并且搜索始终显示找不到该名称。

如何解决此问题将有一些帮助。谢谢。

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

#define NUM_OF_NAMES 8
#define NAME_SIZE 5

void printNames(char names[][5], int size);
void bubbleSortNames(char names[][5], int last);
bool binarySearchNames(char list[][5], int end, char target[], int *location);

int main()
{
        char nameList[8][5]={"Zack","Jake","Jill","Seth","Eric","Beth","Hugh","Rita"},
                  searchText[5],
                  userChoice;
        int  searchLocation,
                  last = 0;

        printNames(nameList, 8);

        printf("\n\nLIST AFTER SORT\n\n");

        bubbleSortNames(nameList, last);
        printNames(nameList, 8);

        printf("Would youy like to search for a name? (y/n) ");
        scanf(" %c", &userChoice);

        while(userChoice == 'y')
        {
                printf("\nPlease try and search for a name: ");
                scanf("%20s", searchText);

                if(binarySearchNames(nameList, 8, searchText, &searchLocation))
                        printf("\nThe name %s was not found. :(\n", searchText);

                else
                        printf("\nThe name %s was found at location %d!", searchText, searchLocation);

                printf("\nWould you like to search for another name? (y/n) ");
                scanf(" %c", &userChoice);
        }
        printf("\nThank you for using this program.\n\n");

        return 0;
}

/*
 Prints out array of names
*/
void printNames(char names[][NAME_SIZE], int size)
{
        for(int index = 0; index <= size; index++)
        puts(names[index]);
        return;
}
/*
 Sorts names alphabetically
*/
void bubbleSortNames(char names[][NAME_SIZE], int last)
{
        char temp[NAME_SIZE];

        for(int current = 0; current < last; current++)
                {
                        for(int walker = last; walker > current; walker--)
                                if(strcmp(names[walker], names[walker - 1]))
                                {
                                        strcpy(temp, names[walker]);
                                        strcpy(names[walker], names[walker - 1]);
                                        strcpy(names[walker - 1], temp);
                                }
                }
        return;
        }
/*
 Searches for name entered
*/
bool binarySearchNames(char list[][NAME_SIZE], int end, char target[], int *location)
{
        char first = 0,
             last = end,
             mid;

        while(first <= last)
        {
                mid = (first + last) / 2;

                if(target > list[mid])
                         first = mid + 1;
                else if(target < list[mid])
                        last = mid - 1;
                else
                        first = last +  1;
        }

        *location = mid;
        return target = list[mid];
}

最佳答案

1)号码错误

last = 0; ----> last = NUM_OF_NAMES -1;

2)执行语句与条件相反。和8 ---->最后

if(binarySearchNames(nameList, 8, searchText, &searchLocation))
    printf("\nThe name %s was not found. :(\n", searchText);
else
    printf("\nThe name %s was found at location %d!", searchText, searchLocation);


3)在printNames

index <= size ----> index < size或致电8 ---->最后致电

4)在bubbleSortNames

if(strcmp(names[walker], names[walker - 1]))



if(strcmp(names[walker], names[walker - 1])<0)

5)在binarySearchNames

使用strcmp比较字符串

08-16 23:55