c - 前缀到前缀的转换

标签 c algorithm expression infix-notation prefix-operator

我正在编写将中缀表达式转换为前缀的代码。如果输入此表达式,我的代码有问题

x*y+((z*k)-(n/m))

我得到的答案是这样的:+x*y**zk/nm
我花了很多时间在这个项目后面,但我还是不能让它工作。
正确答案是:+*xy-*zk/nm
查克的回答
# include <stdio.h>
# include <string.h>
# define MAX 20
void infixtoprefix(char infix[20], char prefix[20]);
void reverse(char array[30]);
char pop();
void push(char symbol);
int isOperator(char symbol);
int prcd(char symbol);
int top = -1;
char stack[MAX];

main() {
char infix[20], prefix[20], temp;
printf("Enter infix operation: ");
gets(infix);
infixtoprefix(infix, prefix);
reverse(prefix);
puts((prefix));
}
//--------------------------------------------------------
void infixtoprefix(char infix[20], char prefix[20]) {
int i, j = 0;
char symbol;
stack[++top] = '#';
reverse(infix);
for (i = 0; i < strlen(infix); i++) {
symbol = infix[i];
if (isOperator(symbol) == 0) {
  prefix[j] = symbol;
  j++;
} else {
  if (symbol == ')') {
    push(symbol);
  } else if (symbol == '(') {
    while (stack[top] != ')') {
      prefix[j] = pop();
      j++;
    }
    pop();
  } else {
    if (prcd(stack[top]) <= prcd(symbol)) {
      push(symbol);
    } else {
      while (prcd(stack[top]) >= prcd(symbol)) {
        prefix[j] = pop();
        j++;
      }
      push(symbol);
    }
  //end for else
     }
   }
  //end for else
   }
  //end for for
  while (stack[top] != '#') {
    prefix[j] = pop();
    j++;
   }
  prefix[j] = '\0';
 }
 ////--------------------------------------------------------
 void reverse(char array[30]) {
 // for reverse of the given expression
 int i, j;
 char temp[100];
 for (i = strlen(array) - 1, j = 0; i + 1 != 0; --i, ++j) {
  temp[j] = array[i];
 }
 temp[j] = '\0';
 strcpy(array, temp);//copying temp array to array
  // return array;
 }
 //--------------------------------
  char pop() {
  char a;
  a = stack[top];
  top--;
  return a;
  }
 //----------------------------------
 void push(char symbol) {
 top++;
 stack[top] = symbol; 
 }
 //------------------------------------------
 int prcd(char symbol) {
 // returns the value that helps in the precedence
 switch (symbol) {
  case '+':
   case '-':
    return 2;
    break;
  case '*':
   case '/':
    return 4;
    break;
   case '$':
   case '^':
    return 6;
    break;
   case '#':
   case '(':
   case ')':
    return 1;
    break;
  } 
}
//-------------------------------------------------
int isOperator(char symbol) {
switch (symbol) {
case '+':
case '-':
case '*':
case '/':
case '^':
case '$':
case '&':
case '(':
case ')':
  return 1;
  break;
default:
  return 0;
 // returns 0 if the symbol is other than given above
  }
 }

这个密码就是答案。

最佳答案

这是因为你不知道

(character == '-') && (stack[top] == '(')

关于c - 前缀到前缀的转换,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29348743/

相关文章:

c# - 按表达式树的可枚举选择

计算 C 字符串中单个字符(元音)的数量

algorithm - 确定一个点是否在矩形的并集内

java - 什么情况下归并排序比选择排序快

.net - 以Func体参数唯一标识匿名方法

expression - 你能简化这个表达式吗?

c - 字数调试

c++ - 使用 snprintf 填充结构体

c - 是否有任何用于 Windows 驱动程序开发的开源 C 库(不是 c++)?

algorithm - 强调实用细节的 C4.5 和 ID3 算法