fork download
  1. #include <iostream>
  2. #include <cmath>
  3. #include <vector>
  4. #include <algorithm>
  5. using namespace std;
  6.  
  7. int main() {
  8. int a = 1;
  9. int b = 2;
  10. int c = -10;
  11. int d = 10;
  12. int n = 100;
  13.  
  14. double h = double(d - c) / n;
  15.  
  16. vector<double> X, Y;
  17. X.reserve(n + 1);
  18. Y.reserve(n + 1);
  19.  
  20. // вычисление (как в Python)
  21. for (int i = 0; i <= n; i++) {
  22. double x = c + i * h;
  23.  
  24. double num = sin(a * x) + pow(b, 2 * c);
  25. double den = b * b + cos(x) * cos(x);
  26. double frac = num / den;
  27.  
  28. double y = cbrt(frac) - sin(x * x) / (a * b);
  29.  
  30. X.push_back(x);
  31. Y.push_back(y);
  32. }
  33.  
  34. // -----------------------------
  35. // ASCII-график
  36. // -----------------------------
  37. int width = 80; // ширина графика
  38. int height = 25; // высота графика
  39.  
  40. double ymin = *min_element(Y.begin(), Y.end());
  41. double ymax = *max_element(Y.begin(), Y.end());
  42.  
  43. vector<string> canvas(height, string(width, ' '));
  44.  
  45. for (int i = 0; i <= n; i++) {
  46. int px = int((double)i / n * (width - 1));
  47. int py = int((Y[i] - ymin) / (ymax - ymin) * (height - 1));
  48.  
  49. py = height - 1 - py; // инверсия оси Y
  50.  
  51. if (px >= 0 && px < width && py >= 0 && py < height)
  52. canvas[py][px] = '*';
  53. }
  54.  
  55. // вывод графика
  56. for (auto &row : canvas)
  57. cout << row << "\n";
  58.  
  59. // вывод диапазонов
  60. cout << "\nX range: [" << c << ", " << d << "]\n";
  61. cout << "Y range: [" << ymin << ", " << ymax << "]\n";
  62.  
  63. return 0;
  64. }
  65.  
Success #stdin #stdout 0s 5256KB
stdin
Standard input is empty
stdout
                                                                       *        
                    * *                         *                  ** *  *      
                  *                            *                                
               *                                                                
**                        *                     *                          *    
                   *   *                      *                                 
                 *        *                                      *              
                                                                                
                      *                                                         
                                        **   *                  *               
                     *   *                *      *                         *    
                  *                        ***     **          *  *  * **    *  
                *       *              *            *              *      *     
*    *     *   *              *                   *                         *   
   *   ** *                                             *   *                   
             **               **                          *                    *
  *                                   *                                         
   *                                                   *                        
                           *         *                     * *                  
                                *                    *         *                
                             *       *                                          
                                                                                
            *               *    *  *                   *     *               * 
    *                              *                  *  *                      
      ** * *                     **                         *                   

X range: [-10, 10]
Y range: [-1.10449, 1.08849]