Object Oriented Programming[202302-
IGS2130-001]
C++ programming language
Name: Akhrorbek
ID: 12214752
LAB 4
Homework 2
2023/09/26
Question1
#include <iostream>
using namespace std;
// Function prototypes
void sort3(double& a, double& b, double& c, bool (*compare)(double&, double&));
void print3(const double& a, const double& b, const double& c);
void swap2(double& a, double& b);
bool ascending(double& a, double& b);
bool descending(double& a, double& b);
int main() {
// Initializes variables
double na1 = 10.3, na2 = -2.1, na3 = 8.0;
double nd1 = 10.3, nd2 = -2.1, nd3 = 8.0;
// Displays original values
cout << "Before sort: ";
print3(na1, na2, na3);
// Sorts in ascending order
sort3(na1, na2, na3, ascending);
// Sorts in descending order
sort3(nd1, nd2, nd3, descending);
// Displays sorted values
cout << "After sort(ascending): ";
print3(na1, na2, na3);
cout << "After sort(descending): ";
print3(nd1, nd2, nd3);
return 0;
}
// Function to sort three numbers using a comparison function
void sort3(double& a, double& b, double& c, bool (*compare)(double&, double&)) {
if (compare(a, b)) swap2(a, b); // Compare and swap a and b if necessary
if (compare(b, c)) swap2(b, c); // Compare and swap b and c if necessary
if (compare(a, b)) swap2(a, b); // Compare and swap a and b if necessary
}
// Function to print three numbers
void print3(const double& a, const double& b, const double& c) {
cout << a << " " << b << " " << c << endl;
}
// Function to swap two numbers
void swap2(double& a, double& b) {
double temp = a;
a = b;
b = temp;
}
// Comparison function for ascending order
bool ascending(double& a, double& b) {
return a > b;
}
// Comparison function for descending order
bool descending(double& a, double& b) {
return a < b;
}
Question 2
Main:
#include <iostream>
#include "bank.h"
using namespace std;
int main(void) {
int choice;
while (1) {
ShowMenu();
cout << "Select menu: ";
cin >> choice;
cout << endl;
switch (bank(choice)) {
case bank::MAKE:
MakeAccount();
break;
case bank::DEPOSIT:
DepositMoney();
break;
case bank::WITHDRAW:
WithdrawMoney();
break;
case bank::INQUIRE:
ShowAllAccInfo();
break;
case bank::EXIT:
return 0;
default:
cout << "Illegal selection.." << endl;
}
}
return 0;
}
Bank.h:
#include <iostream>
#include "bank.h"
using namespace std;
Account accArr[MAX_ACC_NUM]; // Account array
int accNum = 0; // # of accounts
// Function to display the main menu
void ShowMenu(void) {
cout << "-----Menu------" << endl;
cout << "1. Make Account" << endl;
cout << "2. Deposit" << endl;
cout << "3. Withdrawal" << endl;
cout << "4. Display all" << endl;
cout << "5. Exit program" << endl;
}
// Function to make a new account
void MakeAccount(void) {
int id;
char name[NAME_LEN];
int balance;
if (accNum >= MAX_ACC_NUM) {
cout << "Sorry! Cannot make an account anymore." << endl;
return;
}
cout << "[Make Account]" << endl;
cout << "Account ID: ";
cin >> id;
cout << "Customer Name: ";
cin.ignore(); // Clear the newline character from the previous input
cin.getline(name, NAME_LEN);
cout << "Initial balance: ";
cin >> balance;
cout << endl;
if (GetAccIdx(id) != -1) {
cout << "Error: Existing account ID" << endl;
return;
}
accArr[accNum].accID = id;
accArr[accNum].balance = balance;
strncpy(accArr[accNum].cusName, name, NAME_LEN - 1);
accArr[accNum].cusName[NAME_LEN - 1] = '\0';
accNum++;
cout << "Account created successfully." << endl;
}
// Function to deposit money into an account
void DepositMoney(void) {
int money;
int id;
cout << "[Deposit]" << endl;
cout << "Account ID: ";
cin >> id;
cout << "Deposit amount: ";
cin >> money;
for (int i = 0; i < accNum; i++) {
if (accArr[i].accID == id) {
accArr[i].balance += money;
cout << "Deposit completed" << endl << endl;
return;
}
}
cout << "This ID is not valid." << endl << endl;
}
// Function to withdraw money from an account
void WithdrawMoney(void) {
int money;
int id;
cout << "[Withdrawal]" << endl;
cout << "Account ID: ";
cin >> id;
cout << "Withdrawal amount: ";
cin >> money;
for (int i = 0; i < accNum; i++) {
if (accArr[i].accID == id) {
if (accArr[i].balance < money) {
cout << "Not enough balance" << endl << endl;
return;
}
accArr[i].balance -= money;
cout << "Withdrawal completed" << endl << endl;
return;
}
}
cout << "This ID is not valid." << endl << endl;
}
// Function to display information of all accounts
void ShowAllAccInfo(void) {
cout << "-----All Accounts------" << endl;
for (int i = 0; i < accNum; i++) {
cout << "Account ID: " << accArr[i].accID << endl;
cout << "Name: " << accArr[i].cusName << endl;
cout << "Balance: " << accArr[i].balance << endl << endl;
}
}
// Function to get the index of an account by its ID
int GetAccIdx(int id) {
for (int i = 0; i < accNum; i++) {
if (accArr[i].accID == id) {
return i;
}
}
return -1;
}
Bank.cpp:
#include <iostream>
#include "bank.h"
using namespace std;
Account accArr[MAX_ACC_NUM]; // Account array
int accNum = 0; // # of accounts
void ShowMenu(void) {
cout << "-----Menu------" << endl;
cout << "1. Make Account" << endl;
cout << "2. Deposit" << endl;
cout << "3. Withdrawal" << endl;
cout << "4. Display all" << endl;
cout << "5. Exit program" << endl;
}
void MakeAccount(void) {
// Implementation of MakeAccount function
}
void DepositMoney(void) {
// Implementation of DepositMoney function
}
void WithdrawMoney(void) {
// Implementation of WithdrawMoney function
}
void ShowAllAccInfo(void) {
// Implementation of ShowAllAccInfo function
}
int GetAccIdx(int id) {
// Implementation of GetAccIdx function
}