-2
int a = Convert.ToInt32(Convert.ToString(Text1.Text));//here is the exception i am getting.
int b = Convert.ToInt32(Convert.ToString(Text1.Text));
char c = Convert.ToChar(Convert.ToString(Text1.Text));
int result = Convert.ToInt32(Convert.ToString(Text2.Text));

if (c == '+')
{
    result = a + b;
    Text2.Text += result;
}
else if (c == '-')
{
    result = a - b;
    Text2.Text += result;
}
else if (c == '/')
{
    result = a / b;
    Text2.Text += result;
}
else if (c == '*')
{
    result = a * b;
    Text2.Text += result;
}
else
    return;

I am getting a format exception for this code as "Input string was not in a correct format". I know this is a simple question but i didn't get answer any where.

thank you in advance.

4

2 回答 2

3

First off, Convert.ToString(someString) is useless. Secondly, you will actually have to parse the input from Text1 to get all the relevant pieces out.

The simplest way is to split the string by some delimiter:

string[] parts = Text1.Text.Split(' ');
int a = Convert.ToInt32(parts[0]);
int b = Convert.ToInt32(parts[2]);
char c = parts[1][0];

This will handle any input in the form 123 + 456 (with exactly one space between each part). Alternatively, you could use a regular expression:

var match = Regex.Match(Text1.Text, @"^\s*(\d+)\s*([+/*-])\s*(\d+)\s*$");
if (match.Success)
{
    int a = Convert.ToInt32(match.Groups[1].Value);
    int b = Convert.ToInt32(match.Groups[3].Value);
    char c = match.Groups[2].Value[0];
}

Finally, there's no point in parsing Text2 if you're not going to do anything with the result.

于 2013-06-19T05:18:32.567 回答
0

Admittedly this is not a pretty one but it will give the job done for numbers that are positive and at most 3 digits.

I tested these values: 5+2 result 10, 50*2 result 100, 50*2+1 result 101

See my Demo

        string[] num = Regex.Split(textBox1.Text, @"\-|\+|\*|\/").Where(s => !String.IsNullOrEmpty(s)).ToArray(); // get Array for numbers
        string[] op = Regex.Split(textBox1.Text, @"\d{1,3}").Where(s => !String.IsNullOrEmpty(s)).ToArray(); // get Array for mathematical operators +,-,/,*
        int numCtr = 0, lastVal=0; // number counter and last Value accumulator
        string lastOp = ""; // last Operator
        foreach (string n in num)
        {
            numCtr++;
            if (numCtr == 1)
            {
                lastVal = int.Parse(n); // if first loop lastVal will have the first numeric value
            }
            else
            {
                if (!String.IsNullOrEmpty(lastOp)) // if last Operator not empty
                {
                    // Do the mathematical computation and accumulation
                    switch (lastOp) 
                    {
                        case "+":
                            lastVal = lastVal + int.Parse(n);
                            break;
                        case "-":
                            lastVal = lastVal - int.Parse(n);
                            break;
                        case "*":
                            lastVal = lastVal * int.Parse(n);
                            break;
                        case "/":
                            lastVal = lastVal + int.Parse(n);
                            break;

                    }
                }
            }
            int opCtr = 0;
            foreach (string o in op)
            {
                opCtr++;
                if (opCtr == numCtr) //will make sure it will get the next operator
                {
                    lastOp = o;  // get the last operator
                    break;
                }
            }
        }
        MessageBox.Show(lastVal.ToString());
于 2013-06-19T06:15:52.673 回答