如何输出增加的年/当前流行列表?

时间:2016-04-21 17:49:50

标签: c++

我正在上课,我需要输出我的程序看起来像这样。现在我一直坚持如何输出这样的岁月。

我多年来一直在增加一个柜台,并要求当年作为投入?

有人可以帮我一把吗?

             Town A             Town B
               8.5%               0.3%
Year          5000                8000
-------------------------------------------
2016          5425                8024
2017          5886                8048
2018          6386                8072
2019          6929                8096
2020          7518                8121
2021          8157                8145

    cout << "What year is it? ";
    cin >> curYear;
    cout << "Enter the population of town A: ";
    cin >> townA;
    cout << "Enter the growth rate of town A: ";
    cin >> growthA;
    cout << "Enter the population of town B: ";
    cin >> townB;
    cout << "Enter the growth rate of town B: ";
    cin >> growthB;

    while ((townA <= 0) && (growthA <=0) && (townB > townA) && (growthB < growthA) && (growthB > 0))
    {
        cout << "Error: Values must be positive, please try again." << endl;
        cout << "Enter the population of town A: ";
        cin >> townA;
        cout << "Enter the growth rate of town A: ";
        cin >> growthA;
        cout << "Enter the population of town B: ";
        cin >> townB;
        cout << "Enter the growth rate of town B: ";
        cin >> growthB;
        cout << endl;
    }

    years = 0;
    while (townA <= townB)
    {
        finalA = ((growthA / 100) * (townA)) + townA;
        finalB = ((growthB / 100) * (townB)) + townB;
        cout << "Town A " << finalA << endl;
        cout << "Town B " << finalB << endl;
        years++;
        curYear++;
        townA = finalA;
        townB = finalB;
    }

    cout << "\n\n" << endl;
    cout << setw(3) << "Town A" << setw(15) << "Town B" << endl;
    cout << setw(3) << growthA << "%" << setw(14) << growthB << "%" << endl;
    cout << setw(4) << townA << setw(16) << townB << endl;
    cout << "Year" << endl;
    cout << curYear << endl;
    cout << "--------------------------------------------" << endl;
    cout << endl;
    cout << "It took Town A " << years << " years to exceed the population of Town B." << endl;

    return 0;
}

0 个答案:

没有答案