我无法将新电影正确插入 10 部数组

I can't insert a new movie into the array of 10 correctly

本文关键字:插入 数组 电影 新电影      更新时间:2023-10-16

有人可以告诉我我的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

插入电影((

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

有两个问题。

首先,当您试图推开物品时,您没有正确倒数,从而愚弄了物品 i。 尝试:

        for (int j = numMovies - 1; j > i ; --j)
        {
            movies[j] = movies[j - 1];
        }

其次,我不知道这是不是故意的,你不会增加数组的大小。 这意味着阵列中始终只有 10 部电影。

在线演示