Skip to content

Instantly share code, notes, and snippets.

@KaterineM
Last active January 11, 2016 08:40
Show Gist options
  • Save KaterineM/600786db206cb79812ed to your computer and use it in GitHub Desktop.
Save KaterineM/600786db206cb79812ed to your computer and use it in GitHub Desktop.
/*
Copyright (C) 2016 Katerine Muñoz Tello <[email protected]>
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
*** Nombre: Katerine Muñoz Tello.
*** Objetivo: Defina un problema de la vida real, cuya solución se pueda
estructurar con una lista enlazada que contenga otra lista entre sus
datos.
*** SO: Fedora 23.
*** IDE: Sublime Text.
*** Compilador: GNU Make 4.0
*/
#include <iostream>
#include <string.h>
#include <fstream>
using namespace std;
struct airplane{
char code[10]; //Código del avión
char model[20]; //Modelo del avión
int first; //Asientos disponibles en primera clase
int tur; //Asientos disponibles en clase turista
int serv; //Antigüedad del avión
int depa; //Hora de despegue
int arri; //Hora de aterrizaje
airplane *next;
};
struct route{
char rcode[10]; //Código de ruta (distinto para todos las rutas ingresadas)
char from[10]; //Código de procedencia de la ruta
char to[10]; //Código de destino de la ruta
int day; //Día a realizarse la ruta
int month; //Mes a realizarse la ruta
int year; //Año a realizarse la ruta
int quan; //Cantidad de vuelos a realizar la ruta
airplane *a;
route *next;
};
class flight{
route *r;
public:
flight();
~flight();
void in(char rcode[10], char from[10], char to[10], int day, int month, int year, int quan);
void subin(char rcode[10], char code[10], char model[20], int first, int tur, int serv, int depa, int arri);
void out(char rcode[10]);
void showonlyroute(route *r1); //Muestra la informacion de ruta.
void showonlyairplane(airplane *a1); //muestra la informacion del avion.
int routesperday(route *r1); //Retorna la cantidad de rutas que se hacen en un día.
int duration(airplane *a1); //Retorna la duración de un vuelo en horas militares.
int isin(char from[10], char to[10]); //Retorna si acaso una ruta se encuentra en la lista o no.
void show_route(char from[10], char to[10]); //Muestra todos los vuelos de una ruta.
void closest_flight(); //Muestra el vuelo más cercano de la lista.
void farthestflight(); //Muestra el vuelo más lejano de la lista.
void incity(char city[10]); //muestra los vuelos que pasan por una ciudad.
void showbyday(int day, int month, int year); //Muestra las rutas y vuelos de un día en específico.
void buyticket(); //Comprar un boleto de avión.
void returnticket(char rcode[10], char code[10], int i, int s); //Devolver boleto de avion.
void lessfull(); //Avion con más asientos disponibles.
void longestflight(); //Vuelo más largo.
void shortestflight(); //Vuelo más corto.
void show_all(); //Mostrar todos los datos.
};
void menu(flight F); //Función menú.
//Constructor.
flight::flight(){
r = NULL;
}
//Destructor.
flight::~flight(){
while(r){
out(r->rcode);
}
}
//Ingreso lista principal.
void flight::in(char rcode[10], char from[10], char to[10], int day, int month, int year, int quan){
route *r1, *r2, *r3;
airplane *a1 = NULL;
r1 = new route;
strncpy(r1->rcode, rcode, 10);
strncpy(r1->from, from, 10);
strncpy(r1->to, to, 10);
r1->day = day;
r1->month = month;
r1->year = year;
r1->quan = quan;
r1->a = a1;
r1->next = r;
if(!r || year < r->year){
r = r1;
}
else{
r2 = r1->next;
r3 = r1;
while(r2 && r2->year < year){
r3 = r2;
r2 = r2->next;
r1->next = r2;
}
while(r2->year == year){
if((month > r2->month) || (month == r2->month && r2->day < day)){
r3 = r2;
r2 = r2->next;
r1->next = r2;
}
else{
break;
}
}
if(r3 == r1){
r = r1;
}
else{
r3->next = r1;
}
}
}
//Ingreso sub lista.
void flight::subin(char rcode[10], char code[10], char model[20], int first, int tur, int serv, int depa, int arri){
route *r1;
airplane *a1, *prev, *anext;
a1 = new struct airplane;
strncpy(a1->code, code, 10);
strncpy(a1->model, model, 20);
a1->first = first;
a1->tur = tur;
a1->serv = serv;
a1->depa = depa;
a1->arri = arri;
a1->next = NULL;
r1 = r;
while(strcmp(r1->rcode,rcode)){ //Busca la ruta a la que pertenece el vuelo.
r1 = r1->next;
}
if(!r1->a){
r1->a = a1;
}else if(depa <= r1->a->depa){
a1->next = r1->a;
r1->a = a1;
}else{
anext = r1->a->next;
prev = r1->a;
while(anext && (anext->depa < a1->depa)){
prev = prev->next;
anext = anext->next;
}
prev->next = a1;
a1->next = anext;
}
}
//Elimina
void flight::out(char rcode[10]){
route *r1, *r2;
if(!r){
cout << "\t LISTA VACIA" << endl;
}
else{
r1 = r;
if(!strcmp(r->rcode,rcode)){
r = r->next;
delete r1;
}
else{
do{
r2 = r1;
r1 = r1->next;
}while(r1 && strcmp(r->rcode,rcode));
if(r1){
r2->next = r1->next;
delete r1;
}
else{
cout << "\t La ruta que busca no esta en la lista." << endl;
}
}
}
}
//Muestra la información de rutas.
void flight::showonlyroute(route *r1){
cout << "Ruta: " << r1->from << "-" << r1->to << endl;
cout << "Dia: " << r1->day << "/" << r1->month << "/" << r1->year << endl;
cout << "Codigo: " << r1->rcode << endl;
cout << "Numero de aviones a realizar la ruta: " << r1->quan << endl;
}
//Muestra la información de los aviones.
void flight::showonlyairplane(airplane *a1){
int hs, hl, ms, ml;
hs = a1->depa/100;
ms = a1->depa%100;
hl = a1->arri/100;
ml = a1->arri%100;
cout << "\tVuelo: " << a1->code << endl;
cout << "\tModelo de avion: " << a1->model << endl;
cout << "\tAsientos disponibles en primera clase: " << a1->first << " asientos." << endl;
cout << "\tAsientos disponibles en clase turista: " << a1->tur << " asientos." << endl;
cout << "\tAntiguedad del avion: " << a1->serv << " annos." << endl;
cout << "\tHora de salida: " << hs << ":" << ms << " hrs." << endl;
cout << "\tHora de llegada: " << hl << ":" << ml << " hrs." << endl;
cout << "\t=========================================================" << endl;
}
//1. Numero de rutas en un día.
int flight::routesperday(route *r1){
int day, month, year, depa, cont = 0;
day = r1->day;
month = r1->month;
year = r1->year;
while(r1){
if(r1->day == day && r1->month == month && r1->year == year){
cont++;
}
r1 = r1->next;
}
return cont;
}
//2. Duracion de un vuelo en horas militares.
int flight::duration(airplane *a1){
int ha, hd, ma, md, result;
hd = a1->depa/100;
md = a1->depa%100;
ha = a1->arri/100;
ma = a1->arri%100;
if(hd > ha){
result = (hd-ha)*100;
if(md > ma){
result +=(md-ma);
}
else{
result += (ma-md);
}
}
else{
result = ((23-hd)*100) + (ha*100);
int aux = 60 - md + ma;
if(aux >= 60){
result += 100;
aux -=60;
}
result += aux;
}
return result;
}
//3. Retorna si una ruta esta o no en la lista.
int flight::isin(char from[10], char to[10]){
route *r1;
int i = 0;
r1 = r;
while(r1){
if(!strcmp(r1->from,from) && !strcmp(r1->to,to)){
i = 1;
}
r1 = r1->next;
}
return i;
}
//4. Muestra todos los vuelos de una ruta, independiente la fecha.
void flight::show_route(char from[10], char to[10]){
route *r1;
airplane *a1;
int i = 0, h, m;
r1 = r;
cout << "Los vuelos para todas las rutas " << from << "-" << to << " son: " << endl;
while(r1){
if(!strcmp(r1->from,from) && !strcmp(r1->to,to)){
showonlyroute(r1);
a1 = r1->a;
i = 1;
while(a1){
showonlyairplane(a1);
a1 = a1->next;
}
}
r1 = r1->next;
}
if(!i){
cout << "La ruta que usted busca no se encuentra en nuestra base de datos." << endl;
}
}
//5. Vuelo más cercano en la lista.
void flight::closest_flight(){
route *r1, *r2;
airplane *a1, *a2;
r1 = r;
int cont = routesperday(r1);
int n = 0;
a1 = r1->a;
do{
r1 = r1->next;
a2 = r1->a;
if(a2->depa < a1->depa){
r2 = r1;
a1 = a2;
}
n++;
}while(n < cont);
cout << "El vuelo más cercano es: " << endl;
showonlyairplane(a1);
cout << "Que pertenece a: " << endl;
showonlyroute(r2);
}
//6. Vuelo más lejano en la lista.
void flight::farthestflight(){
route *r1, *r2;
airplane *a1, *a2;
int i = 0;
r1 = r;
while(r1){ //Dejo el puntero en el último nodo para tomar los datos de la ultima ruta.
i++;
r1 = r1->next;
}
int cont = routesperday(r1), n = i-cont, m = 0; //Cont=numero de rutas del mismo dia, n=nodos a avanzar para quedar en el ultimo dia.
r2 = r;
do{ //Dejo r2 en el primer nodo del ultimo dia.
m++;
r2 = r2->next;
}while(m < n);
i = 1;
a2 = r2->a;
do{ //Dejo a2 en el ultimo vuelo del ultimo dia.
i++;
a2 = a2->next;
}while(i < r2->quan);
r1 = r2;
do{
r1 = r1->next;
a1 = r1->a;
i = 0;
do{
i++;
a1 = a1->next;
}while(i < r1->quan);
if(a2->depa < a1->depa){
r2 = r1;
a2 = a1;
}
n++;
}while(n < cont);
cout << "El vuelo más lejano es: " << endl;
showonlyairplane(a2);
cout << "Que pertenece a: " << endl;
showonlyroute(r2);
}
//7. Todos los vuelos que pasan por una ciudad.
void flight::incity(char city[10]){
route *r1;
airplane *a1;
int i = 0;
r1 = r;
cout << "Los vuelos que pasan por " << city << " son: " << endl;
while(r1){
if(!strcmp(r1->from,city) || !strcmp(r1->to,city)){
i = 1;
a1 = r1->a;
showonlyroute(r1);
while(a1){
showonlyairplane(a1);
a1 = a1->next;
}
}
r1 = r1->next;
}
if(!i){
cout << "\tNinguno." << endl;
}
}
//8. Todos los vuelos de un día.
void flight::showbyday(int day, int month, int year){
route *r1;
airplane *a1;
int i = 0;
r1 = r;
cout << "Los vuelos para el dia " << day << "/" << month << "/" << year << " son: " << endl;
while(r1){
if(r1->day == day && r1->month == month && r1->year == year){
i = 1;
showonlyroute(r1);
a1 = r1->a;
while(a1){
showonlyairplane(a1);
a1 = a1->next;
}
}
r1 = r1->next;
}
if(!i){
cout << "\tNo existen vuelos para el dia indicado." << endl;
}
}
//9. Comprar un boleto de avión.
void flight::buyticket(){
route *r1;
airplane *a1;
char rcode[10], code[10], from[10], to[10];
int i, as;
cout << "Ingrese ciudad de salida del vuelo (Siglas 'SCL'): "<< endl;
cin >> from;
cout << "Ingrese ciudad de llegada del vuelo (Siglas 'SCL'): "<< endl;
cin >> to;
int is = isin(from, to);
if(is){
show_route(from, to);
cout << "Ingrese el codigo de la ruta elegida: ";
cin >> rcode;
cout << "Ingrese el codigo del vuelo elegido: ";
cin >> code;
r1 = r;
while(strcmp(r1->rcode,rcode)){
r1 = r1->next;
}
a1 = r1->a;
while(strcmp(a1->code,code)){
a1 = a1->next;
}
cout << "Para comprar asiento en clase turista, ingrese '1'." << endl;
cout << "Para comprar asiento en clase ejecutiva, ingrese '2'." << endl;
cin >> i;
switch(i){
case 1 :
cout << "Quedan " << a1->tur << " asientos disponibles. \nIngrese el numero de asientos que desea comprar:" << endl;
cin >> as;
a1->tur -= as;
break;
case 2 :
cout << "Quedan " << a1->first << " asientos disponibles. \nIngrese el numero de asientos que desea comprar:" << endl;
cin >> as;
a1->first -=as;
break;
}
cout << "¡Su compra se ha realizado con exito! A continuacion aparecera la informacion completa de la ruta y vuelo elegido.\n¡Que tenga un excelente viaje! " << endl;
showonlyroute(r1);
showonlyairplane(a1);
cout << "De este vuelo usted tiene " << as << " asientos en ";
if(i){
cout << "clase turista." << endl;
}
else{
cout << "clase ejecutiva." << endl;
}
}
else{
cout << "La ruta que eligio no se encuentra en nuestra base de datos.\n Intente nuevamente." << endl;
}
}
//10. Devolver el boleto de avión.
void flight::returnticket(char rcode[10], char code[10], int i, int s){
route *r1;
airplane *a1;
r1 = r;
while(strcmp(r1->rcode,rcode)){
r1 = r1->next;
}
a1 = r1->a;
while(strcmp(a1->code,code)){
a1 = a1->next;
}
if(i){
a1->tur -= s;
}
else{
a1->first -=s;
}
cout << "¡La devolucion se ha completado con exito!" << endl;
}
//11. Vuelo con más asientos disponibles.
void flight::lessfull(){
route *r1, *r2;
airplane *a1, *a2;
int i = 0, s;
r1 = r;
while(r1){
a1 = r1->a;
while(a1){
s = a1->tur + a1->first;
if(s > i){
i = s;
r2 = r1;
a2 = a1;
}
a1 = a1->next;
}
r1 = r1->next;
}
cout << "Vuelo con mas asientos disponibles corresponde a: " << endl;
showonlyroute(r2);
showonlyairplane(a2);
}
//12. Vuelo más largo.
void flight::longestflight(){
route *r1, *r2;
airplane *a1, *a2;
int i, j;
r1 = r;
a1 = r1->a;
i = duration(a1);
while(r1){
while(a1){
j = duration(a1);
if(j > i){
i = j;
a2 = a1;
r2 = r1;
}
a1 = a1->next;
}
r1 = r1->next;
}
cout << "El vuelo mas largo con " << i/100 << ":" << i%100 << " hrs de vuelo corresponde a: " << endl;
showonlyroute(r2);
showonlyairplane(a2);
}
//13. Vuelo más corto.
void flight::shortestflight(){
route *r1, *r2;
airplane *a1, *a2;
int i, j;
r1 = r;
a1 = r1->a;
i = duration(a1);
while(r1){
while(a1){
j = duration(a1);
if(j < i){
i = j;
a2 = a1;
r2 = r1;
}
a1 = a1->next;
}
r1 = r1->next;
}
cout << "El vuelo mas corto con " << i/100 << ":" << i%100 << " hrs de vuelo corresponde a: " << endl;
showonlyroute(r2);
showonlyairplane(a2);
}
//14. Mostrar todos los datos
void flight::show_all(){
route *r1;
airplane *a1;
r1 = r;
while(r1){
showonlyroute(r1);
a1 = r1->a;
while(a1){
showonlyairplane(a1);
a1 = a1->next;
}
r1 = r1->next;
}
}
//Función menú.
void menu(flight F){
int op;
do
{
cout <<"\n\t\tMENU"<<endl;
cout <<"\t 1. Mostrar todos los vuelos de una ruta."<<endl;
cout <<"\t 2. Mostrar vuelo mas cercano a despegar."<<endl;
cout <<"\t 3. Mostrar vuelo mas lejano a despegar."<<endl;
cout <<"\t 4. Mostrar vuelos que pasan por una ciudad."<<endl;
cout <<"\t 5. Mostrar todos los vuelos de un dia."<<endl;
cout <<"\t 6. Comprar boleto."<<endl;
cout <<"\t 7. Devolver boleto."<<endl;
cout <<"\t 8. Mostrar vuelo con mas asientos disponibles."<<endl;
cout <<"\t 9. Mostrar vuelo de mas larga duracion."<<endl;
cout <<"\t 10. Mostrar vuelo de mas corta duracion"<<endl;
cout <<"\t 11. Mostrar todos los vuelos."<<endl;
cout <<"\t 12. Salir."<<endl;
cout <<" Opcion: ";
cin >> op;
switch(op)
{ case 1 :
char from[10], to[10];
cout << "Ingrese ciudad de origen (Siglas 'SCL')." << endl;
cin >> from;
cout << "Ingrese ciudad de destino (Siglas 'SCL')." << endl;
cin >> to;
F.show_route(from, to);
cout << endl<<"Presione una tecla para continuar"; cin.get();
break;
case 2 :
F.closest_flight();
cout << endl<<"Presione una t.ecla para continuar"; cin.get();
break;
case 3 :
F.farthestflight();
cout <<endl<< "Presione una tecla para continuar"; cin.get();
break;
case 4:
char city[10];
cout << "Ingrese ciudad a buscar (Siglas 'SCL')." << endl;
cin >> city;
F.incity(city);
cout <<endl<< "Presione una tecla para continuar"; cin.get();
break;
case 5:
int day, month, year;
cout << "Ingrese día (DD):" << endl;
cin >>day;
cout << "Ingrese mes (MM):" << endl;
cin >>month;
cout << "Ingrese anno (YYYY):" << endl;
cin >>year;
F.showbyday(day, month, year);
cout <<endl<< "Presione una tecla para continuar"; cin.get();
break;
case 6:
F.buyticket();
cout <<endl<< "Presione una tecla para continuar"; cin.get();
break;
case 7:
char rcode[10], code[10];
int i, s;
cout << "Ingrese codigo de ruta:" << endl;
cin >>rcode;
cout << "Ingrese codigo del vuelo:" << endl;
cin >>code;
cout << "Ingrese '1' si su ticket corresponde a clase turista y '2' si corresponde a clase ejecutiva." << endl;
cin >>i;
cout << "Ingrese numero de asientos que desea devolver." << endl;
cin >>s;
F.returnticket(rcode, code, i, s);
cout <<endl<< "Presione una tecla para continuar"; cin.get();
break;
case 8:
F.lessfull();
cout <<endl<< "Presione una tecla para continuar"; cin.get();
break;
case 9:
F.longestflight();
cout <<endl<< "Presione una tecla para continuar"; cin.get();
break;
case 10:
F.shortestflight();
cout <<endl<< "Presione una tecla para continuar"; cin.get();
break;
case 11:
F.show_all();
cout <<endl<< "Presione una tecla para continuar"; cin.get();
break;
}
}while(op!=12);
}
int main(){
route r;
airplane a;
flight F;
int cont;
ifstream f1;
f1.open("vuelos1.txt", ios::binary);
f1.read((char *)(&r),sizeof(route));
while(!f1.eof()){
F.in(r.rcode, r.from, r.to, r.day, r.month, r.year, r.quan);
cont = 0;
while(cont < r.quan){
f1.read((char *)(&a),sizeof(airplane));
F.subin(r.rcode, a.code, a.model, a.first, a.tur, a.serv, a.depa, a.arri);
cont++;
}
f1.read((char *)(&r),sizeof(route));
}
f1.close();
menu(F);
char rcode[10];
cout << "Ingrese codigo de ruta que desea eliminar: " << endl;
cin >> rcode;
F.out(rcode);
menu(F);
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment