char 값을 읽는 중 문제가 있습니다. 아래 프로그램을 참조하십시오. 중위 표현을 평가하고 싶습니다.
'10', '*', '20'을 읽고 사용하고 싶습니다. 그러나 문자열 인덱서를 사용할 경우 [0]은 '1'이되고 '10'이 아니므로 기대 한 결과를 얻을 수 없습니다. 너희들은 나에게 뭔가 제안 할 수 있니? 운동을 의미 정확히 방법 당신이 문자열을 분할 할 - 코드는 문자열을 분할해야합니다인덱서를 사용하여 문자열에서 2 개의 문자를 가져 오는 문제가 발생했습니다.
class Program
{
static void Main(string[] args)
{
string infix = "10*2+20-20+3";
float result = EvaluateInfix(infix);
Console.WriteLine(result);
Console.ReadKey();
}
public static float EvaluateInfix(string s)
{
Stack<float> operand = new Stack<float>();
Stack<char> operator1 = new Stack<char>();
int len = s.Length;
for (int i = 0; i < len; i++)
{
if (isOperator(s[i])) // I am having an issue here as s[i] gives each character and I want the number 10
operator1.Push(s[i]);
else
{
operand.Push(s[i]);
if (operand.Count == 2)
Compute(operand, operator1);
}
}
return operand.Pop();
}
public static void Compute(Stack<float> operand, Stack<char> operator1)
{
float operand1 = operand.Pop();
float operand2 = operand.Pop();
char op = operator1.Pop();
if (op == '+')
operand.Push(operand1 + operand2);
else
if(op=='-')
operand.Push(operand1 - operand2);
else
if(op=='*')
operand.Push(operand1 * operand2);
else
if(op=='/')
operand.Push(operand1/operand2);
}
public static bool isOperator(char c)
{
bool result = false;
if (c == '+' || c == '-' || c == '*' || c == '/')
result = true;
return result;
}
}
}
을 부결 나는 y는이 질문했다 이해하지? – Learner
나는 페이지의 하단에있는 www.twipler.com/experiment 소스 코드 링크와 비슷한 것을 해왔다. –