Вы находитесь на странице: 1из 18

//Austin Ward //COS 220 Assignment #8 //This program allows users to select seats on an airplane #include<iostream> #include<iomanip> #include<fstream>

#include<string> using namespace std; const int ROWS = 20; const int COLS = 6; const int FIRSTCLASSROWS = 4; void display (char seatchart[][COLS], int rows); //POST: Display the seating chart void purchase (char seatchart[][COLS], int rows); //POST: Allow the user to purchase a seat and mark seat as taken void statistics (char seatchart[][COLS], int rows); //POST: Compute and display percent occupancy bool availableFirstClass (char seatchart[][COLS], int nrows); //PRE: nrows is the number of first class rows at front of plane //POST: Return true if a seat is available in first class; else false void assignFirstClass (char seatchart[][COLS], int nrows, int& row, char& letter); //PRE: nrows is the number of first class //POST: row and letter are set to an available first class seat //number of rows //number of columns //number of first class rows at front of plane

int main () { char seats[ROWS][COLS]; ifstream fin; fin.open ("seats.txt"); for (int r = 0; r < ROWS; r++) for (int c = 0; c < COLS; c++) fin >> seats[r][c]; char seats2[ROWS][COLS];

//sets up two dimensional array //opens file //for loops which input data from file into array

//sets up second two dimensional array

fin.open ("seats2.txt"); for (int r = 0; r < ROWS; r++) for (int c = 0; c < COLS; c++) fin >> seats2[r][c];

//opens file //for loops which input data from file into array

char decision; string seatchoice; cout << "Select choice from menu:\n"; cout << "D to display seating chart\n"; cout << "P to purchase a seat\n"; cout << "S to compute statistics\n"; cout << "Q to quit\n"; cout << "\n"; cin >> decision; while (decision != 'Q' ) { if (decision == 'D') { if (ROWS == 10) display( seats, ROWS); if (ROWS == 20) display( seats2, ROWS); cout << "\n";}

//users menu decision variable // users seat choice variable //menu construction

if (decision == 'P') {purchase( seats2, ROWS); cout << "\n";} if (decision == 'S') {statistics( seats2, ROWS); cout << "\n";} cin >> decision; } system("pause"); return 0; }

