Programming Course in C# ¡Free!

Calculator, params of Main

 Saturday, April 06, 2013 published by Exercises C#
Proposed exercise

Create a program to calculate a sum, subtraction, product or division, analyzing the command line parameters:

calc 5 + 379

(Parameters must be a number, a sign, and another number; allowed signs are + - * x / )

Output



Solution


using System;
class CalcMain
{
static void Main()
{
char operation = Convert.ToChar(args[1]);

int number1 = Convert.ToInt32(args[0]);
int number2 = Convert.ToInt32(args[2]);

int result = 0;
if (args.Length != 3)
{
switch (operation)
{
case '+':
result = number1 + number2;
break;
case '-':
result = number1 - number2;
break;
case 'x':
case 'X':
case '*':
result = number1 * number2;
break;
case '/':
result = number1 / number2;
break;
}
Console.WriteLine("Result: {0}", result);
}
else
Console.WriteLine("Error in arguments");
}
}