// MAR 3/20/00 Example for Use of Generic Functions with Vector // of accounts in Data Structures // Written by Michael Redmond 3/19/00 // La Salle University # include //# include #include #include #include "account.h" using namespace std; void Show(list & all) { // show the accounts - have to use iterator with lists list::iterator acctItr; for (acctItr = all.begin(); acctItr != all.end(); acctItr++ ){ cout << endl << " " << *acctItr; } cout << endl; return; } void AddInOrderHighToLow (list & all, const Account & toAdd) { list::iterator acctItr = all.begin(); bool found = false; list::iterator nextItr; list::iterator newItr; // will be set to where new one is - not that we need that // if account to add is greater than first put new one first if (*acctItr < toAdd) { // insert before first all.push_front(toAdd); // indicate found found = true; } // loop until find right place or get to end while ((acctItr != all.end() ) && ( ! found) ) { // know what is ahead nextItr = acctItr; nextItr++; // see if reached end of list if (nextItr == all.end()) { // insert the new account at end - after last element // newItr = all.insert(acctItr,toAdd); all.push_back(toAdd); // when acctItr is incremented at end of loop that should cause the loop to end // nope sorry - it makes it point to the one just added !! // so need to set found to true to drop out of loop found = true; } else { //cout << endl << " checking between " << *acctItr << " and " << *nextItr << endl; // not at end - see if found place anyway // if account to add is less than current and greater than next // uses < and >= operators defined for Account if (((*acctItr) > toAdd ) && ((*nextItr) <= toAdd ) ) { // found place // insert before next iterator newItr = all.insert(nextItr,toAdd); // indicate that we found the place so loop no more found = true; } } acctItr++; // bump to next item in list } return; } bool aLowBal(const Account & currAccount) { if (currAccount.GetBal() <= 250.00 ) { return true; } else { return false; } } int PickRand(const int max) { unsigned int ran = rand(); int toDep = ran % max; return toDep; } // compare accounts based on account numbers bool CompareAcctNum (const Account & first,const Account & second) { if (first.GetAcctNum() > second.GetAcctNum() ) { return true; } else { return false; } } int main () { char wait; list accts; // define the accounts list // create 5 accounts for (int cnt = 0; cnt < 5; cnt++) { Account newOne; // default to random acct num and zero balance accts.push_back(newOne); } // show the accounts cout << endl << "Accounts: " << endl; Show(accts); cout << "Waiting> " << flush; cin >> wait; list::iterator acctItr; // lets just randomly deposit money into the accounts for (acctItr = accts.begin(); acctItr != accts.end(); acctItr++ ){ int toDep = PickRand(2000); // random num dollars between 0 and 1999 acctItr->Deposit(toDep); } // copy account list using copy constructor list copyAccts(accts); // show the accounts cout << endl << "Accounts with some money: " << endl; Show(accts); cout << "Waiting> " << flush; cin >> wait; // reverse the accounts reverse(accts.begin(),accts.end()); // show the accounts cout << endl << "accts reversed: " << endl; Show(accts); cout << "Waiting> " << flush; cin >> wait; // report the number of low balance accounts int numLow = count_if(accts.begin(),accts.end(),aLowBal); cout << "Accounts has " << numLow << " low balance accounts " << endl; cout << "Waiting> " << flush; cin >> wait; // sort the accounts // below should work - but get 23 errors pointing into microsofts code //sort(accts.begin(),accts.end()); // sorts from low to high // so use the function defined specifically for lists instead of generic accts.sort(); list lowToHigh; lowToHigh = accts; // show the accounts cout << endl << "accts sorted by balance: " << endl; Show(accts); cout << "Waiting> " << flush; cin >> wait; accts.reverse(); // show the accounts cout << endl << "accts reversed using list function: " << endl; Show(accts); cout << "Waiting> " << flush; cin >> wait; cout << "First account " << accts.front() << endl; cout << "Last account " << accts.back() << endl; cout << "Waiting> " << flush; cin >> wait; // show the original accounts cout << endl << "Original Accounts: " << endl; Show(copyAccts); // show the low to high accounts cout << endl << "Accounts Low to High: " << endl; Show(lowToHigh); cout << "Waiting> " << flush; cin >> wait; Account newAcct; int amtToDep = PickRand(1000); // random num dollars between 0 and 1999 newAcct.Deposit(amtToDep); copyAccts.push_front(newAcct); // show the revised accounts cout << endl << " Accounts: with new one at front " << endl; Show(copyAccts); cout << "Waiting> " << flush; cin >> wait; list toPlayWith(copyAccts); toPlayWith.pop_back(); toPlayWith.pop_back(); // show the revised accounts cout << endl << " copy of Accounts: with back two removed " << endl; Show(toPlayWith); cout << "Waiting> " << flush; cin >> wait; list toPlayWith2(accts); cout << endl << " going back to previous copy of Accounts " << endl; Show(toPlayWith2); // want to find, display and remove nth element // for now force n to be 3 instead of asking user int n = 3; int num = toPlayWith2.size(); // make sure really have that many if (n <= num) { list::iterator aItr = toPlayWith2.begin(); int count = 0; // bump forward n times so are on nth element // assuming counting starting with 0 while (count < n) { count++; aItr++; } // now are at the one to display and delete cout << "Deleting " << *aItr << endl; // could do it with erase or remove //toPlayWith2.erase(aItr); toPlayWith2.remove(*aItr); // show the revised accounts cout << endl << " copy of Accounts: with nth element removed " << endl; Show(toPlayWith2); cout << "Waiting> " << flush; cin >> wait; } //list byBalance(copyAccts); // copy accounts so can sort ////byBalance.sort(CompareAcctNum); // sort by acct num instead of balance //byBalance.sort(GreaterThan); //// show the accounts sorted by acct num //cout << endl << " Accounts: byBalance - sorted by account balance " << endl; //Show(byBalance); //cout << "Waiting> " << flush; //cin >> wait; // show the accounts cout << endl << "we're going to add to these accts sorted by balance: " << endl; Show(accts); // create 5 new accounts and add them in sorted order for (int cnt2 = 0; cnt2 < 5; cnt2++ ) { Account newOne; int amtDep = PickRand(2000); // random num dollars between 0 and 1999 newOne.Deposit(amtDep); cout << endl << "Adding: " << newOne << endl << "resulting in: " << endl; AddInOrderHighToLow(accts,newOne); // add it // show the updated accounts Show(accts); cout << "Waiting> " << flush; cin >> wait; } return 0; }