用字符串中的字符替换为其值?

发布于 2025-02-05 20:42:37 字数 1401 浏览 3 评论 0原文

我有一个给定的字符串(a+b)&(a+c),并且创建了一个带有a,b和c值的真相表。现在,问题是通过用真实表中的相应值替换A,B和C来评估逻辑表达式。如何在C中完成?

ex:a = 0 b = 0 c = 0 r =(0+0)&(0+)= 0 a = 0 b = 0 c = 1 r =(0+0)&(0+1)= 0 等等

代码本身看起来像这样

#include <stdio.h>
#include <stdlib.h>
int main()
{
    char c,* str, *vars, **result;
    int i=0,count=0,j=0;
    unsigned long long rows;
    str = (char*) malloc(1*sizeof(char));
    vars=(char*) malloc(1*sizeof(char));
    result=(char**)malloc(1*sizeof(char));
    char values[] = {'F', 'T'};

    while ((c = getchar()) != EOF)
    {
        str[i++] = c;
        str = (char*) realloc(str, (i+1) * sizeof(char));
        if (c >= 'a' && c <= 'z')
        {
            vars[j++]=c;
            vars=(char*) realloc(vars,(j+1)*sizeof(char));
            count++;
        }
    }
    rows=1ULL<<(count);
    result=(char**)realloc(result,(rows+2)*sizeof(char));

    for (i = 0; i < rows+1; i++)
    {
        result[i]=(char*)malloc(sizeof(char)*(count+1));
        for (j = 0; j < count; j++)
        {
            if(i==0)
                result[i][j]=vars[j];
            else
                result[i][j]=values[(i >> j) & 1];
        }
    }

    result[0][count]='R';

    for(i=0;i<rows+1;i++)
    {
        for(j=0;j<count+1;j++)
        {
            //do something
        }

    }

I have a string given (a+b)&(a+c) and I have created a truth table with values of a,b, and c. Now the problem is to evaluate the logic expression by substituting a,b, and c with corresponding values from the truth table. How it can be done in C?

Ex: a=0 b=0 c=0 r=(0+0)&(0+)=0
a=0 b=0 c=1 r=(0+0)&(0+1)=0
and so on

The code itself looks like this

#include <stdio.h>
#include <stdlib.h>
int main()
{
    char c,* str, *vars, **result;
    int i=0,count=0,j=0;
    unsigned long long rows;
    str = (char*) malloc(1*sizeof(char));
    vars=(char*) malloc(1*sizeof(char));
    result=(char**)malloc(1*sizeof(char));
    char values[] = {'F', 'T'};

    while ((c = getchar()) != EOF)
    {
        str[i++] = c;
        str = (char*) realloc(str, (i+1) * sizeof(char));
        if (c >= 'a' && c <= 'z')
        {
            vars[j++]=c;
            vars=(char*) realloc(vars,(j+1)*sizeof(char));
            count++;
        }
    }
    rows=1ULL<<(count);
    result=(char**)realloc(result,(rows+2)*sizeof(char));

    for (i = 0; i < rows+1; i++)
    {
        result[i]=(char*)malloc(sizeof(char)*(count+1));
        for (j = 0; j < count; j++)
        {
            if(i==0)
                result[i][j]=vars[j];
            else
                result[i][j]=values[(i >> j) & 1];
        }
    }

    result[0][count]='R';

    for(i=0;i<rows+1;i++)
    {
        for(j=0;j<count+1;j++)
        {
            //do something
        }

    }

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

枕花眠 2025-02-12 20:42:37

现在问题是通过用真实表中的相应值替换A,B和C来评估逻辑表达式。

除了问题评论中提到的问题外,仅替换 无法评估逻辑表达式。例如,在评估表达式时替换值。 (您没有指定表达式的一般语法,因此我选择支持使用的运算符和较低的案例变量的组合。)

#include <ctype.h>
#include <string.h>

int indx(char *s, char c) { return strchr(s, c)-s; }

char *gstr, *gvars, *vals;  // expression string, variables, value combination

char eval()
{   // evaluate expression "gstr"
    char or = 0;    // neutral element of +
    do
    {
        char and = 1;   // neutral element of &
        do
        {
            char c = *gstr++;   // get next token
            if (islower(c))
                and &= indx("FT", vals[indx(gvars, c)]);
            else
            if (c == '(')
            {   // evaluate subexpression
                and &= eval();
                c = *gstr++;    // get next token
                if (c != ')')
                    printf("error at '%c': expected ')'\n", c), exit(1);
            }
            else
                printf("error at '%c'\n", c), exit(1);
        } while (*gstr == '&' && ++gstr);
        or |= and;
    } while (*gstr == '+' && ++gstr);
    return or;
}

可以从您的main(插入代码,因此,都可以调用它不一致的间距)

    result[0][count]='R';
    gvars = vars;   // make variable names globally accessible
    for (i = 1; i <= rows; ++i)
    {
        gstr = str, vals = result[i],   // globally accessible
        result[i][count] = values[eval()];
        while (isspace(*gstr)) ++gstr;
        if (*gstr)
            printf("error at '%c': expected end of input\n", *gstr), exit(1);
    }

    for(i=0;i<rows+1;i++)
    {
        for(j=0;j<count+1;j++)
        {
            putchar(result[i][j]);
        }
        putchar('\n');
    }

(不要忘记将str [i] ='\ 0';在您的getchar循环中进行。)请注意由于循环计数的给定,真实表条目的顺序有些不寻常,因为所有变量的行f是最后的。

Now the problem is to evaluate the logic expression by substituting a,b, and c with corresponding values from the truth table.

Aside from the issues mentioned in the question's comments, substituting alone won't do the job to evaluate the logic expression. The following function for example substitutes the values while evaluating the expression. (You didn't specify the general syntax of your expressions, so I chose to support combinations of the used operators and lower case variables.)

#include <ctype.h>
#include <string.h>

int indx(char *s, char c) { return strchr(s, c)-s; }

char *gstr, *gvars, *vals;  // expression string, variables, value combination

char eval()
{   // evaluate expression "gstr"
    char or = 0;    // neutral element of +
    do
    {
        char and = 1;   // neutral element of &
        do
        {
            char c = *gstr++;   // get next token
            if (islower(c))
                and &= indx("FT", vals[indx(gvars, c)]);
            else
            if (c == '(')
            {   // evaluate subexpression
                and &= eval();
                c = *gstr++;    // get next token
                if (c != ')')
                    printf("error at '%c': expected ')'\n", c), exit(1);
            }
            else
                printf("error at '%c'\n", c), exit(1);
        } while (*gstr == '&' && ++gstr);
        or |= and;
    } while (*gstr == '+' && ++gstr);
    return or;
}

It can be called from your main (inserted in your code, hence the inconsistent spacing)

    result[0][count]='R';
    gvars = vars;   // make variable names globally accessible
    for (i = 1; i <= rows; ++i)
    {
        gstr = str, vals = result[i],   // globally accessible
        result[i][count] = values[eval()];
        while (isspace(*gstr)) ++gstr;
        if (*gstr)
            printf("error at '%c': expected end of input\n", *gstr), exit(1);
    }

    for(i=0;i<rows+1;i++)
    {
        for(j=0;j<count+1;j++)
        {
            putchar(result[i][j]);
        }
        putchar('\n');
    }

(Don't forget to put str[i] = '\0'; after your getchar loop to make a null-terminated string.) Note that due to the given for loop counting, the order of the truth table entries is somewhat unusual in that the row with all variables F comes last.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文