输出图表/条形图的棒球平均值

Baseball average with chart/bar graph outputted

本文关键字:平均值 条形图 输出      更新时间:2023-10-16

我正在尝试制作一个程序来计算棒球运动员的平均数,并不断询问,直到达到用户设置的球员数量。然后,我必须将数据输出到一个图表中,该图表让球员平均击球,并用*标记它落入的位置。

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

#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 << "nnData 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 名球员),我需要它在一张图上为整个团队输出。我不知道该怎么做。

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

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

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

#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 << "xn";
    }
}
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;
}