-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathmain.cpp
288 lines (265 loc) · 7.64 KB
/
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
#include <iostream>
using namespace std;
#include <locale.h>
#include "Bagenda.h"
#define MAX 100
#define N_CPF 11
#define N_CNPJ 14
#define N_CEL 11
#define N_TEL 10
struct pessoal {
int ID;
long long int cel,CPF;
string nome;
};
struct comercial {
int ID;
long long int tel, CNPJ;
string nome;
};
int tipo_contato(string tipo_operacao){ //SELECIONA QUAL AGENDA INSERIR O CONTATO
int tipo_contato;
while(true){
cout << "Qual tipo de contato você deseja "<<tipo_operacao<<" ? 1 - Pessoal\t2 - Comercial\n";
cin >> tipo_contato;
if(tipo_contato==1){
return 1;
}
if(tipo_contato==2){
return 2;
}
}
}
int tipo_pesquisa(){ //SELECIONA QUAL DADO A SER PESQUISADO
int tipo_pesquisa;
while(true){
cout << "Qual tipo de pesquisa você deseja ?"<<" 1 - ID\t2 - Nome\n";
cin >> tipo_pesquisa;
if(tipo_pesquisa==1){
return 1;
}
if(tipo_pesquisa==2){
return 2;
}
}
}
bool conta_digitos(long long int dado, int n_esperado){ //VERIFICA SE O NÚMERO DE DÍGITOS É O CORRETO
int n_digitos = 0;
while(dado!=0){
dado /= 10;
n_digitos++;
}
if (n_digitos == n_esperado)
return true;
else
return false;
}
istream & operator >> (istream & in,pessoal &ag){//SOBRECARGA PARA INSERIR PESSOAL
cout << "Insira o ID: ";
in >> ag.ID;
cout << "Insira o CPF: ";
do{
in >> ag.CPF;
if (conta_digitos(ag.CPF, N_CPF) == false)
cout << "Número de dígitos inválido. Tente novamente. ";
}
while(conta_digitos(ag.CPF, N_CPF) == false);
cout << "Insira o nome: ";
in >> ag.nome;
cout << "Insira o celular: ";
do{
in >> ag.cel;
if (conta_digitos(ag.cel, N_CEL) == false)
cout << "Número de dígitos inválido. Tente novamente. ";
}
while(conta_digitos(ag.cel, N_CEL) == false);
return in;
}
istream & operator >> (istream & in,comercial &ag){//SOBRECARGA PARA INSERIR COMERCIAL
cout << "Insira o ID: ";
in >> ag.ID;
cout << "Insira o CNPJ: ";
do{
in >> ag.CNPJ;
if (conta_digitos(ag.CNPJ, N_CNPJ) == false)
cout << "Número de dígitos inválido. Tente novamente. ";
}
while(conta_digitos(ag.CNPJ, N_CNPJ) == false);
cout << "Insira o nome: ";
in >> ag.nome;
cout << "Insira o telefone: ";
do{
in >> ag.tel;
if (conta_digitos(ag.tel, N_TEL) == false)
cout << "Número de dígitos inválido. Tente novamente. ";
}
while(conta_digitos(ag.tel, N_TEL) == false);
return in;
}
ostream & operator << (ostream & out,pessoal &ag){//SOBRECARGA PARA IMPRIMIR PESSOAL
cout << "\nID: ";
out << ag.ID;
cout << "\nCPF: ";
out << ag.CPF;
cout << "\nnome: ";
out << ag.nome;
cout << "\ncelular: ";
out << ag.cel;
cout<<endl;
return out;
}
ostream & operator << (ostream & out,comercial &ag){//SOBRECARGA PARA IMPRIMIR COMERCIAL
cout << "\nID: ";
out << ag.ID;
cout << "\nCNPJ: ";
out << ag.CNPJ;
cout << "\nNome: ";
out << ag.nome;
cout << "\nTelefone: ";
out << ag.tel;
cout<<endl;
return out;
}
int main() {
setlocale(LC_ALL,"Portuguese");
int max_ag;
int operacao;
cout << "Quantas posições para as agendas? ";
do{
cin >> max_ag;
}
while(max_ag < 1);
agenda<pessoal> ag1;
agenda<comercial> ag2;
ag1.itens = new pessoal[max_ag];
ag2.itens = new comercial[max_ag];
inicializa_agenda(ag1);
inicializa_agenda(ag2);
string tipo_operacao;
do {
//system("cls");
cout << "\n\nSelecione uma operação:\n1 - Inserir item\n2 - Remover item\n3 - Pesquisar\n4 - Ordenar agenda\n5 - Mostrar todos os itens\n6 - Encerrar\n\n";
cin >> operacao;
switch(operacao) { //ESCOLHE A OPERAÇÃO A SER REALIZADA
//--------------------------------------------------------------------------------
case 1: //OPERAÇÃO DE INSERÇÃO
tipo_operacao="inserir";
if(tipo_contato(tipo_operacao)==1){ //INSERIR TIPO PESSOAL
pessoal ag_temp1;
cin>>ag_temp1;
if (insere(ag1, ag_temp1, max_ag) == true)
cout << "Item inserido com sucesso";
else
cout << "Erro: não foi possível inserir o item na agenda";
}
else{ // INSERE TIPO COMERCIAL
comercial ag_temp2;
cin>>ag_temp2;
if (insere(ag2, ag_temp2, max_ag) == true)
cout << "Item inserido com sucesso";
else
cout << "Erro: não foi possível inserir os dados na agenda";
}
break;
//-----------------------------------------------------------------------------------
case 2: //OPERAÇÃO DE REMOÇÃO
int ID_pesquisa;
tipo_operacao="remover";
if(tipo_contato(tipo_operacao)==1){ //REMOVE TIPO PESSOAL
pessoal ag_temp1;
cout<<"Digite o ID a ser removido: ";
cin>>ID_pesquisa;
}
else{ // REMOVE TIPO COMERCIAL
comercial ag_temp2;
cout<<"Digite o ID a ser excluído: ";
cin>>ID_pesquisa;
ag_temp2.ID=0;
ag_temp2.CNPJ=0;
ag_temp2.nome="";
ag_temp2.tel=0;
remove(ag2,ID_pesquisa,ag_temp2);
}
break;
//-------------------------------------------------------------------------------------------
case 3: //OPERAÇÃO DE PESQUISA
{
tipo_operacao="pesquisar";
int id=0;
string nome="";
if(tipo_contato(tipo_operacao)==1){ //PESQUISA TIPO PESSOAL
if(tipo_pesquisa()==1){
cout<<"Digite o ID: ";
cin>>id;
if(pesquisa(ag1,id,nome)!=-1)
cout<<ag1.itens[pesquisa(ag1,id,nome)];
else
cout<<"Item não encontrado";
}
else{
cout<<"Digite o nome: ";
cin>>nome;
int local;
if(pesquisa(ag1,id,nome)!=-1)
cout<<ag1.itens[pesquisa(ag1,id,nome)];
else
cout<<"Item não encontrado";
}
}
else{ //PESQUISA TIPO COMERCIAL
if(tipo_pesquisa()==1){
cout<<"Digite o ID: ";
cin>>id;
int local;
if(pesquisa(ag1,id,nome)!=-1)
cout<<ag1.itens[pesquisa(ag1,id,nome)];
else
cout<<"Item não encontrado";
}
else{
cout<<"Digite o nome: ";
cin>>nome;
int local;
if(pesquisa(ag1,id,nome)!=-1)
cout<<ag1.itens[pesquisa(ag1,id,nome)];
else
cout<<"Item não encontrado";
}
}
break;
}
//--------------------------------------------------------------------------------------------
case 4:
{
tipo_operacao="ordenar";
if(tipo_contato(tipo_operacao)==1){ //ORDENA TIPO PESSOAL
pessoal ag_min;
pessoal ag_troca;
ordenar(ag1);
}
else{ // ORDENA TIPO COMERCIAL
comercial ag_min1;
comercial ag_troca1;
ordenar(ag2);
}
}
break;
//--------------------------------------------------------------------------------------------
case 5:
tipo_operacao="imprimir";
if(tipo_contato(tipo_operacao)==1){ //IMPRIMI TIPO PESSOAL
cout<<"\nAgenda Pessoal:\n";
imprime(ag1);
}
else{ //IMPRIMI TIPO COMERCIAL
cout<<"\nAgenda Comercial:\n";
imprime(ag2);
}
break;
}
}
while(operacao != 6);
delete []ag1.itens;
delete []ag2.itens;
return 0;
}