如何创建一个向量,用户可以在菜单更改之间不断添加值? [英] How do I create a vector that the user can keep adding values to between menu changes?

查看:75
本文介绍了如何创建一个向量,用户可以在菜单更改之间不断添加值?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我们的作业是创建一个菜单,用户可以在其中选择以下选项:

Our homework assignment is to create a a menu where the user has the following options:

  1. 添加行星
  2. 删除星球
  3. 查找行星(按名称)
  4. 列出所有行星
  5. 排序(字母顺序)
  6. 退出

要求行星必须以名称,直径,质量作为私人成员分类存储,并且分类方法是密度,表面积和重力.但是我意识到,随着用户不断输入行星,我将需要使用向量来动态地不断添加行星.我将如何创建这样的向量?并且由于我必须专门使用类,因此创建矢量的代码会去哪儿?是int main()的开头,里面有一个函数来访问用户输入的值(名称,直径,质量)吗?

The requirements are that the planets must be stored as a class with name, diameter, mass as private members and the methods of the class being the density, surface area, and gravity. However I realized that I will need to use a vector to dynamically keep adding the planets as the user keeps entering them. How would I go about creating such a vector? And because I have to specifically use classes, where would the code for creating the vector go? Would it be at the beginning of int main() with a function inside to access the values (name, diameter, mass) entered by the user?

这是我到目前为止所拥有的:

This is what I have so far:

#include <iostream>
#include <string>
#include <cmath>

using namespace std;

class planet
{
    private:
        string n;
        double d, m;
    public:
        void Density (double d, double m)
        {
            double Den = m/((4.0/3.0)*M_PI*pow((d/2.0), 3.0));
            cout<<"Density: "<<Den<<endl;
        }
        void SurfaceArea(double d)
        {
            double S = 4.0*M_PI*pow((d/2.0), 2.0);
            cout<<"Surface Area: "<<S<<endl;
        }   
        void Name (string n)
        {
            string N = n;
            cout<<"Name: "<<N<<endl;
        }
        void Gravity (double G, double m, double d)
        {
            double F = G*m/pow((d/2.0), 2.0);
            cout<<"Force of gravity: "<<F<<endl;
        }

};

int main()
{
    const double G=6.67384e-11;
    int c=0;
    string n;
    double d=0.0, m=0.0, Den=0.0, S=0.0;

    do 
    {
        cout<<"1. Add a planet\n";
        cout<<"2. Delete planet\n";
        cout<<"3. Find planet (by name)\n";
        cout<<"4. List all planets\n";
        cout<<"5. Sort (alphabetical order)\n";
        cout<<"6. Quit\n";
        cout<<endl;
        cout<<"Please select an option from the menu above."<<endl;
        cin>>c;

        if(c==1)
        {
            planet red;

            cout<<"Enter the planet's name: ";
            cin>>n;
            cout<<"Enter the planet's diameter: ";
            cin>>d;
            cout<<"Enter the planet's mass: ";
            cin>>m;

            red.Name(n);
            red.Density(d, m);
            red.SurfaceArea(d/2.0);
            red.Gravity(G, m, d);



        }
        else if (c==4)
        {
            cout<<N<<endl;
            cout<<Den<<endl;
            cout<<S<<endl;
            cout<<F<<endl;
        }


    } while (c!=6);

    system("pause");

    return 0;
}

如果我需要澄清更多信息,请告诉我:)

If I need to clarify more please let me know nicely :)

谢谢!

推荐答案

您需要创建一个像vector<planet> p这样的向量,才能创建可以存储您的类planet的向量.您希望在程序开始时创建此向量,以便一旦用户开始输入数据就可以立即将其填充.

You would need to create a vector like this vector<planet> p to create a vector that can store your class planet. You'd want to create this vector at the start of the program so that it is ready to get filled as soon as users start entering data.

要添加行星,只需push_back行星.要删除,请使用引导程序的erase功能.

To add a planet, just push_back the planet. To delete, use the vector's erase function.

要在向量中找到行星,只需使用for循环遍历向量并查看是否p.at(i).n == planet_to_find即可.

To find a planet in your vector, just iterate through your vector with a for loop and see if p.at(i).n == planet_to_find .

要打印所有行星,例如查找循环,请遍历行星和cout << p.at(i).n.

To print all the planets, like the find loop, iterate through your planets and cout << p.at(i).n.

要进行排序,可以使用std sort函数按字母顺序进行排序. 有关排序的详细信息.

To sort, you can use the std sort function to sort alphabetically. Details on sort.

我希望这会有所帮助:)

I hope this helps :)

这篇关于如何创建一个向量,用户可以在菜单更改之间不断添加值?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