szymski

Untitled

Jun 20th, 2016
292
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
D 1.06 KB | None | 0 0
  1. import std.stdio, std.math, std.string, std.conv;
  2.  
  3. void main()
  4. {
  5.     double a, b, c;
  6.  
  7.     a = readDouble("a");
  8.     b = readDouble("b");
  9.     c = readDouble("c");
  10.  
  11.     writefln("Obliczam ax^2 + bx + c = 0");
  12.  
  13.     double delta = getDelta(a, b, c);
  14.     writeln("Delta: ", delta);
  15.  
  16.     if(delta > 0) {
  17.         double x1, x2;
  18.  
  19.         x1 = (-b - sqrt(delta)) / (2 * a);
  20.         x2 = (-b + sqrt(delta)) / (2 * a);
  21.  
  22.         writeln("Znaleziono dwa rozwiazania: ");
  23.         writeln("\tx1 = ", x1);
  24.         writeln("\tx2 = ", x2);
  25.     }
  26.     else if(delta == 0) {
  27.         double x;
  28.  
  29.         x = -b / (2 * a);
  30.  
  31.         writeln("Znaleziono jedno rozwiazanie: ");
  32.         writeln("\tx = ", x);
  33.     }
  34.     else
  35.         writeln("Brak rozwiazan.");
  36.  
  37.     readln();
  38. }
  39.  
  40. double readDouble(string name) {
  41.     string buffer;
  42.     double d;
  43.  
  44.     while(true) {
  45.         write("Podaj ", name, ": ");
  46.         readf("%s\n", &buffer);
  47.  
  48.         buffer = buffer.replace(",", ".");
  49.  
  50.         try {
  51.             d = buffer.to!double;
  52.         }
  53.         catch {
  54.             writeln("To chyba nie jest liczba.");
  55.             continue;
  56.         }
  57.  
  58.         return d;
  59.     }
  60. }
  61.  
  62. double getDelta(double a, double b, double c) {
  63.     return (b ^^ 2) - (4 * a * c);
  64. }
Advertisement
Add Comment
Please, Sign In to add comment