Presentation is loading. Please wait.

Presentation is loading. Please wait.

Lecture 1 -- 1Computer Science I - Martin Hardwick Bank Simulation (1) #include using namespace std; // Create bank account data type struct acct { //

Similar presentations


Presentation on theme: "Lecture 1 -- 1Computer Science I - Martin Hardwick Bank Simulation (1) #include using namespace std; // Create bank account data type struct acct { //"— Presentation transcript:

1 Lecture Computer Science I - Martin Hardwick Bank Simulation (1) #include using namespace std; // Create bank account data type struct acct { // bank account data int num; // account number string name; // owner of account float balance; // balance in account }; // Function Prototypes int find(acct bank[ ], int numaccts, int goal); void insert(acct bank[ ], int &numaccts, acct newacct); void remove(acct bank[ ], int &numaccts, int acctnum); rThis program simulates a bank by processing transactions for bank accounts. rAn array is used to hold the list of bank accounts. rA bank account consists of an account number, the name of the person who owns it, and the current balance. l we need a way to combine these three pieces of data into a single piece of account data l we will use a C++ struct to do this

2 Lecture Computer Science I - Martin Hardwick C++ structs rSyntax:Example: rThink of a struct as a way to combine heterogeneous data values together to form a new more complex data type. l you can use this new data type to declare variables l you can create arrays with this new data type rPlace struct definitions at the beginning of a program, after include lines and before functions and function prototypes. rTo access the components of a struct, use the dot operator. l can be used anywhere an ordinary variable can be used struct { ;... }; struct acct { int acctnum; string name; float balance; }; acctmyAccount; myAccount.acctnum = 99999; myAccount.name = Hardwick; myAccount.balance = 10000;

3 Lecture Computer Science I - Martin Hardwick Bank Simulation (2) int main () //PURPOSE: simulate a small bank //PRECONDITIONS: existing // accounts in file account.txt // in project folder //POSTCONDITIONS: finds, inserts, // deletes, and lists accounts { // list of bank accounts acct bank[1000]; // total number of accounts intnumaccts; // file with list of bank accounts ifstreamvault; // user request stringcommand; // account number to find intgoal; rThe main function creates the list of bank accounts and processes user commands to perform transactions. rAssume that there will be no more than 1000 accounts. rThe existing accounts are read from a file at the start of the program. l this creates the bank array for the program to use rUser commands are typed as strings (e.g., find, insert, exit).

4 Lecture Computer Science I - Martin Hardwick Bank Simulation (3) // subscript of account in bank intloc; // a bank account acctaccount; // read existing accounts from file vault.open("accounts.txt"); numaccts=0; vault >> bank[0].num >> bank[0].name >> bank[0].balance; while (!vault.eof()) { numaccts = numaccts + 1; vault >> bank[numaccts].num >> bank[numaccts].name >> bank[numaccts].balance; } // display dollar values correctly cout << setiosflags(ios::fixed) << setprecision(2); rThe program assumes that a file named accounts.txt contains the data for existing accounts. rNote the position of a subscript when using an array of struct types. rThis is the standard while loop for reading data from a file until the end of file is reached. rDollar values should be displayed with two digits to the right of the decimal point. l the last two lines instruct the cout object to do this when displaying float values

5 Lecture Computer Science I - Martin Hardwick Bank Simulation (4) // get ready to process commands cout << "The bank is now open." ; cout << endl << endl; // loop to process user commands cout << endl << " " << endl; cout << "Enter command " << "(exit to stop): "; cin >> command; while (command != "exit") { if (command == "find") { // FIND COMMAND // get account to find cout << "Enter account num: "; cin >> goal; // search for account loc = find(bank, numaccts, goal); rUse the standard while loop to get commands from the user until the user enters the exit command. rUser commands are processed inside the loop. l since commands are strings, cant use a switch statement l therefore, use nested IF- ELSE statements rFirst case is the find command. l get account number to find from user l call function find to search the account list for this account

6 Lecture Computer Science I - Martin Hardwick Function Find (1) int find(acct bank[ ], int numaccts, int goal) //PURPOSE: search for a goal account // in the list of accounts //PRECONDITIONS: 0 <= numaccts, // numaccts is number of accounts // in list of accounts (i.e., bank) //POSTCONDITIONS: returns the // subscript of goal account or -1 if // the goal account is not found { int k;// loop variable rParameters: list of accounts, number of accounts, number of the goal account to find. rReturn: subscript of goal account if found, or –1 to indicate that it is not in the list. rSearch the list from its beginning, element by element until: l find the account l reach the end of the list rThis is called a sequential search.

