#2
鱼游海底2015-10-29 15:32
|
程序代码:
main.cpp
#include <iostream>
#include <algorithm>
#include <functional>
#include <numeric>
#include "Worker.h"
using namespace std;
int main() {
Worker w[4] = {Worker("1405121101", "zhangsan", 18, male, 8000),
Worker("1405121102", "lisi", 19, male, 9000),
Worker("1405121103", "wange", 20, female,10000),
Worker("1405121104", "xiaoli", 21, female, 9999)};
cout << "before sort:\n";
for_each(w, w + 4, mem_fun_ref(&Worker::Print));
sort(w, w + 4, cmpWorkerBySalary);
cout << "after sort:\n";
for_each(w, w + 4, mem_fun_ref(&Worker::Print));
Worker *p = max_element(w, w + 4, cmpWorkerBySalary);
cout << "max_salary of :" << p -> GetName() << endl;
p = min_element(w, w + 4, cmpWorkerBySalary1);
cout << "min_salary of :" << p -> GetName() << endl;
cout << "average salary:" << accumulate(w, w + 4, double(0), AddSalary) / 4.0
<< endl;
if(Man_salary(w, 4) < Woman_salary(w, 4)) cout << "average :man < woman.\n";
if(Man_salary(w, 4) > Woman_salary(w, 4)) cout << "average :man < woman.\n";
if(Man_salary(w, 4) == Woman_salary(w, 4)) cout << "average :man < woman.\n";
}
#include <iostream>
#include <algorithm>
#include <functional>
#include <numeric>
#include "Worker.h"
using namespace std;
int main() {
Worker w[4] = {Worker("1405121101", "zhangsan", 18, male, 8000),
Worker("1405121102", "lisi", 19, male, 9000),
Worker("1405121103", "wange", 20, female,10000),
Worker("1405121104", "xiaoli", 21, female, 9999)};
cout << "before sort:\n";
for_each(w, w + 4, mem_fun_ref(&Worker::Print));
sort(w, w + 4, cmpWorkerBySalary);
cout << "after sort:\n";
for_each(w, w + 4, mem_fun_ref(&Worker::Print));
Worker *p = max_element(w, w + 4, cmpWorkerBySalary);
cout << "max_salary of :" << p -> GetName() << endl;
p = min_element(w, w + 4, cmpWorkerBySalary1);
cout << "min_salary of :" << p -> GetName() << endl;
cout << "average salary:" << accumulate(w, w + 4, double(0), AddSalary) / 4.0
<< endl;
if(Man_salary(w, 4) < Woman_salary(w, 4)) cout << "average :man < woman.\n";
if(Man_salary(w, 4) > Woman_salary(w, 4)) cout << "average :man < woman.\n";
if(Man_salary(w, 4) == Woman_salary(w, 4)) cout << "average :man < woman.\n";
}
程序代码:
Worker.h
#ifndef WORKER_CLASS
#define WORKER_CLASS
#include <iostream>
#include <string>
using namespace std;
enum Gender {female, male};
class Worker {
private:
string id;
string name;
int age;
Gender gender;
double salary;
public:
Worker(const char* sid = "", const char* sn = "", int na = 0,
Gender g = female, double sa = 0);
Worker(const Worker& w);
Worker& operator = (const Worker& rhs);
double GetSalary() const {return salary;}
Gender GetGender() const {return gender;}
string GetName() const {return name;}
void Print(ostream& os = cout);
};
bool cmpWorkerBySalary(const Worker& a, const Worker& b);
bool cmpWorkerBySalary1(const Worker& a, const Worker& b);
double AddSalary(double d, const Worker& s);
double Man_salary(Worker *p, int n);
double Woman_salary(Worker *p, int n);
#endif
#ifndef WORKER_CLASS
#define WORKER_CLASS
#include <iostream>
#include <string>
using namespace std;
enum Gender {female, male};
class Worker {
private:
string id;
string name;
int age;
Gender gender;
double salary;
public:
Worker(const char* sid = "", const char* sn = "", int na = 0,
Gender g = female, double sa = 0);
Worker(const Worker& w);
Worker& operator = (const Worker& rhs);
double GetSalary() const {return salary;}
Gender GetGender() const {return gender;}
string GetName() const {return name;}
void Print(ostream& os = cout);
};
bool cmpWorkerBySalary(const Worker& a, const Worker& b);
bool cmpWorkerBySalary1(const Worker& a, const Worker& b);
double AddSalary(double d, const Worker& s);
double Man_salary(Worker *p, int n);
double Woman_salary(Worker *p, int n);
#endif
程序代码:
Worker.cpp
#include <iostream>
#include <numeric>
#include "Worker.h"
bool cmpWorkerBySalary(const Worker& a, const Worker& b) {
return a.GetSalary() > b.GetSalary();
}
bool cmpWorkerBySalary1(const Worker& a, const Worker& b) {
return a.GetSalary() < b.GetSalary();
}
double AddSalary(double d, const Worker& s) {
return d + s.GetSalary();
}
double Man_salary(Worker *p, int n) {
int m = 0;
for(int i = 0; i < n; ++i) {
if((p + i) -> GetGender() == male) ++m;
}
return accumulate(p, p + m, double(0), AddSalary) / m;
}
double Woman_salary(Worker *p, int n) {
int m = 0;
for(int i = 0; i < n; ++i) {
if((p + i) -> GetGender() == female) ++m;
}
return accumulate(p, p + m, double(0), AddSalary) / m;
}
Worker::Worker(const char* sid, const char* sn, int na,
Gender g, double sa):id(sid), name(sn), age(na),
gender(g), salary(sa) {}
Worker::Worker(const Worker& w):id(w.id), name(w.name), age(w.age),
gender(w.gender), salary(w.salary) {}
Worker& Worker::operator = (const Worker& rhs) {
id = rhs.id;
name = rhs.name;
age = rhs.age;
gender = rhs.gender;
salary = rhs.salary;
return *this;
}
void Worker::Print(ostream& os) {
os << id << " " << name << " " << age << " "
<< gender << " " << salary << endl;
}
#include <iostream>
#include <numeric>
#include "Worker.h"
bool cmpWorkerBySalary(const Worker& a, const Worker& b) {
return a.GetSalary() > b.GetSalary();
}
bool cmpWorkerBySalary1(const Worker& a, const Worker& b) {
return a.GetSalary() < b.GetSalary();
}
double AddSalary(double d, const Worker& s) {
return d + s.GetSalary();
}
double Man_salary(Worker *p, int n) {
int m = 0;
for(int i = 0; i < n; ++i) {
if((p + i) -> GetGender() == male) ++m;
}
return accumulate(p, p + m, double(0), AddSalary) / m;
}
double Woman_salary(Worker *p, int n) {
int m = 0;
for(int i = 0; i < n; ++i) {
if((p + i) -> GetGender() == female) ++m;
}
return accumulate(p, p + m, double(0), AddSalary) / m;
}
Worker::Worker(const char* sid, const char* sn, int na,
Gender g, double sa):id(sid), name(sn), age(na),
gender(g), salary(sa) {}
Worker::Worker(const Worker& w):id(w.id), name(w.name), age(w.age),
gender(w.gender), salary(w.salary) {}
Worker& Worker::operator = (const Worker& rhs) {
id = rhs.id;
name = rhs.name;
age = rhs.age;
gender = rhs.gender;
salary = rhs.salary;
return *this;
}
void Worker::Print(ostream& os) {
os << id << " " << name << " " << age << " "
<< gender << " " << salary << endl;
}