Exercise
Function Calculator, Params Of Main
Objetive
Create a C# 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 / )
Example Code
using System;
public class exercise125
{
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");
}
}