7 Lecture Computer Science I - Martin Hardwick Function find (2) //search for the goal account in //the list of accounts for (k=0; k

8 Lecture Computer Science I - Martin Hardwick Bank Simulation (5) // display account data // or error message if (loc >= 0) { cout << "Account: " << bank[loc].num << "\t" << "Owner: " << bank[loc].name << "\t" << "Balance: " << bank[loc].balance << endl; } else { cout << "Account " << goal <<" does not exist." << endl; } rRemember, just before this is the call to function find with the result saved in variable loc. rIf loc is greater than or equal to 0, it must be the subscript of the goal account in the account list. l in this case, display the account data. rOtherwise, loc must by –1 indicating that the goal account was not found. l in this case, display an error message

9 Lecture Computer Science I - Martin Hardwick Bank Simulation (6) else if (command == "list") { // LIST COMMAND cout << "List of accounts:" << endl; for (loc=0; loc

10 Lecture Computer Science I - Martin Hardwick Bank Simulation (7) else if (command == "insert") { // INSERT COMMAND cout << "Enter new account data:" << endl; cout << "Account number: "; cin >> account.num; cout << "Account name: "; cin >> account.name; cout << "Account balance: "; cin >> account.balance; insert(bank, numaccts, account); } rThe third case inside the while loop that processes user commands is the insert command. rThis command assumes that the list of accounts is ordered by account number. rIt inserts a new account into the account list in the right place to maintain the ordering. rIt prompts the user for the data about the new account. rIf then calls function insert to add the new account to the account list.

11 Lecture Computer Science I - Martin Hardwick Function insert (1) void insert(acct bank[ ], int &numaccts, acct newacct) //PURPOSE: insert a new account into // the account list //PRECONDITIONS: 0 <= numaccts, // numaccts is number of accounts // in account list, newacct is a new // account to be added, account // list is in order by account num //POSTCONDITIONS: account list // modified to add new account, // order of account list maintained { intk,j;// loop variables rParameters: list of accounts, number of accounts, new account to add to the list. rBoth the account list (i.e., bank) and the number of accounts (i.e., numaccts) will change. l numaccts must be passed by reference l arrays are always passed by reference rSince the list order must be maintained, we must find where in the list the new account should be placed. rOnce we know where it goes, we must make room in the list to insert the new account.

12 Lecture Computer Science I - Martin Hardwick Inserting Into An Order List 1.Find where the new item goes. 2.Make room for the new item. 3.Insert the new item into the list Number of elements in the list: Insert

13 Lecture Computer Science I - Martin Hardwick Function insert (2) // find place to put new account in list // to maintain order of list k=0; while ((bank[k].num < newacct.num) && (k < numaccts)) { k = k + 1; } // add new account to list if (k == numaccts) { // goes at end bank[numaccts] = newacct; numaccts = numaccts + 1; } rThe loop terminates when: l we find the first account in the list with a larger account number l we reach the end of the list having found no larger account numbers rIf there are no larger account number in the list, then k equals the number of accounts at the end of the loop. l in this case, add the new account to the end of the list

14 Lecture Computer Science I - Martin Hardwick Function insert (3) else { // goes in middle // make room to add it for (j=numaccts; j>k; j--) { bank[j] = bank[j-1]; } // add it to vacated spot bank[k] = newacct; numaccts = numaccts + 1; } rOtherwise, the new account belongs in element k of the list. rMove all elements from k to the end of the list, one element to the right. l it is easiest to do this in reverse order rNow we can put the new account into element k of the list without overwriting another account. rVariable numaccts must be incremented since a new account has been added to the list.

15 Lecture Computer Science I - Martin Hardwick Bank Simulation (8) else { // INVALID COMMAND cout << "Invalid command." << endl; } cout << endl << " " << endl; cout << "Enter a command "; << "(exit to stop): "; cin >> command; } // exit command from user cout << endl << " " << endl; cout << "The bank is now closed."; cout << endl << endl; return 0; } rThe final case in the while loop processing user commands is the default case for invalid commands. l display a suitable error message rWhen the user types the exit command, the while loop terminates. l display a message closing the bank and terminate the program l a real bank program would write the new account list to a file l this file would be loaded into the program the next time it runs


Download ppt "Lecture 1 -- 1Computer Science I - Martin Hardwick Bank Simulation (1) #include using namespace std; // Create bank account data type struct acct { //"

Similar presentations


Ads by Google