Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                

F 79 Ce 6589 B 3 B 377 B 9 Ac 7

Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 5

//Client File

/*
Isaac Bernal
Fundamentals of Programming 2
Professor Lara
4/14/22
This program demostrates the use of linear search on a list of faculty members' first name, last
name, and I.D.
*/

#include <iostream>
#include <fstream>
#include <string>
#include <sstream>
#include "Faculty.h"
using namespace std;

//Function Prototypes
void welcome();
void SelectionSort(Faculty [], int);
int getData(Faculty[], const int );
void printData(Faculty[], int);

int main() {
const int SIZE = 4;
Faculty instructorArray[SIZE];//Class Array
ifstream inputfile;//to access files
int lcount = 0;

welcome();

lcount = getData(instructorArray, SIZE);

SelectionSort(instructorArray, lcount);

printData(instructorArray, lcount);

return 0;
}

//Function Definitions
//***********************************************************
void welcome()
{
cout << "\t\t_-_Welcome To Faculty Member Sort_-_\n\n\tSorting By Last Name in Descending
Order...\n";
}

//***********************************************************
//To Receive the Faculty class array and sorts in descending order by last name
// Returns nothing
void SelectionSort(Faculty fArray[], int numbersSize)
{
int i;
int j;
int indexSmallest;
Faculty temp; // Temporary variable for swap

for (i = 0; i < numbersSize - 1; ++i)


{
// Find index of smallest remaining element
indexSmallest = i;
for (j = i + 1; j < numbersSize; ++j)
{
if ( fArray[j].getLastName() > fArray[indexSmallest].getLastName())
{
indexSmallest = j;
}
}

// Swap numbers[i] and numbers[indexSmallest]


temp = fArray[i];
fArray[i] = fArray[indexSmallest];
fArray[indexSmallest] = temp;
}
}

//***********************************************************
//Reads the text file and store in array
//returns the counter of total members of the array
int getData(Faculty arrayN[], const int SIZE)
{
istringstream inSS;//each members data is in a single line
ifstream inputfile;// to access txt file
inputfile.open("list.txt");
string lineString;

string fName , lName;//stores its own data from file


short iD;
int counter = 0;
while (counter < SIZE)
{
getline(inputfile, lineString);//stores each line of data in a string
inSS.clear();
inSS.str(lineString);//stores buffer in string
inSS >> fName >> lName >> iD;//assign to each variable

arrayN[counter].setFirstName(fName);//set to class to be stored


arrayN[counter].setLastName(lName);
inputfile.clear();

arrayN[counter].setID(iD);

counter = counter +1;


}

inputfile.close();

return counter;
}

//***********************************************************
void printData(Faculty fArray[], int SIZE)
{
cout << "\n\nThe list is: \n\n";
for (int counter = 0; counter < SIZE; ++counter)
{
cout << (counter + 1) << ". " << fArray[counter].getLastName() << endl;

cout << (counter + 1) << ". " << fArray[counter].getFirstName() << "\n";

cout << (counter + 1) << ". " << fArray[counter].getID() << "\n\n";
}
}
//* --- This file is the IMPLEMENTATION FILE ---

#include <string>
using namespace std;
#include "Faculty.h"

//Class Function Definitions


void Faculty::setFirstName(string f)
{
firstName = f;
}

void Faculty::setLastName(string l)
{
lastName = l;
}

void Faculty::setID(short i)
{
facultyID = i;
}

string Faculty::getFirstName()
{
return firstName;
}

string Faculty::getLastName()
{
return lastName;
}

short Faculty::getID()
{
return facultyID;
}
/*
--- This file is the SPECIFICATION FILE ---
*/

#include <string>
using namespace std;

#ifndef FACULTY_H
#define FACULTY_H

class Faculty
{
private:
string lastName;
string firstName;
short facultyID;
public:
//Prototypes
void setFirstName(string f);
void setLastName(string l);
void setID(short i);
string getFirstName();
string getLastName();
short getID();
};

#endif

You might also like