Geavenceerde Calculator heeft een paar foutjes ?

  • Onderwerp starter Onderwerp starter Yvar
  • Startdatum Startdatum
Status
Niet open voor verdere reacties.

Yvar

Gebruiker
Lid geworden
30 jan 2012
Berichten
8
Goedemorgen , of middag ligt eraan omdat hij nu vijf voor twaalf is als ik dit schrijf. :rolleyes:

Ik ben een calculator aan het maken doormiddel van twee textboxes, namelijk de formule en het antwoord.
Deze calculator gebruik de functies For en while, om te zoeken waar de nummers zijn om uit te rekenen.
Stel je hebt de formule:

5*5,

dan met de while zoekt hij naar "*", en dan totdat hij het begin of een andere symbool ziet kijkt hij voor het nummer. Vervolgens die erachter,
dit doe ik doormiddel van ints, alleen het probleem is: Hij calculeert niet !
Ik hoop dat jullie me ermee kunnen helpen, dit is zover mijn code:

Code:
        private void textBox1_TextChanged(object sender, EventArgs e)
        {
            string sum = textBox1.Text;

            while (sum.Contains("*"))
            {
                int found = sum.IndexOf('*');
                int startReplace = 0;
                int endReplace = 0;
                int startNumber = 0;
                int endNumber = 0;

                for (int i = found - 1 ; i > 0; i--)
                {
                    if (sum.Substring(i, 1).ToString() == "-" || sum.Substring(i, 1).ToString() == "*" || sum.Substring(i, 1).ToString() == "+" || sum.Substring(i, 1).ToString() == "/")
                    {
                        startReplace = i - 1;
                        startNumber = Convert.ToInt16(sum.Substring(i - 1 , found - 1).Replace(" ",""));
                        break;
                    }
                }

                for (int i = found + 1; i < sum.Length; i += 1)
                {
                    if (sum.Substring(i, 1).ToString() == "-" || sum.Substring(i, 1).ToString() == "*" || sum.Substring(i, 1).ToString() == "+" || sum.Substring(i, 1).ToString() == "/")
                    {
                        endReplace = i + 1;
                        endNumber = Convert.ToInt16(sum.Substring(i + 1, found + 1).Replace(" ", ""));
                        break;
                    }
                }

               sum =  sum.Replace(sum.Substring(startReplace, endReplace),Convert.ToString(startNumber * endNumber));
            }


            textBox2.Text = sum;
        }
 
Als je de formule 5*5 gebruikt zal hij nooit een ander teken tegenkomen dus zal alles op 0 blijven staan.
 
Na wat klungelen ben ik hier op uit gekomen :P
Probeer dit eens, kijken of het werkt zoals het zou moeten.

(heb even een method bijgevoegd voor if check)

Code:
public bool IsOperator(string input)
{
	return input.Equals("/") || input.Equals("*") || input.Equals("-") || input.Equals("+");
}

public string RefactoredCalculateInput(string input)
{
	//start by trimming the input
	string inputSum = input.Replace(" ", "");
	
	while (inputSum.Contains("*"))
	{
		int operatorIndex = inputSum.IndexOf('*');

		int replaceStartIndex = 0;

		for (int i = operatorIndex - 1; i >= 0; i--)
		{
			if (IsOperator(inputSum.Substring(i, 1)))
			{
				replaceStartIndex = i + 1;
				break;
			}
		}

		int secondlength = inputSum.Length - (operatorIndex + 1);
		for (int i = operatorIndex + 1; i < inputSum.Length; i++)
		{
			if (IsOperator(inputSum.Substring(i, 1)))
			{
				secondlength = i - (operatorIndex + 1);
				break;
			}
		}

		int replaceLength = secondlength + 1 + (operatorIndex - replaceStartIndex);

		int x = Convert.ToInt32(inputSum.Substring(replaceStartIndex, Math.Max(1, operatorIndex - replaceStartIndex)));
		int y = Convert.ToInt32(inputSum.Substring(operatorIndex + 1, secondlength));

		inputSum = inputSum.Replace(inputSum.Substring(replaceStartIndex, replaceLength), (x * y).ToString());
	}

	return inputSum;
}
 
Na wat klungelen ben ik hier op uit gekomen :P
Probeer dit eens, kijken of het werkt zoals het zou moeten.

(heb even een method bijgevoegd voor if check)

Code:
public bool IsOperator(string input)
{
	return input.Equals("/") || input.Equals("*") || input.Equals("-") || input.Equals("+");
}

public string RefactoredCalculateInput(string input)
{
	//start by trimming the input
	string inputSum = input.Replace(" ", "");
	
	while (inputSum.Contains("*"))
	{
		int operatorIndex = inputSum.IndexOf('*');

		int replaceStartIndex = 0;

		for (int i = operatorIndex - 1; i >= 0; i--)
		{
			if (IsOperator(inputSum.Substring(i, 1)))
			{
				replaceStartIndex = i + 1;
				break;
			}
		}

		int secondlength = inputSum.Length - (operatorIndex + 1);
		for (int i = operatorIndex + 1; i < inputSum.Length; i++)
		{
			if (IsOperator(inputSum.Substring(i, 1)))
			{
				secondlength = i - (operatorIndex + 1);
				break;
			}
		}

		int replaceLength = secondlength + 1 + (operatorIndex - replaceStartIndex);

		int x = Convert.ToInt32(inputSum.Substring(replaceStartIndex, Math.Max(1, operatorIndex - replaceStartIndex)));
		int y = Convert.ToInt32(inputSum.Substring(operatorIndex + 1, secondlength));

		inputSum = inputSum.Replace(inputSum.Substring(replaceStartIndex, replaceLength), (x * y).ToString());
	}

	return inputSum;
}

Dankje !!!! Werkt perfect !
 
Het is nog wel handig om het even in een try/catch block te zetten voor eventuele exceptions, heb in principe nergens checks voor toegevoegd ;)
 
Ik heb eerder eens een stack calculator gemaakt na aanleiding van een vraag die de standaard rekenregels volgt ( *,-,+,/ en haakjes).
Misschien heb je er nog wat aan ter referentie: http://dl.dropbox.com/u/3380837/Calcv2.rar

verder ziet het er naar uit dat als je 5-5 invoert dat die dit niet uitrekend ( while(input.contains("*") ) of was dit juist de bedoeling (testfase) ?
 
Status
Niet open voor verdere reacties.
Terug
Bovenaan Onderaan