Mail.ruПочтаМой МирОдноклассникиВКонтактеИгрыЗнакомстваНовостиКалендарьОблакоЗаметкиВсе проекты

Задание C++ Помогите дописать сортировку

Flame SV Ученик (193), закрыт 6 месяцев назад
Ниже приведен код, нужно дописать сортировку по стажу, помогите пж

#include <iostream>
#include <string>
#include <algorithm>

using namespace std;

struct Sotrudnik
{
string Familia;
string Imya;
int opit;
int Vozrast;
char pol;
};

int main()
{
setlocale(0, "rus");
const int N = 8;
Sotrudnik Mass[N];
char letter;
int count = 0;

// Ввод данных с клавиатуры
for (int i = 0; i < N; i++)
{
cout << "Введите данные сотрудника #" << i+1 << ":" << endl;
cout << "Фамилия: ";
cin >> Mass[i].Familia;
cout << "Имя: ";
cin >> Mass[i].Imya;
cout << "Стаж работы: ";
cin >> Mass[i].opit;
cout << "Возраст: ";
cin >> Mass[i].Vozrast;
cout << "Пол (М/Ж): ";
cin >> Mass[i].pol;
}
// Вывод количества сотрудников
if (count > 0)
{
cout << "Количество сотрудников, фамилия которых начинается на букву '" << letter
<< "': " << count << endl;
}
else
{
cout << "Сотрудников с такой фамилией нет." << endl;
}

// Вывод данных и поиск сотрудников по первой букве фамилии
cout << endl << "Список сотрудников:" << endl;
for (int i = 0; i < N; i++)
{
cout << Mass[i].Familia << " " << Mass[i].Imya << ", стаж: " << Mass[i].opit
<< ", возраст: " << Mass[i].Vozrast << ", пол: " << Mass[i].pol << endl;
if (i == 0)
{
cout << "Введите букву для поиска сотрудников: ";
cin >> letter;
}
if (Mass[i].Familia[0] == letter)
{
count++;
}
}
system("pause");
return 0;
}
Лучший ответ
Емельян Пугачёв Мудрец (13930) 11 месяцев назад
// если правильно понял, чо ты хош:

 #include  
#include
#include

using namespace std;

struct Sotrudnik
{
string Familia;
string Imya;
int opit;
int Vozrast;
char pol;
};

int main()
{
setlocale(0, "rus");
const int N = 8;
Sotrudnik Mass[N];
char letter;
int count = 0;

// Ввод данных с клавиатуры
for (int i = 0; i < N; i++)
{
cout << "Введите данные сотрудника #" << i+1 << ":" << endl;
cout << "Фамилия: ";
cin >> Mass[i].Familia;
cout << "Имя: ";
cin >> Mass[i].Imya;
cout << "Стаж работы: ";
cin >> Mass[i].opit;
cout << "Возраст: ";
cin >> Mass[i].Vozrast;
cout << "Пол (М/Ж): ";
cin >> Mass[i].pol;
}
// ****

// Вывод данных и поиск сотрудников по первой букве фамилии
cout << endl << "Список сотрудников:" << endl;
for (int i = 0; i < N; i++)
{
cout << Mass[i].Familia << " " << Mass[i].Imya << ", стаж: " << Mass[i].opit
<< ", возраст: " << Mass[i].Vozrast << ", пол: " << Mass[i].pol << endl;
if (i == 0)
{
cout << "Введите букву для поиска сотрудников: ";
cin >> letter;
}
if (Mass[i].Familia[0] == letter)
{
count++;
}
}

// ****

// Вывод количества сотрудников
if (count > 0)
{
cout << "Количество сотрудников, фамилия которых начинается на букву '" << letter
<< "': " << count << endl;
}
else
{
cout << "Сотрудников с такой фамилией нет." << endl;
}

// #### Сортировка по стажу
Sotrudnik tmp;

for (int i = 0; i < N; i++)
{
for(int j = i + 1; j < N; j++)
{
if(Mass[i].opit < Mass[j].opit) // по возрастанию.
{
tmp.Familia = Mass[i].Familia;
tmp.Imya = Mass[i].Imya;
tmp.opit = Mass[i].opit;
tmp.Vozrast = Mass[i].Vozrast;
tmp.pol = Mass[i].pol;

Mass[i].Familia = Mass[j].Familia;
Mass[i].Imya = Mass[j].Imya;
Mass[i].opit = Mass[j].opit;
Mass[i].Vozrast = Mass[j].Vozrast;
Mass[i].pol = Mass[j].pol;

Mass[j].Familia = tmp.Familia;
Mass[j].Imya = tmp.Imya;
Mass[j].opit = tmp.opit;
Mass[j].Vozrast = tmp.Vozrast;
Mass[j].pol = tmp.pol;
}
}
}

// @@@@@@ снова на консоль

cout << endl << endl << "Список сотрудников (после сортировки):" << endl;
for (int i = 0; i < N; i++)
{
cout << Mass[i].Familia << " " << Mass[i].Imya << ", стаж: " << Mass[i].opit
<< ", возраст: " << Mass[i].Vozrast << ", пол: " << Mass[i].pol << endl;
}


system("pause");
return 0;
}
Остальные ответы
Evgeny Мастер (1448) 11 месяцев назад
 #include   
#include
#include
#include
#include
#include
#include
#include
using namespace std;
struct Employee
{
private:
string name_;
string surname_;
int years_of_experience_;
int age_;
char sex_;
public:
Employee(const string& n,const string& sn, int yoe, int a,char s):
name_(n),
surname_(sn),
years_of_experience_(yoe),
age_(a),
sex_(toupper(s))
{
transform(name_.begin(), name_.end(), name_.begin(),
[](unsigned char c){ return toupper(c); }
);
transform(surname_.begin(), surname_.end(), surname_.begin(),
[](unsigned char c){ return toupper(c); }
);

}

// comparison of records by age of experience.
bool operator<(const Employee& rhs) const{
return years_of_experience_ < rhs.years_of_experience_;
}

bool surname_starts_from(char c) const {
return surname_[0]==toupper(c);
}
friend ostream& operator<<(ostream& os, const Employee& e){
return os
< <<"\t"< << ",\tстаж:" << setw(2)< << ",\tвозраст:"<< setw(2) << e.age_
<< ",\tпол:"<< string(1,e.sex_)
<< endl;
}
};

typedef vector DB;

Employee get_records_from_stream(int i,istream& is=cin){
string name;
string surname;
int experience;
int age;
char sex;
cout << "Введите данные сотрудника #" << i << ":" < cout << "Фамилия: ";
is >> surname;
cout << "Имя: ";
is >> name;
cout << "Стаж работы: ";
is >> experience;
cout << "Возраст: ";
is >> age;
cout << "Пол (М/Ж): ";
is >> sex;
return Employee(name,surname,experience,age,sex);
}
ostream& operator<< (ostream& os,const DB db){
os << "Список сотрудников:" << endl;
for(auto e: db){
os << e;
}
return os;
}
void print_records_starting_from(const DB db,char c){
bool is_found = false;
copy_if(db.begin(), db.end(),
ostream_iterator(cout),
[c,&is_found](const Employee& e) {
bool b = e.surname_starts_from(c);
if(b) is_found = true;
return b;});
if(!is_found)
cout << "Сотрудников с такой фамилией нет.";
}
int main(){
const int N = 3;
DB records;
for (int i = 0; i < N; i++){
records.emplace_back(get_records_from_stream(i,cin));
}
sort(records.begin(),records.end());
cout << records; char letter = 'j';
cout << "Введите букву для поиска сотрудников: ";
cin >> letter;
print_records_starting_from(records,letter);
system("pause");
return 0;
}
Похожие вопросы