【问题标题】:How to evaluate a string expression and return the value in C? [closed]如何评估字符串表达式并返回 C 中的值? [关闭]
【发布时间】:2013-01-12 14:07:43
【问题描述】:

我正在尝试评估一个作为字符数组的表达式并返回表达式的结果。

例如:

char *myeExpression []= "(1+2) * 3"

应该返回结果 9。

这是我的代码:

struct node {
double element;
struct node *next;
} *head;

void push(int c);      // function to push a node onto the stack
int pop();             // function to pop the top node of stack
void traceStack();      // function to //print the stack values

 int prece(char j)
{
if(j=='*'||j=='/')
{
   j=3;
}
else
{
    if(j=='+'||j=='-')
    {
       j=2;
    }
    else
    {
       j=1;
    }
}
return j;
}


 int evaluate(char *  a) {
int i = 0, j = 0,k,l,a1,b1;   // indexes to keep track of current position
char *exp = (char *)malloc(sizeof(char)*100);
double res = 0;

char stack[5];
char tmp;
head = NULL;

//  converting an infix to a postfix

for(i=0;i<10;i++)
{
    a1=prece(a[i]);
    b1=prece(stack[k]);
    if(a1<=b1)
    {
        exp[l]=a[i];
        l++;
    }
    else
    {
        stack[k]=a[i];
        k++;

    }
}
for(i=k;i>0;i--)
{
    exp[l]=stack[i];
    l++;
}



//end
i=0;
j=0;
k=0;


while( (tmp=exp[i++]) != '\0') {    // repeat till the last null terminator
    // if the char is operand, pust it into the stack
    if(tmp >= '0' && tmp <= '9') {
        int no = tmp - '0';
        push(no);
        continue;
    }

    if(tmp == '+') {
        int no1 = pop();
        int no2 = pop();
        push(no1 + no2);
    } else if (tmp == '-') {
        int no1 = pop();
        int no2 = pop();
        push(no1 - no2);
    } else if (tmp == '*') {
        int no1 = pop();
        int no2 = pop();
        push(no1 * no2);
    } else if (tmp == '/') {
        int no1 = pop();
        int no2 = pop();
        push(no1 / no2);
    }
}
return pop();

}

void push(int c) {
if(head == NULL) {
    head = malloc(sizeof(struct node));
    head->element = c;
    head->next = NULL;
} else {
    struct node *tNode;
    tNode = malloc(sizeof(struct node));
    tNode->element = c;
    tNode->next = head;
    head = tNode;
}
}

 int pop() {
struct node *tNode;
tNode = head;
head = head->next;
return tNode->element;
}

中缀表达式评估发生但不完全。 得到错误的结果,即 3 而不是 9。

【问题讨论】:

  • 如果这是您的完整代码,而您“迷路”了,那么您需要首先考虑您要做什么,考虑算法和步骤,然后继续前进。你还有很多事情要做。
  • Soory,发布了错误的代码..请立即检查。
  • 要求人们发现代码中的错误并不高效。您应该使用调试器(或添加打印语句)来隔离问题,然后构造一个minimal test-case
  • 您的堆栈将double 存储在struct node 中;您将值从堆栈中弹出到int。这似乎是一个奇怪的安排。对于调试,要么使用调试器单步调试代码,要么添加足够多的打印语句以查看发生了什么。
  • 首先做一个反向润色实现可能是一个很好的练习。 (例如,解析1 2+3*)。这更容易做到。

标签: c string expression evaluation


【解决方案1】:

您的代码没有明确地忽略或以其他方式处理空格;这是麻烦的原因之一吗?编译器指出:

  • k 未初始化
  • l 未初始化

这些未初始化的值用作数组的索引。您使用for (i = 0; i &lt; 10; i++) 扫描字符串,而不管其实际长度如何。这不是幸福的秘诀。

括号的优先级为 1(低);通常,它们具有很高的优先级。在从中缀转换为前缀时,您需要决定如何处理它们。

在将任何内容压入堆栈之前,您将事物与 stack[k] 的优先级进行比较。一般来说,您的中缀到前缀转换似乎不可靠。在继续之前,您应该专注于正确地做到这一点。

您必须学会在调试器中运行代码,逐行逐行查看问题所在,或者添加调试打印语句(这是我通常的工作方式)。

【讨论】:

    【解决方案2】:

    您可以学习 lex 和 yacc,因为它们消除了构建词法分析器和解析器的大部分困难。 “calc”是一个可以计算简单算术表达式的标准示例。您可以找到它,例如,here

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2022-01-17
      • 2020-04-10
      • 1970-01-01
      • 2011-08-03
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多