【发布时间】: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
【问题讨论】: