有人可以告诉我InsertMovie()有什么问题吗?并帮我解决它?
#include <iostream>
#include <string>
#include <algorithm>
#include "Movie.h"
#include <iomanip>
using namespace std;
#ifndef _Movie_
#define _Movie_
#include <iostream>
struct Movie
{
std::string title;
int criticRating; // from 0 to 100
int audienceRating; // from 0 to 100
};
#endif /*defined _Movie_*/
void PrintMovies(Movie*, int);
void SortMoviesByAudienceRating(Movie*, int);
void InsertMovie(Movie*, int, const Movie&);
int main()
{
//auto automatically assumes what type
//constexpr = static const int
constexpr auto NumMovies = 10;
Movie* movie = new Movie[NumMovies];
movie[0] = { "Kung Fu Panda 3", 81, 86};
movie[1] = { "Hail, Caesar", 81, 46};
movie[2] = { "Star Wars VII - The Force Awakens", 92, 90};
movie[3] = { "The Revenant", 82, 85};
movie[4] = { "The Choice", 7, 67};
movie[5] = { "Pride and Prejudice and Zombies", 45, 59};
movie[6] = { "The Finest Hours", 59, 72};
movie[7] = { "Ride Along 2", 13, 55};
movie[8] = { "The Boy", 31, 45};
movie[9] = { "Dirty Grandpa", 9, 51};
PrintMovies(movie, NumMovies);
SortMoviesByAudienceRating(movie, NumMovies);
PrintMovies(movie, NumMovies);
Movie newMovie = { "The Jungle Book", 84, 80 };
InsertMovie(movie, NumMovies, newMovie);
PrintMovies(movie, NumMovies);
return 0;
}
void InsertMovie(Movie* movies, int numMovies, const Movie& movie)
{
Movie temp1, temp2;
for (int i = 0; i < numMovies; ++i)
{
if (movie.audienceRating >= movies[i].audienceRating)
{
for (int j = numMovies - 1; j > numMovies - i + 1; --j)
{
movies[j] = movies[j - 1];
}
temp1 = movies[i];
movies[i] = movie;
break;
}
}
}
输出是这样的:
//在InsertMovie()
之前Movies:
Critic Audience Title
------------------------------
92 90 Star Wars VII - The Force Awakens
81 86 Kung Fu Panda 3
82 85 The Revenant
59 72 The Finest Hours
7 67 The Choice
45 59 Pride and Prejudice and Zombies
13 55 Ride Along 2
9 51 Dirty Grandpa
81 46 Hail, Caesar
31 45 The Boy
//在InsertMovie()之后
Movies:
Critic Audience Title
------------------------------
92 90 Star Wars VII - The Force Awakens
81 86 Kung Fu Panda 3
82 85 The Revenant
84 80 The Jungle Book
7 67 The Choice
45 59 Pride and Prejudice and Zombies
13 55 Ride Along 2
9 51 Dirty Grandpa
81 46 Hail, Caesar
81 46 Hail, Caesar
答案 0 :(得分:0)
有两个问题。
首先,当你试图推开愚弄物品的物品时,你不能正确倒数。试试:
for (int j = numMovies - 1; j > i ; --j)
{
movies[j] = movies[j - 1];
}
其次,我不知道它是否有目的,你不会增加阵列的大小。这意味着阵列中总是只有10部电影。