Rimack Zelnick - Challenge 4 - C++

Ir em baixo

Rimack Zelnick - Challenge 4 - C++

Mensagem  rmzelnick em Dom Out 02, 2011 12:47 am

1-) Escreva um programa de cadastramento de funcionários da empresa do SR.Wolts. Neste programa, haverá campos para preencher com nome, idade, estado civil , se sofre algum tipo de deficiência física, sexo, rg , cpf , número da carteira de trabalho, se é portador de alguma doença, experiência , contrato.

É importante ressaltar que o programa precisa salvar os dados no HD. Pois, se forem salvos na memória e o computador precisar reiniciar, todos dados serão apagados. O programa mais criativo , terá a maior pontuação.

MaritalStatus.h

 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
#ifndef __MARITALSTATUS_H__
#define __MARITALSTATUS_H__

namespace challenge{
class MaritalStatus{
public:
MaritalStatus();
MaritalStatus(const MaritalStatus&);
MaritalStatus& operator=(const MaritalStatus&);
~MaritalStatus();

bool operator==(const MaritalStatus&) const;
bool operator!=(const MaritalStatus&) const;

friend std::ostream& operator<<(
std::ostream&,
const MaritalStatus&
);

friend std::istream& operator>>(
std::istream&,
MaritalStatus&
);

friend std::ofstream& operator<<(
std::ofstream&,
const MaritalStatus&
);

friend std::ifstream& operator>>(
std::ifstream&,
MaritalStatus&
);

static MaritalStatus Single();
static MaritalStatus Married();
static MaritalStatus Divorced();
static MaritalStatus Widowed();
static MaritalStatus Cohabiting();
static MaritalStatus CivilUnion();
static MaritalStatus DomesticPartnership();
static MaritalStatus UnmarriedPartners();
private:
explicit MaritalStatus(int);
int mstatus;
};
}

#endif


MaritalStatus.cpp

  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
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
#include <iostream>
#include <fstream>

#include <maritalstatus.h>

#include <cstring>

challenge::MaritalStatus::MaritalStatus(){
#ifdef DEBUG
std::cout << "Constructing a type \'challenge::MaritalStatus\' object\n";
#endif
}

challenge::MaritalStatus::MaritalStatus(const challenge::MaritalStatus& rhs){
#ifdef DEBUG
std::cout << "Copy constructing a type \'challenge::MaritalStatus\' object\n";
#endif
mstatus = rhs.mstatus;
}

challenge::MaritalStatus& challenge::MaritalStatus::operator=(
const challenge::MaritalStatus& rhs){
#ifdef DEBUG
std::cout << "Assigning a type \'challenge::MaritalStatus\' object\n";
#endif
mstatus = rhs.mstatus;

return *this;
}

challenge::MaritalStatus::~MaritalStatus(){
#ifdef DEBUG
std::cout << "Destroying a type \'challenge::MaritalStatus\' object\n";
#endif
}

bool challenge::MaritalStatus::operator==(
const challenge::MaritalStatus& rhs) const{
#ifdef DEBUG
std::cout << "Comparing a type \'challenge::MaritalStatus\' object\n";
#endif
return (mstatus == rhs.mstatus);
}

bool challenge::MaritalStatus::operator!=(
const challenge::MaritalStatus& rhs) const{
#ifdef DEBUG
std::cout << "Comparing a type \'challenge::MaritalStatus\' object\n";
#endif
return (mstatus != rhs.mstatus);
}

std::ostream& challenge::operator<<(
std::ostream& stream,
const challenge::MaritalStatus& rhs
){
if (rhs == challenge::MaritalStatus::Single())
stream << "Single";
else if (rhs == challenge::MaritalStatus::Married())
stream << "Married";
else if (rhs == challenge::MaritalStatus::Divorced())
stream << "Divorced";
else if (rhs == challenge::MaritalStatus::Widowed())
stream << "Widowed";
else if (rhs == challenge::MaritalStatus::Cohabiting())
stream << "Cohabiting";
else if (rhs == challenge::MaritalStatus::CivilUnion())
stream << "Civil Union";
else if (rhs == challenge::MaritalStatus::DomesticPartnership())
stream << "Domestic Partnership";
else if (rhs == challenge::MaritalStatus::UnmarriedPartners())
stream << "Unmarried Partners";

return stream;
}

std::istream& challenge::operator>>(
std::istream& stream,
challenge::MaritalStatus& rhs
){
char line[50];

stream.getline(line, 50);

if (!std::strcmp(line, "Single"))
rhs = challenge::MaritalStatus::Single();
else if (!std::strcmp(line, "Married"))
rhs = challenge::MaritalStatus::Married();
else if (!std::strcmp(line, "Divorced"))
rhs = challenge::MaritalStatus::Divorced();
else if (!std::strcmp(line, "Widowed"))
rhs = challenge::MaritalStatus::Widowed();
else if (!std::strcmp(line, "Cohabiting"))
rhs = challenge::MaritalStatus::Cohabiting();
else if (!std::strcmp(line, "Civil Union"))
rhs = challenge::MaritalStatus::CivilUnion();
else if (!std::strcmp(line, "Domestic Partnership"))
rhs = challenge::MaritalStatus::DomesticPartnership();
else if (!std::strcmp(line, "Unmarried Partners"))
rhs = challenge::MaritalStatus::UnmarriedPartners();

return stream;
}

