12389 - Tsai DS 20190926 C++ STL   

Description

One day, Billy Gates wants to design a C++ code to sort his employee’s salary in decreasing order. Eaach one is paid according to working time * hourly rate. Finish the TODO part below:

#include<iostream>
#include<vector>    // std::vector
#include<algorithm> // std::sort
 
using namespace std;
 
class employee{
    private:
        int id;
        int time;
        int hourly_rate;
    public:
        employee():id(0),time(0),hourly_rate(0){}
        employee(int e_id,int timeint hourly_rate):id(e_id),time(time),hourly_rate(hourly_rate){}
        int getSalary(){
            //TODO (This function can return employee's salary)
        }
        int get_id(){
            //TODO (This function can return employee's id)
        }
};
 
bool SalaryCMP(employee i,employee j){
     //TODO (Make sorting be increasing or decreasing)
}
 
void sortvector( vector<employee> &company){
    //TODO
    //Hint : sort(begin,end,compare)  
}
 
int main(){
 
    int employee_num; 
    cin>>employee_num;
 
    vector<employee> Microsoft;
    
    //TODO(initialize vector Microsoft)
 
    cout<<"Before sorting"<<endl;
    cout<<"id:Salary"<<endl;
 
    //TODO(Display the vector Microsoft before sorting)
 
    sortvector(Microsoft);
    cout<<"After sorting"<<endl;
    cout<<"id:Salary"<<endl;
 
    //TODO(Display the vector Microsoft after sorting)
    
}

Hints:

Certain functions associated with the vector are:

  1. begin()– Returns an iterator pointing to the first element in the vector

  2. end() – Returns an iterator pointing to the theoretical element that follows the last element in the vector

  3. push_back()– It pushes the elements into a vector from the back

  4. vector<int>::iterator ptr; // Declaring iterator to a vector

Input

input example:

3 // number of employees

1 3 // id=1, the first number means "time" and the other means "hourly rate"

3 3 // id=2, the first number means "time" and the other means "hourly rate"

2 3 // id=3, the first number means "time" and the other means "hourly rate"

Output

output example:

Before sorting
id:Salary
1:3   // 3=1*3(time*hourly_rate)
2:9   // 9=3*3(time*hourly_rate)
3:6   // 6=2*3(time*hourly_rate)
After sorting
id:Salary
2:9
3:6
1:3

Sample Input  Download

Sample Output  Download

Tags

12389



Discuss