I am trying to make a calculator. Whenever I put in something like 25 * 4 / 10 , it divides 25 by 4. Here is the part of the code I think might be the problem:
private void button16_Click(object sender, RoutedEventArgs e)
{
string[] calCulation = CALCULATION.Text.Split('-', '+', '/', 'X');
int numOfItems = calCulation.Length;
int count = 1;
char[] Arius = new char[Hey.Length];
foreach(char words in Hey)
{
int outlie = 0;
Arius[outlie] = words;
outlie++;
}
decimal final = 0M;
decimal[] calCulate = new decimal[numOfItems];
int countfreak = 0;
foreach (string word in calCulation)
{
calCulate[countfreak] = Convert.ToDecimal(word);
countfreak++;
}
int counting = 1;
int countinghey = 0;
decimal final2 = calCulate[0];
while(count < numOfItems){
switch(Arius[countinghey])
{
case 'X':
/*
final2 += final * calCulate[counting -1];
final2 = final2 * calCulate[counting];
*/
final2 = final2 * calCulate[counting];
break;
case '-':
final2 = final2 - calCulate[counting];
break;
case '+':
final2 = final2 + calCulate[counting];
break;
case '/':
final2 = final2 / calCulate[counting];
break;
}
counting++;
countinghey++;
count++;
}
开发者_如何学运维 CALCULATION.Text = Convert.ToString(final2);
}
public bool Parshing(string value, string typee)
{
int hixty = value.Length;
string six = value.Substring(hixty - 1, value.Length - hixty + 1);
int lam;
bool result = Int32.TryParse(six, out lam);
if (result == true||six == "")
{
CALCULATION.Text += typee;
Hey += typee;
}
else
{
}
return result;
}
Right off the bat, you are not splitting correctly.
string[] calCulation = CALCULATION.Text.Split('-', '+', '/', 'X');
given that you typed:
25 * 4 / 10
you should change your split from 'X' to '*'
string[] calCulation = CALCULATION.Text.Split('-', '+', '/', '*');
You will further need to change your case statement. Or make sure your input is correct.
精彩评论