c#使用堆栈方法对后缀进行修复 [英] c# infix to postfix using stack method
问题描述
我设法解决了将中缀转换为后缀(使用堆栈)的问题,但是
一次只能处理一位数字.所以这意味着我正在使用字符对其进行扫描
从左到右.
请帮助我或给我一些技巧,使我的代码可以处理两位数或更多位数.
示例:
每个操作数只有一位
输入中缀为:2 + 3 * 6/7
每个操作数都有两个或多个数字.
输入中缀为:23 + 345 * 6/32
在我的代码中:
我将输入字符串转换为字符数组
input.toCharArray();
然后从左到右扫描阵列.
字符串输入= txtInput.Text;
char [] toArray = new char [input.Length];
toArray = input.ToCharArray();
创建评估每个字符的方法[示例代码]:
公共无效Evaluate(char [] array)
{
for(int i = 0; i< array.Length; i ++)
{
char current = array [i];
开关(当前)
{
大小写``+'':
大小写``-'':
处理(1,当前);
休息;
大小写``*'':
大小写''/'':
处理(2,当前);
休息;
大小写``('':
st.Push(current);
休息;
情况'')'':
while(!this.isEmpty())
{
char pop =(char)st.Pop();
如果(pop ==''('')
休息;
其他
postString + = pop;
}
休息;
默认值:
如果(char.IsLetterOrDigit(current))
{
postString + = current.ToString();
}
休息;
}
}
试试
{
while(!this.isEmpty())
{
char pop =(char)st.Pop();
MessageBox.Show(左移:" + pop);
postString + = pop;
}
}
赶上
{
}
==========================谢谢你======================= ============
I have managed to solve the problem in converting infix to postfix (using stack) but
it can only handle one digit at a time. so it means i am using character to scan it
from left to right.
Please help me or give me some tricks on how i can make my code handle two digits or more.
Example:
This has only one digit every operand
input infix is : 2+3*6/7
This has two or more digit every operand.
input infix is : 23+345*6/32
In my code:
I convert the input string into an array of characters
input.toCharArray();
and scan the the array from left to right.
string input = txtInput.Text;
char[] toArray = new char[input.Length];
toArray = input.ToCharArray();
Creating method to evaluate each character[Example code]:
public void Evaluate(char[] array)
{
for (int i = 0; i < array.Length; i++)
{
char current = array[i];
switch (current)
{
case ''+'':
case ''-'':
Process(1, current);
break;
case ''*'':
case ''/'':
Process(2, current);
break;
case ''('':
st.Push(current);
break;
case '')'':
while (!this.isEmpty())
{
char pop =(char) st.Pop();
if (pop == ''('')
break;
else
postString += pop;
}
break;
default:
if (char.IsLetterOrDigit(current))
{
postString += current.ToString();
}
break;
}
}
try
{
while (!this.isEmpty())
{
char pop = (char)st.Pop();
MessageBox.Show("Popped left: " + pop);
postString += pop;
}
}
catch
{
}
==========================Thank You==================================
推荐答案
如果您要从字符数组中进行操作并且可以采用整数值,那么当您获得一个数字值,则需要累加该数字,但通常无法将其存储在char变量中.
有可用的通用解析器(使用Google),但是如果我要编写一个通用解析器,我不会以字符数组开头,而是解析包含数学符号,关键字,数字,变量名称等的令牌".
If you are going to do it from a character array and can assume integer values, then when you get a numeric value, you will need to accumulate the number, but you won''t generally be able to store that in a char variable.
There are generalized parsers available (use Google), but if I were going to write one, I would not start with a character array, but parse "tokens" that would be math symbols, keywords, numbers, variable names, etc.
这篇关于c#使用堆栈方法对后缀进行修复的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!