xdecchk.cpp

来自「这是C++数值算法(第二版)的源代码,其中包含了目前一些比较常用的数值计算的算法」· C++ 代码 · 共 57 行

CPP
57
字号
#include <string>
#include <iostream>
#include <iomanip>
#include "nr.h"
using namespace std;

// Driver for routine decchk

int main(void)
{
        int j,k,l,n,nbad=0,ntot=0;
        bool iok,jok;
        char ch,chh;
        string lin;

        // test all jump transpositions of the form 86jlk41
        for (j=48;j<=57;j++) {
          for (k=48;k<=57;k++) {
            for (l=48;l<=57;l++) {
              lin="86xxx41";
              if (j != k) {
                ntot++;
                lin[2]=char(j);
                lin[3]=char(l);
                lin[4]=char(k);
                iok=NR::decchk(lin,ch);
                lin += ch;
                iok=NR::decchk(lin,chh);
                lin[2]=char(k);
                lin[4]=char(j);
                jok=NR::decchk(lin,chh);
                if (!iok || jok) nbad++;
              }
            }
          }
        }
        cout << "Total tries:" << setw(15) << " " << setw(3) << ntot << endl;
        cout << "Bad tries:" << setw(17) << " " << setw(3) << nbad << endl;
        cout << "Fraction good:" << setw(12) << " ";
        cout << fixed << setprecision(2);
        cout << setw(4) << DP(ntot-nbad)/ntot << endl;
        for (;;) {
          cout << "enter string terminated by <CR> (or just <CR> to end):";
          cout << endl;
          getline(cin,lin);
          n=lin.length();
          if (n == 0) break;
          iok=NR::decchk(lin,ch);
          lin += '-';
          lin += ch;
          jok=NR::decchk(lin,chh);
          cout << lin << " checks as " << (jok ? 'T' : 'F') << endl;
        }
        cout << "Normal completion" << endl;
        return 0;
}

⌨️ 快捷键说明

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