【问题标题】:getting bubble sort to work with structure让冒泡排序与结构一起工作
【发布时间】:2020-07-26 15:50:45
【问题描述】:

我不确定如何使用结构进行冒泡排序,例如函数 sortMovies 能够按标题按字母顺序对电影进行排序,但我得到了下面列出的这些错误。

#include <stdio.h>
#include <conio.h>
#define CONST 100
void sortMovies(struct movies main[CONST]);
void changeMovie(struct movies main);
int findMovie(struct movies main, int nOfMovies, struct movies tempMovie);
struct movies newMovie();
typedef struct movies{
    char title[30];
    char UPC[12];
    int qnty;
    double price;
}movies;

int main()
{
    int nOfMovies = 0, findR, stop = 0, nOfMoviesArr[CONST];
    char decider;
    while (stop != 1)
    {
        movies main[CONST];
        movies tempMovie;
        printf("(A)dd a new movie\n(C)hange a Movie's Information \n(D)elete a Movie \n(L)ist All Movies\n(Q)uit");
        scanf(" %c", &decider);

        switch (decider)
        {
        case 'a':
        case 'A':
            tempMovie = newMovie();
            nOfMovies = findMovie(main[CONST], nOfMovies, tempMovie);
            nOfMovies++;
            break;
        case 'c':
        case 'C':
            printf("enter movie upc:");
            scanf("%s", main);

            break;
        case 'l':
        case 'L':
            sortMovies(main[CONST]);
            break;
        case 'q':
        case 'Q':
            return 0;
            break;
        default:
            printf("An invalid option was selected!");
        }
    }


}

struct movies newMovie() {
    movies new;

    printf("enter movie upc:");
    scanf("%s", new.UPC);
    printf("enter movie title:");
    scanf("%s", new.title);
    printf("enter movie qauntity:");
    scanf("%d", new.qnty);
    if (new.qnty <= 0)
    {
        printf("quanitity must be greater than 0");
        printf("enter movie qauntity:");
        scanf("%d", new.qnty);
    }

    printf("enter movie price:");
    scanf("%lf", new.price);
    if (new.price <= 0)
    {
        printf("price must be greater than 0");
        printf("enter movie price:");
        scanf("%lf", new.price);
    }
    return new;
}


int findMovie(struct movies main, int nOfMovies, struct movies tempMovie)
{

    int count;
    for (count = 0; count < nOfMovies; count++)
    {
        if (tempMovie.UPC == main.UPC)
        {
            return count;
        }
        else
        {
            printf("error");
            return -1;
        }
    }

}
void changeMovie(struct movies main)
{
    char decider;

    printf("would you like to change the value? y or no input");
    switch (decider)
    {
    case 'y':
    case 'Y':
        printf("enter movie title:");
        scanf("%c", main.title);
        printf("enter movie qauntity:");
        scanf("%d", main.qnty);
        if (main.qnty <= 0)
        {
            printf("quanitity must be greater than 0");
            printf("enter movie qauntity:");
            scanf("%d", main.qnty);
        }

        printf("enter movie price:");
        scanf("%lf", main.price);
        if (main.price <= 0)
        {
            printf("price must be greater than 0");
            printf("enter movie price:");
            scanf("%lf", main.price);
        }
        break;

        return 0;

    }

}
void sortMovies(struct movies main[CONST])
{
    int i, sflag, count = 0;
    char temp;

    do
    {
        sflag = 0;
        for (i = 1; i < CONST; i++)
        {
            if (main[i - 1].title > main[i].title)
            {
                temp = main[i - 1].title;
                main[i - 1].title = main[i].title;
                main[i].title = temp;
                sflag = 1;
            }
        }
        count++;
    } while (sflag);

    for (i = 0; i < CONST; i++)
    {
        printf("%c\t%c\t%d\t%lf", main[i].title, main[i].UPC, main[i].qnty, main[i].price);
    }


} ```

严重性代码描述项目文件行抑制状态抑制状态 错误 C2440“函数”:无法从“电影”转换为“电影 *”40
错误(活动)“电影”类型的 E0167 参数与“结构电影 *”类型的参数不兼容 40
错误(活动)E0137 表达式必须是可修改的左值 148
错误 C2106“=”:左操作数必须是左值 148
错误(活动)E0137 表达式必须是可修改的左值 149
错误 C2106 '=': 左操作数必须是左值 149


【问题讨论】:

    标签: c sorting struct


    【解决方案1】:

    错误(活动)E0167 “movies”类型参数与“struct movies *”类型参数不兼容 40

    发生这种情况是因为main[CONST] 尝试传递main 数组的单个元素(索引CONST 处的元素),而不是sortMovies 方法需要数组时的整个数组.要将整个数组传递给函数,请使用sortMovies(main);

    错误(活动)E0137 表达式必须是可修改的左值 148 错误 C2106 '=':左操作数必须是左值 148 错误(活动)E0137 表达式必须是可修改的左值 149 错误 C2106 '=':左操作数必须是左值 149

    发生这种情况是因为您尝试使用无效的 = 运算符将一个 char 数组的内容复制到另一个。请改用strcpy(main[i - 1].title, main[i].title)strcpy(main[i].title, temp)

    另外,在您的sortMovies 函数中,您将temp 声明为一个字符,您尝试使用它来交换title 字符串。你在这里想要的是char* temp。如果您不熟悉这种语法,我建议您阅读 C 中的指针。它们是该语言中非常重要的结构,更好地理解指针也可能会帮助您解决很多问题用这个程序。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-10-09
      • 2017-04-03
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多