d2r5.cpp

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

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

void main()
{
    //program d2r5
    //driver for routine locate
    double x,xx[101];
    int i,j,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<<endl;
    cout<<"   locate      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;
        locate(xx, n, x, j);
        if (j == 0)
		{ 
            cout<<setprecision(1)<<setw(8)<<x;
            cout<<setprecision(0)<<setw(8)<<j;
            cout<<setprecision(6)<<setw(15)<<"lower lim";
            cout<<setprecision(6)<<setw(15)<<xx[j + 1]<<endl;
		}
        else 
		{
			if (j == n )
			{
				cout<<setprecision(1)<<setw(8)<<x;
				cout<<setprecision(0)<<setw(8)<<j;
				cout<<setprecision(6)<<setw(15)<<xx[j];
				cout<<setprecision(6)<<setw(15)<< "upper lim"<<endl;
			}
            else
			{
				cout<<setprecision(1)<<setw(8)<<x;
				cout<<setprecision(0)<<setw(8)<<j;
				cout<<setprecision(6)<<setw(15)<<xx[j];
				cout<<setprecision(6)<<setw(15)<<xx[j + 1]<<endl;
			}
        }
    }
} 

⌨️ 快捷键说明

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