0

我正在尝试制作一个计算棒球运动员平均值的程序,并一直询问直到达到用户设定的球员数量。然后我必须将数据输出到一个图表中,该图表有球员的平均击球率,并带有 * 标记。

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

#include <iostream>
#include <string>

using namespace std;

class BattingInfo
{
        public:
                char fname[25];
                char lname[25];
                float hits;
                float battimes;
                float games;
                float tgames;
                float average;
                float averaget;
                float gaverage;

};

bool getd(BattingInfo& bi);
void displayd(BattingInfo& bi);

int main()
{
        int players;
    BattingInfo bi[9999];
    cout <<"Please input the number of players:\n";
        cin >> players;

        int index;
    index = 0;

    while (getd(bi[index])&& index < players - 1)
    {
        index++;
    }

        cout << "\n\nData Entries: \n";

    for (int i = 0; i <= index; i++)
    {
        displayd(bi[i]);
    }
}

bool getd(BattingInfo& bi)
        {       cout <<"Please input the Players first name:\n";
                cin >> bi.fname;
                cout <<"Please input the Players last name:\n";
                cin >> bi.lname;
                cout << "Please input the number of number of successful hits of the player.\n";
                cin >> bi.hits;
                cout << "Please input the numer of times at bat.\n";
                cin >> bi.battimes;
                cout << "Please input the number of games the player participated in.\n";
                cin >> bi.games;
                cout <<"Please input the total numer of games the player could have batted in.\n";
                cin >> bi.tgames;

        system("cls");

                return true;
        }

void displayd(BattingInfo& bi)
        {
                bi.average = bi.hits/bi.battimes;
                bi.gaverage = bi.games/bi.tgames;
                bi.averaget = bi.average*bi.gaverage;
        cout <<"Batting Average: " << bi.averaget << endl;
        cout << ".000 - .099";
        if (bi.averaget > .0 && bi.averaget < .1)
        { cout << "*";}
        cout << "\n.100 - .199";
        if (bi.averaget > .099 && bi.averaget < .2)
        { cout << "*";}
        cout << "\n.200 - .299";
        if (bi.averaget > .199 && bi.averaget < .3)
        { cout << "*";}
        cout << "\n.300 - .399";
        if (bi.averaget > .299 && bi.averaget < .4)
        { cout << "*";}
        cout << "\n.400 - .499";
        if (bi.averaget > .399 && bi.averaget < .5)
        { cout << "*";}
        cout << "\n.500 - .599";
        if (bi.averaget > .499 && bi.averaget < .6)
        { cout << "*";}
        cout << "\n.600 - .699";
        if (bi.averaget > .599 && bi.averaget < .7)
        { cout << "*";}
        cout << "\n.700 - .799";
        if (bi.averaget > .699 && bi.averaget < .8)
        { cout << "*";}
        cout << "\n.800 - .899";
        if (bi.averaget > .799 && bi.averaget < .9)
        { cout << "*";}
        cout << "\n.000 - .999";
        if (bi.averaget > .899 && bi.averaget < .1)
        { cout << "*";}
        cout << "\n1";
        if (bi.averaget > .999)
        { cout << "*\n";}
        system("\nPAUSE");
        system("cls");
        }

它有点工作,但图表是为每个球员输出的(平均只有 1 名球员),我需要它在一个图表上为整个团队输出。我不知道该怎么做。

4

1 回答 1

0

这是一个非常基本的图表,从左到右显示值。您可以将其用作基础并对其进行更改以显示您的数据。如果这不是您想要的,您可能需要使用适当的 GUI 库。

请注意我使用 vector<> 而不是数组。除非必须,否则不要使用数组。使用向量。

我猜这可能是一个学校项目,所以最好自己完成。

#include <iostream>
#include <string>
#include <vector>

using namespace std;

void display_as_graph(vector<int>& numbers)
{
    char space = ' ';
    for(int i = 0; i < numbers.size(); i++)
    {
        for(int num_spaces = 0; num_spaces < numbers[i]; num_spaces++)
        {
            cout << space;
        }
        cout << "x\n";
    }
}

int main()
{
    vector<int> numbers;
    numbers.push_back(4);
    numbers.push_back(6);
    numbers.push_back(8);
    numbers.push_back(3);

    display_as_graph(numbers);

    string wait;
    cin >> wait;

    return 0;
}
于 2013-11-01T05:10:54.907 回答