std::ofstream& challenge::operator<<(
std::ofstream& stream,
const challenge::MaritalStatus& rhs
){
stream.write((const char*)&rhs.mstatus, sizeof(int));

return stream;
}

std::ifstream& challenge::operator>>(
std::ifstream& stream,
challenge::MaritalStatus& rhs
){
stream.read((char*)&rhs.mstatus, sizeof(int));

return stream;
}

challenge::MaritalStatus challenge::MaritalStatus::Single(){
return challenge::MaritalStatus(0);
}

challenge::MaritalStatus challenge::MaritalStatus::Married(){
return challenge::MaritalStatus(1);
}

challenge::MaritalStatus challenge::MaritalStatus::Divorced(){
return challenge::MaritalStatus(2);
}

challenge::MaritalStatus challenge::MaritalStatus::Widowed(){
return challenge::MaritalStatus(3);
}

challenge::MaritalStatus challenge::MaritalStatus::Cohabiting(){
return challenge::MaritalStatus(4);
}

challenge::MaritalStatus challenge::MaritalStatus::CivilUnion(){
return challenge::MaritalStatus(5);
}

challenge::MaritalStatus challenge::MaritalStatus::DomesticPartnership(){
return challenge::MaritalStatus(6);
}

challenge::MaritalStatus challenge::MaritalStatus::UnmarriedPartners(){
return challenge:: MaritalStatus(7);
}

challenge::MaritalStatus::MaritalStatus(int _m) : mstatus(_m){
#ifdef DEBUG
std::cout << "Constructing a type \'challenge::MaritalStatus\' object\n";
#endif
}


Última edição por Rimack Zelnick em Seg Out 03, 2011 11:02 pm, editado 2 vez(es)
avatar
rmzelnick

Mensagens : 39
Data de inscrição : 02/07/2011
Idade : 25
Localização : Huntington, NY

Ver perfil do usuário http://www.markzelnick.me/

Voltar ao Topo Ir em baixo

Re: Rimack Zelnick - Challenge 4 - C++

Mensagem  rmzelnick em Dom Out 02, 2011 12:49 am

Rg.h

 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
#ifndef __RG_H__
#define __RG_H__

namespace challenge{
class Rg{
private:
char uf[3];
int n[3];

public:
Rg();
Rg(char[2], int, int, int);
Rg(const Rg&);
Rg& operator=(const Rg&);
~Rg();

bool operator==(const Rg&) const;
bool operator!=(const Rg&) const;

friend std::ostream& operator<<(std::ostream&, const Rg&);
friend std::istream& operator>>(std::istream&, Rg&);

friend std::ofstream& operator<<(std::ofstream&, const Rg&);
friend std::ifstream& operator>>(std::ifstream&, Rg&);
};
}

#endif


Rg.cpp

 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
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
#include <iostream>
#include <fstream>
#include <rg.h>

#include <cstdio>
#include <cstring>

challenge::Rg::Rg(){
#ifdef DEBUG
std::cout << "Constructing a type \'challenge::Rg\' object\n";
#endif
}

challenge::Rg::Rg(char _uf[2], int n1, int n2, int n3){
#ifdef DEBUG
std::cout << "Constructing a type \'challenge::Rg\' object\n";
#endif
n[0] = n1;
n[1] = n2;
n[2] = n3;
std::strncpy(uf, _uf, 2);
uf[2] = '\0';
}

challenge::Rg::Rg(const challenge::Rg& rhs){
#ifdef DEBUG
std::cout << "Copy constructing a type \'challenge::Rg\' object\n";
#endif
std::strncpy(uf, rhs.uf, 2);
uf[2] = '\0';
std::memcpy(n, rhs.n, 3 * sizeof(int));
}

challenge::Rg& challenge::Rg::operator=(const challenge::Rg& rhs){
#ifdef DEBUG
std::cout << "Assigning a type \'challenge::Rg\' object\n";
#endif
std::strncpy(uf, rhs.uf, 2);
uf[2] = '\0';
std::memcpy(n, rhs.n, 3 * sizeof(int));

return *this;
}

challenge::Rg::~Rg(){
#ifdef DEBUG
std::cout << "Destroying a type \'challenge::Rg\' object\n";
#endif
}

bool challenge::Rg::operator==(const challenge::Rg& rhs) const{
return (std::strncmp(rhs.uf, uf, 2) == 0 && std::memcmp(rhs.n, n, 3 * sizeof(int)) == 0);
}

bool challenge::Rg::operator!=(const challenge::Rg& rhs) const{
return (std::strncmp(rhs.uf, uf, 2) != 0 && std::memcmp(rhs.n, n, 3 * sizeof(int)) != 0);
}

std::ostream& challenge::operator<<(
std::ostream& stream,
const challenge::Rg& rhs
){
stream << rhs.uf << '-' << rhs.n[0] << '.' << rhs.n[1] << '.' << rhs.n[2];

return stream;
}

std::istream& challenge::operator>>(
std::istream& stream,
challenge::Rg& rhs
){
char line[50];

stream.getline(line, 50);
std::sscanf(line, "%2s-%d.%d.%d", rhs.uf, &rhs.n[0], &rhs.n[1], &rhs.n[2]);

return stream;
}

