Ejercicio
Función Calculadora, Parámetros Y Valor De Retorno De Main
Objectivo
Crear un programa en C# para calcular una suma, resta, producto o división, analizando los parámetros de la línea de comandos:
calc 5 + 379
(Los parámetros deben ser un número, un signo y otro número; los signos permitidos son + - * x / )
Esta versión debe devolver los siguientes códigos de error:
1 si el número de parámetros no es 3
2 si el segundo parámetro no es un signo aceptado
3 si el primer o tercer parámetro no es un número válido
0 de lo contrario
Ejemplo Ejercicio C#
Mostrar Código C#
// Import the System namespace to use basic classes like Console
using System;
class Program
{
// Main method to drive the program
public static int Main(string[] args)
{
// Check if the correct number of arguments (3) are passed
if (args.Length != 3)
{
// Return error code 1 if the number of parameters is incorrect
Console.WriteLine("Error: Please provide two numbers and an operator.");
return 1;
}
// Try to parse the first number from the command line arguments
double num1;
if (!double.TryParse(args[0], out num1))
{
// Return error code 3 if the first parameter is not a valid number
Console.WriteLine("Error: The first parameter is not a valid number.");
return 3;
}
// Try to parse the second number from the command line arguments
double num2;
if (!double.TryParse(args[2], out num2))
{
// Return error code 3 if the third parameter is not a valid number
Console.WriteLine("Error: The third parameter is not a valid number.");
return 3;
}
// Get the operator from the second parameter
string operatorSign = args[1];
// Perform the calculation based on the operator
double result = 0;
bool validOperation = true;
// Switch statement to handle different operations
switch (operatorSign)
{
case "+":
result = num1 + num2; // Perform addition
break;
case "-":
result = num1 - num2; // Perform subtraction
break;
case "*":
case "x": // Allow 'x' as multiplication sign
result = num1 * num2; // Perform multiplication
break;
case "/":
if (num2 == 0)
{
// Handle division by zero
Console.WriteLine("Error: Cannot divide by zero.");
validOperation = false;
}
else
{
result = num1 / num2; // Perform division
}
break;
default:
// Return error code 2 if the operator is invalid
Console.WriteLine("Error: Invalid operator. Use +, -, *, x, or /.");
validOperation = false;
break;
}
// Output the result if the operation is valid
if (validOperation)
{
Console.WriteLine($"Result: {result}");
return 0; // Return 0 to indicate successful operation
}
// Return an error code if the operation was not valid
return 2; // Return error code 2 if the operator is invalid
}
}
Salida
-- Case 1: Execution with incorrect number of arguments
dotnet run
Error: Please provide two numbers and an operator.
Return code: 1
-- Case 2: Execution with valid addition operation
dotnet run 5 + 3
Result: 8
Return code: 0
-- Case 3: Execution with valid subtraction operation
dotnet run 10 - 4
Result: 6
Return code: 0
-- Case 4: Execution with valid multiplication operation using '*' symbol
dotnet run 6 * 7
Result: 42
Return code: 0
-- Case 5: Execution with valid multiplication operation using 'x' symbol
dotnet run 6 x 7
Result: 42
Return code: 0
-- Case 6: Execution with valid division operation
dotnet run 8 / 2
Result: 4
Return code: 0
-- Case 7: Execution with division by zero
dotnet run 10 / 0
Error: Cannot divide by zero.
Return code: 2
-- Case 8: Execution with invalid operator
dotnet run 5 & 3
Error: Invalid operator. Use +, -, *, x, or /.
Return code: 2
-- Case 9: Execution with invalid first parameter (not a number)
dotnet run abc + 3
Error: The first parameter is not a valid number.
Return code: 3
-- Case 10: Execution with invalid third parameter (not a number)
dotnet run 5 + abc
Error: The third parameter is not a valid number.
Return code: 3
Código de Ejemplo Copiado!
Comparte este Ejercicio C# Sharp