miércoles, 5 de junio de 2013
lunes, 3 de junio de 2013
Practica 7
Practica 7
Problema 1a
Codigo:
#include <fstream>
#include <conio.h>
#include <iomanip>
#include <string.h>
#include <stdio.h>
void agregar_datos_file()
{
ofstream f1;
char nombre [20], categoria, sigue, fecha[10];
int numemp;
float pago;
f1.open ("C://Datos//empleados.dat",ios::app);
do
{
cout<<"Ingese la informacion a almacenar en: empleados.dat\n"<<endl;
cout<<"Nombre: ";
gets(nombre);
cout<<"Categoria: ";
cin>>categoria;
cout<<"No. de empleado: ";
cin>>numemp;
cout<<"Tarifa por hora: ";
cin>>pago;
cout<<"Fecha de ingreso: ";
cin>>fecha;
f1<<nombre<<" "<<categoria<<" "<<numemp<<" "<<pago<<" "<<fecha<<endl;
cout<<"Registrar otro empleado? (s/n) ";
cin>>sigue;
clrscr();
}
while (sigue=='s'||sigue=='S');
cout<<"\nArchivo creado correctamente."<<endl;
f1.close();
}
int main ()
{
agregar_datos_file();
getch ();
return 0;
}
Programa:
Problema 1b
Codigo:
#include <fstream.h>
#include <conio.h>
#include <iomanip.h>
#include <string.h>
#include <stdio.h>
void show_data_file()
{
ifstream f1;
f1.open ("c://Prog//empleados1.dat");
char nombre [15], tipo, fecha[8];
int numemp;
float pago;
cout<<"Empleado Categoria No. de empleado Tarifa por hora Fecha de ingreso"<<endl<<endl;
f1>>nombre>>tipo>>numemp>>pago>>fecha;
do
{
cout<<nombre<<"\t\t\t"<<tipo<<"\t"<<numemp<<"\t\t"<<pago<<"\t\t"<<fecha<<endl;
f1>>nombre>>tipo>>numemp>>pago>>fecha;
}
while (!f1.eof());
cout<<"\nPresione 'enter' para copiar los datos..."<<endl;
getch();
}
void copy_data_file()
{
ifstream f1;
f1.open ("C://Prog//empleados1.dat");
ofstream f2;
f2.open ("C://Prog//empleados.bak");
char nombre [15], tipo, fecha[8];
int numemp;
float pago;
f1>>nombre>>tipo>>numemp>>pago>>fecha;
do
{
f1>>nombre>>tipo>>numemp>>pago>>fecha;
f2<<nombre<<" "<<tipo<<" "<<numemp<<" "<<pago<<" "<<fecha<<endl;
}
while (!f1.eof());
}
int main()
{
show_data_file();
copy_data_file();
cout<<"\nLos datos del archivo 'empleados.dat' se copiaron exitosamente"<<endl;
cout<<"al archivo 'empleados.bak'"<<endl<<endl;
getch();
}
Programa:
Problema 2a
Codigo:
#include <fstream.h>
#include <conio.h>
#include <iomanip.h>
#include <string.h>
#include <stdio.h>
#include <iomanip.h>
void show_data_file()
{
ifstream f1;
f1.open ("c://Prog//empleados1.dat");
char nombre [15], tipo, fecha[8];
int numemp, i=0;
float pago;
cout<<"Empleado Categoria No. de empleado Tarifa por hora Fecha de ingreso"<<endl<<endl;
do
{
f1>>nombre>>tipo>>numemp>>pago>>fecha;
cout<<(i+1)<<" "<<nombre<<setw(6)<<"\t\t "<<tipo<<"\t "<<numemp<<"\t\t "<<pago<<" \t\t"<<fecha<<endl;
}
while (!f1.eof()&&(++i));
getch();
}
int main()
{
show_data_file();
getch();
}
Programa:
Problema 2b
Codigo:
/*Practica 7 ejercicio 2b Fecha: 22/05/13
Programa que imprime los datos almacenados en un archivo
Nota: Para compilar utilizamos Devc++ 5.3.0.4 ya que no funciono en borland
no pudimos probar la funcion prn ya que esta solo utiliza impresoras
por puerto paralelo, no contamos con ninguna de este tipo para
probar el programa.*/
#include <windows.h> #include <fstream> #include <string> #include <iostream> #include <conio.h> #include <iomanip>
using namespace std;
void show_data_file() { std::ifstream f1;
f1.open ("d://empleados.dat"); char nombre [15], tipo, fecha[8]; int numemp, i=0; float pago; std::cout<<"Empleado Categoria No. de empleado Pago/hora Fecha de ingreso"<<std::endl<<std::endl; do { f1>>nombre>>tipo>>numemp>>pago>>fecha; std::cout<<(i+1)<<" "<<nombre<<std::setw(6)<<"\t\t "<<tipo<<"\t "<<numemp<<"\t\t "<<std::setw(5)<<pago<<" \t\t"<<fecha<<std::endl; } while (!f1.eof()&&(++i)); std::cout<<"\nPresione una tecla para iniciar la impresion."<<std::endl; getch(); }
typedef std::basic_ifstream<TCHAR> tifstream; typedef std::basic_string<TCHAR> tstring; void Outtextxy(HDC hdc,int x,int y,tstring Msg) { TextOut(hdc,x,y,Msg.c_str(),static_cast<int>(Msg.length())); } void ShowError(tstring strMsg) { MessageBox (NULL,strMsg.c_str(),TEXT("Imprimir"),MB_ICONERROR ); exit(1); } void ShowInformation(tstring strText) { MessageBox (NULL,strText.c_str(),TEXT("Imprimir"),MB_ICONINFORMATION); } void PrintFile(tifstream& f) { PRINTDLG pd; DOCINFO di; tstring strLine; int y=300; memset (&pd, 0, sizeof(PRINTDLG)); memset (&di, 0, sizeof(DOCINFO)); di.cbSize = sizeof(DOCINFO); di.lpszDocName = TEXT("Imprimiendo"); pd.lStructSize = sizeof(PRINTDLG); pd.Flags = PD_PAGENUMS | PD_RETURNDC; pd.nFromPage = 1; pd.nToPage = 1; pd.nMinPage = 1; pd.nMaxPage = 0xFFFF;
if(f.fail()) ShowError(TEXT("Error el archivo no se pudo abrir para lectura"));
if (PrintDlg (&pd)){ if (pd.hDC){ if (StartDoc (pd.hDC, &di) != SP_ERROR){ cout << "\nImprimiendo...\nEspere un momento" << endl; StartPage (pd.hDC); while(!f.eof()){ getline(f,strLine); Outtextxy(pd.hDC,500,y,strLine.c_str()); y+=100; } EndPage (pd.hDC); EndDoc (pd.hDC); } else ShowError(TEXT("Error: No se pudo comenzar a imprimir.")); } else ShowError(TEXT("Error: No se pudo crear el contexto de dispositivo")); } else ShowInformation(TEXT("Se cancelo la impresion"));
ShowInformation(TEXT("Termino la impresion correctamente.")); }
int main(int argc,char *argv[]) { std::cout<<"Se imprimira el archivo 'empleados.dat' con la suiguiente informacion"<<std::endl<<std::endl; show_data_file(); tifstream in(TEXT("D://empleados.dat")); PrintFile(in); in.close(); return 0; }
no pudimos probar la funcion prn ya que esta solo utiliza impresoras
por puerto paralelo, no contamos con ninguna de este tipo para
probar el programa.*/
#include <windows.h> #include <fstream> #include <string> #include <iostream> #include <conio.h> #include <iomanip>
using namespace std;
void show_data_file() { std::ifstream f1;
f1.open ("d://empleados.dat"); char nombre [15], tipo, fecha[8]; int numemp, i=0; float pago; std::cout<<"Empleado Categoria No. de empleado Pago/hora Fecha de ingreso"<<std::endl<<std::endl; do { f1>>nombre>>tipo>>numemp>>pago>>fecha; std::cout<<(i+1)<<" "<<nombre<<std::setw(6)<<"\t\t "<<tipo<<"\t "<<numemp<<"\t\t "<<std::setw(5)<<pago<<" \t\t"<<fecha<<std::endl; } while (!f1.eof()&&(++i)); std::cout<<"\nPresione una tecla para iniciar la impresion."<<std::endl; getch(); }
typedef std::basic_ifstream<TCHAR> tifstream; typedef std::basic_string<TCHAR> tstring; void Outtextxy(HDC hdc,int x,int y,tstring Msg) { TextOut(hdc,x,y,Msg.c_str(),static_cast<int>(Msg.length())); } void ShowError(tstring strMsg) { MessageBox (NULL,strMsg.c_str(),TEXT("Imprimir"),MB_ICONERROR ); exit(1); } void ShowInformation(tstring strText) { MessageBox (NULL,strText.c_str(),TEXT("Imprimir"),MB_ICONINFORMATION); } void PrintFile(tifstream& f) { PRINTDLG pd; DOCINFO di; tstring strLine; int y=300; memset (&pd, 0, sizeof(PRINTDLG)); memset (&di, 0, sizeof(DOCINFO)); di.cbSize = sizeof(DOCINFO); di.lpszDocName = TEXT("Imprimiendo"); pd.lStructSize = sizeof(PRINTDLG); pd.Flags = PD_PAGENUMS | PD_RETURNDC; pd.nFromPage = 1; pd.nToPage = 1; pd.nMinPage = 1; pd.nMaxPage = 0xFFFF;
if(f.fail()) ShowError(TEXT("Error el archivo no se pudo abrir para lectura"));
if (PrintDlg (&pd)){ if (pd.hDC){ if (StartDoc (pd.hDC, &di) != SP_ERROR){ cout << "\nImprimiendo...\nEspere un momento" << endl; StartPage (pd.hDC); while(!f.eof()){ getline(f,strLine); Outtextxy(pd.hDC,500,y,strLine.c_str()); y+=100; } EndPage (pd.hDC); EndDoc (pd.hDC); } else ShowError(TEXT("Error: No se pudo comenzar a imprimir.")); } else ShowError(TEXT("Error: No se pudo crear el contexto de dispositivo")); } else ShowInformation(TEXT("Se cancelo la impresion"));
ShowInformation(TEXT("Termino la impresion correctamente.")); }
int main(int argc,char *argv[]) { std::cout<<"Se imprimira el archivo 'empleados.dat' con la suiguiente informacion"<<std::endl<<std::endl; show_data_file(); tifstream in(TEXT("D://empleados.dat")); PrintFile(in); in.close(); return 0; }
Programa:

