exercícios da aula de lab 03

12
Programação Estruturada II EXERCÍCIOS DA AULA DE LAB 03 2015.1 Prof. Thomás da Costa [email protected]

Upload: thomas-da-costa

Post on 22-Jul-2015

121 views

Category:

Technology


0 download

TRANSCRIPT

Page 1: Exercícios da Aula de LAB 03

Programação Estruturada II

EXERCÍCIOS DA AULA DE LAB 03 – 2015.1

Prof. Thomás da Costa

[email protected]

Page 2: Exercícios da Aula de LAB 03

1) Explique a funcionalidade do programa abaixo:

#include <iostream> #include <fstream> #include <strings.h> using namespace std; void gravar_arquivo(); int main() { gravar_arquivo(); } void gravar_arquivo() { string valor; valor = "Ola Mundo"; ofstream ofs; ofs.open("ola_mundo.txt", ios::out); ofs << valor; ofs.close(); }

Resposta:

_______________________________________________________________________________________________

_______________________________________________________________________________________________

_______________________________________________________________________________________________

_______________________________________________________________________________________________

_______________________________________________________________________________________________

_______________________________________________________________________________________________

Page 3: Exercícios da Aula de LAB 03

2) Explique a funcionalidade do programa abaixo:

#include <iostream> #include <fstream> #include <strings.h> using namespace std; void ler_arquivo(); int main() { ler_arquivo(); } void ler_arquivo() { string valor; ifstream ifs; ifs.open("ola_mundo.txt", ios::in); if (!ifs.is_open()) { cout << "Não foi possivel abrir o arquivo" << endl; return; } getline(ifs, valor); cout << "Conteúdo do Arquivo:" << valor << endl; ifs.close(); }

Resposta:

_______________________________________________________________________________________________

_______________________________________________________________________________________________

_______________________________________________________________________________________________

_______________________________________________________________________________________________

_______________________________________________________________________________________________

_______________________________________________________________________________________________

Page 4: Exercícios da Aula de LAB 03

3) Explique a funcionalidade do programa abaixo:

#include <iostream> #include <fstream> #include <strings.h> using namespace std; void gravar_arquivo(); int main() { gravar_arquivo(); } void gravar_arquivo() { int valor_1; double valor_2; float valor_3; char valor_4; cin >> valor_1; cin >> valor_2; cin >> valor_3; cin >> valor_4; ofstream ofs; ofs.open("numeros.txt", ios::out); ofs << valor_1 << endl; ofs << valor_2 << endl; ofs << valor_3 << endl; ofs << valor_4 << endl; ofs.close(); }

Resposta:

_______________________________________________________________________________________________

_______________________________________________________________________________________________

_______________________________________________________________________________________________

_______________________________________________________________________________________________

_______________________________________________________________________________________________

_______________________________________________________________________________________________

Page 5: Exercícios da Aula de LAB 03

4) Explique a funcionalidade do programa abaixo:

#include <iostream> #include <fstream> #include <strings.h> using namespace std; void ler_arquivo(); int main() { ler_arquivo(); } void ler_arquivo() { int valor_1; double valor_2; float valor_3; char valor_4; ifstream ifs; ifs.open("numeros.txt", ios::in); if (!ifs.is_open()) { cout << "Não foi possivel abrir o arquivo" << endl; return; } ifs >> valor_1; ifs >> valor_2; ifs >> valor_3; ifs >> valor_4; cout << "Valores do arquivo:" << valor_1 << "," << valor_2 << "," << valor_3 << "," << valor_4; ifs.close(); }

Resposta:

_______________________________________________________________________________________________

_______________________________________________________________________________________________

_______________________________________________________________________________________________

_______________________________________________________________________________________________

_______________________________________________________________________________________________

_______________________________________________________________________________________________

Page 6: Exercícios da Aula de LAB 03

5) Explique a funcionalidade do programa abaixo:

#include <iostream> #include <fstream> #include <strings.h> using namespace std; void gravar_fim_arquivo(); int main() { gravar_fim_arquivo(); } void gravar_fim_arquivo() { string valor; valor = "Nova linha no arquivo"; ofstream ofs; ofs.open("arquivo_linhas.txt", ios::out | ios::app); ofs << valor << endl; ofs.close(); }

Resposta:

_______________________________________________________________________________________________

_______________________________________________________________________________________________

_______________________________________________________________________________________________

_______________________________________________________________________________________________

_______________________________________________________________________________________________

_______________________________________________________________________________________________

Page 7: Exercícios da Aula de LAB 03

6) Explique a funcionalidade do programa abaixo:

