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
|
#include <iostream>
#include <cstdlib>
#include <fstream>
#include <iomanip>
#include <string>
using namespace std;
struct Employee{
int id;
string first;
string last;
double salary;
};
void get_data(ifstream &inp, Employee data[], int &count);
void sort_id(ifstream &inp, Employee data[], int &count);
void sort_pay(ifstream &inp, Employee data[],int &count);
int main(){
int count = 0;
string input_filename;
Employee data[100];
cout << "Enter the name of the file you would like to be sorted: ";
cin >> input_filename;
ifstream inp;
inp.open(input_filename.c_str());
get_data(inp, data, count);
sort_id(inp, data, count);
sort_pay(inp, data, count);
return EXIT_SUCCESS;
}
void get_data(ifstream &inp, Employee data[], int &count){
for (int i = 0; i < 6; i++){
inp >> data[count].id;
inp >> data[count].first;
inp >> data[count].last;
inp >> data[count].salary;
count++;
}
}
void sort_id(ifstream &inp, Employee data[], int &count){
int temp = -1;
for (int i=0; i< count; i++)
{
if (data[i].id > data[i+1].id){
temp = data[i+1].id;
data[i+1].id = data[i].id;
data[i].id = temp;
}
}
for (int i = 0; i < count; i++){
cout << data[i].id << endl;
}
}
void sort_pay(ifstream &inp, Employee data[], int &count){
int temp = -1;
for (int i=0; i< count; i++)
{
if (data[i].salary > data[i+1].salary){
temp = data[i+1].salary;
data[i+1].salary = data[i].salary;
data[i].salary = temp;
}
}
for (int i = 0; i < count; i++){
cout << data[i].salary << endl;
}
}
|