std::ofstream& challenge::operator<<(
std::ofstream& stream,
const challenge::Rg& rhs
){
stream.write((const char*)rhs.uf, 2);
stream.write((const char*)rhs.n, 3 * sizeof(int));

return stream;
}

std::ifstream& challenge::operator>>(
std::ifstream& stream,
challenge::Rg& rhs
){
stream.read((char*)rhs.uf, 2);
rhs.uf[2] = '\0';
stream.read((char*)rhs.n, 3 * sizeof(int));

return stream;
}


Última edição por Rimack Zelnick em Seg Out 03, 2011 11:04 pm, editado 2 vez(es)
avatar
rmzelnick

Mensagens : 39
Data de inscrição : 02/07/2011
Idade : 25
Localização : Huntington, NY

Ver perfil do usuário http://www.markzelnick.me/

Voltar ao Topo Ir em baixo

Re: Rimack Zelnick - Challenge 4 - C++

Mensagem  rmzelnick em Dom Out 02, 2011 12:50 am

Cpf.h

 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
#ifndef __CPF_H__
#define __CPF_H__

namespace challenge{
class InvalidCpf : public std::exception{
public:
const char * what() const throw();
};

class Cpf{
private:
int n[4];
bool evaluate();

public:
Cpf();
Cpf(int, int, int, int) throw (InvalidCpf);
Cpf(const Cpf&);
Cpf& operator=(const Cpf&);
~Cpf();

bool operator==(const Cpf&) const;
bool operator!=(const Cpf&) const;

friend std::ostream& operator<<(std::ostream&, const Cpf&);
friend std::istream& operator>>(std::istream&, Cpf&) throw (InvalidCpf);

friend std::ofstream& operator<<(std::ofstream&, const Cpf&);
friend std::ifstream& operator>>(std::ifstream&, Cpf&);
};
}

#endif


Cpf.cpp

  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
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
#include <iostream>
#include <fstream>
#include <exception>

#include <cpf.h>

#include <cstdio>
#include <cstring>

const char * challenge::InvalidCpf::what() const throw(){
return "Invalid CPF number";
}

challenge::Cpf::Cpf(){
#ifdef DEBUG
std::cout << "Constructing a type \'Challenge::Cpf\' object\n";
#endif
}

challenge::Cpf::Cpf(int n1, int n2, int n3, int n4)
throw (challenge::InvalidCpf){
#ifdef DEBUG
std::cout << "Constructing a type \'Challenge::Cpf\' object\n";
#endif

n[0] = n1;
n[1] = n2;
n[2] = n3;
n[3] = n4;

if (!evaluate())
throw challenge::InvalidCpf();
}

challenge::Cpf::Cpf(const challenge::Cpf& rhs){
#ifdef DEBUG
std::cout << "Copy constructing a type \'Challenge::Cpf\' object\n";
#endif
std::memcpy(n, rhs.n, 4 * sizeof(int));
}

challenge::Cpf& challenge::Cpf::operator=(const challenge::Cpf& rhs){
#ifdef DEBUG
std::cout << "Assigning a type \'Challenge::Cpf\' object\n";
#endif
std::memcpy(n, rhs.n, 4 * sizeof(int));

return *this;
}

challenge::Cpf::~Cpf(){
#ifdef DEBUG
std::cout << "Destroying a type \'Challenge::Cpf\' object\n";
#endif
}

bool challenge::Cpf::operator==(const challenge::Cpf& rhs) const{
#ifdef DEBUG
std::cout << "Comparing a type \'Challenge::Cpf\' object\n";
#endif
return (std::memcmp(n, rhs.n, 4 * sizeof(int)) == 0);
}

bool challenge::Cpf::operator!=(const challenge::Cpf& rhs) const{
#ifdef DEBUG
std::cout << "Comparing a type \'Challenge::Cpf\' object\n";
#endif
return (std::memcmp(n, rhs.n, 4 * sizeof(int)) != 0);
}

std::ostream& challenge::operator<<(
std::ostream& stream,
const challenge::Cpf& rhs
){
stream << rhs.n[0] << '.' << rhs.n[1] << '.' << rhs.n[2] << '-' << rhs.n[3];

return stream;
}

std::istream& challenge::operator>>(
std::istream& stream,
challenge::Cpf& rhs
) throw (challenge::InvalidCpf){
char line[50];

stream.getline(line, 50);
std::sscanf(line, "%d.%d.%d-%d", &rhs.n[0], &rhs.n[1], &rhs.n[2], &rhs.n[3]);

if (!rhs.evaluate())
throw challenge::InvalidCpf();

return stream;
}
std::ofstream& challenge::operator<<(
std::ofstream& stream,
const challenge::Cpf& rhs
){
stream.write((const char*)rhs.n, 4 * sizeof(int));

return stream;
}

std::ifstream& challenge::operator>>(
std::ifstream& stream,
challenge::Cpf& rhs
){
stream.read((char*)rhs.n, 4 * sizeof(int));

return stream;
}