#include <iostream> #include <fstream> #include <strings.h> using namespace std; void gravar_arquivo(); int main() { gravar_arquivo(); } void gravar_arquivo() { string valor; ofstream ofs; ofs.open("arquivo_linhas.txt", ios::out); valor = "O que são funções:"; ofs << valor << endl; valor = "São rotinas que tem como objetivo, " "executar trechos de códigos de forma modular, " "melhorando a organização do programa e evitando repetição de código."; ofs << valor << endl; valor = "As funções são reutilizáveis dentro de um programa."; ofs << valor << endl; ofs.close(); }

Resposta:

_______________________________________________________________________________________________

_______________________________________________________________________________________________

_______________________________________________________________________________________________

_______________________________________________________________________________________________

_______________________________________________________________________________________________

_______________________________________________________________________________________________

Page 8: Exercícios da Aula de LAB 03

7) Explique a funcionalidade do programa abaixo:

#include <iostream> #include <fstream> #include <strings.h> using namespace std; void ler_arquivo(); int main() { ler_arquivo(); } void ler_arquivo() { string linha; ifstream ifs; ifs.open("arquivo_linhas.txt", ios::in); if (!ifs.is_open()) { cout << "Não foi possivel abrir o arquivo" << endl; return; } while (getline(ifs, linha)) { cout << linha << endl; } ifs.close(); }

Resposta:

_______________________________________________________________________________________________

_______________________________________________________________________________________________

_______________________________________________________________________________________________

_______________________________________________________________________________________________

_______________________________________________________________________________________________

_______________________________________________________________________________________________

Page 9: Exercícios da Aula de LAB 03

8) Explique a funcionalidade do programa abaixo:

#include <iostream> #include <fstream> #include <strings.h> #include <limits> using namespace std; struct alunos { long id; char nome[255]; char ra[30]; int idade; }; void gravar_arquivo_registros(); int main() { gravar_arquivo_registros(); } void gravar_arquivo_registros() { alunos alu; cout << "Digite o nome do aluno:"; gets(alu.nome); cout << "Digite o RA do aluno:"; gets(alu.ra); cout << "Digite a idade do aluno:"; cin >> alu.idade; fstream fst; fst.open("registros.dat", ios::in | ios::out | ios::app | ios::binary); if (!fst.is_open()) { cout << "Não foi possivel abrir o arquivo" << endl; return; } fst.write((char *)&alu, sizeof(alu)); fst.close(); }

Resposta:

_______________________________________________________________________________________________

_______________________________________________________________________________________________

_______________________________________________________________________________________________

_______________________________________________________________________________________________

_______________________________________________________________________________________________

_______________________________________________________________________________________________

Page 10: Exercícios da Aula de LAB 03

9) Explique a funcionalidade do programa abaixo:

#include <iostream> #include <fstream> #include <strings.h> #include <limits> using namespace std; struct alunos { long id; char nome[255]; char ra[30]; int idade; }; void ler_arquivo_registros(); int main() { ler_arquivo_registros(); } void ler_arquivo_registros() { alunos alu; fstream fst; fst.open("registros.dat", ios::in | ios::out | ios::app | ios::binary); if (!fst.is_open()) { cout << "Não foi possivel abrir o arquivo"; return; } while (fst.read((char *)&alu,sizeof(alu))) { cout << "************************" << endl; cout << "Nome:" << alu.nome << endl; cout << "RA:" << alu.ra << endl; cout << "Idade:" << alu.idade << endl; } fst.close(); }

Resposta:

_______________________________________________________________________________________________

_______________________________________________________________________________________________

_______________________________________________________________________________________________

_______________________________________________________________________________________________

_______________________________________________________________________________________________

_______________________________________________________________________________________________

Page 11: Exercícios da Aula de LAB 03

10) Explique a funcionalidade do programa abaixo:

#include <iostream> #include <fstream> #include <strings.h> #include <limits> using namespace std; struct alunos { long id; char nome[255]; char ra[30]; int idade; }; void alterar_registros(); int main() { alterar_registros(); } void alterar_registros() { alunos alu; char ra[30]; char nome[255]; cout << "Digite o RA:" << endl; gets(ra); cout << "Digite o novo nome do aluno:" << endl; gets(nome); fstream fst; fst.open("registros.dat", ios::in | ios::out | ios::binary); if (!fst.is_open()) { cout << "Não foi possivel abrir o arquivo"; return; } int posicao; while (fst.read((char *)&alu,sizeof(alu))) { posicao++; if (strcmp(ra, alu.ra) == 0) { strcpy(alu.nome, nome); break; } } fst.seekp((posicao - 1) * sizeof(alu)); fst.write((char *)&alu,sizeof(alu)); fst.close(); }

Page 12: Exercícios da Aula de LAB 03

Resposta:

_______________________________________________________________________________________________

_______________________________________________________________________________________________

_______________________________________________________________________________________________

_______________________________________________________________________________________________

_______________________________________________________________________________________________

_______________________________________________________________________________________________