Advertisement
Guest User

lenguaje D, D de mierda

a guest
Oct 21st, 2018
139
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
D 3.19 KB | None | 0 0
  1. import std.stdio;
  2. import std.file;
  3. import std.string;
  4. import std.conv;
  5. int main(char[][]argv)
  6. {
  7.  
  8.     pasaAlumnosNotas();
  9.     writef("");
  10.     string name;
  11.     name = readln();
  12.     return 0;
  13. }
  14.  
  15. //Caso de prueba, un programa que lea archivos txt del usuario el cual debera sacar el promedio de notas del archivo leido.
  16.  
  17.  
  18. void pasaAlumnosNotas(){
  19.     try{
  20.         File file = File("Alumnos.txt", "r"); //archivo a leer
  21.         while (!file.eof()) {
  22.             string line = chomp(file.readln());
  23.             writeln("Nombre Alumno -", line);
  24.             writeln("Manejo Excepciones Del Lenguaje!");
  25.             writeln("-----------------------------------");
  26.             manejoExcepcionesLenguaje(line);
  27.             writeln("-----------------------------------");
  28.             writeln("Manejo Excepciones Del Usuario!");
  29.             manejoExcepcionesPorUsuario(line);
  30.         }
  31.     }catch(Exception e){
  32.         writeln(e.msg);
  33.     }
  34. }
  35. //Para este ejemplo usaremos un txt
  36. void manejoExcepcionesLenguaje(string nombreAlumno){
  37. //archivo leido no existe
  38. //al ingresar el total de notas se ingresa un 0, lo que hace que division sea por 0
  39. //al ingresar el total de notas se ingresa un char o algun otro simbolo que no sea un numero
  40.     double calculopromedio=0;
  41.     try{
  42.         File A = File(nombreAlumno~".txt", "r"); //archivo que se leera
  43.         while (!A.eof()) {
  44.             string line = chomp(A.readln());
  45.             writeln("Nota - ",line);
  46.             double b = to!double(line);
  47.             calculopromedio=b+calculopromedio;
  48.         }
  49.  
  50.         writef("ingrese total de notas del alumno "~nombreAlumno~" - ");
  51.         string a;
  52.         a = readln();
  53.         double totalnotas= parse!double(a);
  54.         double promedioalumno = calculopromedio/totalnotas;
  55.         writeln(promedioalumno);
  56.         A.close();
  57.     }catch(Exception ex){
  58.         writeln(ex.msg);
  59.     }
  60. }
  61. void manejoExcepcionesPorUsuario(string nombreAlumno){
  62.     double calculopromedio=0;
  63.     double contadorNotas=0;
  64.     double cantidadmaximadenotas=5;
  65.     double contadorNotasreales=0;
  66.     bool saltar= false;
  67.     try{
  68.         File A = File(nombreAlumno~".txt", "r"); //archivo que se leera
  69.         while (!A.eof()) {
  70.             string line = chomp(A.readln());
  71.             double b = to!double(line);
  72.             if(b>=10 && b<=70){
  73.                 contadorNotas=contadorNotas+1;
  74.                 if(contadorNotas<=cantidadmaximadenotas || contadorNotas==0){
  75.                     contadorNotasreales++;
  76.                     writeln("Nota - ",line);
  77.                     calculopromedio=b+calculopromedio;
  78.                 }else{
  79.                     writeln("Se exedio del maximo de notas. Revise al archivo txt");
  80.                     saltar=true;
  81.                     break;
  82.                 }
  83.             }else{
  84.                 writeln("Hubo una nota erronea! revise el archivo txt");
  85.                 saltar=true;
  86.                 break;
  87.             }
  88.  
  89.         }
  90.         if(saltar==false){
  91.             bool salida=false;
  92.             writef("ingrese total de notas del alumno "~nombreAlumno~" - ");
  93.             string a;
  94.             a = readln();
  95.             double totalnotas= parse!double(a);
  96.             while(salida!=true){
  97.                 if(contadorNotasreales == totalnotas){
  98.                     double promedioalumno = calculopromedio/totalnotas;
  99.                     writeln(promedioalumno);
  100.                     salida=true;
  101.                 }else{
  102.                     writeln("La cantidad de notas del alumno ingresadas es incorrectas!, Ingreselas nuevamente, ");
  103.                     writef("ingrese total de notas del alumno "~nombreAlumno~" - ");
  104.                     string x;
  105.                     x = readln();
  106.                     totalnotas= parse!double(x);
  107.                 }
  108.             }
  109.             A.close();
  110.         }else{
  111.             writeln("");
  112.         }
  113.     }catch(Exception ex){
  114.         writeln(ex.msg);
  115.     }
  116. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement