Foros aprenderaprogramar.com
Aprender a programar => C, C++, C#, Java, Visual Basic, HTML, PHP, CSS, Javascript, Ajax, Joomla, MySql y más => Mensaje iniciado por: Gabrield Marquez en 05 de Enero 2016, 00:06
-
Código del ejemplo:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace try_catch_1
{
public class Matriz
{
private int cantFila;
private int cantColumnas;
private int[,] matriz;
public Matriz(int nf, int nc)
{
cantFila = nf;
cantColumnas = nc;
matriz = new int[nf, nc];
}
public void CargarMatriz()
{
for (int i = 0; i < this.cantFila; ++i) {
for (int j = 0; j < this.cantColumnas; ++j) {
bool hayError = true;
while (hayError) {
Console.Write(" Valor ( {0}, {1}) : ", i, j);
try
{
int valor = int.Parse(Console.ReadLine());
matriz[i, j] = valor;
hayError = false;
}
catch (Exception e) {
Console.WriteLine("Valor incorrecto, verficar......");
}
}
}
}
}
public int GetCantidadFilas()
{
return this.matriz.GetLength (0);
}
public int GetCantidadColumnas ()
{
return this.matriz.GetLength (1);
}
public static Matriz Sumar (Matriz matrizA, Matriz matrizB)
{
int cfA = matrizA.GetCantidadFilas ();
int ccA = matrizA.GetCantidadColumnas ();
int cfB = matrizB.GetCantidadFilas ();
int ccB = matrizB.GetCantidadColumnas ();
if ((cfA != cfB) || (ccA != ccB))
{
throw new Exception ("Matrices incompatibles para la Suma.");
}
Matriz r = new Matriz (cfA, ccA);
for (int i = 0; i < cfA; ++i)
{
for (int j = 0; j < ccA; ++j)
{
r.GetMatriz () [i,j] = matrizA.GetMatriz () [i,j] + matrizB.GetMatriz () [i,j];
}
}
return r;
}
public static Matriz operator + (Matriz matrizA, Matriz matrizB)
{
return Sumar (matrizA, matrizB);
}
public int[,] GetMatriz ()
{
return this.matriz;
}
}
}