A Simple Problem About Truth Table

源代码:

#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;
}

 

时间: 2024-10-27 07:55:44

A Simple Problem About Truth Table的相关文章

NYOJ 707 A Simple Problem(结构体排序) 睡前一水~~

链接:click here 题意: A Simple Problem 时间限制:3000 ms  |  内存限制:65535 KB 难度:2 描述 You know, just as the title imply, this is a simple problem. In a contest, given the team-id, solved, penalty of all the teams, tell me the champion.If the numbers of solved pr

E - A Simple Problem with Integers

#include <iostream> #include <stdio.h> #include <string.h> #include <stdlib.h> using namespace std; #define N 100002 struct node { int l,r; long long lz,w; }q[4*N]; void pushup(int rt) { q[rt].w=q[rt*2].w+q[rt*2+1].w; } void pushdo

POJ 3468 A Simple Problem with Integers

链接:http://poj.org/problem?id=3468 A Simple Problem with Integers Time Limit: 5000MS Memory Limit: 131072K Total Submissions: 77302 Accepted: 23788 Case Time Limit: 2000MS Description You have N integers, A1, A2, ... , AN. You need to deal with two ki

poj 3468 A Simple Problem with Integers(线段树+区间更新+区间求和)

题目链接:id=3468http://">http://poj.org/problem? id=3468 A Simple Problem with Integers Time Limit: 5000MS   Memory Limit: 131072K Total Submissions: 83959   Accepted: 25989 Case Time Limit: 2000MS Description You have N integers, A1, A2, ... , AN. Yo

poj3468 A Simple Problem with Integers 线段树区间更新

A Simple Problem with Integers Time Limit: 5000MS   Memory Limit: 131072K Total Submissions: 97722   Accepted: 30543 Case Time Limit: 2000MS Description You have N integers, A1, A2, ... , AN. You need to deal with two kinds of operations. One type of

POJ 3468 A Simple Problem with Integers(树状数组区间更新)

A Simple Problem with Integers Time Limit: 5000MS   Memory Limit: 131072K Total Submissions: 97217   Accepted: 30358 Case Time Limit: 2000MS Description You have N integers, A1, A2, ... , AN. You need to deal with two kinds of operations. One type of

POJ-3468 A Simple Problem with Integers(线段树)

A Simple Problem with Integers Time Limit: 5000MS   Memory Limit: 131072K Total Submissions: 94899   Accepted: 29568 Case Time Limit: 2000MS Description You have N integers, A1, A2, ... , AN. You need to deal with two kinds of operations. One type of

hdu 5349 MZL&#39;s simple problem

Problem Description A simple problem Problem Description You have a multiple set,and now there are three kinds of operations: 1 x : add number x to set 2 : delete the minimum number (if the set is empty now,then ignore it) 3 : query the maximum numbe

【multiset】hdu 5349 MZL&#39;s simple problem

[multiset]hdu 5349 MZL's simple problem 题目链接:hdu 5349 MZL's simple problem 题目大意 n次操作,插入元素.删除最小元素.查询最大元素并输出. C++STL的multiset的使用 set--多元集合(元素不可重复),multiset--可重复元素的多元集合 多元集合(MultiSets)和集合(Sets)相像,只不过支持重复对象.(具体用法请参照set容器) set和multiset内部是以平衡二叉树实现的: 从内部数据结