user11243368
user11243368

Reputation:

Parsing with and without spaces C#

I have a program that, for example, calculates 1 + 1 with a space in between, but how do I make it possible in my code that it also calculates 1 + 1 without a space? I was thinking about a regex or a split string but i can't succeed!

Can someone help me out?

This is my code:

private char[] SPACE = new char[] { ' ' };
private void GetAnswer(string clipboardText)
{
    //Loop through all questions and answers//
    foreach (question q in questionList)
    {
        //If we have found an answer that is exactly the same show an Notification//

        //Startwith zoekt naar alle vragen die matchen vanaf het begin van de zin en Endwith alle vragen die matchen vanaf het eind van de zin//
        if (q._question.StartsWith(clipboardText) || q._question.EndsWith(clipboardText))
        {
            ShowNotification(q._question, q._answer);
            break;
        }
    }
    var parts = clipboardText.Split(SPACE);
    var isValid = true;
    Double a, b;

    // Make sure it's format A # B
    if (parts.Length != 3)
        return;

    // Parse first number
    isValid = Double.TryParse(parts[0], out a);
    if (!isValid)
        return;

    var validOperators = new char[] { '+', '-', ':', 'x', '%' };

    // Parse operator
    if (parts[1].Length != 1)
        return;
    var op = parts[1][0];
    if (!validOperators.Contains(op))
        return;

    // Parse 2nd number
    isValid = Double.TryParse(parts[2], out b);
    if (!isValid)
        return;

    // Now calculate the answer
    string answer = null;
    switch (op)
    {
        case '+':
            answer = (a + b).ToString();
            break;
        case '-':
            answer = (a - b).ToString();
            break;
        case ':':
            if (b == 0)
                answer = "NaN";
            else
                answer = (a / b).ToString();
            break;
        case 'x':
            answer = (a * b).ToString();
            break;
        // rekent percentage van een bedrag 
        case '%':
            answer = (a / b * 100).ToString();
            break;
        default:
            throw new InvalidOperationException();
    }

    // Show the answer
    ShowNotification(clipboardText, answer);
}

Thanks in advance!

Upvotes: 0

Views: 352

Answers (3)

Kazkans
Kazkans

Reputation: 11

You can try var parts = clipboardText.Replace(" ", ""); this should make your output always without space.

private static void GetAnswer(string clipboardText)
{
    //Loop through all questions and answers//
    foreach (question q in questionList)
    {
        //If we have found an answer that is exactly the same show an Notification//

        //Startwith zoekt naar alle vragen die matchen vanaf het begin van de zin en Endwith alle vragen die matchen vanaf het eind van de zin//
        if (q._question.StartsWith(clipboardText) || q._question.EndsWith(clipboardText))
        {
            ShowNotification(q._question, q._answer);
            break;
        }
    }

    var parts = clipboardText.Replace(" ", "");
    var isValid = true;
    Double a, b;

    // Make sure it's format A # B

    char? op = null;
    int end;
    var validOperators = new char[] { '+', '-', ':', 'x', '%' };
    // find operator 
    foreach (char oper in validOperators)
    {
        if (parts.Contains(oper))
        {
            end = parts.IndexOf(oper);
            op = oper;
        }
    }
    if (!op.HasValue)
        return;
    // split to argument with op
    var arguments = parts.Split(op.Value);


    // Parse first number
    isValid = Double.TryParse(arguments[0], out a);
    if (!isValid)
        return;




    // Parse 2nd number
    isValid = Double.TryParse(arguments[1], out b);
    if (!isValid)
        return;

    // Now calculate the answer
    string answer = null;
    switch (op)
    {
        case '+':
            answer = (a + b).ToString();
            break;
        case '-':
            answer = (a - b).ToString();
            break;
        case ':':
            if (b == 0)
                answer = "NaN";
            else
                answer = (a / b).ToString();
            break;
        case 'x':
            answer = (a * b).ToString();
            break;
        // rekent percentage van een bedrag 
        case '%':
            answer = (a / b * 100).ToString();
            break;
        default:
            throw new InvalidOperationException();
    }

    // Show the answer
    ShowNotification(clipboardText,answer);
}

Upvotes: 1

Mong Zhu
Mong Zhu

Reputation: 23732

actually I would suggest first try to extract the operator from the clipboardText. If it works, then split by it and remove the empty entries(spaces) when splitting:

var validOperators = new char[] { '+', '-', ':', 'x', '%' };

char op = validOperators.FirstOrDefault(o => clipboardText.Contains(o));
if (op == default(char))
    return;

var parts = clipboardText.Split(new char[] { op}, StringSplitOptions.RemoveEmptyEntries);

one last thing is now your format will be only the numbers! parts will be only 2 elements(hopefully):

// Make sure it's format A # B
if (parts.Length != 2)
    return;

that also means to take the first and the last number:

// Parse first number
isValid = Double.TryParse(parts.First(), out a);
if (!isValid)
    return;

// Parse last number
isValid = Double.TryParse(parts.Last(), out b);
if (!isValid)
    return;

no you can get rid of the operator conversion and the check at this point:

// Parse operator
if (parts[1].Length != 1)
    return;
var op = parts[1][0];
if (!validOperators.Contains(op))
    return;

Upvotes: 0

Sheraz Ahmed
Sheraz Ahmed

Reputation: 453

Assuming you will have only two numbers always as described in the question Here is the solution

// this code will work for all of the text mentioned in comments 
//string text = "1 + 1";
//string text = "1+1";
//string text = "100.998+ 2000.7";
//string text = "10       +   2000.7";

        string text = "100+ 2000";
        text = Regex.Replace(text, @"\s+", "");
        double num1 = 0;
        double num2 = 0;
        char operand  ;
        int startIndex = 0;



        for (int i = 0; i < text.Length; i++)
        {
            // look for first operator
            if (isOperator(text[i]) || (i+1) == text.Length) {
                if (startIndex == 0) {
                    double.TryParse(text.Substring(startIndex, i), out num1);
                    operand = text[i];
                    startIndex = i + 1;
                }
                else
                {
                    double.TryParse(text.Substring(startIndex), out num2);
                    break;
                }
            }

        }
        // calculate(num1,operand,num2) // Implement this method with a simple switch case and this will calculate the final answer for you 

Implementation for the isOperator method :

 public static bool isOperator(char opt) {
            switch (opt)
            {
                case '+':
                    return true;
                case '-':
                    return true;
                case '*':
                    return true;
                case '/':
                    return true;
                default:
                    return false;
            }
        }

Upvotes: 0

Related Questions