bool challenge::Cpf::evaluate(){
int d1, d2;

d1 = ((n[0] / 100) * 10) +
(((n[0] % 100) / 10) * 9) +
(((n[0] % 100) % 10) * 8) +
((n[1] / 100) * 7) +
(((n[1] % 100) / 10) * 6) +
(((n[1] % 100) % 10) * 5) +
((n[2] / 100) * 4) +
(((n[2] % 100) / 10) * 3) +
(((n[2] % 100) % 10) * 2);

d1 = 11 - (d1 % 11);
d1 = (d1 >= 10) ? 0 : d1;

d2 = ((n[0] / 100) * 11) +
(((n[0] % 100) / 10) * 10) +
(((n[0] % 100) % 10) * 9) +
((n[1] / 100) * 8) +
(((n[1] % 100) / 10) * 7) +
(((n[1] % 100) % 10) * 6) +
((n[2] / 100) * 5) +
(((n[2] % 100) / 10) * 4) +
(((n[2] % 100) % 10) * 3);
d2 += d1 * 2;
d2 = 11 - (d2 % 11);
d2 = (d2 >= 10) ? 0 : d2;

return (d1 == (n[3] / 10) && d2 == (n[3] % 10));
}


Última edição por Rimack Zelnick em Seg Out 03, 2011 11:08 pm, editado 1 vez(es)
avatar
rmzelnick

Mensagens : 39
Data de inscrição : 02/07/2011
Idade : 25
Localização : Huntington, NY

Ver perfil do usuário http://www.markzelnick.me/

Voltar ao Topo Ir em baixo

Re: Rimack Zelnick - Challenge 4 - C++

Mensagem  rmzelnick em Dom Out 02, 2011 12:51 am

Gender.h

 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
#ifndef __GENDER_H__
#define __GENDER_H__

namespace challenge{
class Gender{
public:
Gender();
Gender(const Gender&);
Gender& operator=(const Gender&);
~Gender();

bool operator==(const Gender&) const;
bool operator!=(const Gender&) const;

static Gender Male();
static Gender Female();

friend std::ostream& operator<<(std::ostream&, const Gender&);
friend std::istream& operator>>(std::istream&, Gender&);

friend std::ofstream& operator<<(std::ofstream&, const Gender&);
friend std::ifstream& operator>>(std::ifstream&, Gender&);
private:
explicit Gender(int);
int gender;
};
}

#endif


Gender.cpp

  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
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
#include <iostream>
#include <fstream>

#include <gender.h>

#include <cstring>

challenge::Gender::Gender(){
#ifdef DEBUG
std::cout << "Constructing a type \'challenge::Gender\' object\n";
#endif
}

challenge::Gender::Gender(const challenge::Gender& rhs){
#ifdef DEBUG
std::cout << "Copy constructing a type \'challenge::Gender\' object\n";
#endif
gender = rhs.gender;
}

challenge::Gender& challenge::Gender::operator=(const challenge::Gender& rhs){
#ifdef DEBUG
std::cout << "Assigning a type \'challenge::Gender\' object\n";
#endif
gender = rhs.gender;

return *this;
}

challenge::Gender::~Gender(){
#ifdef DEBUG
std::cout << "Destroying a type \'challenge::Gender\' object\n";
#endif
}

bool challenge::Gender::operator==(const challenge::Gender& rhs) const{
#ifdef DEBUG
std::cout << "Comparing a type \'challenge::Gender\' object\n";
#endif
return (gender == rhs.gender);
}

bool challenge::Gender::operator!=(const challenge::Gender& rhs) const{
#ifdef DEBUG
std::cout << "Comparing a type \'challenge::Gender\' object\n";
#endif
return (gender != rhs.gender);
}

challenge::Gender challenge::Gender::Male(){
return challenge::Gender(0);
}

challenge::Gender challenge::Gender::Female(){
return challenge::Gender(1);
}

challenge::Gender::Gender(int _g) : gender(_g){
#ifdef DEBUG
std::cout << "Constructing a type \'challenge::Gender\' object\n";
#endif
}

std::ostream& challenge::operator<<(
std::ostream& stream,
const challenge::Gender& rhs
){
if (rhs == challenge::Gender::Male())
stream << "Male";
else if (rhs == challenge::Gender::Female())
stream << "Female";

return stream;
}

std::istream& challenge::operator>>(
std::istream& stream,
challenge::Gender& rhs
){
char line[50];

stream.getline(line, 50);

if (!std::strcmp(line, "Male"))
rhs = challenge::Gender::Male();
else if (!std::strcmp(line, "Female"))
rhs = challenge::Gender::Female();

return stream;
}

std::ofstream& challenge::operator<<(
std::ofstream& stream,
const challenge::Gender& rhs
){
stream.write((const char*)&rhs.gender, sizeof(int));

return stream;
}

std::ifstream& challenge::operator>>(
std::ifstream& stream,
challenge::Gender& rhs
){
stream.read((char*)&rhs.gender, sizeof(int));

return stream;
}


Última edição por Rimack Zelnick em Seg Out 03, 2011 11:10 pm, editado 1 vez(es)
avatar
rmzelnick

Mensagens : 39
Data de inscrição : 02/07/2011
Idade : 25
Localização : Huntington, NY

Ver perfil do usuário http://www.markzelnick.me/

Voltar ao Topo Ir em baixo

Re: Rimack Zelnick - Challenge 4 - C++

Mensagem  rmzelnick em Dom Out 02, 2011 12:52 am

Employee.h

 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
