Thursday, 7 July 2016

Console application pass parameters

Target of application to be like calculator, you will pass the string like n1+n2-n3/n4*n5 and it will calculate the value and give the result. e.g. 2+2-2*2/2 will give the result as 2


1.       Now creating the console application, select the console application from there.




2.       From nuget package select the commandlineargumentparser and install it to your system.



3.       Import the below two namespace;

using CommandLine;
using CommandLine.Text;
using System.Text.RegularExpressions;

4.       Write the code for command line helper, which will help you to pass the parameters.
In option it will defined the option type that you are going to pass.

public static class CommandLineHelper
    {
        public static Dictionary<string, string> ParseArguments(string[] args)
        {
            Dictionary<string, string> argumentDictionary = new Dictionary<string, string>();
            Options options = new Options();

            if (CommandLine.Parser.Default.ParseArguments(args, options))
            {
                argumentDictionary.Add(Constants.Calculation, options.CalculateValues);
            }
            else
            {
                Environment.Exit(-1);
            }

            return argumentDictionary;
        }

    }

    public class Options
    {
        [Option('m', "calculateit", Required = true, HelpText = "calculate the values pass only +,-,*,/")]
        public string CalculateValues { get; set; }
    }

    public static class Constants
    {
        // Command line argument constants
        public const string Calculation = "calclateit";
    }


5.       Main login to calculating the string as like n1+n2-n3/n4*n5.
Checking for the type of pattern that have either *,/,- or + with combination of number and based on the operator it will perform the operation and that concatenate the result string with current string. Like :
Step 1 : n1+n2-n3/x1.
Step 2 : n1+n2-x2.
Step 3 : : n1+x3.
Step 4 : : x4. [Result]


   public static void FrameArray(ref string str, char c)
        {
            string pat = @"(\d+[" + c + @"]\d+)";
            Regex r = new Regex(pat, RegexOptions.IgnoreCase);
            Match m = r.Match(str);
            if (m.Success)
            {
                string[] va = m.Value.ToString().Split(c);
                if (c == '*')
                {
                    string myval = (Convert.ToDouble(va[0]) * Convert.ToDouble(va[1])).ToString();
                    str =  str.Replace(m.Value, myval);
                }
                else if (c == '/')
                {
                    string myval = (Convert.ToDouble(va[0]) / Convert.ToDouble(va[1])).ToString();
                    str = str.Replace(m.Value, myval);
                }
                else if (c == '+')
                {
                    string myval = (Convert.ToDouble(va[0]) + Convert.ToDouble(va[1])).ToString();
                    str = str.Replace(m.Value, myval);
                }
                else if(c == '-')
                {
                    string myval = (Convert.ToDouble(va[0]) - Convert.ToDouble(va[1])).ToString();
                    str =str.Replace(m.Value, myval);
                }
              

            }

        }

6.       From main method calling the above method as:

static int i = 0;
        static void Main(string[] args)
        {
            Dictionary<string, string> argumentDictionary = CommandLineHelper.ParseArguments(args);
            string calcultaionString = argumentDictionary[Constants.Calculation]; //"2*2-1+2/2"; //
            i = -1;
            for(int i =0;calcultaionString.Length > 0; i++)
            {
                if (calcultaionString.Contains('*'))
                {
                    FrameArray(ref calcultaionString, '*');
                    i = 0;
                }
                else if (calcultaionString.Contains('/'))
                {
                    FrameArray(ref calcultaionString, '/');
                    i = 0;
                }
                else if (calcultaionString.Contains('-'))
                {
                    FrameArray(ref calcultaionString, '-');
                    i = 0;
                }
                else if (calcultaionString.Contains('+'))
                {
                    FrameArray(ref calcultaionString, '+');
                    i = 0;
                }
               
                else
                {
                    break;
                }
             
            }
            Console.WriteLine("Result is : {0}",calcultaionString);
        }

7.       Now you need to run the command as in the path where exe will present.

>calculator -m 2+2-2*2/2

No comments:

Post a Comment