Skip to content

Commit 0d91894

Browse files
authored
Add files via upload
1 parent 58c2e8a commit 0d91894

File tree

5 files changed

+367
-0
lines changed

5 files changed

+367
-0
lines changed

P9/E1/E1.cpp

Lines changed: 365 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,365 @@
1+
#include <cstdio>
2+
#include <cstdlib>
3+
#include <fstream>
4+
#include <iostream>
5+
using namespace std;
6+
7+
void borra(){
8+
#ifdef _WIN32
9+
system("cls");
10+
#else
11+
system("clear");
12+
#endif
13+
}
14+
15+
void pausa(){
16+
#ifdef _WIN32
17+
system("pause");
18+
#else
19+
std::cin.ignore(1024, '\n');
20+
std::cout<<"Presiona enter para continuar..."<<endl;
21+
std::cin.get();
22+
#endif
23+
}
24+
25+
bool es_valida_opcion_menu(string opcion_menu){
26+
for(int i=0; i<opcion_menu.size(); i++){
27+
if(isdigit(opcion_menu[i])!=true){
28+
return false;
29+
}
30+
}
31+
int aux;
32+
aux=stoi(opcion_menu);
33+
if(aux<1 || aux>6){
34+
return false;
35+
}
36+
return true;
37+
}
38+
39+
bool es_verdadero(string &DNI){
40+
if(DNI.size()!=9){return false;}
41+
for(int i=0; i<8; i++){
42+
if(isdigit(DNI[i])==false){return false;}
43+
}
44+
if(isdigit(DNI[8])==true){return false;}
45+
DNI[8]=toupper(DNI[8]);
46+
int numero=stoi(DNI);
47+
char letras[]="TRWAGMYFPDXBNJZSQVHLCKE";
48+
if(DNI[8]!=letras[numero%23]){return false;}
49+
return true;
50+
}
51+
52+
bool iguales(string DNI1, string DNI2){
53+
for(int i=0; i<DNI2.size(); i++){
54+
if(DNI1[i] != DNI2[i]){return false;}
55+
}
56+
return true;
57+
}
58+
59+
bool es_valida_edad(string edad){
60+
for(int i=0; i<edad.size(); i++){
61+
if(isdigit(edad[i])!=true){return false;}
62+
}
63+
int aux;
64+
aux=stoi(edad);
65+
if(aux<0){return false;}
66+
return true;
67+
}
68+
69+
bool es_valida_nota(string nota){
70+
for(int i=0; i<nota.size(); i++){
71+
if(isdigit(nota[i])!=true){return false;}
72+
}
73+
int aux;
74+
aux=stoi(nota);
75+
if(aux<0 || aux >10){return false;}
76+
return true;
77+
}
78+
79+
int main(int argc, char **argv){
80+
borra();
81+
string aux, opcion_menuaux, DNI, edad, nota,espacio=" ", buscar, reemplazar;
82+
int opcion_menu=0;
83+
if(argc !=2){
84+
cout<<"Error no se ha introducido el nombre del archivo"<<endl;
85+
return -1;
86+
}
87+
ofstream file;
88+
file.open(argv[1], ios::app);
89+
if(file.is_open()==false){
90+
cout<<"Error el fichero no se pudo abrir"<<endl;
91+
return -1;
92+
}
93+
if(file.is_open()==true){
94+
ifstream file;
95+
file.open(argv[1]);
96+
file >> aux;
97+
if(file.eof()==true){
98+
ofstream file;
99+
file.open(argv[1], ios::app);
100+
file<<" DNI Edad Nota"<<endl;
101+
}
102+
}
103+
do{
104+
cout<<"Bienvenido a su base de datos de alumnos seleccione lo que desea hacer:"<<endl;
105+
do{
106+
cout<<"Pulse 1 para añadir un alumno."<<endl;
107+
cout<<"Pulse 2 para imprimir los datos de un alumno."<<endl;
108+
cout<<"Pulse 3 para imprimir los datos de todos los alumnos."<<endl;
109+
cout<<"Pulse 4 para modificar los datos de un alumno."<<endl;
110+
cout<<"Pulse 5 para eliminar los datos un alumno."<<endl;
111+
cout<<"Pulse 6 para salir."<<endl;
112+
cin>>opcion_menuaux;
113+
if(es_valida_opcion_menu(opcion_menuaux)!=true){
114+
borra();
115+
cout<<"Error introduzca un caracter de selección valido"<<endl;
116+
}
117+
}while(es_valida_opcion_menu(opcion_menuaux)!=true);
118+
opcion_menu=stoi(opcion_menuaux);
119+
switch(opcion_menu){
120+
case 1:{
121+
borra();
122+
ifstream file;
123+
file.open(argv[1]);
124+
cout<<"Introduzca el dni del alumno o pulse 0 para salir"<<endl;
125+
do{
126+
cin>>DNI;
127+
borra();
128+
if(es_verdadero(DNI)==false && DNI!="0"){cout<<"Error inserte un DNI valido o pulse 0 para salir"<<endl;}
129+
}while(es_verdadero(DNI)!=true && DNI!="0");
130+
if(DNI=="0"){
131+
borra();
132+
pausa();
133+
}
134+
else{
135+
int i=-1;
136+
while(file.eof()==false && i<0){
137+
file >> aux;
138+
if(iguales(aux, DNI)==true){i++;}
139+
}
140+
if(i >=0){
141+
borra();
142+
cout<<"Error el DNI ya esta registrado en la base de datos"<<endl;
143+
pausa();
144+
}
145+
else{
146+
cout<<"Introduzca la edad del alumno"<<endl;
147+
do{
148+
cin>>edad;
149+
borra();
150+
if(es_valida_edad(edad)!=true){cout<<"Error introduzca una edad valida"<<endl;}
151+
}while(es_valida_edad(edad)!=true);
152+
borra();
153+
cout<<"Introduzca la nota del alumno"<<endl;
154+
do{
155+
cin>>nota;
156+
borra();
157+
if(es_valida_nota(nota)!=true){cout<<"Error inserte una nota valida entre 0 y 10"<<endl;}
158+
}while(es_valida_nota(nota)!=true);
159+
ofstream file;
160+
file.open(argv[1], ios::app);
161+
file<<DNI<<" "<<edad<<" "<<nota<<endl;
162+
}
163+
}
164+
}break;
165+
case 2:{
166+
borra();
167+
ifstream file;
168+
file.open(argv[1]);
169+
cout<<"Introduzca el dni del alumno del que desee imprimir sus datos o pulse 0 para salir"<<endl;
170+
do{
171+
cin>>DNI;
172+
borra();
173+
if(es_verdadero(DNI)==false && DNI!="0"){cout<<"Error inserte un DNI valido o pulse 0 para salir"<<endl;}
174+
}while(es_verdadero(DNI)!=true && DNI!="0");
175+
if(DNI=="0"){
176+
borra();
177+
pausa();
178+
}
179+
else{
180+
int i=-1;
181+
while(file.eof()==false && i<0){
182+
file >> aux;
183+
if(iguales(aux, DNI)==true){i++;}
184+
}
185+
if(i>=0){
186+
int edad, nota;
187+
file>>edad>>nota;
188+
cout<<"Los datos del alumno son: DNI: "<<DNI<<" Edad: "<<edad<<" Nota: "<<nota<<endl;
189+
pausa();
190+
}
191+
else{
192+
cout<<"Error el alumno no se encuentra en la base de datos"<<endl;
193+
pausa();
194+
}
195+
}
196+
}break;
197+
case 3:{
198+
borra();
199+
ifstream file;
200+
file.open(argv[1]);
201+
string linea;
202+
while(getline(file, linea)){
203+
cout<<linea<<endl;
204+
}
205+
pausa();
206+
}break;
207+
case 4:{
208+
int i=-1;
209+
borra();
210+
ifstream file;
211+
file.open(argv[1]);
212+
string linea;
213+
while(getline(file, linea)){
214+
cout<<linea<<endl;
215+
}
216+
cout<<"Introduzca el DNI del alumno que desea editar o pulse 0 para salir"<<endl;
217+
do{
218+
cin>>DNI;
219+
borra();
220+
if(es_verdadero(DNI)==false && DNI!="0"){
221+
cout<<"Error inserte un DNI valido o pulse 0 para salir"<<endl;
222+
ifstream file;
223+
file.open(argv[1]);
224+
string linea;
225+
while(getline(file, linea)){
226+
cout<<linea<<endl;
227+
}
228+
}
229+
}while(es_verdadero(DNI)!=true && DNI!="0");
230+
if(DNI=="0"){
231+
borra();
232+
pausa();
233+
}
234+
else{
235+
ifstream file;
236+
file.open(argv[1]);
237+
while(file.eof()==false && i<0){
238+
file >> aux;
239+
if(iguales(aux, DNI)==true){
240+
i++;
241+
file>>edad>>nota;
242+
buscar=DNI+espacio+espacio+espacio+edad+espacio+espacio+espacio+espacio+nota;
243+
}
244+
}
245+
if(i<0){
246+
cout<<"Error el alumno no se encuentra en la base de datos"<<endl;
247+
pausa();
248+
}
249+
else{
250+
borra();
251+
cout<<"Introduzca la edad del alumno"<<endl;
252+
do{
253+
cin>>edad;
254+
borra();
255+
if(es_valida_edad(edad)!=true){cout<<"Error introduzca una edad valida"<<endl;}
256+
}while(es_valida_edad(edad)!=true);
257+
borra();
258+
cout<<"Introduzca la nota del alumno"<<endl;
259+
do{
260+
cin>>nota;
261+
borra();
262+
if(es_valida_nota(nota)!=true){cout<<"Error inserte una nota valida entre 0 y 10"<<endl;}
263+
}while(es_valida_nota(nota)!=true);
264+
reemplazar=DNI+espacio+espacio+espacio+edad+espacio+espacio+espacio+espacio+nota;
265+
string linea;
266+
ifstream fs(argv[1]);
267+
ofstream fstemp("temporal.txt");
268+
while(getline(fs, linea)){
269+
if(linea==buscar){
270+
linea = reemplazar;
271+
}
272+
fstemp << linea<<endl;
273+
}
274+
fs.close();
275+
fstemp.close();
276+
ifstream fstamp("temporal.txt");
277+
ofstream fsa(argv[1]);
278+
while(getline(fstamp, linea)){
279+
fsa<<linea<<endl;
280+
}
281+
fsa.close();
282+
fstamp.close();
283+
remove("temporal.txt");
284+
pausa();
285+
}
286+
}
287+
}break;
288+
case 5:{
289+
int i=-1;
290+
borra();
291+
ifstream file;
292+
file.open(argv[1]);
293+
string linea;
294+
while(getline(file, linea)){
295+
cout<<linea<<endl;
296+
}
297+
cout<<"Introduzca el DNI del alumno que desea eliminar o pulse 0 para salir"<<endl;
298+
do{
299+
cin>>DNI;
300+
borra();
301+
if(es_verdadero(DNI)==false && DNI!="0"){
302+
cout<<"Error inserte un DNI valido o pulse 0 para salir"<<endl;
303+
ifstream file;
304+
file.open(argv[1]);
305+
string linea;
306+
while(getline(file, linea)){
307+
cout<<linea<<endl;
308+
}
309+
}
310+
}while(es_verdadero(DNI)!=true && DNI!="0");
311+
if(DNI=="0"){
312+
borra();
313+
pausa();
314+
}
315+
else{
316+
ifstream file;
317+
file.open(argv[1]);
318+
while(file.eof()==false && i<0){
319+
file >> aux;
320+
if(iguales(aux, DNI)==true){
321+
i++;
322+
file>>edad>>nota;
323+
buscar=DNI+espacio+espacio+espacio+edad+espacio+espacio+espacio+espacio+nota;
324+
}
325+
}
326+
if(i<0){
327+
cout<<"Error el alumno no se encuentra en la base de datos"<<endl;
328+
pausa();
329+
}
330+
else{
331+
borra();
332+
string linea;
333+
int c=0;
334+
ifstream fs(argv[1]);
335+
ofstream fstemp("temporal.txt");
336+
while(getline(fs, linea)){
337+
if(linea==buscar){
338+
fs>>DNI>>edad>>nota;
339+
linea = DNI+espacio+espacio+espacio+edad+espacio+espacio+espacio+espacio+nota;
340+
}
341+
fstemp << linea<<endl;
342+
c++;
343+
}
344+
fs.close();
345+
fstemp.close();
346+
ifstream fstamp("temporal.txt");
347+
ofstream fsa(argv[1]);
348+
int l=0;
349+
while(getline(fstamp, linea) && l<c-1){
350+
fsa<<linea<<endl;
351+
l++;
352+
}
353+
fsa.close();
354+
fstamp.close();
355+
remove("temporal.txt");
356+
pausa();
357+
}
358+
}
359+
}break;
360+
}
361+
borra();
362+
}while(opcion_menu!=6);
363+
pausa();
364+
}
365+

P9/E1/E1.eje

52.3 KB
Binary file not shown.

P9/E1/E1.exe

1.09 MB
Binary file not shown.

P9/E1/datos.txt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
DNI Edad Nota
2+
30481124Y 60 5

P9/Práctica 9.pdf

76.2 KB
Binary file not shown.

0 commit comments

Comments
 (0)