81
82
83
84
85
86
87
88
89
90
91
92
93
94
#ifndef __EMPLOYEE_H__
#define __EMPLOYEE_H__

namespace challenge{
class Employee{
private:
std::string name, experience, contract;
unsigned char age;
MaritalStatus mstatus;
std::set<std::string> disability;
Gender gender;
Rg rg;
Cpf cpf;
int work;
std::set<std::string> disease;
public:
Employee();
Employee(
const std::string&,
unsigned char,
const MaritalStatus&,
std::set<std::string>,
const Gender&,
const Rg&,
const Cpf&,
int,
std::set<std::string>,
const std::string&,
const std::string&
);
Employee(const Employee&);
Employee& operator=(const Employee&);
~Employee();

bool operator==(const Employee&) const;
bool operator!=(const Employee&) const;
bool operator<(const Employee&) const;
bool operator>(const Employee&) const;

void setName(const std::string&);
void setAge(unsigned char);
void setMaritalStatus(const MaritalStatus&);
void setDisabilities(const std::set<std::string>&);
void setGender(const Gender&);
void setRg(const Rg&);
void setCpf(const Cpf&);
void setWork(int);
void setDiseases(const std::set<std::string>&);
void setExperience(const std::string&);
void setContract(const std::string&);

std::string getName() const;
std::string getExperience() const;
std::string getContract() const;

unsigned char getAge() const;

MaritalStatus getMaritalStatus() const;

std::set<std::string> getDisabilities() const;
std::set<std::string> getDiseases() const;

Gender getGender() const;

Rg getRg() const;

Cpf getCpf() const;

int getWork() const;

friend std::ostream& operator<<(
std::ostream&,
const Employee&
);

friend std::ofstream& operator<<(
std::ofstream&,
const Employee&
);

friend std::ifstream& operator>>(
std::ifstream&,
Employee&
);
};

std::ifstream& getline(
std::ifstream&,
std::string&,
size_t
) throw (std::bad_alloc);
}

#endif


Employee.cpp

  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
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
#include <iostream>
#include <fstream>
#include <exception>
#include <string>
#include <set>

#include <maritalstatus.h>
#include <gender.h>
#include <rg.h>
#include <cpf.h>

#include <employee.h>

#include <cstdlib>

challenge::Employee::Employee(){
#ifdef DEBUG
std::cout << "Constructing a type \'challenge::Employee\' object\n";
#endif
}

challenge::Employee::Employee(
const std::string& _name,
unsigned char _age,
const challenge::MaritalStatus& _mstatus,
std::set<std::string> _disability,
const challenge::Gender& _gender,
const challenge::Rg& _rg,
const challenge::Cpf& _cpf,
int _work,
std::set<std::string> _disease,
const std::string& _experience,
const std::string& _contract
) : name(_name),
age(_age), mstatus(_mstatus), disability(_disability), gender(_gender), rg(_rg), cpf(_cpf), work(_work),
disease(_disease), experience(_experience), contract(_contract){
#ifdef DEBUG
std::cout << "Constructing a type \'challenge::Employee\' object\n";
#endif
}

challenge::Employee::Employee(const challenge::Employee& rhs){
setName(rhs.name);
setAge(rhs.age);
setMaritalStatus(rhs.mstatus);
setDisabilities(rhs.disability);
setGender(rhs.gender);
setRg(rhs.rg);
setCpf(rhs.cpf);
setWork(rhs.work);
setDiseases(rhs.disease);
setExperience(rhs.experience);
setContract(rhs.contract);
#ifdef DEBUG
std::cout << "Copy constructing a type \'challenge::Employee\' object\n";
#endif
}

challenge::Employee& challenge::Employee::operator=(const challenge::Employee& rhs){
#ifdef DEBUG
std::cout << "Assigning constructing a type \'challenge::Employee\' object\n";
#endif
setName(rhs.name);
setAge(rhs.age);
setMaritalStatus(rhs.mstatus);
setDisabilities(rhs.disability);
setGender(rhs.gender);
setRg(rhs.rg);
setCpf(rhs.cpf);
setWork(rhs.work);
setDiseases(rhs.disease);
setExperience(rhs.experience);
setContract(rhs.contract);

return *this;
}

challenge::Employee::~Employee(){
#ifdef DEBUG
std::cout << "Destroying a type \'challenge::Employee\' object\n";
#endif
}

bool challenge::Employee::operator==(const challenge::Employee& rhs) const{
#ifdef DEBUG
std::cout << "Comparing a type \'challenge::Employee\' object\n";
#endif

return (
rg == rhs.rg &&
cpf == rhs.cpf &&
work == rhs.work
);
}

bool challenge::Employee::operator!=(const challenge::Employee& rhs) const{
#ifdef DEBUG
std::cout << "Comparing a type \'challenge::Employee\' object\n";
#endif

return (
rg != rhs.rg &&
cpf != rhs.cpf &&
work != rhs.work
);
}

bool challenge::Employee::operator<(const challenge::Employee& rhs) const{
#ifdef DEBUG
std::cout << "Comparing a type \'challenge::Employee\' object\n";
#endif
return (name < rhs.name);
}

bool challenge::Employee::operator>(const challenge::Employee& rhs) const{
#ifdef DEBUG
std::cout << "Comparing a type \'challenge::Employee\' object\n";
#endif
return (name > rhs.name);
}

