-1

授業用のプログラムを書くのに助けが必要です。投稿された手順は次のとおりです。

ステップ 1: プログラムには、空席と空席を示す画面を表示する関数が必要です。取得済みの座席は # 記号で表し、空席は * 記号で表す必要があります。プログラムで最初にすべきことは、すべての座席を空席 (*) に初期化し、座席表を表示することです。(ヒント: 座席表は 2 次元配列である必要があります。)

ステップ 2: 講堂の列ごとにチケット価格が異なります。したがって、行 0 のチケットはそれぞれ 5.00 であり、行 1 のチケットはそれぞれ 10.00 である可能性があります。プログラムには、prices.dat という入力ファイルから各行のチケット価格を読み取る FUNCTION が必要です。各行のチケット価格は、1 次元配列に格納する必要があります。

ステップ 3: プログラムには、販売されたチケットの総数と、販売されたすべてのチケットの総収入を追跡する変数が必要です。

ステップ 4: プログラムは、ユーザーが一度に 1 枚ずつチケットを販売できるようにする必要があります。ユーザーは、好きなだけチケットを販売できる必要があります (これにはループが必要です)。別のチケットを販売したいかどうかをユーザーに尋ねる何らかのプロンプトまたはメニューでこれを行います。必要に応じて、入力データを検証することを忘れないでください。

