0

我对 OOP 编程很陌生,想知道如何让我的代码更干净。该程序有效,但我不知道如何为流派、玩家输入等制作不同的类。

我尝试使用 .h 和 .cpp 文件为 Genres 创建一个类,但我仍然没有太多关于如何使我的代码更干净的知识,因为所有内容都是在:int main () {}

bookPicker.cpp

int main()
{
    //Declaring a user account
    std::string name, lastName;

    //Genre's
    std::string ice, fire, earth, wind;
    ice = "Ice", fire = "Fire", earth = "Earth", wind = "Wind";

    //Titles
    int a, b, c, d;


    //Recommendation
    std::string r;
    r = "Type yess if this is the genre of your choice\n";

    std::string iceS[4] = { "The Ice Gauntlet", "The Formal Ice Queen", "Frozen in Time", "Frost Lake" };
    std::string fireS[4] = { "The Fire Gauntlet", "The Formal Fire Queen", "Hot Air", "Fire Lake" };
    std::string earthS[4] = { "The Earth Gauntlet", "The Formal Earth Queen", "Stuck in Time", "The Swamp" };
    std::string windS[4] = { "The Wind Gauntlet", "The Formal Wind Queen", "Blown in Time", "Wind Lake" };    

    //Welcome
    std::string w, wU;
    w = "Welcome ";
    wU = " to The Four Elemets Book Store!\n";

    //Creating user account
    std::cout << "Please enter your name" << std::endl;
    std::cin >> name;
    std::cout << "Please enter your lastname" << std::endl;
    std::cin >> lastName;

    std::string userAccount = name + lastName;

    //Ask for input
    std::cout << w << userAccount << std::endl;
    std::cout << "What kind of genre do you like to read? \n" << std::endl;
    std::cout << ice << "\n" << fire << "\n" << earth << "\n" << wind << "\n" << std::endl;
    std::cout << "Please pick your genre\n" << std::endl;

    //create the choice string variable
    std::string choice;
    std::cin >> choice;

    //if statement after the input

        if (choice == ice) {
            std::cout << r << std::endl;
            std::cin >> a;
            std::cout << "\n";

            for (int i = 0; i < 4; i++) {
                std::cout << iceS[i] << "\n";
            }
        } if (choice == fire) {
            std::cout << r << std::endl;
            std::cin >> b;
            std::cout << "\n";

            for (int y = 0; y < 4; y++) {
                std::cout << fireS[y] << "\n";
            }
        } if (choice == earth) {
            std::cout << r << std::endl;
            std::cin >> c;
            std::cout << "\n";

            for (int x = 0; x < 4; x++) {
                std::cout << earthS[x] << "\n";
            }
        } if (choice == wind) {
            std::cout << r << std::endl;
            std::cin >> d;
            std::cout << "\n";

            for (int o = 0; o < 4; o++) {
                std::cout << windS[o] << "\n";
            }
        }



    return 0;

} 
4

2 回答 2

2

您不需要在一堆文件中编写类来清理它。

如果你摆脱一些变量的丰富性,使用描述性的名称,并利用标准库,你可以取得很大的进步。

一个建议:

int main()
{
    const std::map<std::string, std::vector<std::string>> library =
    {
        {"Ice" , { "The Ice Gauntlet", "The Formal Ice Queen", "Frozen in Time", "Frost Lake" }},
        {"Fire", { "The Fire Gauntlet", "The Formal Fire Queen", "Hot Air", "Fire Lake" }},
        {"Earth", { "The Earth Gauntlet", "The Formal Earth Queen", "Stuck in Time", "The Swamp" }},
        {"Wind", { "The Wind Gauntlet", "The Formal Wind Queen", "Blown in Time", "Wind Lake" }}
    };

    std::string name;
    std::string lastName;
    std::cout << "Please enter your name" << std::endl;
    std::cin >> name;
    std::cout << "Please enter your lastname" << std::endl;
    std::cin >> lastName;
    std::string userAccount = name + lastName;

    std::cout << "Welcome, " << userAccount << std::endl;
    std::cout << "What kind of genre do you like to read? \n" << std::endl;
    for (const auto& entry: library)
    {
        std::cout << entry.first << "\n";
    }

    std::cout << "Please pick your genre\n" << std::endl;
    std::string choice;
    std::cin >> choice;
    auto it = library.find(choice);
    if (it != library.end())
    {
        std::cout << "Type yess if this is the genre of your choice\n";
        std::string answer;
        std::cin >> answer;
        for (const auto& title: it->second)
        {
            std::cout << title << "\n";
        }
    }
    else
    {
        std::cout << choice << " is not a known genre.";
    }
}
于 2019-01-22T12:03:56.857 回答
0

