Last active
October 23, 2025 18:26
-
-
Save davidmanueldev/ccf71ac58869be6b1471f0d6606213b0 to your computer and use it in GitHub Desktop.
Calculadora Simple (main.dart) + Gestor de Estudiantes (estudiantes.dart)
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| // Opciones: 1) Agregar 2) Buscar por nombre 3) Mostrar todos 4) Promedio general 5) Salir | |
| import 'dart:convert'; | |
| import 'dart:io'; | |
| const double kMinNota = 0; | |
| const double kMaxNota = 100; | |
| class Estudiante { | |
| String nombre; | |
| List<double> notas; | |
| Estudiante(this.nombre, this.notas); | |
| double get promedio { | |
| if (notas.isEmpty) return 0; | |
| return notas.reduce((a, b) => a + b) / notas.length; // reduce sirve para sumar todos los elementos | |
| } | |
| } | |
| final List<Estudiante> estudiantes = []; | |
| void main() { | |
| while (true) { | |
| print('\n=== Gestor de Estudiantes ==='); | |
| print('1. Agregar estudiante'); | |
| print('2. Buscar estudiante por nombre'); | |
| print('3. Mostrar todos los estudiantes'); | |
| print('4. Mostrar promedio general de notas'); | |
| print('5. Salir'); | |
| final opcion = _leerLinea('Elige una opción (1-5):'); | |
| switch (opcion) { | |
| case '1': | |
| _agregarEstudiante(); | |
| break; | |
| case '2': | |
| _buscarEstudiante(); | |
| break; | |
| case '3': | |
| _mostrarTodos(); | |
| break; | |
| case '4': | |
| _promedioGeneral(); | |
| break; | |
| case '5': | |
| print('Saliendo... Fuera'); | |
| return; | |
| default: | |
| print('Opción inválida. Intenta de nuevo.'); | |
| } | |
| } | |
| } | |
| void _agregarEstudiante() { | |
| final nombre = _leerLinea('Nombre del estudiante:').trim(); | |
| if (nombre.isEmpty) { | |
| print('El nombre no puede estar vacío.'); | |
| return; | |
| } | |
| print('Ingresa las notas (0-100) separadas por comas o espacios. Ejemplo: 75, 83.5 90'); | |
| final entradaNotas = _leerLinea('Notas:'); | |
| final notas = _parsearNotas(entradaNotas); | |
| if (notas.isEmpty) { | |
| print('No se agregaron notas válidas. Operación cancelada.'); | |
| return; | |
| } | |
| estudiantes.add(Estudiante(nombre, notas)); | |
| print('Estudiante agregado: $nombre con ${notas.length} nota(s).'); | |
| } | |
| void _buscarEstudiante() { | |
| final termino = _leerLinea('Buscar por nombre (coincidencia parcial, sin mayúsculas/minúsculas):').toLowerCase().trim(); | |
| if (termino.isEmpty) { | |
| print('El término de búsqueda no puede estar vacío.'); | |
| return; | |
| } | |
| final resultados = estudiantes.where((e) => e.nombre.toLowerCase().contains(termino)).toList(); | |
| if (resultados.isEmpty) { | |
| print('No se encontraron estudiantes que coincidan con "$termino".'); | |
| return; | |
| } | |
| print('\nResultados (${resultados.length}):'); | |
| for (var i = 0; i < resultados.length; i++) { | |
| _imprimirEstudiante(resultados[i], i + 1); | |
| } | |
| } | |
| void _mostrarTodos() { | |
| if (estudiantes.isEmpty) { | |
| print('No hay estudiantes registrados.'); | |
| return; | |
| } | |
| print('\nListado de estudiantes:'); | |
| for (var i = 0; i < estudiantes.length; i++) { | |
| _imprimirEstudiante(estudiantes[i], i + 1); | |
| } | |
| } | |
| void _promedioGeneral() { | |
| final todasLasNotas = estudiantes.expand((e) => e.notas).toList(); | |
| if (todasLasNotas.isEmpty) { | |
| print('No hay notas registradas para calcular el promedio.'); | |
| return; | |
| } | |
| final suma = todasLasNotas.reduce((a, b) => a + b); | |
| final promedio = suma / todasLasNotas.length; | |
| print('Promedio general de todas las notas (0-100): ${_fmt(promedio)}'); | |
| } | |
| void _imprimirEstudiante(Estudiante e, int indice) { | |
| final notasStr = e.notas.map(_fmt).join(', '); | |
| print('$indice) ${e.nombre} | Notas: [$notasStr] | Promedio (0-100): ${_fmt(e.promedio)}'); | |
| } | |
| String _leerLinea(String prompt) { | |
| stdout.write('$prompt '); | |
| return stdin.readLineSync(encoding: utf8) ?? ''; | |
| } | |
| List<double> _parsearNotas(String entrada) { | |
| // Extrae números permitiendo coma o punto decimal y separadores mixtos. | |
| // Valida que cada nota esté en [0, 100]. | |
| final regexNumero = RegExp(r'(\d+(?:[.,]\d+)?)'); | |
| final notas = <double>[]; | |
| for (final m in regexNumero.allMatches(entrada)) { | |
| final token = m.group(0)!.replaceAll(',', '.'); | |
| final valor = double.tryParse(token); | |
| if (valor != null) { | |
| if (valor >= kMinNota && valor <= kMaxNota) { | |
| notas.add(valor); | |
| } else { | |
| print('Nota fuera de rango ignorada: ${_fmt(valor)} (debe estar entre ${_fmt(kMinNota)} y ${_fmt(kMaxNota)}).'); | |
| } | |
| } | |
| } | |
| if (notas.isEmpty) { | |
| print('No se detectaron números válidos. Asegúrate de usar comas o espacios como separadores y valores 0-100.'); | |
| } | |
| return notas; | |
| } | |
| String _fmt(num n) => n.toStringAsFixed(2); // esto es una función que formatea un número a 2 decimales pa no hacerlo tan largo pe |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| import 'dart:io'; | |
| void main() { | |
| while (true) { | |
| print('\n=== Menú ==='); | |
| print('1. Sumar'); | |
| print('2. Restar'); | |
| print('3. Multiplicar'); | |
| print('4. Dividir'); | |
| print('5. Salir'); | |
| stdout.write('Elige una opción (1-5): '); | |
| final opcion = int.tryParse(stdin.readLineSync() ?? ''); | |
| if (opcion == null) { | |
| print('Opción no válida.'); | |
| continue; | |
| } | |
| if (opcion == 5) { | |
| print('Saliendo...'); | |
| break; | |
| } | |
| final a = leerNumero('Ingresa el primer número: '); | |
| final b = leerNumero('Ingresa el segundo número: '); | |
| switch (opcion) { | |
| case 1: | |
| print('Resultado (Sumar): ${a + b}'); | |
| break; | |
| case 2: | |
| print('Resultado (Restar): ${a - b}'); | |
| break; | |
| case 3: | |
| print('Resultado (Multiplicar): ${a * b}'); | |
| break; | |
| case 4: | |
| if (b == 0) { | |
| print('Error: no se puede dividir entre cero.'); | |
| } else { | |
| print('Resultado (Dividir): ${a / b}'); | |
| } | |
| break; | |
| default: | |
| print('Opción no válida.'); | |
| } | |
| } | |
| } | |
| double leerNumero(String mensaje) { | |
| while (true) { | |
| stdout.write(mensaje); | |
| final input = stdin.readLineSync(); | |
| final valor = double.tryParse((input ?? '').trim()); | |
| if (valor != null) return valor; | |
| print('Entrada no válida. Intenta de nuevo.'); | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment