Left to Right

Jin has just started his schooling and loves to be at school but he hates his mathematics classes. Recently his teacher taught him basic arithmetic. But the problem is that he doesn’t understand the hierarchy of the operators. For any given arithmetic expression he starts from left side and move to right evaluating the expression. His teacher gave him some expressions to evaluate .It’s 15 August and Jin wants to go to India gate. So he asks you to help him in his mathematics homework.

Input 

The input file will contain one or more test cases. Each line will contain a string (35 chars at max) containing digits between 1-9 and operators in between them. Operators can be +, -, *, /.You have to evaluate the string left to right.

 

Output 

For every input string calculate its value and print on a newline.

 

Sample Input 

2+3/5

3-4

5*2-9

 

Sample Output 

1

-1

1
 

using System;

using System.Collections.Generic;

using System.Text;

using System.Text.RegularExpressions;

using System.Collections;

 

namespace ConsoleApplication1

{

    class Program

    {

        static void Main(string[] args)

        {

            string str = @"2/2+1+2+1/5", temp = "";

            int num = 0, flag = 0, value = 0;

            char oper = ‘ ‘;

            for (int i = 0; i < str.Length; i++)

            {

                if (char.IsDigit(str, i))

                {

                    temp = temp + str.Substring(i, 1);

                }

               

                if(!char.IsDigit(str, i) || (i + 1) == str.Length)

                {

                    if (flag == 0)

                    {

                        oper = str[i];

                        value = int.Parse(temp);

                        temp = "";

                        flag = 1;

                    }

                    else

                    {

                        num = int.Parse(temp);

                        temp = "";

                        switch (oper)

                        {

                            case ‘+’:

                                value = value + num;

                                break;

                            case ‘-‘:

                                value = value – num;

                                break;

                            case ‘*’:

                                value = value * num;

                                break;

                            case ‘/’:

                                value = value / num;

                                break;

                            default:

                                break;

                        }

                        oper = str[i];

                    }

                }

            }

            Console.WriteLine("Value of the expression " + str + " is " + value);

        }

    }

}

Advertisements
This entry was posted in Information Technology. Bookmark the permalink.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s