如何在不使用数组的情况下缩短 C++ 代码?

问题描述

我目前正在做我的大学教授给我的一个练习,其中一个问题是使用数据结构创建一个程序,该程序输入任何主题的数据并以整洁的格式输出它们。问题是问题明确指出不要使用数组,较短的代码会授予奖励分数。 我应该如何缩短下面的代码

#include <iostream>
#include <iomanip> 
#include <string>
using namespace std;

struct musicRecord //structure name
{
    //structure members
    string title;
    string artist;
    string album;
    string genre;
    string releaseYear;
};

int main()
{
    int numRecords;
    musicRecord music1,music2,music3,music4,music5; //structure variables

    //input
    cout << "===================================================";
    cout << "\nRecord No. 1"<< endl;
    cout << "\nEnter the title : ";
    getline(cin,music1.title);
    cout << "\nEnter the artist's name : ";
    getline(cin,music1.artist);
    cout << "\nEnter the album name : ";
    getline(cin,music1.album);
    cout << "\nEnter the genre of the title : ";
    getline(cin,music1.genre);
    cout << "\nEnter the year the title was released : ";
    getline(cin,music1.releaseYear);
    cout << endl << "===================================================";
    cout << "\nRecord No. 2" << endl;
    cout << "\nEnter the title : ";
    getline(cin,music2.title);
    cout << "\nEnter the artist's name : ";
    getline(cin,music2.artist);
    cout << "\nEnter the album name : ";
    getline(cin,music2.album);
    cout << "\nEnter the genre of the title : ";
    getline(cin,music2.genre);
    cout << "\nEnter the year the title was released : ";
    getline(cin,music2.releaseYear);
    cout << endl << "===================================================";
    cout << "\nRecord No. 3" << endl;
    cout << "\nEnter the title : ";
    getline(cin,music3.title);
    cout << "\nEnter the artist's name : ";
    getline(cin,music3.artist);
    cout << "\nEnter the album name : ";
    getline(cin,music3.album);
    cout << "\nEnter the genre of the title : ";
    getline(cin,music3.genre);
    cout << "\nEnter the year the title was released : ";
    getline(cin,music3.releaseYear);
    cout << endl << "===================================================";
    cout << "\nRecord No. 4" << endl;
    cout << "\nEnter the title : ";
    getline(cin,music4.title);
    cout << "\nEnter the artist's name : ";
    getline(cin,music4.artist);
    cout << "\nEnter the album name : ";
    getline(cin,music4.album);
    cout << "\nEnter the genre of the title : ";
    getline(cin,music4.genre);
    cout << "\nEnter the year the title was released : ";
    getline(cin,music4.releaseYear);
    cout << endl << "===================================================";
    cout << "\nRecord No. 5" << endl;
    cout << "\nEnter the title : ";
    getline(cin,music5.title);
    cout << "\nEnter the artist's name : ";
    getline(cin,music5.artist);
    cout << "\nEnter the album name : ";
    getline(cin,music5.album);
    cout << "\nEnter the genre of the title : ";
    getline(cin,music5.genre);
    cout << "\nEnter the year the title was released : ";
    getline(cin,music5.releaseYear);

    //output
    cout << endl << "================================================================================================================";
    cout << endl << "\t\t\t\t\t\tMUSIC RECORDS";
    cout << endl << "================================================================================================================";
    cout << endl << setw(5) << "Record No. |" << setw(10) << " Title" << setw(25) << "| Artist " << setw(20) << "| Album " << setw(20) << "| Genre " << setw(25) << "| Release Year |";
    cout << endl << "----------------------------------------------------------------------------------------------------------------";
    
    cout << endl << setw(12) << left << " 1";
    cout << setw(27) << left << music1.title;
    cout << setw(21) << left << music1.artist;
    cout << setw(20) << left << music1.album;
    cout << setw(20) << left << music1.genre;
    cout << setw(15) << left << music1.releaseYear;
    cout << endl << "----------------------------------------------------------------------------------------------------------------";
    cout << endl << setw(12) << left << " 2";
    cout << setw(27) << left << music2.title;
    cout << setw(21) << left << music2.artist;
    cout << setw(20) << left << music2.album;
    cout << setw(20) << left << music2.genre;
    cout << setw(15) << left << music2.releaseYear;
    cout << endl << "----------------------------------------------------------------------------------------------------------------";
    cout << endl << setw(12) << left << " 3";
    cout << setw(27) << left << music3.title;
    cout << setw(21) << left << music3.artist;
    cout << setw(20) << left << music3.album;
    cout << setw(20) << left << music3.genre;
    cout << setw(15) << left << music3.releaseYear;
    cout << endl << "----------------------------------------------------------------------------------------------------------------";
    cout << endl << setw(12) << left << " 4";
    cout << setw(27) << left << music4.title;
    cout << setw(21) << left << music4.artist;
    cout << setw(20) << left << music4.album;
    cout << setw(20) << left << music4.genre;
    cout << setw(15) << left << music4.releaseYear;
    cout << endl << "----------------------------------------------------------------------------------------------------------------";
    cout << endl << setw(12) << left << " 5";
    cout << setw(27) << left << music5.title;
    cout << setw(21) << left << music5.artist;
    cout << setw(20) << left << music5.album;
    cout << setw(20) << left << music5.genre;
    cout << setw(15) << left << music5.releaseYear;

    cout << endl << "================================================================================================================";
    cout << endl << "\t\t\t\t\tEND OF PROGRAM,THANK YOU";
    cout << endl << "================================================================================================================";

    return 0;
}

