需要帮助编写类程序。以下是张贴的说明:
步骤1:程序应该有一个FUNCTION,显示一个屏幕,显示哪些座位是可用的,哪些座位已被占用。已占用的座位用#符号表示,剩余的座位用*符号表示。程序应该做的第一件事是将所有座位初始化为可用的(*)并显示座位表。(提示:座位表应该是二维数组。)
第二步:礼堂里的每一排都有不同的票价。所以第0排的票可能是每张5美元第1排的票可能每张10美元。您的程序应该有一个FUNCTION,从一个名为prices.dat的输入文件中读取每一行的票价。每一行的票价应该存储在一个一维数组中。
步骤3:你的程序应该有变量来跟踪售出的门票总数和所有售出的门票的总收入。
步骤4:您的程序应该允许用户一次只出售一张票。用户应该能够出售尽可能多的票,因为他们想(你需要一个循环)。通过一些提示或菜单询问用户是否想要出售另一张票。如果需要的话,不要忘记验证输入数据。
要允许用户出售票,您的程序应该让用户输入他们想要出售的票的排号和座位号。程序应该用这些信息做四件事:
应该检查这个座位是否有空位。如果座位被占用,程序不应该允许用户出售车票。如果发生这种情况,打印一条消息给用户,说票不可用,并提示用户看看他们是否想出售另一张票。
如果座位是空的,程序应该更新座位表,在座位表中该座位的位置加上已占用的符号(#)。
程序应该查找已售出座位的排价。您的程序应该有一个跟踪总收入的变量,每次销售后应将售出座位的价格添加到该总额中。
您的程序应该有一个变量来跟踪售出的总门票。当卖出一张票时,你的程序应该做的下一件事是更新售出的总票数。
步骤5:一旦用户完成售票,打印出更新的座位表,然后是售出的总门票和这些门票产生的总收入。
注意:您需要在这个程序中使用两个数组,一个用于座位表,另一个用于存储每一行的价格。还需要使用两个函数:一个用于显示座位表,另一个用于读取每行价格数据并将其存储在包含每行价格的数组中。如果需要,您可以使用其他函数,但它们不是必需的。
注意:本次运行的测试数据文件:prices.txt
10 10 10 9 9 9 8 8 8 7 7 7 6 6 6
我被困在第二步了,它说:
"你的程序应该有一个函数,从一个叫做prices.dat的输入文件中读取每一行的票价。每一行的票价应该存储在一维数组中。"
我不知道如何创建输入文件或检查它们。这是我到目前为止的代码。在检查了重复的座位后,我停在了我的"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 ticketn";
cout << "2) Total sell and exitnn";
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 availablen";
cout << "# Reserved Seatsn";
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;
}
}
下面的程序几乎与您的需求相似,除了从文件中读取输入。这个程序将从控制台读取输入(用户必须输入每行的价格)。同样,这不是你想要的,但我希望它能帮助你。如果可能的话,我将很快发布另一个程序,可以从文件中读取输入。
#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 Pricesnn";
for (int count = 0; count < rows; count++)
{
cout << "The price for row " << (count + 1) << ": ";
cout << price [count] << endl;
}
break;
case 2:
cout << "Purchase a Ticketnn";
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 Seatsnn";
Show_Chart ();
break;
case 4:
cout << "Total ticket sales: "<<total<<".nn";
break;
case 5:
cout << "quitn";
break;
default : cout << "Error inputn";
}
} while (choice != 5);
return 0;
}
//********************************************************************************
//********************************************************************************
//** **
//** Define Functions. **
//** **
//********************************************************************************
//********************************************************************************
int Show_Menu()
{
int MenuChoice;
cout << endl << endl;
cout << " tMAIN MENUn";
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 << "_____________________nn";
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 30n";
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;
}