就像现在的示例程序一样,OOP 并没有太多改进。当然,根据程序的发展方式,情况可能会发生变化。如果我要上课,可能是这个:

class account {
public:
    std::string name;
    std::string lastName;

    std::string getFullName() {
        return name + " " + lastName;
    }
};

在这里,accountnow 代表一个用户记录,并且getFullName可以用于清晰打印他们的姓名。您可以这样创建帐户:

//Declaring a user account
account userAcc;

您可以这样设置名称:

std::cout << "Please enter your name" << std::endl;
std::cin >> userAcc.name;
std::cout << "Please enter your lastname" << std::endl;
std::cin >> userAcc.lastName;

然后以这种方式打印名称:

//Ask for input
std::cout << w << userAcc.getFullName() << std::endl;

当然,这不会按原样改进任何东西,因为无论如何您只有一个用户。if我认为构成您代码一半左右的重复链有更大的改进潜力。相反,我会利用您的四个元素以这种方式枚举事物的事实:

enum element {
    ICE, FIRE, EARTH, WIND, NONE
};

std::vector<std::vector<std::string>> books = {
    {"The Ice Gauntlet", "The Formal Ice Queen", "Frozen in Time", "Frost Lake" },
    { "The Fire Gauntlet", "The Formal Fire Queen", "Hot Air", "Fire Lake" },
    { "The Earth Gauntlet", "The Formal Earth Queen", "Stuck in Time", "The Swamp" },
    { "The Wind Gauntlet", "The Formal Wind Queen", "Blown in Time", "Wind Lake" }
};

现在我们有一个element枚举,但稍后会详细介绍。注意这些书现在是一个字符串向量的向量,一个很像表格的结构。现在要找出用户想要的元素,我们可以这样查询:

//if statement after the input
element chosenElement = NONE;

if (choice == ice) chosenElement = ICE;
else if (choice == fire) chosenElement = FIRE;
else if (choice == earth) chosenElement = EARTH;
else if (choice == wind) chosenElement = WIND;

现在chosenElement表示用户想要的元素,NONE如果他输入了无效的内容。现在您可以通过这种方式打印所有书籍:

if (chosenElement == NONE) {
    std::cout << "Sorry, we don't have this genre." << std::endl;
}
else for (std::string i : books[chosenElement]) {
    std::cout << i << std::endl;
}

if这将替换您之前拥有的整个链条。books[chosenElement]代表一个流派中的所有书籍(books[ICE]所有冰书也是如此,等等)并且i是各自的书籍,并且它们都以这种方式打印。

该程序也可以通过这种方式更容易地扩展。假设您有一个新元素,您所要做的就是将其值添加到enum之前的定义中NONE,将书籍行添加到books字符串向量的向量中,添加else if (choice...对新元素的检查,然后就完成了。无需复制/粘贴任何if块。此外,您不再依赖于硬编码每种类型的书籍数量。您可以将第五本书添加到一个流派中,它会像那样工作。无需告诉它该类型有多少本书,因为基于范围的for循环自己发现了这一点。

另外,如果您只需要一个字符串并且不会修改它,我建议您不要std::string为它制作一个。而不是这个:

std::string ice, fire, earth, wind;
ice = "Ice", fire = "Fire", earth = "Earth", wind = "Wind";
...
if (choice == ice)

我会这样做:

if (choice == "Ice")

这是有效的,因为它具有必要的重载,因此当您使用这样的字符串文字std::string时,它知道您想要进行字符串比较。==也是如此std::string w, wU;,我只是将它们取出并将字符串文字放在您打印它们的位置。

于 2019-01-22T10:57:29.727 回答