⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 equation.cpp

📁 经常使用的一些源程序.包括一些模板.在VC++6.0下通过.
💻 CPP
字号:
#include <iostream>
#include <iomanip>
#include <cmath>
using namespace std;
double coefficient[10] = {0, 0, 0, 0, 0, 0, 1, -5, 16, -80};

/*main information*/
void info()
{
    int infoj = 0;
    for (int infoi = 0; infoi < 9; infoi++)
    {
        if (coefficient[infoi] != 0)
        {
            if (coefficient[infoi] > 0 && infoj != 0)
            {
                cout << " + ";
            }
            else
            {
                cout << " ";
            }
            if (coefficient[infoi] != 1)
            {
                cout << coefficient[infoi] << "*x";
            }
            else
            {
                cout << "x";
            }
            for (int infoifi = 1; infoifi < 9 - infoi; infoifi++)
            {
                cout << "*x";
            }
            infoj++;
        }
    }
    if (coefficient[9] > 0)
    {
        cout << " + " << coefficient[9];
    }
    else if (coefficient[9] < 0)
    {
        cout << " " << coefficient[9];
    }
    cout << " = 0       \n\n";
}

/*exchange the array*/
void exchange(double array[10])
{
    for (int exi = 0; exi < 5; exi++)
    {
        double temp = array[exi];
        array[exi] = array[9 - exi];
        array[9 - exi] = temp;
    }
}

/*array show*/
void show_array(double array[10])
{
    for (int showi = 0; showi < 10; showi++)
    {
        cout << "\n" << array[showi];
    }
}

/*f*/
double f(double x)
{
    return coefficient[0]
         + coefficient[1] * x
         + coefficient[2] * x * x
         + coefficient[3] * x * x * x
         + coefficient[4] * x * x * x * x
         + coefficient[5] * x * x * x * x * x
         + coefficient[6] * x * x * x * x * x * x
         + coefficient[7] * x * x * x * x * x * x * x
         + coefficient[8] * x * x * x * x * x * x * x * x
         + coefficient[9] * x * x * x * x * x * x * x * x * x;
}

/*xian x*/
double xpoint(double x1, double x2)
{
    return (x1 * f(x2) - x2 * f(x1))/(f(x2) - f(x1));
}

/*get root*/
double root(double x1, double x2)
{
    double x, y, y1 = f(x1);
    do
    {
        x = xpoint(x1, x2);
        y = f(x);
        if (y * y1 > 0)
        {
            y1 = y;
            x1 = x;
        }
        else
        {
            x2 = x;
        }
    }while (fabs(y) >= 0.00001);
    return x;
}

/*source code*/
void test()
{
    //input code here......
    exchange(coefficient);
    cout << setiosflags(ios::fixed) << setprecision(7);
    cout << root(1, 2) << endl;
    cout << root(2, 3) << endl;
    cout << root(9, 1) << endl;
}

/*information of source code owner*/
void end()
{
    cout << "\n\n______________\n";
    cout << "loveplus\n";
    cout << "inxk@163.com\n";
    cout << "Sep,2006";
    getchar();
}

/*the main code*/
void main()
{
    info();
    test();
    end();
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -