-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathbasicas.h
More file actions
221 lines (184 loc) · 6.18 KB
/
basicas.h
File metadata and controls
221 lines (184 loc) · 6.18 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
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
/* ===================================================================================
basicas.h - librería C++ -- by MarkTM
Contiene las siguientes funciones para usar :
====== Funciones para mostrar mensajes ======
- void mensaje(char cadena);
- void mensaje_ok(char cadena);
- void mensaje_error(char cadena);
====== Funciones para Leer ======
- void leer_entero_valido(char msje,int min, int max, char msjeError, int variable);
- void leer_real_valido(char msje,float min, float max, char msjeError, float variable);
- void leer_double_valido(char msje,double min, double max, char msjeError, double variable);
- void leer_cadena(char msje, char cadena, int maxCaracteres);
- void leer_cadena_valida(char msje, char cadena, int max);
- void leer_cadena_numerica(char msje, char cadena, int max);
====== Funciones para imprimir ======
- void escribir_entero(char msje, int variable);
- void escribir_real(char msje, int variable);
- void escribir_double(char msje, double variable);
- void escribir_cadena(char msje, char cadena);
====== Funciones para presentacion ======
- int elegir_opcion(char msje, char tablaOpciones, int cantOpciones, char msjeError);
=================================================================================== */
#include <stdio.h>
#include <conio.h>
#include <string.h>
#include <ctype.h>
/* ===================================================================================
FUNCIONES BASICAS
=================================================================================== */
void prefix_lee(){
printf("\n < ");
}
void prefix_escribir(){
printf("\n > ");
}
void mensaje(char *msje){
printf("%s: ",msje);
}
void mensaje_ok(char *msje){
printf("\n\tExito -> %s\n",msje);
}
void mensaje_error(char *msje){
printf("\n\tError -> %s\n",msje);
}
/* ===================================================================================
FUNCIONES CON ENTEROS
=================================================================================== */
void leer_entero(char *msje, int &entero){
prefix_lee();
mensaje(msje);
scanf("%d",&entero);
}
void leer_entero_valido(char *msje,int min, int max, char *msjeError, int &entero){
do{
leer_entero(msje,entero);
if(entero < min || entero > max){
mensaje_error(msjeError);
}
}while(entero < min || entero > max);
}
void escribir_entero(char *msje, int entero){
prefix_escribir();
printf("%s: %d",msje,entero);
}
/*====================================================================================
FUNCIONES CON REALES
=================================================================================== */
void leer_real(char *msje, float &real){
prefix_lee();
mensaje(msje);
scanf("%.2f",&real);
}
void leer_real_valido(char *msje,float min, float max, char *msjeError, float &real){
do{
leer_real(msje,real);
if(real < min || real > max){
mensaje_error(msjeError);
}
}while(real < min || real > max);
}
void escribir_real(char *msje, int real){
prefix_escribir();
printf("%s: %.2f",msje,real);
}
/*====================================================================================
FUNCIONES CON DOUBLE
=================================================================================== */
void leer_double(char *msje, double &real){
prefix_lee();
mensaje(msje);
scanf("%lf",&real);
}
void leer_double_valido(char *msje,double min, double max, char *msjeError, double &real){
do{
leer_double(msje,real);
if(real < min || real > max){
mensaje_error(msjeError);
}
}while(real < min || real > max);
}
void escribir_double(char *msje, double real){
prefix_escribir();
printf("%s: %lf",msje,real);
}
/*====================================================================================
FUNCIONES CON CADENA
=================================================================================== */
void leer_cadena(char *msje, char cadena[], int max){
int repeticiones;
int maxCaracteres;
do{
prefix_lee();
mensaje(msje);
fflush(stdin);
gets(cadena);
maxCaracteres = strlen(cadena);
if(maxCaracteres < 1 || maxCaracteres > (max-1))
mensaje_error("No ingreso caracteres o sobre paso el limite");
} while (maxCaracteres < 1 || maxCaracteres > (max-1));
}
void leer_cadena_valida(char *msje, char cadena[] , int max){
int repeticiones;
int maxCaracteres;
do{
repeticiones = 0;
leer_cadena(msje,cadena,max);
maxCaracteres = strlen(cadena);
for (int i = 0; i < maxCaracteres; ++i){
if (isalpha(cadena[i]) || cadena[i] == ' ')
repeticiones++;
}
if (repeticiones != maxCaracteres)
mensaje_error("Solo acepta letras");
}while(repeticiones != maxCaracteres);
}
void leer_cadena_numerica(char *msje, char cadena[], int max){
int repeticiones;
int maxCaracteres;
do{
repeticiones = 0;
leer_cadena(msje,cadena,max);
maxCaracteres = strlen(cadena);
for (int i = 0; i < maxCaracteres; ++i){
if (isdigit(cadena[i]))
repeticiones++;
}
if (repeticiones != maxCaracteres)
mensaje_error("Solo acepta numeros");
}while(repeticiones != maxCaracteres);
}
void escribir_cadena(char *msje, char cadena[]){
prefix_escribir();
printf("%s: %s",msje,cadena);
}
/*====================================================================================
FUNCIONES DE PRESENTACION
=================================================================================== */
int elegir_opcion(char *msje, char *tablaOpciones[], int cantOpciones, char *msjeError){
int op;
for (int i = 0; i < cantOpciones; ++i){
printf("\n [%d] %s",i+1,tablaOpciones[i]);
}
leer_entero_valido(msje,1,cantOpciones,msjeError,op);
return op;
}
/*====================================================================================
FUNCIONES DE FECHA
=================================================================================== */
bool es_anio_bisiesto(int anio){
if (anio % 4 == 0 && anio % 100 != 0 || anio % 400 == 0 )
return true;
else
return false;
}
bool fecha_correcta(int dia, int mes, int anio){
int arrayDiasMes[12] = {31,29,31,30,31,30,31,31,30,31,30,31};
if(dia == 0 || mes == 0 || anio == 0)
return false;
if(arrayDiasMes[mes - 1] < dia)
return false;
if(mes == 2 && dia == 29 && !es_anio_bisiesto(anio))
return false;
return true;
}