1. Defina una estructura llamada `Point2D` con campos para coordenadas y colores RGB.
2. Cree un array con capacidad para almacenar hasta 1000 puntos.
3. Muestre un menú con las siguientes opciones:
- Añadir un nuevo punto
- Mostrar todos los puntos almacenados
- Calcular y mostrar los valores promedio de las coordenadas x e y
- Salir del programa
4. Implemente la validación de la entrada del usuario para las entradas numéricas.
5. Asegúrese de que el menú se ejecute en bucle hasta que el usuario decida salir.
Escriba un programa en C# que expanda el ejercicio anterior (array de puntos) para que muestre un menú donde el usuario pueda elegir:
- Añadir datos para un punto
- Mostrar todos los puntos introducidos
- Calcular (y mostrar) los valores promedio de x e y
- Salir del programa
using System;
class Program
{
// Defining a struct to store 2D point data with RGB color components
struct Point2D
{
public short x, y; // Coordinates
public byte r, g, b; // RGB color values
}
static void Main()
{
const int MAX_POINTS = 1000; // Maximum number of points
Point2D[] points = new Point2D[MAX_POINTS]; // Array to store points
int count = 0; // Number of points stored
while (true)
{
Console.WriteLine("\nMenu:");
Console.WriteLine("1. Add a new point");
Console.WriteLine("2. Display all points");
Console.WriteLine("3. Calculate average x and y values");
Console.WriteLine("4. Exit");
Console.Write("Choose an option: ");
string option = Console.ReadLine();
Console.WriteLine();
switch (option)
{
case "1":
if (count < MAX_POINTS)
{
points[count] = ReadPoint();
count++;
Console.WriteLine("Point added successfully.");
}
else
{
Console.WriteLine("Error: Maximum number of points reached.");
}
break;
case "2":
if (count == 0)
{
Console.WriteLine("No points stored yet.");
}
else
{
Console.WriteLine("Stored Points:");
for (int i = 0; i < count; i++)
{
DisplayPoint($"Point {i + 1}", points[i]);
}
}
break;
case "3":
if (count == 0)
{
Console.WriteLine("No points stored yet.");
}
else
{
CalculateAverage(points, count);
}
break;
case "4":
Console.WriteLine("Exiting the program.");
return;
default:
Console.WriteLine("Invalid option. Please choose a valid number.");
break;
}
}
}
// Method to read a single point's data from user input
static Point2D ReadPoint()
{
Point2D point;
point.x = ReadShort("X coordinate: ");
point.y = ReadShort("Y coordinate: ");
point.r = ReadByte("Red component (0-255): ");
point.g = ReadByte("Green component (0-255): ");
point.b = ReadByte("Blue component (0-255): ");
return point;
}
// Method to safely read a short value
static short ReadShort(string message)
{
short value;
while (true)
{
Console.Write(message);
if (short.TryParse(Console.ReadLine(), out value))
return value;
Console.WriteLine("Invalid input. Please enter a valid short integer.");
}
}
// Method to safely read a byte value
static byte ReadByte(string message)
{
byte value;
while (true)
{
Console.Write(message);
if (byte.TryParse(Console.ReadLine(), out value))
return value;
Console.WriteLine("Invalid input. Please enter a number between 0 and 255.");
}
}
// Method to display a point's data
static void DisplayPoint(string name, Point2D point)
{
Console.WriteLine($"{name} -> X: {point.x}, Y: {point.y}, Color: RGB({point.r}, {point.g}, {point.b})");
}
// Method to calculate and display the average x and y values
static void CalculateAverage(Point2D[] points, int count)
{
int sumX = 0, sumY = 0;
for (int i = 0; i < count; i++)
{
sumX += points[i].x;
sumY += points[i].y;
}
double avgX = (double)sumX / count;
double avgY = (double)sumY / count;
Console.WriteLine($"Average X: {avgX:F2}, Average Y: {avgY:F2}");
}
}
Output
Menu:
1. Add a new point
2. Display all points
3. Calculate average x and y values
4. Exit
Choose an option: 1
X coordinate: 10
Y coordinate: 20
Red component (0-255): 255
Green component (0-255): 0
Blue component (0-255): 0
Point added successfully.
Menu:
1. Add a new point
2. Display all points
3. Calculate average x and y values
4. Exit
Choose an option: 1
X coordinate: -5
Y coordinate: 15
Red component (0-255): 0
Green component (0-255): 255
Blue component (0-255): 255
Point added successfully.
Menu:
1. Add a new point
2. Display all points
3. Calculate average x and y values
4. Exit
Choose an option: 2
Stored Points:
Point 1 -> X: 10, Y: 20, Color: RGB(255, 0, 0)
Point 2 -> X: -5, Y: 15, Color: RGB(0, 255, 255)
Menu:
1. Add a new point
2. Display all points
3. Calculate average x and y values
4. Exit
Choose an option: 3
Average X: 2.50, Average Y: 17.50
Código de ejemplo copiado