Está en la página 1de 9

ESCUELA DE CIENCIAS BSICAS TECNOLOGAS E INGENIERA

INTRODUCCIN A LA PROGRAMACIN
GRUPO 301304_47

INTRODUCCIN A LA PROGRAMACIN
FASE 3 DEPURAR PROGRAMA
PRCTICA 2 - EVALUACIN FINAL

PRESENTADO POR

TUTOR

UNIVERSIDAD NACIONAL ABIERTA Y ADISTANCIA UNAD


ESCUELA DE CIENCIAS BASICAS, TECNOLOGIA E INGENIERIA
DICIEMBRE 2016
INTRODUCCION

En este trabajo se realizan las lecturas de los recursos tericos correspondientes a la Unidad 3,
con el fin de obtener conocimientos e implementarlos para disear y desarrollar programas
ESCUELA DE CIENCIAS BSICAS TECNOLOGAS E INGENIERA
INTRODUCCIN A LA PROGRAMACIN
GRUPO 301304_47

optimizados que solucionan problemas reales de procesamiento de datos utilizando arreglos,


matrices y mtodos de ordenacin y bsqueda en lenguaje C++. Por medio de los diversos
conocimientos adquiridos en el transcurso del curso se consigue disear y desarrollar programas
utilizando vectores, matrices y cadenas.

DESAROLLO DE LA ACTIVIDAD

PROBLEMA

1
ESCUELA DE CIENCIAS BSICAS TECNOLOGAS E INGENIERA
INTRODUCCIN A LA PROGRAMACIN
GRUPO 301304_47

Disear un directorio telefnico que incluya nombre y telfono de N personas tendiendo


presente que un nmero telefnico no se puede repetir. El programa debe permitir ordenar los
datos por nmero telefnico y realizar consulta por nombre.

DISEO DE LA SOLUCION

#include <iostream>
#include <fstream>
#include <cstdlib>
#include <string>
#include <vector>
#include <algorithm>
#ifdef __linux__
#include <termios.h>
#include <unistd.h>
#define CLEAR_SCREEN "clear"
#define aacute "\xC3\xA1"
#define oacute "\xC3\xB3"
#define uacute "\xC3\xBA"
#define Uacute "\xC3\x9A"
int _getch ();
#else
#include <conio.h>
#define CLEAR_SCREEN "cls"
#define aacute "\240"
#define oacute "\242"
#define uacute "\243"
#define Uacute "\351"
#endif
using namespace std;

class Contacto {
public:
string telefono;
string nombre;

Contacto() {}
~Contacto() {}

bool operator==(const Contacto &contacto) const


{
return this==&contacto || this->telefono==contacto.telefono;
}

bool operator!=(const Contacto &contacto) const

2
ESCUELA DE CIENCIAS BSICAS TECNOLOGAS E INGENIERA
INTRODUCCIN A LA PROGRAMACIN
GRUPO 301304_47

{
return this!=&contacto && this->telefono!=contacto.telefono;
}

bool operator<(const Contacto &contacto) const


{
return this->telefono<contacto.telefono;
}

Contacto& operator=(const Contacto &contacto)


{
if (this!=&contacto)
{
this->telefono = contacto.telefono;
this->nombre = contacto.nombre;
}
return *this;
}

static void imprimir (Contacto &contacto, int *contador);


static void imprimir_en_archivo (Contacto &contacto, ostream *archivo);
};

void pausar (const char *mensaje);


string leer_cadena (const char *mensaje);
int leer_entero (const char *mensaje);
bool leer_campo (istream &archivo, char *campo, char delimitador);

const char *ruta = "contactos.tsv";

int main ()
{
vector<Contacto> contactos;
vector<Contacto>::iterator i;
Contacto *dato, contacto;
int contador=0, opcion;
char campo[255];
ifstream entrada (ruta);
if (entrada!=NULL)
{
while (leer_campo (entrada, campo, '\t'))
{
contacto.telefono = campo;
leer_campo (entrada, campo, '\n');
contacto.nombre = campo;
contactos.push_back (contacto);
}
entrada.close();
3
ESCUELA DE CIENCIAS BSICAS TECNOLOGAS E INGENIERA
INTRODUCCIN A LA PROGRAMACIN
GRUPO 301304_47

}
do {
system (CLEAR_SCREEN);
cout << "MEN" Uacute << 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 << "Seleccione una opci" oacute "n:" << endl;
do
opcion = _getch ();
while (opcion<'1' || opcion>'7');
cout << (char)opcion << endl << endl;
if (contactos.empty() && opcion!='1' && opcion!='7')
{
pausar ("No hay registros.\n");
continue;
}
if (opcion<'5')
{
contacto.telefono = leer_cadena ("Ingrese el telefono del contacto");
i = find (contactos.begin(), contactos.end(), contacto);
dato = i != contactos.end() ? &contactos[i - contactos.begin()] : NULL;
if (dato!=NULL)
{
cout << endl;
Contacto::imprimir (*dato, &contador);
}
}
if (opcion=='1' && dato!=NULL)
cout << "El registro ya existe." << endl;
else if (opcion>='2' && opcion<='4' && dato==NULL)
cout << endl << "Registro no encontrado." << endl;
else switch (opcion)
{
case '1':
contacto.nombre = leer_cadena ("Ingrese el nombre");
contactos.push_back (contacto);
cout << endl << "Registro agregado correctamente." << endl;
break;
case '3':
dato->nombre = leer_cadena ("Ingrese el nuevo nombre");
cout << endl << "Registro actualizado correctamente." << endl;
break;
case '4':
4
ESCUELA DE CIENCIAS BSICAS TECNOLOGAS E INGENIERA
INTRODUCCIN A LA PROGRAMACIN
GRUPO 301304_47

contactos.erase (find (contactos.begin(), contactos.end(), *dato));


cout << "Registro borrado correctamente." << endl;
break;
case '5':
sort (contactos.begin(), contactos.end());
cout << "Registros ordenados correctamente." << endl;
break;
case '6':
contador = 0;
for (i=contactos.begin(); i!=contactos.end(); i++)
Contacto::imprimir (*i, &contador);
cout << "Total de registros: " << contador << "." << endl;
break;
}
if (opcion!='7')
pausar ("");
} while (opcion!='7');
ofstream salida (ruta);
if (salida!=NULL)
{
for (i=contactos.begin(); i!=contactos.end(); i++)
Contacto::imprimir_en_archivo (*i, &salida);
salida.close();
}
return EXIT_SUCCESS;
}

