This repository was archived by the owner on May 2, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPunto.cpp
More file actions
96 lines (77 loc) · 1.86 KB
/
Punto.cpp
File metadata and controls
96 lines (77 loc) · 1.86 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
/*
* File: Punto.cpp
* Author: Aybar
*
* Created on September 14, 2017, 1:14 PM
*/
#include "Punto.h"
#include <fstream>
#include <stdexcept>
#include <string>
using std::fstream;
using std::string;
Punto::Punto() : IOInterface("punto.txt"){
abscisa = 0;
ordenada = 0;
persist = std::vector<Punto>();
try {
leer();
} catch (string) {
}
}
Punto::Punto(const int x, const int y) {
abscisa = x;
ordenada = y;
}
Punto Punto::buscar(const unsigned x) {
try {
Punto p = persist.at(x);
return p;
} catch (const std::out_of_range&) {
string mensaje = "El elemento buscado no existe.";
throw mensaje;
}
}
bool Punto::alta(const int x, const int y) {
persist.push_back(Punto(x, y));
grabar();
return true;
}
bool Punto::baja(const unsigned pos) {
persist.erase(persist.begin() + pos);
grabar();
return true;
}
void Punto::leer() {
fstream archivo;
archivo.open(getNombreArchivo(), std::ios::in);
char pipe;
int x;
int y;
if (!archivo.fail()) {
persist.clear(); // Limpia elementos del vector.
while (archivo >> x) {
archivo >> pipe;
archivo >> y;
alta(x, y);
}
} else {
string mensaje = "El archivo no se abrio correctamente";
throw mensaje;
}
archivo.close();
}
// Escribir en el archivo.
void Punto::grabar() {
fstream archivo;
archivo.open(getNombreArchivo(), std::ios::out);
if (!archivo.fail()) {
for (auto punto : persist) {
archivo << punto.abscisa << "\t|\t" << punto.ordenada << "\r\n";
}
} else {
string mensaje = "El archivo no se abrio correctamente";
throw mensaje;
}
archivo.close();
}