|
| 1 | +#include <stddef.h> |
| 2 | +#include <string.h> |
| 3 | +#include <stdio.h> |
| 4 | +#include <stdbool.h> |
| 5 | +#include <stdlib.h> |
| 6 | +#include <string.h> |
| 7 | + |
| 8 | +/* |
| 9 | +//---------------- Array |
| 10 | +int numbers[] = {0, 1, 2, 3, 4, 5}; |
| 11 | +
|
| 12 | +// Acceder a un elemento |
| 13 | +printf("%d\n", numbers[2]); |
| 14 | +
|
| 15 | +// Cambiar un elemento |
| 16 | +numbers[2] = 7; |
| 17 | +printf("%d\n", numbers[2]); |
| 18 | +
|
| 19 | +// Iterar |
| 20 | +printf("{"); |
| 21 | +for (int i = 0; i < 6; i++) { |
| 22 | + printf("%d, ", numbers[i]); |
| 23 | +} |
| 24 | +printf("}\n"); |
| 25 | +
|
| 26 | +// Iniciar con tamaño fijo |
| 27 | +int fourNumbers[4]; |
| 28 | +fourNumbers[0] = 0; |
| 29 | +fourNumbers[1] = 1; |
| 30 | +fourNumbers[2] = 2; |
| 31 | +fourNumbers[3] = 3; |
| 32 | +
|
| 33 | +// Tamaño del array |
| 34 | +int length = sizeof(fourNumbers) / sizeof(fourNumbers[0]); |
| 35 | +printf("%d\n", length); |
| 36 | +
|
| 37 | +//Multidimensional |
| 38 | +int matrix[3][3] = {{1, 2, 3}, {4, 5, 6}, {7, 8, 9}}; |
| 39 | +
|
| 40 | +for (int i = 0; i < 3; i++) { |
| 41 | +
|
| 42 | + for (int j = 0; j < 3; j++) { |
| 43 | + printf("%d, ", matrix[i][j]); |
| 44 | + } |
| 45 | + printf("\n"); |
| 46 | +} |
| 47 | +
|
| 48 | +//------------------ Structs |
| 49 | +struct myStructure { |
| 50 | + int number; |
| 51 | + char character; |
| 52 | +}; |
| 53 | +
|
| 54 | +struct myStructure s1; |
| 55 | +s1.character = 'A'; |
| 56 | +s1.number = 10; |
| 57 | +printf("%c\n", s1.character); |
| 58 | +printf("%d\n", s1.number); |
| 59 | +
|
| 60 | +//------------- Enumeration |
| 61 | +enum Level { |
| 62 | + LOW, |
| 63 | + MEDIUM, |
| 64 | + HIGH |
| 65 | +}; |
| 66 | +
|
| 67 | +
|
| 68 | +enum Level { |
| 69 | + LOW = 25, |
| 70 | + MEDIUM = 50, |
| 71 | + HIGH = 75 |
| 72 | +}; |
| 73 | +
|
| 74 | +enum Level myLevel1 = LOW; |
| 75 | +enum Level myLevel2 = MEDIUM; |
| 76 | +enum Level myLevel3 = HIGH; |
| 77 | +printf("%d\n", myLevel1); |
| 78 | +printf("%d\n", myLevel2); |
| 79 | +printf("%d\n", myLevel3); |
| 80 | +*/ |
| 81 | + |
| 82 | +#define MAX_CONTACTS 100 |
| 83 | +#define MAX_NAME_LENGTH 100 |
| 84 | + |
| 85 | +struct Contact { |
| 86 | + int number; |
| 87 | + char name[MAX_NAME_LENGTH]; |
| 88 | +}; |
| 89 | + |
| 90 | +struct Contact contacts[MAX_CONTACTS]; |
| 91 | +int contactCount = 0; |
| 92 | + |
| 93 | +void clearInputBuffer() { |
| 94 | + int c; |
| 95 | + while ((c = getchar()) != '\n' && c != EOF); |
| 96 | +} |
| 97 | + |
| 98 | +void removeElement(struct Contact arr[], int index) { |
| 99 | + if (index < 0 || index >= MAX_CONTACTS) { |
| 100 | + printf("Índice fuera de rango\n"); |
| 101 | + return; |
| 102 | + } |
| 103 | + |
| 104 | + // Desplazar los elementos posteriores hacia adelante |
| 105 | + for (int i = index; i < MAX_CONTACTS - 1; i++) { |
| 106 | + arr[i] = arr[i + 1]; |
| 107 | + } |
| 108 | + |
| 109 | + // Inicializar el último elemento a valores predeterminados |
| 110 | + arr[contactCount - 1].number = 0; |
| 111 | + arr[contactCount - 1].name[0] = '\0'; |
| 112 | + |
| 113 | + contactCount--; |
| 114 | + } |
| 115 | + |
| 116 | +struct Contact search() { |
| 117 | + char contactName[MAX_NAME_LENGTH]; |
| 118 | + struct Contact contact = {0, ""}; |
| 119 | + |
| 120 | + clearInputBuffer(); |
| 121 | + |
| 122 | + printf("\nIntroduce el nombre del contacto: "); |
| 123 | + if (fgets(contactName, sizeof(contactName), stdin) == NULL) { |
| 124 | + fprintf(stderr, "\nError al leer el nombre\n"); |
| 125 | + } |
| 126 | + |
| 127 | + // Eliminar el carácter de nueva línea al final de la cadena |
| 128 | + size_t len = strlen(contactName); |
| 129 | + if (len > 0 && contactName[len - 1] == '\n') { |
| 130 | + contactName[len - 1] = '\0'; |
| 131 | + } |
| 132 | + |
| 133 | + for (int i = 0; i < MAX_CONTACTS; i++) { |
| 134 | + if (strcmp(contacts[i].name, contactName) == 0) { |
| 135 | + contact = contacts[i]; |
| 136 | + break; |
| 137 | + } |
| 138 | + } |
| 139 | + |
| 140 | + return contact; |
| 141 | +} |
| 142 | + |
| 143 | +int addContact() { |
| 144 | + if (contactCount >= MAX_CONTACTS) { |
| 145 | + fprintf(stderr, "\nNo se pueden añadir más contactos\n"); |
| 146 | + return 1; |
| 147 | + } |
| 148 | + |
| 149 | + struct Contact contact; |
| 150 | + char contactName[MAX_NAME_LENGTH]; |
| 151 | + int contactNumber; |
| 152 | + |
| 153 | + clearInputBuffer(); |
| 154 | + |
| 155 | + printf("\nIntroduce el nombre del contacto: "); |
| 156 | + if (fgets(contactName, sizeof(contactName), stdin) == NULL) { |
| 157 | + fprintf(stderr, "\nError al leer el nombre\n"); |
| 158 | + return 1; |
| 159 | + } |
| 160 | + |
| 161 | + // Eliminar el carácter de nueva línea al final de la cadena |
| 162 | + size_t len = strlen(contactName); |
| 163 | + if (len > 0 && contactName[len - 1] == '\n') { |
| 164 | + contactName[len - 1] = '\0'; |
| 165 | + } |
| 166 | + |
| 167 | + // Solicitar el número del contacto |
| 168 | + printf("Introduce el número del contacto: "); |
| 169 | + if (scanf("%d", &contactNumber) != 1) { |
| 170 | + fprintf(stderr, "\nError al leer el número\n"); |
| 171 | + clearInputBuffer(); |
| 172 | + return 1; |
| 173 | + } |
| 174 | + |
| 175 | + strcpy(contact.name, contactName); |
| 176 | + contact.number = contactNumber; |
| 177 | + |
| 178 | + contacts[contactCount++] = contact; |
| 179 | + printf("Contacto añadido correctamente\n"); |
| 180 | + |
| 181 | + return 0; |
| 182 | +} |
| 183 | + |
| 184 | +int update() { |
| 185 | + int contactNumber; |
| 186 | + struct Contact contact = search(); |
| 187 | + |
| 188 | + // Solicitar el nuevo número del contacto |
| 189 | + printf("Introduce el nuevo número del contacto: "); |
| 190 | + if (scanf("%d", &contactNumber) != 1) { |
| 191 | + fprintf(stderr, "\nError al leer el número\n"); |
| 192 | + clearInputBuffer(); |
| 193 | + return 1; |
| 194 | + } |
| 195 | + |
| 196 | + if (contact.number) { |
| 197 | + for (int i = 0; i < MAX_CONTACTS; i++) { |
| 198 | + if (strcmp(contacts[i].name, contact.name) == 0) { |
| 199 | + contacts[i].number = contactNumber; |
| 200 | + printf("Contacto actualizado\n"); |
| 201 | + break; |
| 202 | + } |
| 203 | + } |
| 204 | + } else { |
| 205 | + printf("El contacto no existe\n"); |
| 206 | + } |
| 207 | + |
| 208 | + return 0; |
| 209 | +} |
| 210 | + |
| 211 | +int removeContact() { |
| 212 | + struct Contact contact = search(); |
| 213 | + |
| 214 | + if (contact.number) { |
| 215 | + for (int i = 0; i < MAX_CONTACTS; i++) { |
| 216 | + if (strcmp(contacts[i].name, contact.name) == 0) { |
| 217 | + removeElement(contacts, i); |
| 218 | + printf("Contacto eliminado\n"); |
| 219 | + break; |
| 220 | + } |
| 221 | + } |
| 222 | + } else { |
| 223 | + printf("El contacto no existe\n"); |
| 224 | + } |
| 225 | + |
| 226 | + return 0; |
| 227 | +} |
| 228 | + |
| 229 | +int main() { |
| 230 | + |
| 231 | + int choice; |
| 232 | + bool finish = false; |
| 233 | + struct Contact contact; |
| 234 | + |
| 235 | + while (!finish) { |
| 236 | + printf("\nQué dese hacer?:\n"); |
| 237 | + printf("1- Buscar\n"); |
| 238 | + printf("2- Añadir\n"); |
| 239 | + printf("3- Actualizar\n"); |
| 240 | + printf("4- Eliminar\n"); |
| 241 | + printf("5- Salir\n"); |
| 242 | + |
| 243 | + if (scanf("%d", &choice) != 1) { |
| 244 | + fprintf(stderr, "\n⚠️ Opción incorrecta\n"); |
| 245 | + } |
| 246 | + |
| 247 | + switch (choice) { |
| 248 | + case 1: |
| 249 | + contact = search(); |
| 250 | + if (contact.number) { |
| 251 | + printf("%s - %d\n", contact.name, contact.number); |
| 252 | + } else { |
| 253 | + printf("No existe el contacto\n"); |
| 254 | + } |
| 255 | + break; |
| 256 | + case 2: |
| 257 | + addContact(); |
| 258 | + break; |
| 259 | + case 3: |
| 260 | + update(); |
| 261 | + break; |
| 262 | + case 4: |
| 263 | + removeContact(); |
| 264 | + break; |
| 265 | + case 5: |
| 266 | + finish = true; |
| 267 | + printf("👋 Hasta pronto"); |
| 268 | + break; |
| 269 | + default: |
| 270 | + printf("\n⚠️ Opción incorrecta\n"); |
| 271 | + } |
| 272 | + } |
| 273 | + |
| 274 | + |
| 275 | + return 0; |
| 276 | +} |
0 commit comments