functions.cpp

// Programmer: Bushra Anjum

// file: functions.cpp

#include <iostream>

#include <cstdlib>

#include "header.h"

//#include "stdio.h"

using namespace std;

void establishDB(Bullet db [], const int size)

{

for(int i=0; i<size; i++)

{

//setting caliber

//get random number 0, 1 or 2

int rn = rand()%3;

db[i].m_caliber = CALIBER[rn];

//setting number of lands and grooves

rn = rand()%3;

db[i].m_numberOfLG = NUMBER_OF_LG[rn];

//setting land width and groove width

db[i].m_widthLand = get_random_number(LOWER_LIMIT, UPPER_LIMIT);

db[i].m_widthGroove = get_random_number(LOWER_LIMIT, UPPER_LIMIT);

//seting marking

db[i].m_isMarking = rand()%2 == 0? false : true;

//taking name from user

// cout<<"Enter name for perpetrator number "<<i+1<<": ";

// cin>>db[i].name;

db[i].m_name=VILLAIN[i];

}

}

void printDB(const Bullet db[], const int size)

{

for(int i=0; i<size; i++)

{

cout<<"("<<db[i].m_caliber<<", "<<db[i].m_numberOfLG<<", "<<db[i].m_widthLand<<", ";

cout<<db[i].m_widthGroove<<", "<<(db[i].m_isMarking?"marks":"no marks");

cout<<", "<<db[i].m_name<<")"<<endl;

}

}

void printBullet(const Bullet & bullet)

{

cout<<"\n\nLargest groove width .38 caliber bullet is: \n\t(";

cout<<bullet.m_caliber<<", "<<bullet.m_numberOfLG<<", "<<bullet.m_widthLand;

cout<<", "<<bullet.m_widthGroove<<", "<<(bullet.m_isMarking?"marks":"no marks");

cout<<")"<<endl;

cout<<"\tIt belongs to the no good scoundrel and thievein' "<<bullet.m_name<<endl;

}

void sortDB(Bullet db[], const int size)//bubble sort

{

for(int i =0;i<size;i++)

{

for(int j =0;j<size;j++)

{

if(db[i].m_caliber < db[j].m_caliber)

{

swap_bullets(db[i], db[j]);

}

}

}

}

Bullet searchLargest38(const Bullet db [], const int size)

{

Bullet b;

b.m_widthGroove=0;

for(int i =0;i<size;i++)

{

if(db[i].m_caliber == CALIBER[1] && db[i].m_widthGroove>b.m_widthGroove)

{

b=db[i];

}

}

return b;

}

bool searchBullet(Bullet & b, Bullet db[], const int size)

{

for(int i =0;i<size;i++)

{

if(b.m_caliber == db[i].m_caliber && b.m_numberOfLG==db[i].m_numberOfLG &&

isbetween(b.m_widthLand,db[i].m_widthLand,TOLERANCE) &&

isbetween(b.m_widthGroove, db[i].m_widthGroove, TOLERANCE) &&

b.m_isMarking == db[i].m_isMarking)

{

b.m_name=db[i].m_name;

return true;

}

}

return false;

}

void presentMenu()

{

cout<<"\n\tWelcome to Bullet Matching Program\n";

cout<<"\t----------------------------\n";

cout<<"1. Search the Database\n";

cout<<"2. Exit\n";

}

Bullet getBullet()

{

Bullet b;

cout<<"Please enter caliber (0.22, 0.38, 0.50): ";

cin>>b.m_caliber;

cout<<"Number of land and grooves. ";

b.m_numberOfLG = getInteger(5,7);

cout<<"Please enter width of land [0.025, 0.030]: ";

cin>>b.m_widthLand;

cout<<"Please enter width of groove [0.025, 0.030]: ";

cin>>b.m_widthGroove;

b.m_isMarking=getBoolean("Are there markings on the bullet ");

return b;

}

void goodbye()

{

cout<<"\nSigning of now ...\n\n";

}

int getInteger(int min, int max)

{

int val;

cout<<"Please enter a value between ["<<min<<", "<<max<<"]: ";

cin>>val;

while(val<min || val >max)

{

cout<<"Invalid entry ... Please enter a value between ["<<min<<", "<<max<<"]: ";

cin>>val;

}

return val;

}

bool getBoolean(string question)

{

int val;

cout<<question<<" Enter 1 for Yes, 0 for No: ";

cin>>val;

while(val!=1 && val!=0)

{

cout<<"Invalid entry ... Enter 1 for Yes, 0 for No: ";

cin>>val;

}

return val == 0? false : true;

}

bool isbetween(float val, float mid, float tolerance)

{

if(val>= mid-tolerance && val<=mid+tolerance)

return true;

return false;

}

float get_random_number(const float lower_limit, const float upper_limit)

{

return (lower_limit + rand()/ (RAND_MAX / (upper_limit - lower_limit)));

}

void swap_bullets(Bullet &b1, Bullet &b2)

{

Bullet temp_bullet = b1;

b1 = b2;

b2 = temp_bullet;

}