hozer

Lab1cpp

Sep 3rd, 2014
298
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 2.12 KB | None | 0 0
  1. #include <iostream>
  2. #include <iomanip>
  3. #include <cmath>
  4. using namespace std;
  5. #define MAX 1
  6.  
  7. struct object{
  8.     float a, b;
  9.  
  10.     void input()
  11.     {
  12.         cout << "Input a b: ";
  13.         cin >> a >> b;
  14.     }
  15.  
  16.     void sim() { a = -a; }
  17.  
  18.     int check() { return a == 0 || b == 0 ? -1 : 1; }
  19.     int argument(float &x1, float &x2, float y);
  20.     int isPeretyn(float y);
  21.     void output(float x1, float x2, int p);
  22. };
  23.  
  24.  
  25. int object::argument(float &x1, float &x2, float y)
  26. {
  27.     float d = 0.0;
  28.     b -= y;
  29.     d =a*a + 4*b;
  30.     if (d > 0) {
  31.         x1 = (-a - sqrt(d))/-2;
  32.         x2 = (-a + sqrt(d))/-2;
  33.     }
  34.     else if (d == 0) {x1 = -a / 2 * -1; x2 = x1;}
  35.     else return 0;
  36.     return 1;
  37. }
  38.  
  39. int object::isPeretyn(float y)
  40. {
  41.     return y >= 0 ? 1 : -1;
  42. }
  43.  
  44.  
  45. void object::output(float x1, float x2, int p)
  46. {
  47.     cout << "x1 = " << setprecision(3) << x1 << " x2 = " << setprecision(3) << x2 << endl;
  48.     cout << "Parabola " << (p == 1 ? "crosses" : "doesn't cross") << " OX\n";
  49.     cout << "-x^2 + " << setprecision(3) << a << "x + " << setprecision(3) << b << "\n";
  50. }
  51.  
  52. int main()
  53. {
  54.     int v = 0, i;
  55.     float x1, x2;
  56.     object ob[MAX];
  57.  
  58.     for (i = 0; i < MAX; i++)
  59.     {
  60.         ob[i].input();
  61.     }
  62.     for (i = 0; i < MAX; i++)
  63.     {
  64.         v = ob[i].check();
  65.         if (v == 1) {
  66.             int peret = 0;
  67.             float y = 0.0;
  68.             cout << "\ninput y: ";
  69.             cin >> y;
  70.             if (ob[i].argument(x1, x2, y))
  71.             {
  72.                 peret = ob[i].isPeretyn(y);
  73.                 ob[i].sim();
  74.                 ob[i].output(x1, x2, peret);
  75.             }
  76.             else cout << "error: d < 0\n";
  77.         }
  78.         else
  79.         {
  80.             cout<<"\nIncorrect coefficients\n";
  81.         }
  82.     }
  83.  
  84.     cin.ignore();
  85.     cin.get();
  86.     return 0;
  87. }
  88.  
  89.  
  90. /*Квадратична функція y=-x2+Ax+B. Поле first – дійсне число, коефіцієнт A;
  91. поле second – дійсне число, коефіцієнт B. Реалізувати методи argument(ob) –
  92. обчислення для заданого y відповідних значень x, isPeretyn(ob) – визначення,
  93. чи перетинається парабола з віссю OX, sim(ob) – перетворення параболи у симетричну
  94. до неї параболу відносно осі OY.
  95. */
Advertisement
Add Comment
Please, Sign In to add comment