void display (char seatchart[][COLS], int rows){ //POST: Display the seating chart cout << "Seating Chart: \n"; cout << "\n"; int c = 0; int r = 0; if (COLS == 4) {cout << " AB CD\n"; while(r < ROWS){ cout << setw(2) << r+1 << " " << seatchart[r][c] << seatchart[r][c+1] << " " << seatchart[r][c+2] << seatchart[r][c+3] << endl; r++; }} c = 0; r = 0; if (COLS == 6) {cout << " ABC DEF\n"; while(r < ROWS){ cout << setw(2) << r+1 << " " << seatchart[r][c] << seatchart[r][c+1] << seatchart[r][c+2] << " "<< seatchart[r][c+3] << seatchart[r][c+4] << seatchart[r][c+5] << endl; r++; }} } void purchase (char seatchart[][COLS], int rows){ //POST: Allow the user to purchase a seat and mark seat as taken string section = "Business"; //variable for plane section string seatchoice; //variable for seat choice int row = 0; //variable for row int col = 0; //variable for col string first; //variable first character of seat choice string second; //variable for first two characters of seatchoice string third; //variable for third character in seatchoice string sec; //variable for second character of seatchoice cout << "Enter your row number and column letter (ex. 1A) : "; cin >> seatchoice; first = seatchoice.substr(0,1); second = seatchoice.substr(0,2); third = seatchoice.substr(2,1); sec = seatchoice.substr(1,1); if ((sec == "A" || (third) == "A")) col = 1; if ((sec == "B" || (third) == "B")) col = 2; if ((sec == "C" || (third) == "C")) col = 3;

if if if if if if if if if if if if if if if if if if if if if if if if if if if if if if if if if

((sec == "D" || (third) == "D")) col = 4; ((sec == "E" || (third) == "E")) col = 5; ((sec == "F" || (third) == "F")) col = 6; (first == "1") row = 1; (first == "2") row = 2; (first == "3") row = 3; (first == "4") row = 4; (first == "5") row = 5; (first == "6") row = 6; (first == "7") row = 7; (first == "8") row = 8; (first == "9") row = 9; (second == "10") row = 10; (second == "11") row = 11; (second == "12") row = 12; (second == "13") row = 13; (second == "14") row = 14; (second == "15") row = 15; (second == "16") row = 16; (second == "17") row = 17; (second == "18") row = 18; (second == "19") row = 19; (second == "20") row = 20; (second == "21") row = 21; (second == "22") row = 22; (second == "23") row = 23; (second == "24") row = 24; (second == "25") row = 25; (second == "26") row = 26; (second == "27") row = 27; (second == "28") row = 28; (second == "29") row = 29; (second == "30") row = 30;

cout << row << " " << col << endl; if (row > ROWS) cout << "Invalid Row\n"; if (col == 0) cout << "Invalid Collumn\n"; if (row <= FIRSTCLASSROWS) section = "First"; if (seatchart[row-1][col-1] == 'X' && row != 0 && col != 0) cout << section << " class seat " << seatchoice << " is not available.\n"; if (seatchart[row-1][col-1] == '.' && row != 0 && col != 0) cout << section << " class seat " << seatchoice << " is available.\n";

char decision; if (section != "First" && col != 0 && row < ROWS && seatchart[row-1][col-1] == '.') cout << "Would you like to upgrade to first class for an additional $75? (Y/N) :"; cin >> decision;; if (availableFirstClass( seatchart, FIRSTCLASSROWS) && decision == 'Y') assignFirstClass(seatchart, FIRSTCLASSROWS, row, decision); if (!availableFirstClass( seatchart, FIRSTCLASSROWS) && decision == 'Y') cout << "There is no available first class seats at this time.\n"; if (decision == 'N') seatchart[row - 1][col -1] = 'X'; if (seatchart[row-1][col-1] == '.' && section == "First") seatchart[row - 1][col -1] = 'X'; } void statistics (char seatchart[][COLS], int rows){ //POST: Compute and display percent occupancy double sumempty = 0; //variable for total empty seats double sumfull = 0; //variable for total full seats double sumemptyfc = 0; //variable for total empty first class seats double sumfullfc = 0; //variable for total full first class seats double sumemptybc = 0; //variable for empty business class seats double sumfullbc = 0; //variable for full business class seats int r = 0; //counter variable int c = 0; //counter variable for (int r = 0; r < ROWS; r++) for (int c = 0; c < COLS; c++) if( seatchart[r][c] == '.') sumempty = sumempty + 1; if( seatchart[r][c] == 'X') sumfull = sumfull + 1;

r = 0; c = 0; for (int r = 0; r for (int c if( if(

< FIRSTCLASSROWS; r++) = 0; c < COLS; c++) seatchart[r][c] == '.') sumemptyfc = sumemptyfc + 1; seatchart[r][c] == 'X') sumfullfc = sumfullfc + 1;

r = 0; c = 0; for (int r = FIRSTCLASSROWS; r < ROWS; r++) for (int c = 0; c < COLS; c++) if( seatchart[r][c] == '.') sumemptybc = sumemptybc + 1; if( seatchart[r][c] == 'X') sumfullbc = sumfullbc + 1; int totalbseats = (ROWS - FIRSTCLASSROWS) * COLS;

cout << "Overall percent occupancy is " << ((ROWS * COLS) - sumempty) / (ROWS * COLS)*100 << "%" << endl; cout << "Overall first class percent occupancy is " << ((FIRSTCLASSROWS * COLS) - sumemptyfc) / (FIRSTCLASSROWS * COLS)*100 << "%" << endl; cout << "Overall business percent occupancy is " << ((totalbseats - sumemptybc)/totalbseats)*100 << "%" << endl; } bool availableFirstClass (char seatchart[][COLS], int nrows){ //PRE: nrows is the number of first class rows at front of plane //POST: Return true if a seat is available in first class; else false for (int r = 0; r < FIRSTCLASSROWS; r++) for (int c = 0; c < COLS; c++) if( seatchart[r][c] == '.') return true; } void assignFirstClass (char seatchart[][COLS], int nrows, int& row, char& letter){ //PRE: nrows is the number of first class //POST: row and letter are set to an available first class seat int col; for (int r = 0; r < FIRSTCLASSROWS; r++) for (int c = 0; c < COLS; c++) if( seatchart[r][c] == '.') { row = r + 1 ; col = c; } seatchart[row - 1][col -1] = 'X'; if (col if (col if (col if (col if (col if (col cout << } == 1) letter = == 2) letter = == 3) letter = == 4) letter = == 5) letter = == 6) letter = "You have been 'A'; 'B'; 'C'; 'D'; 'E'; 'F'; assigned to first class seat " << row << letter << endl;

Screen shot 1 parts 1 & 2

Screen Shot 2

Screen Shot 4. Parts 1 & 2

//Austin Ward //COS 220 Assignment 7 //This program allows user to manage their inventory #include<fstream> #include<iostream> #include<iomanip> #include<string> using namespace std; struct Inventory { string name; int quantity; double price; }; //sets up struct

void edit( Inventory a[], string b); //PRE: array of structs containing inventory, desired item to edit, amount to edit //POST: edited array void insert ( Inventory d[], string e, int f, double g); //PRE: array of inventory, ID of new item, amount of new item, price of new item //POST: edited array void remove ( Inventory h[], string i ); //PRE: array of inventory, item to remove //POST: edited array int main () { char decision; int len = 0; cout << "Select from menu\n"; cout << "E - Edit\n"; cout << "I - Insert\n"; cout << "R - Remove\n"; cout << "D - Display Inventory\n"; cout << "Q - Quit\n"; cout << "\n"; Inventory list[100]; ifstream fin;

//allows user to edit, remove, display, and insert, into inventory

// creates array of structs

int count = 0; Inventory temp; double amount = 0;

//counter variable //creates temporary array //used to find total //opens text file //priming read

fin.open ("inventory.txt"); fin >> temp.name >> temp.quantity >> temp.price; while (!fin.fail () ) {list[count] = temp; count++; fin >> temp.name >> temp.quantity >> temp.price;} cin >> decision; cout.setf (ios::left);

//inputs decision

while ( decision != 'Q' ) //allows user to quit { if ( decision == 'D' ){ double sum = 0; cout << setw(13) << "ID" << setw(15) << "Stock" << "Unit Price\n"; for (len = 0; len <= count - 1; len++){ sum = sum + (list[len].quantity * list[len].price); cout << setw(13) << list[len].name << setw(15) << list[len].quantity << list[len].price << endl; } cout << "Total Inventory: "<< sum << endl; } if (decision == 'E') { int change; string choice; cout << "Enter product: "; cin >> choice; edit ( list, choice); } if (decision == 'I') { string id; int stock; double price; cout << "Enter new product

//inputed amount to change //item to change

//puts values into functions

//variable for item id //variable for inputed stock //variable for inputed price ID, stock, and unit price: ";

cin >> id >> stock >> price; insert ( list, id, stock, price); } if (decision == 'R') { string id; cout << "Enter ID of product to remove: "; cin >> id; remove( list, id); } cout << "\n"; cout << "Enter letter of choice: "; cin >> decision; } }

//inputs values into function

//inputed id value

void edit( Inventory a[], string b){ //PRE: array of structs containing inventory, desired item to edit, amount to edit //POST: edited array int change; int y = 0; int count = 0; bool x = false; while ( count < 100 ) { if (a[count].name == b){ x = true; y = count;} count++; } if( x == false) cout << "Item " << b << " is not found.\n"; if (x == true) { cout << "Enter amount to add (+) or subtract (-): "; cin >> change; a[y].quantity = a[y].quantity + change; cout << "Item " << b << " has the new amount of in stock " << a[y].quantity << endl;} cout << "\n"; } void insert ( Inventory d[], string e, int f, double g) { //PRE: array of inventory, ID of new item, amount of new item, price of new item //POST: edited array

int y = 0; int count = 0; while (count < 7) { if (d[count].name == e ) y = 1; count++; } if (y == 1) cout << "Item " << e << " already exists.\n"; if (y == 0) { d[7].name = e; d[7].quantity = f; d[7].price = g; cout << "Item " << e << " is inserted.\n"; } } void remove ( Inventory h[], string i ) { //PRE: array of inventory, item to remove //POST: edited array+ int count = 0; bool x = true; while (count < 10 ) { if (h[count].name == i ) { h[count] = h[7]; cout << "Item " << i << " has been removed."; x = false;} count++; } if (x == true) cout << "Item " << i << " does not exist"; cout << "\n"; }

Вам также может понравиться