lista con ficheros

9
UNIVERSIDAD DE LAS FUERZAS ARMADAS ESPE ESTRUCTURA DE DATOS NOMBRE : ROGER LUJE NRC : FECHA : 15-06-2015 1. Realizar un programa en C++ de una lista simple de empleados manejando archivos.con las siguientes funciones a. Ingreso() b. Ver() c. Modificar() d. Eliminar e. Cargar(); //CODIGO // // main.cpp // C++FicheroEmpleado // // Created by Roger on 14/6/15. // Copyright (c) 2015 Roger. All rights reserved. // #include <string> #include <fstream> #include <iostream> using namespace std; struct Fecha { int aa,mm,dd; }; class Empleado { public: Fecha f; int codigo; string nombre; float sueldo; Empleado *sig; public: Empleado(int cod,string nom,float suel,Fecha naci,Empleado *siguiente=NULL) { codigo=cod; nombre=nom; sueldo=suel; f=naci;

Upload: roger-destructivo-pm

Post on 11-Sep-2015

212 views

Category:

Documents


0 download

DESCRIPTION

empleo de fstream en c++

TRANSCRIPT

UNIVERSIDAD DE LAS FUERZAS ARMADASESPEESTRUCTURA DE DATOSNOMBRE:ROGER LUJENRC:FECHA:15-06-2015

1. Realizar un programa en C++ de una lista simple de empleados manejando archivos.con las siguientes funcionesa. Ingreso()b. Ver()c. Modificar()d. Eliminare. Cargar();

//CODIGO

//// main.cpp// C++FicheroEmpleado//// Created by Roger on 14/6/15.// Copyright (c) 2015 Roger. All rights reserved.//#include #include #include using namespace std;struct Fecha { int aa,mm,dd;};

class Empleado {public: Fecha f; int codigo; string nombre; float sueldo; Empleado *sig; public: Empleado(int cod,string nom,float suel,Fecha naci,Empleado *siguiente=NULL) { codigo=cod; nombre=nom; sueldo=suel; f=naci; sig=siguiente; } void AddNodo(int cod,string nom,float suel,Fecha naci); void PrintNodo(void); void Modificar(int cod,string auxNombre); void Delete(int cod);};

Empleado *cabeza=NULL;void Empleado::Modificar(int cod,string auxNombre){ if (cabeza!=NULL) { Empleado *aux; aux=cabeza;

while (aux!=NULL) { if (aux->codigo==cod) { aux->nombre=auxNombre; } aux=aux->sig; } }}void Empleado::AddNodo(int cod, string nom,float suel,Fecha naci) //METODO{

Empleado *aux,*nuevo; nuevo=new Empleado(cod,nom,suel,naci); nuevo->sig=NULL; aux=cabeza; if (aux==NULL) { cabeza=nuevo;

} else { while (aux->sig!=NULL) { aux=aux->sig; } aux->sig=nuevo;

}

}void Empleado::PrintNodo(void){ Empleado *aux; aux=cabeza; int i=1; //char c[]=" "; printf("\nLista generada......\n"); coutf.aa,aux->f.mm,aux->f.dd); aux=aux->sig; i++; } printf("\n===============================================================\n");}void Empleado::Delete(int cod){ if (cabeza!=NULL) { Empleado *borrar; Empleado *anterior; borrar=cabeza; anterior=NULL; while (borrar!=NULL&&borrar->codigo!=cod) { anterior=borrar; borrar=borrar->sig; } if (borrar==NULL) { printf("\nNodo no encontrado..."); }else if(anterior==NULL){ cabeza=cabeza->sig; free(borrar); }else{ anterior->sig=borrar->sig; free(borrar); } }}

void CargarDatos(){ fstream lectura; Fecha f; string nombre,linea; float sueldo; int codigo; char s='/'; lectura.open("Empleados.txt",ios::in|ios::out); if (lectura.fail()) { cout>nombre>>sueldo>>f.aa>>s>>f.mm>>s>>f.dd; //printf("\n%d %s %.2f %d/%d/%d",codigo,nombre.c_str(),sueldo,f.aa,f.mm,f.dd); cabeza->AddNodo(codigo, nombre, sueldo, f); } } lectura.close();}

void AddNodoFichero(int cod, string nom,float suel,Fecha f1) //F. EXTERNA{ fstream Empleados; Empleados.open("Empleados.txt",ios::app|ios::out); Empleados