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
/*
    Este código no compila en Turbo C++. Si trabajas con este compilador te
    sugerimos usar el botón de traducción y seleccionar la opción Arreglo dinámico en C.
*/

#include <iostream>
#include <fstream>
#include <cstdlib>
#include <string>
#include <vector>
#include <algorithm>
#ifdef __linux__
    #include <termios.h>
    #include <unistd.h>
    #define CLEAR_SCREEN "clear"
    int _getch ();
#else
    #include <conio.h>
    #define CLEAR_SCREEN "cls"
#endif
using namespace std;

class Paciente {
public:
    int identificacion;
    string nombre;
    int servicio;
    int pabellon;
    int consultorio;

    Paciente() {}
    ~Paciente() {}

    bool operator==(const Paciente &paciente) const
    {
        return this==&paciente || this->identificacion==paciente.identificacion;
    }

    bool operator!=(const Paciente &paciente) const
    {
        return this!=&paciente && this->identificacion!=paciente.identificacion;
    }

    bool operator<(const Paciente &paciente) const
    {
        return this->identificacion<paciente.identificacion;
    }

    Paciente& operator=(const Paciente &paciente)
    {
        if (this!=&paciente)
        {
            this->identificacion = paciente.identificacion;
            this->nombre = paciente.nombre;
            this->servicio = paciente.servicio;
            this->pabellon = paciente.pabellon;
            this->consultorio = paciente.consultorio;
        }
        return *this;
    }

    static void print (Paciente &paciente, int *counter);
    static void print_on_file (Paciente &paciente, ostream *file_stream);
};

void   pause_screen (const char *message);
string read_str (const char *message);
int    read_integer (const char *message);
bool   read_field (istream &file_stream, char *field, char delimiter);

const char *path = "pacientes.tsv";

int main ()
{
    vector<Paciente> pacientes;
    vector<Paciente>::iterator i;
    Paciente *datum, paciente;
    int counter=0, main_option, suboption;
    char field[255];
    ifstream file_stream (path);
    if (file_stream!=NULL)
    {
        while (read_field (file_stream, field, '\t'))
        {
            paciente.identificacion = atoi (field);
            read_field (file_stream, field, '\t');
            paciente.nombre = field;
            read_field (file_stream, field, '\t');
            paciente.servicio = atoi (field);
            read_field (file_stream, field, '\t');
            paciente.pabellon = atoi (field);
            read_field (file_stream, field, '\n');
            paciente.consultorio = atoi (field);
            pacientes.push_back (paciente);
        }
        file_stream.close();
    }
    do {
        cout << endl;
        system (CLEAR_SCREEN);
        cout << "MEN\351" << endl;
        cout << "1.- Altas" << endl;
        cout << "2.- Consultas" << endl;
        cout << "3.- Actualizaciones" << endl;
        cout << "4.- Bajas" << endl;
        cout << "5.- Ordenar registros" << endl;
        cout << "6.- Listar registros" << endl;
        cout << "7.- Salir" << endl;
        cout << "Selecciona una opci\242n: ";
        do
            main_option = _getch ();
        while (main_option<'1' || main_option>'7');
        cout << (char)main_option << endl << endl;
        if (pacientes.empty() && main_option!='1' && main_option!='7')
        {
            pause_screen ("No hay registros.\n");
            continue;
        }
        if (main_option<'5')
        {
            paciente.identificacion = read_integer ("Ingresa el identificacion del paciente");
            i = find (pacientes.begin(), pacientes.end(), paciente);
            datum = i != pacientes.end() ? &pacientes[i - pacientes.begin()] : NULL;
            if (datum!=NULL)
            {
                cout << endl;
                Paciente::print (*datum, &counter);
            }
        }
        if (main_option=='1' && datum!=NULL)
            cout << "El registro ya existe." << endl;
        else if (main_option>='2' && main_option<='4' && datum==NULL)
            cout << endl << "Registro no encontrado." << endl;
        else switch (main_option)
        {
            case '1':
                paciente.nombre = read_str ("Ingresa el nombre");
                paciente.servicio = read_integer ("Ingresa el servicio");
                paciente.pabellon = read_integer ("Ingresa el pabellon");
                paciente.consultorio = read_integer ("Ingresa el consultorio");
                pacientes.push_back (paciente);
                cout << endl << "Registro agregado correctamente." << endl;
                break;
            case '3':
                cout << "Men\243 de actualizaci\242n de campos" << endl;
                cout << "1.- nombre" << endl;
                cout << "2.- servicio" << endl;
                cout << "3.- pabellon" << endl;
                cout << "4.- consultorio" << endl;
                do {
                    suboption = read_integer ("Selecciona el n\243mero de campo a modificar");
                    if (suboption<1 || suboption>4)
                        printf ("Opci\242n incorrecta\n");
                } while (suboption<1 || suboption>4);
                switch (suboption)
                {
                    case 1:
                        datum->nombre = read_str ("Ingresa el nuevo nombre");
                        break;
                    case 2:
                        datum->servicio = read_integer ("Ingresa el nuevo servicio");
                        break;
                    case 3:
                        datum->pabellon = read_integer ("Ingresa el nuevo pabellon");
                        break;
                    case 4:
                        datum->consultorio = read_integer ("Ingresa el nuevo consultorio");
                        break;
                }
                cout << endl << "Registro actualizado correctamente." << endl;
                break;
            case '4':
                pacientes.erase (find (pacientes.begin(), pacientes.end(), *datum));
                cout << "Registro eliminado correctamente." << endl;
                break;
            case '5':
                sort (pacientes.begin(), pacientes.end());
                cout << "Registros ordenados correctamente." << endl;
                break;
            case '6':
                counter = 0;
                for (i=pacientes.begin(); i!=pacientes.end(); i++)
                    Paciente::print (*i, &counter);
                cout << "Total de registros: " << counter << "." << endl;
                break;
        }
        if (main_option!='7')
            pause_screen ("");
    } while (main_option!='7');
    ofstream output_stream (path);
    if (output_stream!=NULL)
    {
        for (i=pacientes.begin(); i!=pacientes.end(); i++)
            Paciente::print_on_file (*i, &output_stream);
        output_stream.close();
    }
    return EXIT_SUCCESS;
}

void Paciente::print (Paciente &paciente, int *counter)
{
    cout << "identificacion: " << paciente.identificacion << endl;
    cout << "nombre        : " << paciente.nombre.c_str() << endl;
    cout << "servicio      : " << paciente.servicio << endl;
    cout << "pabellon      : " << paciente.pabellon << endl;
    cout << "consultorio   : " << paciente.consultorio << endl;
    cout << endl;
    (*counter)++;
}

void Paciente::print_on_file (Paciente &paciente, ostream *file_stream)
{
    *file_stream << paciente.identificacion << "\t";
    *file_stream << paciente.nombre.c_str() << "\t";
    *file_stream << paciente.servicio << "\t";
    *file_stream << paciente.pabellon << "\t";
    *file_stream << paciente.consultorio << endl;
}

string read_str (const char *message)
{
    char string_var[255];
    cout << message << ": ";
    cin.get (string_var, sizeof (string_var), '\n');
    cin.get();
    string str(string_var);
    return str;
}

int read_integer (const char *message)
{
    int integer_var;
    cout << message << ": ";
    cin >> integer_var;
    cin.get();
    return integer_var;
}

bool read_field (istream &file_stream, char *str, char delimiter)
{
    file_stream.get (str, 255, delimiter);
    file_stream.get();
    return !file_stream.eof();
}

void pause_screen (const char *message)
{
    cout << message << endl << "Presiona una tecla para continuar . . . ";
    _getch ();
}

#ifdef __linux__

int _getch ()
{
    int ch;
    struct termios oldt, newt;
    tcgetattr (STDIN_FILENO, &oldt);
    newt = oldt;
    newt.c_lflag &= ~(ICANON | ECHO);
    tcsetattr (STDIN_FILENO, TCSANOW, &newt);
    ch = getchar();
    tcsetattr (STDIN_FILENO, TCSANOW, &oldt);
    return ch;
}

#endif