Problema 3a
Codigo:
#include <fstream.h>
#include <conio.h>
#include <iomanip.h>
#include <string.h>
#include <stdio.h>
void file_empleado()
{
ofstream f1;
f1.open("c://Prog//Archivo1.dat", ios::app);
char nombre [15],sigue;
char numemp[11];
float pago;
char fecha [8];
do
{
cout<<"Nombre del empleado : ";
cin.ignore();
cin.getline (nombre,15,'\n');
cout<<"Numero de seguro social : ";
cin>>numemp;
cout<<"Tarifa por hora : ";
cin>>pago;
cout<<"Horas trabajadas : ";
cin>>fecha;
f1<<nombre<<" "<<numemp<<" "<<pago<<" "<<fecha<<endl;
cout<<" Desea procesar otro empleado (s/n) :";
cin>>sigue;
}
while (sigue=='s'||sigue=='S');
f1.close();
}
void listado_datos_file()
{
ifstream entrada;
entrada.open ("c://Prog//Archivo1.dat");
char nombre[1], apellido[8], nosocl[12];
int htr;
float tph;
clrscr ();
entrada>>nombre>>apellido>>nosocl>>tph>>htr;
do
{
cout<<nombre<<" "<<apellido<<setw(3)<<"\t "<<nosocl<<"\t "<<tph<<"\t\t"<<htr<<endl;
entrada>>nombre>>apellido>>nosocl>>tph>>htr;
}
while (!entrada.eof());
getch();
}
int menu()
{
int opcion;
cout<<"1) Agregar datos del empleado \n";
cout<<"2) Listado de empleados \n";
cout<<"3) Fin de programa \n";
cout<<"4) Elige opcion ==> \n";
cin>>opcion;
clrscr();
return opcion;
}
void main()
{
int opc;
do
{
clrscr();
opc=menu();
switch(opc)
{
case 1: file_empleado();
break;
case 2: listado_datos_file();
break;
case 3:
clrscr();
cout<<"\n\nFin del programa \n\n";
getch();
break;
default: cout<<"Error en opcion";
}
}
while(opc !=3);
}
Solucion:
Problema 3b
Codigo:
#include <fstream.h>
#include <conio.h>
#include <iomanip.h>
#include <string.h>
#include <stdio.h>
void listado_datos_file()
{
ifstream entrada;
entrada.open ("c://Prog//Archivo1.dat");
char nombre[1], apellido[8], nosocl[12];
int htr;
float tph;
clrscr ();
cout<<"Nombres \tNo.Seg. Socl. Tarifa/h Horas trabajadas Pago bruto\n"<<endl;
entrada>>nombre>>apellido>>nosocl>>tph>>htr;
do
{
cout<<nombre<<" "<<apellido<<setw(3)<<"\t "<<nosocl<<"\t "<<tph<<"\t\t"<<htr<<"\t | "<<(tph*htr)<<"\t|"<<endl;
entrada>>nombre>>apellido>>nosocl>>tph>>htr;
}
while (!entrada.eof());
getch();
}
int main()
{
listado_datos_file();
getch();
return 0;
}
Solucion:
Suscribirse a:
Entradas (Atom)