void Contacto::imprimir (Contacto &contacto, int *contador)


{
cout << "telefono: " << contacto.telefono.c_str() << endl;
cout << "nombre : " << contacto.nombre.c_str() << endl;
cout << endl;
(*contador)++;
}

void Contacto::imprimir_en_archivo (Contacto &contacto, ostream *archivo)


{
*archivo << contacto.telefono.c_str() << "\t";
*archivo << contacto.nombre.c_str() << endl;
}

string leer_cadena (const char *mensaje)


{
char cadena[255];
cout << mensaje << ": ";
cin.get (cadena, sizeof (cadena), '\n');
cin.get();
string str(cadena);
5
ESCUELA DE CIENCIAS BSICAS TECNOLOGAS E INGENIERA
INTRODUCCIN A LA PROGRAMACIN
GRUPO 301304_47

return str;
}

int leer_entero (const char *mensaje)


{
int entero;
cout << mensaje << ": ";
cin >> entero;
cin.get();
return entero;
}

bool leer_campo (istream &archivo, char *cadena, char delimitador)


{
archivo.get (cadena, 255, delimitador);
archivo.get();
return !archivo.eof();
}

void pausar (const char *mensaje)


{
cout << mensaje << endl << "Presione una tecla para continuar . . . " << endl;
_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

CONCLUSIONES

6
ESCUELA DE CIENCIAS BSICAS TECNOLOGAS E INGENIERA
INTRODUCCIN A LA PROGRAMACIN
GRUPO 301304_47

Se aplican las estructuras de secuencias, seleccin e iteracin con el paradigma de programacin


estructurada en el lenguaje C++, adicional se depura prototipos codificados implementando
funciones para optimizar programas en lenguaje C++. Se implementa soluciones en lenguaje C+
+ utilizando estructuras estticas para almacenar datos, combinados con mtodos de ordenacin
y bsqueda.

Se disea y desarrolla programas utilizando vectores, matrices y cadenas, se utiliza


adecuadamente mecanismos de almacenamientos y recuperacin de informacin, as como los
mtodos de ordenacin y bsqueda.

Por medio del desarrollo de esta fase se promueve la investigacin en el estudiante porque
requiere de planeacin, implementacin y evaluacin de proyectos. Involucra al estudiante con
la investigacin para tomar decisiones. Favorece el aprendizaje autnomo.

REFERENCIAS

7
ESCUELA DE CIENCIAS BSICAS TECNOLOGAS E INGENIERA
INTRODUCCIN A LA PROGRAMACIN
GRUPO 301304_47

Benavides Ruano, Mirian. 2016. Funciones. Universidad Nacional Abierta y a Distancia


UNAD. Disponible en: http://datateca.unad.edu.co/contenidos/301304/2016-2/U2/Unidad_2_-
_Funciones.pdf

Joyanes Aguilar, Luis, and Snchez Garca, Lucas. Programacin en C++: un enfoque prctico :
un enfoque prctico. Madrid, ES: McGraw-Hill Espaa, 2009. ProQuest ebrary . Funciones y
mdulos. Disponible en http://datateca.unad.edu.co/contenidos/301304/2016-
2/U2/Funciones_y_modulos.pdf

Joyanes Aguilar, Luis, and Snchez Garca, Lucas. Programacin en C++: un enfoque prctico :
un enfoque prctico. Madrid, ES: McGraw-Hill Espaa, 2009. ProQuest ebrary . Estructuras de
control repetitivas. Disponible en
http://bibliotecavirtual.unad.edu.co:2077/lib/unadsp/reader.action?
ppg=1&docID=10491298&tm=1467436778252

Joyanes Aguilar, Luis, Castillo Sanz, Andrs, and Snchez Garca, Lucas. C algoritmos,
programacin y estructuras de datos. Madrid, ES: McGraw-Hill Espaa, 2009. ProQuest ebrary.
http://bibliotecavirtual.unad.edu.co:2077/lib/unadsp/reader.action?
ppg=1&docID=10491350&tm=1467436944090

También podría gustarte