解决方法

您可能会注意到,您为每个变量复制/粘贴代码。

因此创建函数以避免重复代码,例如:

musicRecord inputMusicRecord(int recordNb)
{
    musicRecord res;
    cout << "===================================================";
    cout << "\nRecord No. " << recordNb << endl;
    cout << "\nEnter the title : ";
    getline(cin,res.title);
    cout << "\nEnter the artist's name : ";
    getline(cin,res.artist);
    cout << "\nEnter the album name : ";
    getline(cin,res.album);
    cout << "\nEnter the genre of the title : ";
    getline(cin,res.genre);
    cout << "\nEnter the year the title was released : ";
    getline(cin,res.releaseYear);
    return res;
}

void print(const musicRecord& music)
{
    cout << endl << setw(12) << left << " 1";
    cout << setw(27) << left << music.title;
    cout << setw(21) << left << music.artist;
    cout << setw(20) << left << music.album;
    cout << setw(20) << left << music.genre;
    cout << setw(15) << left << music.releaseYear;
}

然后,你可能会这样做:

void print_header()
{
    cout << endl << "================================================================================================================";
    cout << endl << "\t\t\t\t\t\tMUSIC RECORDS";
    cout << endl << "================================================================================================================";
    cout << endl << setw(5) << "Record No. |" << setw(10) << " Title" << setw(25) << "| Artist " << setw(20) << "| Album " << setw(20) << "| Genre " << setw(25) << "| Release Year |";
}

void print_separator()
{
    cout << endl << "----------------------------------------------------------------------------------------------------------------";
}

void print_footer()
{
    cout << endl << "================================================================================================================";
    cout << endl << "\t\t\t\t\tEND OF PROGRAM,THANK YOU";
    cout << endl << "================================================================================================================";
}

int main()
{
    const musicRecord music1 = inputMusicRecord(1);
    const musicRecord music2 = inputMusicRecord(2);
    const musicRecord music3 = inputMusicRecord(3);
    const musicRecord music4 = inputMusicRecord(4);
    const musicRecord music5 = inputMusicRecord(5);

    print_header();
    print_separator();
    print(music1);
    print_separator();
    print(music2);
    print_separator();
    print(music3);
    print_separator();
    print(music4);
    print_separator();
    print(music5);
    print_footer();
}