在JavaScript中将Infix转换为前缀表示法 [英] Converting Infix to prefix notation in JavaScript

查看:111
本文介绍了在JavaScript中将Infix转换为前缀表示法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

(过去我确实问过类似的问题,但是文档是错误的,所以这是该过去问题的正确版本)

(I did ask a similar question in the past, but the documentation was wrong, so this is the correct version of that past question)

请用JavaScript帮我:我正在编码的程序是一个采用前缀表示法的表达式并以中缀表示法输出相同的表达式的程序.该程序背后的思想如下:

Please help me in JavaScript: The program that I am coding is one that takes in an expression in prefix notation and outputs the same expression in infix notation. The idea behind this program is as follows:

如果用户输入+ 1 2,则预期输出为1 +2.所有有效符号为+,-,*,/和%.用户可以输入的数字数量应该是无限的(例如,如果我输入+ + + + + + + + + 1 2 3 4 5 6 7 8 9 10,则程序应返回1 + 2 + 3 + 4 + 5 + 6 + 7 + 8 + 9 + 10).

if the user enters + 1 2 the expected output is 1 + 2. All valid symbols are +, -, *, /, and %. The amount of numbers that the user can enter should be limitless (so for example, if I enter + + + + + + + + + 1 2 3 4 5 6 7 8 9 10, the program should return 1 + 2 + 3 + 4 + 5 + 6 + 7 + 8 + 9 + 10).

有人可以帮我填写循环中的注释部分吗,如果您认为有解决此问题的更好方法,那么我很乐意!

Could someone please help me fill in the comment out portion of the loop, and if you think there's a better approach to the problem entirely, I am open to that!

function infix(input) {
  var x = input.split(''); // splits each variable and stores it in an array
  var output = [];
  var final = " "; // will be used to store our infix expression
  for (var i = 0; i < x.length; i++) {
    //if x[i] is any of the following : "+, -, *, /, or %" , store it in array output at index 0
    //else if x[i] is a number : store it in an index of array output that is >= 1

  }
  for (var j = 0; j < output.length; j++) {
    var final = x[0] + x[j];
  }
  console.log(final);
}

infix("1 + 2 + 3") // should output "+ + 1 2 3"
infix("1 - 2 % 3 + 1 * 4") // should output "- % + * 1 2 3 1 4"

推荐答案

您的代码大部分位于此处.

Your code is mostly there.

程序的主要逻辑需要将输入字符串分成两个部分(数组).一个用于符号/operands,另一个用于numbers.检查x[i]是否为"+,-,..."将使我们能够检查当前符号是否为操作数,这是一个很好的开始.但是,我们不想将其存储在output数组的索引0中.如果这样做,则所有符号都将以相反的顺序输入(这不是您想要的).相反,您可以将符号添加到数组的末尾.在下面的代码片段中,我已将所有符号压入operands数组.

The main logic of your program needs to split your input string into two sections (arrays). One for symbols/operands, and another for numbers. Checking if x[i] is "+, -, ..." will allow us to check if the current symbol is an operand, which is a good start. However, we don't want to store it at index 0 of the output array. If you did this, then all of the symbols would be inputted in reverse order (which isn't what you want). Instead, you can add your symbols to the end of your array. In the code snippet below, I have pushed all symbols into the operands array.

那么,我们如何检查x[i]是否是符号?一种方法是像这样检查每个符号的x[i]:

So, how can we check if x[i] is a symbol? One way is to check x[i] against every symbol like so:

if(x[i] === "+" || x[i] === "-" || ... ) {
  operands.push(x[i]); // add the symbol to the end of the array  
}

或者,您可以使用.includes()更加简洁地编写此代码,从而无需使用多个||:

Or, you could write this a little more cleanly using .includes(), which removes the need to multiple ||:

var symbols = ['+', '-', '*', '/', '%'];
...
if(symbols.includes(x[i])) {
  operands.push(x[i]); 
}

如果当前字符(x[i])不是符号,则它必须是数字(因为我们在空格处分割).因此,您可以添加else,并将当前字符推入numbers数组.

If the current character (x[i]) isn't a symbol then it must be a number (as we split on spaces). So, you can then add an else, and push the current character into the numbers array.

遍历所有字符后,将有一个操作数数组(按它们在输入字符串中出现的顺序)和一个数字数组(也按它们在输入字符串中出现的顺序).我们的最后一步是将operandsnumbers数组转换为字符串,其中这些数组中的每个元素都由空格分隔.最简单的方法是使用.join()方法,该方法会将数组中的每个元素连接到一个字符串中,并用您提供的参数将每个元素分隔开.

After you've looped through all characters, you'll have an array of operands, in the order that they appeared in the input string, and an array of numbers, also in the order they appeared in the input string. Our last step is to convert the operands and numbers array into strings, where each element in these arrays are separated by a space. The easiest way to do this is by using the .join() method, which will join every element in your array into a string, separating each element by the argument you give it.

通过所有这些操作,您可以获得所需的输出:

By doing all of this, you can get your desired output:

function infix(input) {
  var x = input.split(' '); // splits each character and stores it in an array
  var operands = [];
  var numbers = [];
  var symbols = ['+', '-', '/', '*', '%'];

  for (var i = 0; i < x.length; i++) {
    if(symbols.includes(x[i])) {
      operands.push(x[i]);
    } else {
      numbers.push(x[i]);
    }
  }
  var final = operands.join(' ') +' ' +numbers.join(' ');
  return final;
}

console.log(infix("1 + 2 + 3")); // "+ + 1 2 3"
console.log(infix("1 - 2 % 3 + 1 * 4")); // "- % + * 1 2 3 1 4"

或者,通过使用.match()方法获取非数字(或空格)字符,使用另一个获取数字字符,您可以更简洁地做到这一点:

Or, you can do this more concisely by using the .match() method to get the non-numeric (or space) characters and another to get the numeric characters:

const infix = input =>
   [...input.match(/[^\d\s]/g), ...input.match(/\d+/g)].join(' ');

console.log(infix("1 + 2 + 3")); // "+ + 1 2 3"
console.log(infix("1 - 2 % 3 + 1 * 4")); // "- % + * 1 2 3 1 4"

这篇关于在JavaScript中将Infix转换为前缀表示法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