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 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121
|
#include <iostream>
#include <string>
#include <iomanip>
#include <fstream>
using namespace std;
//Function Prototype
void Read_Data(string accountNumbers[], double balance[], int &length);
void Sort_Accounts(string accountNumbers[], double balance[], int length);
int Search_Accounts(string accountNumbers[], double balance[], int length, string searchNumber);
string Read_Account_Number();
void main() {
int i = 0, length;
const int maxsize = 999;
string accountNumbers[maxsize];
double balance[maxsize];
//Call Read_Data Function
Read_Data(accountNumbers, balance, length);
//Call Sort_Account Function
Sort_Accounts(accountNumbers, balance, length);
cout <<"Account Number: "<< accountNumbers[1] << " Balance is: " << balance[1] << endl;
//Search account numbers
string searchAccountNumber;
cout << "please enter account number" << endl;
cin >> searchAccountNumber;
while (true)
{
searchAccountNumber = Read_Account_Number();
//Terminate loop when -99 entered
if (searchAccountNumber == "-99")
break;
//Calling Binary search
int index = Search_Accounts(accountNumbers, balance, length, searchAccountNumber);
if (index == -1) {
cout << "Cannot find account" << endl;
}
else {
cout << (accountNumbers[index]) << " " << (balance[index]) << endl;
}
}
}//end main
//Read_Data Function Defined
void Read_Data(string accountNumbers[], double balance[], int &length) {
int i = 0;
ifstream infile;
ifstream infile("Final_Random_Accounts_Unsorted.txt");
while (infile >> accountNumbers[i] >> balance[i]) { // Note: The stream returns false on eof
i++;
}
length = i - 1;
}
//Sorting function defined
void Sort_Accounts(string accountNumbers[], double balance[], int length)
{
int i, j, min;
string temp;
double tempBalance;
for (i = 0; i < length - 1; i++)
{
min = i;
for (j = i + 1; j < length; j++)
{
if (accountNumbers[j]<accountNumbers[min])
{
min = j;
}
}
temp = accountNumbers[i];
accountNumbers[i] = accountNumbers[min];
accountNumbers[min] = temp;
tempBalance = balance[i];
balance[i] = balance[min];
balance[min] = tempBalance;
}
}
//Binary Search Function defined
int Search_Accounts(string accountNumbers[], double balance[], int length, string searchNumber)
{
int l = 0, h = length - 1;
int m = l + (h - l) / 2;
while (l <= h)
{
m = l + (h - l) / 2;
if (accountNumbers[m] == searchNumber) {
return m;
}
else if (accountNumbers[m]<searchNumber) {
l = m + 1;
}
else {
h = m - 1;
}
}
return -1;
}
string Read_Account_Number()
{
return string();
}
|