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 人のプレーヤーの平均のみ)。チーム全体を 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 に答える