ユーザーがチケットを販売できるようにするには、プログラムでユーザーに、販売したいチケットの行番号と座席番号を入力させる必要があります。プログラムは、この情報を使用して次の 4 つのことを行う必要があります。

  1. 座席が空いているかどうかを確認する必要があります。席が取られた場合、プログラムはユーザーがチケットを販売できないようにする必要があります。これが発生した場合は、チケットが利用できないというメッセージをユーザーに出力し、別のチケットを販売するかどうかを確認するようユーザーに促します。

  2. 座席が空いている場合、プログラムは、チャート内のその座席の位置に使用済み記号 (#) を配置して、座席表を更新する必要があります。

  3. 次に、プログラムは、販売されたシートの行の価格を検索する必要があります。プログラムには総収益を追跡する変数が必要です。販売された座席の価格は、販売ごとにこの合計に追加される必要があります。

  4. プログラムには、販売されたチケットの総数を追跡する変数が必要です。チケットを販売するときにプログラムが次に行うべきことは、販売されたチケットの合計を更新することです。

ステップ 5: ユーザーがチケットの販売を終了したら、更新された座席表を印刷し、続いて販売されたチケットの合計とそれらのチケットから得られる合計の収益を印刷します。

注: このプログラムでは、2 つの配列を使用する必要があります。1 つは座席表用で、もう 1 つは各行の価格を格納するためです。また、2 つの関数を使用する必要があります。1 つは座席表を表示するため、もう 1 つは行ごとの価格データを読み取り、各行の価格を含む配列に格納するためです。必要に応じて他の関数を使用することもできますが、必須ではありません。

注: この実行のテスト データ ファイル: price.txt

10
10
10
9
9
9
8
8
8
7
7
7
6
6
6

私はそれが言うステップ2で立ち往生しています:

「プログラムには、prices.dat という入力ファイルから各行のチケット価格を読み取る FUNCTION が必要です。各行のチケット価格は、1 次元配列に格納する必要があります。」

入力ファイルの作成方法やチェック方法がわかりません。これまでの私のコードは次のとおりです。重複した座席を確認した後、ケース「1」で停止しました。次に、データ ファイルから請求したい価格と行を一致させるにはどうすればよいでしょうか?

#include <iostream>
#include <iomanip>
#include <string>
#include <istream>
#include <fstream>
using namespace std;

const int numberOfRow = 15;
const int numberOfCol = 20;

void print(char matrix[][20], int numberOfRow, int numberOfCol);

int main()
{
char matrix[numberOfRow][numberOfCol], seat[numberOfRow][numberOfCol];
char option;
int i, j;
int row,col;
int ticketsold = 0;
bool another =true;

for(i = 0; i < numberOfRow; i++)
    for(j = 0; j < numberOfCol; j++)
        matrix[i][j] = '*';

while(another)
{
    print( matrix, numberOfRow, numberOfCol );

    cout << "\nMenu:\n";
    cout << "1)  Buy ticket\n";
    cout << "2)  Total sell and exit\n\n";
    cout << "Enter your choice  : ";
    cin >> option;
    cout << endl << endl;

    switch (option)
    {
    case '1' :
        {
            cout << "Enter row: ";
            cin >> row;
            cout << "\nEnter seat: ";
            cin >> col;

            if( matrix[row][col] == '*')
                {
                    matrix[row][col] = '#';
                    ticketsold++;
                }
              else
                { 
                  cout << "Invalid seat choice";
                }


            //total revenue
        }

    /*case '2' :
        {
            another=false;
        }

    default :
        cout << "Invalid choice";*/

    }
}





system("pause");
}


void print(char matrix[][20], int numberOfRow, int numberOfCol)
{
int row, col, i, j;

cout << "* Seats available\n";
cout << "# Reserved Seats\n";
cout << "Seats:  0  1  2  3  4  5  6  7  8  9 10 11 12 13 14 15 16 17 18 19" <<  endl;

for(i = 0; i < numberOfRow; i++)
{
    cout << "Row" << setw(3) << i;
    for(j=0; numberOfCol > j; j++)
        cout << setw(3) << matrix[i][j];

    cout << endl;
}
}
4

1 に答える 1

0

以下のプログラムは、ファイルから入力を読み取ることを除いて、要件とほぼ同じです。このプログラムはコンソールからの入力を読み取ります (ユーザーは各行の価格をキー入力する必要があります)。可能であれば、すぐにファイルから入力を読み取ることができる別のプログラムを投稿します。

#include <iostream>
#include <iomanip>
using namespace std;

int Show_Menu ();       
void Show_Chart ();     
const char FULL = '*';  
const char EMPTY = '#'; 
const int rows = 15;    
const int columns = 30; 
char map [rows][columns];
double price;
int total = 0;
int seat = 450;
int seat2 = 0;
int Quit = 1;
int main ()
{
const int Num_Rows = 15;
int price [Num_Rows];
int row2, column2, cost;
int answer;

    cout << "\t*********************************************************" << endl;
    cout << "\t*                                                       *" << endl;
    cout << "\t*    Welcome to our small town Theater                  *" << endl;
    cout << "\t*                                                       *" << endl;
    cout << "\t*********************************************************" << endl;
    cout << endl << endl;

    for (int count = 0; count < rows; count++)
        {
            cout << "Please enter the price for row " << (count + 1) << ": ";
                cin >> price [count];

        }
    for (int i = 0; i < rows; i++)
        {
            for (int j = 0; j < columns; j++)
                map [i][j] = EMPTY;
        }
int choice;
    do
    {
        choice = Show_Menu();
        switch (choice)
        {
            case 1:
                cout << "View Seat Prices\n\n";

                for (int count = 0; count < rows; count++)
                {
                    cout << "The price for row " << (count + 1) << ": ";
                    cout << price [count] << endl;
                }
                break;
            case 2:
                cout << "Purchase a Ticket\n\n";
                do 
                {
                    cout << "Please select the row you would like to sit in: ";
                    cin >> row2;
                    cout << "Please select the seat you would like to sit in: ";
                    cin >> column2;
                    if (map [row2] [column2] == '*')
                        {
                            cout << "Sorry that seat is sold-out, Please select a new seat.";
                            cout << endl;
                        }
                    else 
                    {
                        cost = price [row2] + 0;
                        total = total + cost;
                        cout << "That ticket costs: " << cost << endl;
                        cout << "Confirm Purchase? Enter (1 = YES / 2 = NO)";
                        cin >> answer;
                        seat = seat - answer;
                        seat2 += answer;

                        if (answer == 1)
                        { 
                            cout << "Your ticket purchase has been confirmed." << endl;
                            map [row2][column2] = FULL;
                        }
                        else if (answer == 2)
                        {
                            cout << "Would you like to look at another seat? (1 = YES / 2 = NO)";
                            cout << endl;
                            cin >> Quit;
                        }

                        cout << "Would you like to look at another seat?(1 = YES / 2 = NO)";
                        cin >> Quit;
                    }
                }
                while (Quit == 1);

                break;
            case 3:
                cout << "View Available Seats\n\n";
                Show_Chart ();
                break;
            case 4:
                cout << "Total ticket sales: "<<total<<".\n\n";
                break;
            case 5:
                cout << "quit\n";
                break;
            default : cout << "Error input\n";
        }
    } while (choice != 5);
return 0;
}
//********************************************************************************
//********************************************************************************
//**                                                                            **
//**                              Define Functions.                          **
//**                                                                            **
//********************************************************************************
//********************************************************************************

int Show_Menu()
{
    int MenuChoice;
        cout << endl << endl;
        cout << " \tMAIN MENU\n";
        cout << " 1. View Seat Prices.\n";
        cout << " 2. Purchase a Ticket.\n";
        cout << " 3. View Available Seats.\n";
        cout << " 4. View Ticket Sales.\n";
        cout << " 5. Quit the program.\n";
        cout << "_____________________\n\n";
        cout << "Please enter your choice: ";
        cin >> MenuChoice;
        cout << endl << endl;
    return MenuChoice;
}

void Show_Chart ()
{
    cout << "\tSeats" << endl;
    cout << "   1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30\n";
        for (int count = 0; count < 15; count++)
        {
            cout << endl << "Row " << (count + 1);
            for (int count2 = 0; count2 < 30; count2++)
            {
                cout << " " <<  map [count] [count2];
            }
        }
            cout << endl;
}
于 2016-10-17T12:43:55.250 に答える