码迷,mamicode.com
首页 > 其他好文 > 详细

A Simple Problem About Truth Table

时间:2017-07-02 13:41:53      阅读:151      评论:0      收藏:0      [点我收藏+]

标签:排列   break   note   输入   进制   sort   初始化   代码   .com   

技术分享

源代码:

#include<iostream>
#include<string>
#include<cmath>
using namespace std;

// 一个全局变量
char num[10];                      
//函数声明
void p_sort(const string & binary, int n);
void to_binary(string binary, int i, int n, string expression);
void judgement(string expression);
//函数定义
void to_binary(string binary, int i, int n, string expression)
{
    int x = i, j = n, count = 0, note;
    while (x != 0)
    {
        binary[j - 1] = (x % 2) + 48;    // 将余数赋值给 binary 数组 
        x /= 2;                     // 将除数赋值给 x  
        j--;
    }
    p_sort(binary, n);
    for (i = 0; i < expression.length(); i++)           //将字母与存储顺序一一对应
    {
        if (a <= expression[i] && expression[i] <= z)
        {
            for (j = 0; j < n; j++)
                if (expression[i] == num[j]) expression[i] = j + 48;
        }
    }
    for (i = 0; i < expression.length(); i++) //将所有的字母修改为 T or F。
    {
        if (0 <= expression[i] && expression[i] <= 9)
        {
            for (j = 0; j < n; j++) 
            {
                if (expression[i] == j + 48)
                    expression[i] = binary[j] == 1 ? T : F;
            }
        }
    }
    for (i = 0; i < expression.length(); i++)//将所有前面有 ‘~‘ 号的字母和第一个 ‘~‘ 修改为 T or F
    {
        if (expression[i] == ~)
        {
            count++;
            if (count == 1) note = i;
        }
        if (expression[i] == ~ && expression[i + 1] != ~)
        {
            if (count % 2 != 0) expression[note] = expression[i + 1] = expression[i + 1] == T ? F : T;
            else expression[note] = expression[i + 1];
            count = 0;
        }
    }
    judgement(expression);
}
void p_sort(const string & binary, int n)
{
    int i;

    for (i = 0; i < n; i++)
    {                        // 打印 3 ~ n 行真值表 
        if (binary[i] == 1) cout << "T  ";
        else cout << "F  ";
    }
}
void judgement(string expression)    // 判断表达式的最终结果
{
    int i;
    char res = T;
    for (i = 0; i < expression.length(); i++)
    {
        switch (expression[i]) 
        {
        case*:               //A AND B
            if (expression[i + 1] == T && expression[i - 1] == T) res = expression[i + 1] = T;
            else res = expression[i + 1] = F;
            break;
        case+:               //A OR B
            if (expression[i + 1] != F || expression[i - 1] != F) res = expression[i + 1] = T;
            else res = expression[i + 1] = F;
            break;
        case%:               //A XOR B
            if (expression[i + 1] == expression[i - 1]) res = expression[i + 1] = F;
            else res = expression[i + 1] = T;
            break;
        case>:               //A IMPLY B
            if (expression[i - 1] == T && expression[i + 1] == F) res = expression[i + 1] = F;
            else res = expression[i + 1] = T;
            break;
        case^:               //A IF AND ONLY IF B
            if (expression[i - 1] == T && expression[i + 1] == F) res = expression[i + 1] = F;
            else res = expression[i + 1] = T;
            break;
        default:
            break;
        }
    }
    cout << "|  " << res << endl;
}

int main()
{
    int n, i, count, tem_n;
    string binary = "            ";
    string temp = "            ";
    string expression;
    while (cin >> n)
    {

        for (i = 0; i < n; i++) // 输入数据 
            cin >> num[i];


        for (i = 0; i < n; i++) // 初始化二进制转换 
            binary[i] = 0;

        temp = binary;

        cin >> expression; // 输入表达式 

        for (i = 0; i < n; i++) // 打印第一行真值表 
            cout << num[i] << "  ";
        cout << "|  ";
        cout << expression;
        cout << "\n";
        for (i = 0; i < (n + 1) * 3 + expression.length(); i++)
            cout << "-";
        cout << endl;

        tem_n = n;

        i = n = pow(2, n); // 计算排列组合的基数 例如 2 ^ 3 = 8;

        while (i--)
        {
            temp = binary;
            to_binary(temp, i, tem_n, expression); //十进制转二进制 
            
        }
        cout << endl;
    }
    return 0;
}

技术分享

 

 

A Simple Problem About Truth Table

标签:排列   break   note   输入   进制   sort   初始化   代码   .com   

原文地址:http://www.cnblogs.com/Breathmint/p/7105746.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!