Advertisement
Wojtekd

Wielomianowy Sekret

May 9th, 2016
80
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.49 KB | None | 0 0
  1. #include <iostream>
  2. #include <cstdlib>
  3. #include <ctime>
  4.  
  5. using namespace std;
  6.  
  7.  
  8. double sekret_punktow( int x1, int y1, int x2, int y2, int x3, int y3 )
  9. {
  10.     double wspolczynnik_b = ((y1-y3) * (x1*x1 - x2*x2) - (x1*x1 - x3*x3)*(y1-y2)) / ((x1-x3)*(x1*x1 - x2*x2) - (x1-x2)*(x1*x1-x3*x3));
  11.     double wspolczynnik_a = (y1 - y2 - wspolczynnik_b*(x1 - x2)) / (x1*x1 - x2*x2);
  12.    
  13.     double wspolczynnik_c = (wspolczynnik_a *x1*x1 + wspolczynnik_b * x1 - y1) * -1;
  14.            
  15.     return wspolczynnik_c;
  16. }
  17.  
  18. int main()
  19. {
  20.     srand(time(NULL));
  21.    
  22.     int sekret = 0;
  23.     cout << "podaj sekretna liczbe: " << endl;
  24.     cin >> sekret;
  25.    
  26.     int a = (rand() % 5) + 1;
  27.     int b = (rand() % 5) + 1;
  28.  
  29.     cout << "wybrany wielomian: " << a << "x^2" << " + " << b << "x" << " + " << sekret << endl << endl;
  30.    
  31.     int lx = rand() % 15 + 3;  
  32.     float x1 = lx;
  33.     float y1 = a * lx * lx + b * lx + sekret;  
  34.     cout << "x1: " << x1 << "    y1: " << y1 << endl;
  35.    
  36.     lx = rand() % 10 + 15;
  37.     float x2 = lx;
  38.     float y2 = a * lx * lx + b * lx + sekret;  
  39.     cout << "x2: " << x2 << "    y2: " << y2 << endl;
  40.    
  41.     lx = rand() % 15 + 7;
  42.     float x3 = lx;
  43.     float y3 = a * lx * lx + b * lx + sekret;  
  44.     cout << "x3: " << x3 << "    y3: " << y3 << endl;  
  45.  
  46.    
  47.     cout << "czy chcesz odszyfrowac sekret na podstawie punktow? (y/n)" << endl;
  48.     string input;
  49.     cin >> input;
  50.    
  51.     if(input == "y")
  52.     {
  53.         double sekretna_liczba = sekret_punktow( x1, y1, x2, y2, x3, y3 );
  54.         cout << "sekret to: " << sekretna_liczba << endl;      
  55.     }  
  56.        
  57.     system("pause");
  58.     return 0;
  59. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement