Problema 1a
Código:
#include <iomanip>
#include <iostream>
#include <fstream>
using namespace std;
unsigned int find_length(char *string) {
unsigned int count = 0;
while (*string)
count++, string++;
return count;}
int main(void)
{ fstream file;
file.open("empleado.dat", fstream::out);
char *stuff[5][5] = {
{"Anthony", "A", "10031", "7.82", "12/18/05"},
{"Burrows", "w", "10067", "9.14", "06/09/04"},
{"Fain", "B", "10083", "8.79", "05/18/04"},
{"Janney", "P", "10095", "10.57", "09/28/04"},
{"Smith", "G", "10105", "8.50", "12/20/03"} };
unsigned int length[5] = {0,0,0,0,0};
for (int j = 0; j < 5; j++)
for (int i = 0; i < 5; i++) {
unsigned int len;
if ( (len = find_length(stuff[i][j])) > length[j])
length[j] = len; }
for (int i = 0; i < 5; i++) {
for (int j = 0; j < 5; j++)
file << setw(length[j] + 1) << stuff[i][j];
file << endl; }
file.close();
return 0;}
Solución:
Problema 1b
Código:
#include <iostream>
#include <fstream>
using namespace std;
int main(void) {
fstream r_file, w_file;
r_file.open("empleado.dat", fstream::in);
w_file.open("empleado.bak", fstream::out);
char c;
while (true) {
c = r_file.get();
if (r_file.eof())
break;
else
w_file << c;}
r_file.close();
w_file.close();
return 0;}
Solución:
Problema 1c
Código:
#include <iostream>
#include <fstream>
using namespace std;
int main(void) {
char *r_name, *w_name;
cout << "como se llama el archivo original? ";
cin >> r_name;
cout << "como se llamara la copia de dicho archivo? ";
cin >> w_name;
fstream r_file, w_file;
r_file.open(r_name, fstream::in);
w_file.open(w_name, fstream::out);
char *buff;
r_file >> buff;
w_file << buff;
r_file.close();
w_file.close();
return 0;}
Solución:
Problema 2
Código:
#include <iomanip>
#include <iostream>
#include <fstream>
using namespace std;
int main(int argc, char *argv[]) {
// char *filename;
// cout << "Que archivo deseas abrir?: ";
// cin >> filename;
fstream file;
file.open(argv[1], fstream::in);
char c;
int i = 0;
bool startline = true;
while (true) {
c = (char)file.get();
if (file.eof())
break;
if (startline) {
cout << setw(3) << ++i << "| ";
startline = false;}
else
cout << c;
if (c == '\n')
startline = true;}
file.close();
return 0;}
Solución:
Problema 3a
Código:
#include <iomanip>
#include <iostream>
#include <fstream>
using namespace std;
unsigned int find_length(char *string) {
unsigned int count = 0;
while (*string)
count++, string++;
return count;}
int main(void) {
fstream file;
file.open("archivo.txt", fstream::out);
char *stuff[5][5] = {
{"B", "Caldwell", "555-88-2222", "7.32", "37"},
{"D", "Memcheck", "555-77-4444", "8.32", "40"},
{"R", "Potter", "555-77-6666", "6.54", "40"},
{"W", "Rosen", "555-99-8888", "9.80", "35"},
{"A", "Smith", "555-99-9999", "8.89", "36"}};
unsigned int length[5] = {0,0,0,0,0};
for (int j = 0; j < 5; j++)
for (int i = 0; i < 5; i++) {
unsigned int len;
if ( (len = find_length(stuff[i][j])) > length[j])
length[j] = len;}
for (int i = 0; i < 5; i++) {
for (int j = 0; j < 5; j++)
file << setw(length[j] + 1) << stuff[i][j];
file << endl;}
file.close();
return 0;}
Solución
Problema 3b
Código:
#include <iomanip>
#include <iostream>
#include <fstream>
using namespace std;
void to_next(fstream file) {
char c;
do
if (file.eof())
return;
else
c = file.get();
while (c != ' ' && c != '\t');
do
c = file.get();
while (c == ' ' && c == '\t' && !file.eof());}
unsigned int find_length(char *string) {
unsigned int count = 0;
while (*string)
count++, string++;
return count;}
int main(void) {
fstream file;file.open("archivo.txt", fstream::in);
struct {char initial;char name[20];char ssn[20];
float pph;int hours;} data;
file.ignore(256, ' ');
for (int i = 0; i < 5; i++) {
file >> data.initial>> data.name>> data.ssn>> data.pph>> data.hours;
cout << data.ssn << " "<< data.initial << " "<< data.name << " ";
//int count = find_length(data.name);
//for (int i = 0; i < 10 - count; i++)
// cout << "-";
cout << data.pph * data.hours<< endl;
//cout << "length is " << count << endl;}
return 0;}
Solución:






No hay comentarios:
Publicar un comentario