Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <vector>
- #include <functional>
- #include <fstream>
- struct Point{
- double x;
- double y;
- Point(double x, double y): x(x), y(y) {};
- };
- struct Graph{
- std::vector<Point> points;
- void AddPoint(const Point& point) {
- points.emplace_back(point);
- }
- void PrintToFile(std::string filename) {
- std::ofstream out;
- out.open(filename);
- for (auto point: points) {
- out << point.x << ' ' << point.y << std::endl;
- }
- out.close();
- }
- };
- double CalculateValueGorner(const std::vector<double>& coefficients, double x) {
- double result = coefficients.back();
- if (coefficients.size() == 1) {
- return result;
- }
- for (int i = int(coefficients.size()) - 2; i >= 0; --i) {
- result = coefficients[i] + result*x;
- }
- return result;
- }
- Graph GetPoints(std::function<double(const std::vector<double>& coefficients, double x)> calculate,
- double start, double end, double step, std::vector<double>& coefficients) {
- Graph result;
- for (;start < end; start += step) {
- result.AddPoint(Point(start, calculate(coefficients, start)));
- }
- return result;
- }
- Graph Check(double start, double end, double step) {
- Graph result;
- for (; start < end; start += step) {
- double current_result = start - 2;
- double multiply = current_result;
- for(int i = 1; i < 9; ++i) {
- current_result *= multiply;
- }
- result.AddPoint(Point(start, current_result));
- }
- return result;
- }
- int main() {
- std::cout << "Введите степень многочлена" << std::endl;
- size_t range;
- std::cin >> range;
- std::vector<double> coefficients(range+1);
- std::cout << "Введите коэффициенты, от меньшей степени к большей" << std::endl;
- for (size_t i = 0; i < coefficients.size(); ++i) {
- std::cin >> coefficients[i];
- }
- Graph gorner_result = GetPoints(CalculateValueGorner, 1.92, 2.08, 0.0001, coefficients);
- Graph true_result = Check(1.92, 2.08, 0.0001);
- gorner_result.PrintToFile("gorner_result.txt");
- true_result.PrintToFile("true_result.txt");
- }
- //ввод для (x-2)^9
- //9
- //-512 2304 -4608 5376 -4032 2016 -672 144 -18 1
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement