d2r6.cpp

来自「visual c++查过能用数值算法 附带光盘里面的源代码」· C++ 代码 · 共 58 行

CPP
58
字号
# include<iostream.h>
# include<math.h>
# include<iomanip.h>

void main()
{
    //program d2r6
    //driver for routine hunt
	double x,xx[101];
    int j,ji,i,n = 100;

    //create array to be searched
    for (i = 1;i<=n;i++)
        xx[i] = exp(double(i) / 20.0) - 74.0;
    cout<<endl;
    cout<< "Result of:    j=0 indicates x too small"<<endl;
    cout<< "              j=100 indicates x too large"<<endl;
    cout<< "   locate    guess     j         xx[j]          xx[j+1]"<<endl;
    //for test   
	cout<<setprecision(6)<<setiosflags(ios::fixed);
    for( i = 1; i<=19; i++)
	{
        x = -100.0 + 200.0 * i / 20.0;
    //trial parameter
        ji = 5 * i;
        j = ji;
    //begin search
         hunt(xx, n, x, j);
        if (j== 0 )
		{
            cout<<setprecision(1)<<setw(8)<<x;
            cout<<setprecision(0)<<setw(8)<<ji;
            cout<<setprecision(0)<<setw(8)<<j;
            cout<<setprecision(6)<<setw(16)<<"lower lim";
            cout<<setprecision(6)<<setw(16)<<xx[j + 1]<<endl;
		}
        else
		{
			if (j== n )
			{
				cout<<setprecision(1)<<setw(8)<<x;
				cout<<setprecision(0)<<setw(8)<<ji;
				cout<<setprecision(0)<<setw(8)<<j;
				cout<<setprecision(6)<<setw(16)<<xx[j];
				cout<<setprecision(6)<<setw(16)<<"upper lim"<<endl;
			}
            else
			{
				cout<<setprecision(1)<<setw(8)<<x;
				cout<<setprecision(0)<<setw(8)<<ji;
				cout<<setprecision(0)<<setw(8)<<j;
				cout<<setprecision(6)<<setw(16)<<xx[j];
				cout<<setprecision(6)<<setw(16)<<xx[j + 1]<<endl;
			}
        }
    }
} 

⌨️ 快捷键说明

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