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

📄 d4r2.cpp

📁 Visual C++ 常用数值算法集 源代码
💻 CPP
字号:
#include <iostream.h>
#include <math.h>
#include <iomanip.h>
#include <stdlib.h>
#include <fstream.h>
#include <string>

double gammln(double xx)
{
    int j;
    float temp;
    double cof[6],stp,half,one,fpf,x,tmp,ser;
    cof[1] = 76.18009173;
    cof[2] = -86.50532033;
    cof[3] = 24.01409822;
    cof[4] = -1.231739516;
    cof[5] = 0.00120858003;
    cof[6] = -0.00000536382;
    stp = 2.50662827465;
    half = 0.5;
    one = 1.0;
    fpf = 5.5;
    x = xx - one;
    tmp = x + fpf;
    tmp = (x + half) * log(tmp) - tmp;
    ser = one;
    for (j = 1;j<=6;j++)
    {
        x = x + one;
        ser = ser + cof[j] / x;
    }
    temp = tmp + log(stp * ser);
    return temp;
}
double factrl(int n)
{
    double a[33],temp;
    int ntop,j;
    ntop = 0;
    a[1] = 1.0;
    if( n < 0 )
    {
        cout<<"negative factorial";
        exit(1);
    }
    else if (n <= ntop)
    {
        temp = a[n + 1];
    }
    else if (n <= 32)
    {
        for( j = ntop + 1;j<=n;j++)
            a[j + 1] = j * a[j];
        ntop = n;
        temp = a[n + 1];
    }
    else
        temp = exp(gammln(n + 1.0));
    return temp;
}


void main()
{
    //program d4r2
    //driver for routine factrl
    int i,x;
    char text[20];
    double nval,actual; 
    const double pi = 3.1415926;
    fstream fin;
    fin.open("d:\\vc常用数值算法集\\data\\fncval.dat",ios::in);
    while (strcmp(text,"N-factorial")!=0)
    {
        fin>>text;
    }
    fin>>nval; 
    cout<<text<<" "<<endl;
    fin>>text;
    cout<<endl;
    cout<<"   x            actual         facval(n)"<<endl;
    for (i = 1; i<=nval; i++)
    {
        fin>>x;
        fin>>actual;
        if (actual<10000000000.0)
        {
            cout<<setw(4)<<x;
            cout<<setw(18)<<actual;
            cout<<setw(18)<<factrl(x)<<endl;
        }
        else 
        {
            cout<<setw(4)<<x;
            cout<<setw(18)<<actual;
            cout<<setw(18)<<factrl(x)<<endl;
        }
    }
}

⌨️ 快捷键说明

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