void challenge::Employee::setName(const std::string& _name){
name = _name;
}

void challenge::Employee::setAge(unsigned char _age){
age = _age;
}

void challenge::Employee::setMaritalStatus(const challenge::MaritalStatus& _mstatus){
mstatus = _mstatus;
}

void challenge::Employee::setDisabilities(const std::set<std::string>& _disability){
disability = _disability;
}

void challenge::Employee::setGender(const challenge::Gender& _gender){
gender = _gender;
}

void challenge::Employee::setRg(const challenge::Rg& _rg){
rg = _rg;
}

void challenge::Employee::setCpf(const challenge::Cpf& _cpf){
cpf = _cpf;
}

void challenge::Employee::setWork(int _work){
work = _work;
}

void challenge::Employee::setDiseases(const std::set<std::string>& _disease){
disease = _disease;
}

void challenge::Employee::setExperience(const std::string& _experience){
experience = _experience;
}

void challenge::Employee::setContract(const std::string& _contract){
contract = _contract;
}

std::string challenge::Employee::getName() const{
return name;
}

std::string challenge::Employee::getExperience() const{
return experience;
}

std::string challenge::Employee::getContract() const{
return contract;
}

unsigned char challenge::Employee::getAge() const{
return age;
}

challenge::MaritalStatus challenge::Employee::getMaritalStatus() const{
return mstatus;
}

std::set<std::string> challenge::Employee::getDisabilities() const{
return disability;
}

std::set<std::string> challenge::Employee::getDiseases() const{
return disease;
}

challenge::Gender challenge::Employee::getGender() const{
return gender;
}

challenge::Rg challenge::Employee::getRg() const{
return rg;
}

challenge::Cpf challenge::Employee::getCpf() const{
return cpf;
}

int challenge::Employee::getWork() const{
return work;
}

std::ostream& challenge::operator<<(
std::ostream& stream,
const challenge::Employee& rhs
){
stream << "Name: " << rhs.name << "\nAge: " << int(rhs.age) << "\nMarital Status: " << rhs.mstatus
<< "\nDisability(ies):\n";

if (!rhs.disability.empty())
for (std::set<std::string>::const_iterator it = rhs.disability.begin(); it != rhs.disability.end(); it++)
stream << '\t' << *it << '\n';
else
stream << "\tNone\n";

stream << "Gender: " << rhs.gender << "\nRG: " << rhs.rg << "\nCPF: " << rhs.cpf << "\nWork number: " << rhs.work
<< "\nDisease(s):\n";

if (!rhs.disease.empty())
for (std::set<std::string>::const_iterator it = rhs.disease.begin(); it != rhs.disease.end(); it++)
stream << '\t' << *it << '\n';
else
stream << "\tNone\n";

stream << "Experience:\n\t" << rhs.experience << "\nContract:\n\t" << rhs.contract;

return stream;
}

std::ofstream& challenge::operator<<(
std::ofstream& stream,
const challenge::Employee& rhs
){
size_t len;

len = rhs.name.length();
stream.write((const char*)&len, sizeof(size_t));
stream.write((const char*)rhs.name.c_str(), len);

stream.write((const char*)&rhs.age, sizeof(unsigned char));
stream << rhs.mstatus;

len = (size_t)rhs.disability.size();
stream.write((const char*)&len, sizeof(size_t));
if (len > 0)
for (std::set<std::string>::const_iterator it = rhs.disability.begin(); it != rhs.disability.end(); it++){
len = it->length();
stream.write((const char*)&len, sizeof(size_t));
stream.write((const char*)it->c_str(), len);
}

stream << rhs.gender << rhs.rg << rhs.cpf;

stream.write((const char*)&rhs.work, sizeof(int));

len = (size_t)rhs.disease.size();
stream.write((const char*)&len, sizeof(size_t));
if (len > 0)
for (std::set<std::string>::const_iterator it = rhs.disease.begin(); it != rhs.disease.end(); it++){
len = (size_t)it->length();
stream.write((const char*)&len, sizeof(size_t));
stream.write((const char*)it->c_str(), len);
}

len = (size_t)rhs.experience.length();
stream.write((const char*)&len, sizeof(size_t));
stream.write((const char*)rhs.experience.c_str(), len);

len = (size_t)rhs.contract.length();
stream.write((const char*)&len, sizeof(size_t));
stream.write((const char*)rhs.contract.c_str(), len);

return stream;
}

std::ifstream& challenge::operator>>(
std::ifstream& stream,
challenge::Employee& rhs
){
try{
size_t len;

stream.read((char*)&len, sizeof(size_t));
getline(stream, rhs.name, len);

stream.read((char*)&rhs.age, sizeof(unsigned char));
stream >> rhs.mstatus;

stream.read((char*)&len, sizeof(size_t));
if (len > 0){

std::string str;

for (size_t tmp = 0, i = 0; i < len; i++){
stream.read((char*)&tmp, sizeof(size_t));
getline(stream, str, tmp);
rhs.disability.insert(str);
}
}

stream >> rhs.gender >> rhs.rg >> rhs.cpf;
stream.read((char*)&rhs.work, sizeof(int));

stream.read((char*)&len, sizeof(size_t));
if (len > 0){
std::string str;

for (size_t tmp = 0, i = 0; i < len; i++){
stream.read((char*)&tmp, sizeof(size_t));
getline(stream, str, tmp);
rhs.disease.insert(str);
}
}

stream.read((char*)&len, sizeof(size_t));
getline(stream, rhs.experience, len);

stream.read((char*)&len, sizeof(size_t));
getline(stream, rhs.contract, len);
}catch (std::bad_alloc& ba){
throw ba;
}

return stream;
}

