真值表(C++)

     对于 真值表都不陌生。其原理略 

     代码如下:


#include <iostream>
#include <cmath>

void TruthTable(const int n);
enum { FALSE, TRUE };

int main()
{
    const int nBoolNum = 5;
    TruthTable(nBoolNum);
    return 0;
}

void TruthTable(const int N)
{
    int nState = (int)pow(2.0, N); 
    int nTotalNum = nState * N;
    int* nPtr = new int[nTotalNum];
    int nRem, nDiv, nCol;
    int nCount = 0; // N incerments 
    for (int nIndex = 0; nIndex < nState; ++nIndex)
    {
        nDiv = nIndex;
        for (nCol = N - 1 + nCount; nCol >= 0 + nCount; --nCol)
        {
            nRem = nDiv % 2;
            nDiv /= 2;
            if (!nRem) 
                nPtr[nCol] = FALSE;
            else 
                nPtr[nCol] = TRUE;
        }
        for (int i = nCount; i < N + nCount; ++i) // Display
            std::cout << nPtr[i] << '\t';
        nCount += N;
        std::cout << std::endl;
    }
    delete[] nPtr;
}

运行结果:正确且每行生成的数值对应成功


真值表(C++)_第1张图片






你可能感兴趣的:(C++,delete)