char値の読み取りに問題があります。 以下のプログラムを参照してください。中置式を評価したい
「10」、「*」、「20」を読み込んで使用したいのですが、文字列インデクサーs [0]が '1'で '10'私は期待される結果を得ることができません。 あなたは私に何かを提案できますか?あなたは文字列を分割する方法を正確をワークアウトを意味します - コードは、文字列を分割する必要がありますC#でインデクサーを使用して文字列から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ソースコードリンク。 –