std::ifstream& challenge::getline(
std::ifstream& stream,
std::string& str,
size_t len
) throw (std::bad_alloc){
try{
char * p = new char[len+1];

stream.read(p, len);
p[len] = '\0';
str = p;

delete p;
}catch (std::bad_alloc& ba){
throw ba;
}

return stream;
}


Última edição por Rimack Zelnick em Seg Out 03, 2011 11:14 pm, editado 2 vez(es)
avatar
rmzelnick

Mensagens : 39
Data de inscrição : 02/07/2011
Idade : 25
Localização : Huntington, NY

Ver perfil do usuário http://www.markzelnick.me/

Voltar ao Topo Ir em baixo

Re: Rimack Zelnick - Challenge 4 - C++

Mensagem  rmzelnick em Dom Out 02, 2011 12:54 am

main.cpp

  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
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
#include <iostream>
#include <fstream>
#include <maritalstatus.h>
#include <rg.h>
#include <exception>
#include <cpf.h>
#include <gender.h>
#include <set>
#include <string>
#include <employee.h>
#include <list>

#include <cstring>

int main(int argc, char ** argv){
std::list<challenge::Employee> employee;
int option;

std::ifstream in(
"employee.dat",
std::ios::binary |
std::ios::in
);

if (in.is_open()){
challenge::Employee e;
size_t len;

in.read((char*)&len, sizeof(size_t));

for (size_t i = 0; i < len; i++){
in >> e;
employee.push_back(e);
}

in.close();
}

do{
std::cout << "[1] - Sign up employee\n[2] - Search an employee\n[3] - Display all employees names\n[4] - Edit employee information\n[0] - Exit\n->";
std::cin >> option;

switch (option){
case 1:{
char name[50], experience[2048],
contract[1024], str[100];
int age, work;
challenge::Rg rg;
challenge::Cpf cpf;
challenge::Gender gender;
challenge::MaritalStatus mstatus;
std::set<std::string> disability, disease;
bool isValid = false;

std::cout << "Enter name: ";
std::cin.ignore(50, '\n');
std::cin.getline(name, 50);

std::cout << "Enter age: ";
std::cin >> age;

std::cout << "Enter marital status(\n\tSingle\n\tMarried\n\tDivorced\n\tWidowed\n\tCohabiting\n\tCivil Union\n\tDomestic Partnership\n\tUnmarried Partners\n\t): ";
std::cin.ignore(50, '\n');
std::cin >> mstatus;

std::cout << "Disability(ies) (type \'exit\' when you're done):\n";
do{
std::cout << '\t';
std::cin.getline(str, 100);

if (std::strcmp(str, "exit"))
disability.insert(str);
}while (std::strcmp(str, "exit"));

std::cout << "Enter gender (Male or Female): ";
std::cin >> gender;

std::cout << "Enter RG number (UF-XXX.XXX.XXX): ";
std::cin >> rg;

do{
try{
std::cout << "Enter cpf number (XXX.XXX.XXX-XX): ";
std::cin >> cpf;
isValid = true;
}catch (challenge::InvalidCpf& ex){
std::cerr << "The following error occurred: " << ex.what() << '\n';
}
}while (!isValid);

std::cout << "Enter working number: ";
std::cin >> work;
std::cin.ignore(100, '\n');

std::cout << "Disease(s) (type \'exit\' when you're done):\n";
do{
std::cout << '\t';
std::cin.getline(str, 100);

if (std::strcmp(str, "exit"))
disease.insert(str);
}while (std::strcmp(str, "exit"));

std::cout << "Experience:\n\t";
std::cin.getline(experience, 2048);

std::cout << "Contract:\n\t";
std::cin.getline(contract, 1024);

employee.push_back(
challenge::Employee(
name,
age,
mstatus,
disability,
gender,
rg,
cpf,
work,
disease,
experience,
contract
)
);
}break;

case 2:{
int option;

do{
std::list<challenge::Employee>::const_iterator it;
bool found = false;

std::cout << "[1] - Search by name\n[2] - Search by RG\n[3] - Search by CPF\n[4] - Search by working number\n[0] - cancel search\n->";
std::cin >> option;

switch (option){
case 1:{
char name[50];

std::cout << "Enter name: ";
std::cin.ignore(50, '\n');
std::cin.getline(name, 50);

for (it = employee.begin(); !found && it != employee.end(); it++)
if (!std::strcmp(it->getName().c_str(), name))
found = true;
}break;
case 2:{
challenge::Rg rg;

std::cout << "Enter RG number (UF-XXX.XXX.XXX): ";
std::cin.ignore(50, '\n');
std::cin >> rg;

for (it = employee.begin(); !found && it != employee.end(); it++)
if (it->getRg() == rg)
found = true;

}break;
case 3:{
bool isValid = false;
challenge::Cpf cpf;
std::cin.ignore(50, '\n');

do{
try{
std::cout << "Enter cpf number (XXX.XXX.XXX.XX): ";
std::cin >> cpf;
isValid = true;
}catch(challenge::InvalidCpf& ex){
std::cerr << "The following error occurred: " << ex.what() << '\n';
}
}while (!isValid);

for (it = employee.begin(); !found && it != employee.end(); it++)
if (it->getCpf() == cpf)
found = true;
}break;
case 4:{
int work;

std::cout << "Enter working number: ";
std::cin >> work;

for (it = employee.begin(); !found && it != employee.end(); it++)
if (it->getWork() == work)
found = true;
}break;

case 0:{
}break;

default:
std::cout << "Invalid option!\n";
}

if (found){
std::cout << "Employee found!\n";
std::cout << *--it << '\n';
}else
std::cout << "Employee not found\n";
}while (option != 0);
}break;

case 3:{
std::list<challenge::Employee>::const_iterator it;

for (it = employee.begin(); it != employee.end(); it++)
std::cout << it->getName() << '\n';
}break;

case 4:{
std::list<challenge::Employee>::iterator it;

char name[50], experience[2048],
contract[1024], str[100];
bool found = false;
int age, work;
challenge::Rg rg;
challenge::Cpf cpf;
challenge::Gender gender;
challenge::MaritalStatus mstatus;
std::set<std::string> disability, disease;
bool isValid = false;

std::cin.ignore(50, '\n');
do{
std::cout << "Enter name of employee: ";
std::cin.getline(name, 50);

for (it = employee.begin(); !found && it != employee.end(); it++)
if (!strcmp(it->getName().c_str(), name))
found = true;
}while (!found);

std::cout << *--it << '\n';

std::cout << "Enter name: ";
std::cin.getline(name, 50);
it->setName(name);

std::cout << "Enter age: ";
std::cin >> age;
it->setAge(age);

std::cout << "Enter marital status(\n\tSingle\n\tMarried\n\tDivorced\n\tWidowed\n\tCohabiting\n\tCivil Union\n\tDomestic Partnership\n\tUnmarried Partners\n\t): ";
std::cin.ignore(50, '\n');
std::cin >> mstatus;
it->setMaritalStatus(mstatus);

std::cout << "Disability(ies) (type \'exit\' when you're done):\n";
do{
std::cout << '\t';
std::cin.getline(str, 100);

if (std::strcmp(str, "exit"))
disability.insert(str);
}while (std::strcmp(str, "exit"));
it->setDisabilities(disability);

std::cout << "Enter gender (Male or Female): ";
std::cin >> gender;
it->setGender(gender);

std::cout << "Enter RG number (UF-XXX.XXX.XXX): ";
std::cin >> rg;
it->setRg(rg);

do{
try{
std::cout << "Enter cpf number (XXX.XXX.XXX-XX): ";
std::cin >> cpf;
isValid = true;
}catch (challenge::InvalidCpf& ex){
std::cerr << "The following error occurred: " << ex.what() << '\n';
}
}while (!isValid);
it->setCpf(cpf);

std::cout << "Enter working number: ";
std::cin >> work;
std::cin.ignore(100, '\n');
it->setWork(work);

std::cout << "Disease(s) (type \'exit\' when you're done):\n";
do{
std::cout << '\t';
std::cin.getline(str, 100);

if (std::strcmp(str, "exit"))
disease.insert(str);
}while (std::strcmp(str, "exit"));
it->setDiseases(disease);

std::cout << "Experience:\n\t";
std::cin.getline(experience, 2048);
it->setExperience(experience);

std::cout << "Contract:\n\t";
std::cin.getline(contract, 1024);
it->setContract(contract);
}break;

case 0:{
std::ofstream out(
"employee.dat",
std::ios::binary |
std::ios::out
);

if (out.is_open()){
std::list<challenge::Employee>::const_iterator it;
size_t len;

len = (size_t)employee.size();
out.write((const char*)&len, sizeof(size_t));
for (it = employee.begin(); it != employee.end(); it++)
out << *it;

out.close();
}
}break;

default:
std::cout << "Invalid option!\n";
}
}while (option != 0);

return 0;
}


Resolvi não comentar as linhas, mas eu vou escrever um relatório do programa em detalhes. Desculpe-me por um monte de postagens, eu tentei colocar tudo em uma só postagem, mas o limite autorizado tinha ultrapassado.


Última edição por Rimack Zelnick em Seg Out 03, 2011 11:16 pm, editado 1 vez(es)
avatar
rmzelnick

Mensagens : 39
Data de inscrição : 02/07/2011
Idade : 25
Localização : Huntington, NY

Ver perfil do usuário http://www.markzelnick.me/

Voltar ao Topo Ir em baixo

Muito bom!!!

Mensagem  ricardonho em Dom Out 02, 2011 4:59 pm

Curti muito, super programador surgindo ai!

ricardonho

Mensagens : 1
Data de inscrição : 02/10/2011

Ver perfil do usuário

Voltar ao Topo Ir em baixo

Re: Rimack Zelnick - Challenge 4 - C++

Mensagem  Conteúdo patrocinado


Conteúdo patrocinado


Voltar ao Topo Ir em baixo

Voltar ao Topo

- Tópicos similares

 
Permissão deste fórum:
Você não pode responder aos tópicos neste fórum