Você está na página 1de 4

Programacin II

Programacin Orientada a Objetos


Lic. Viviana Pardo

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace SobrecargaOperadores1
{
class Program
{
static void Main(string[] args)
{
int tam;
Console.WriteLine("Ingrese el tamao del vector: ");
tam = int.Parse(Console.ReadLine());

VectorEnteros v1 = new VectorEnteros(tam);


Console.WriteLine("Carga del primer vector");
v1.Leer();
VectorEnteros v2 = new VectorEnteros(tam);
Console.WriteLine("Carga del segundo vector");
v2.Leer();
Console.WriteLine("Primer Vector");
v1.Mostrar();
Console.WriteLine("Segundo Vector");
v2.Mostrar();
VectorEnteros vt = new VectorEnteros(tam); ;
vt = v1 + v2;
Console.WriteLine("Vector Resultante");
vt.Mostrar();

Console.ReadKey();
}
}
class VectorEnteros
{
private int[] vec;

public VectorEnteros(int n )
{
vec = new int[n];
}

public void Leer()


{
for (int f = 0; f < vec.Length; f++)
{
Console.Write("Ingrese componente:");
vec[f] = int.Parse(Console.ReadLine());
}
}

public void Mostrar()


{
for (int f = 0; f < vec.Length; f++)
{
Console.Write(vec[f] + " ");
}
Console.WriteLine();
}
Programacin II
Programacin Orientada a Objetos
Lic. Viviana Pardo

public static VectorEnteros operator +(VectorEnteros v1, VectorEnteros


v2)
{
VectorEnteros su = new VectorEnteros(v1.vec.Length);
for (int f = 0; f < v1.vec.Length; f++)
{
su.vec[f] = v1.vec[f] + v2.vec[f];
Console.Write(su.vec[f] + " ");
}
return su;
}

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace CASobrecargaMatriz
{
class matrizEnteros
{
private int[,] mat;

public matrizEnteros(int n, int m)


{

mat = new int[n, m];


}

public void Leer(int n, int m)


{
for (int i = 0; i < n; i++)
{
for (int j = 0; j < 3; j++)
{
Console.Write("M[{0}][{1}]:", i, j);
mat[i, j] = int.Parse(Console.ReadLine());
}
}
}

public void Mostrar(int n, int m)


{
for (int i = 0; i < n; i++)
{
for (int j = 0; j< m; j++)
{
Console.Write(mat[i, j] + " ");
}
Console.WriteLine(" ");
}
Console.WriteLine();
}
Programacin II
Programacin Orientada a Objetos
Lic. Viviana Pardo

public static matrizEnteros operator +(matrizEnteros m1, matrizEnteros


m2)
{
matrizEnteros su = new matrizEnteros(m1.mat.GetLength(0),
m1.mat.GetLength(1));
for (int i = 0; i < m1.mat.GetLength(0); i++)
{
for (int j = 0; j < m1.mat.GetLength(1); j++)
{
su.mat[i, j] = m1.mat[i, j] + m2.mat[i, j];
}
}
return su;
}
public static matrizEnteros operator *(matrizEnteros m1, matrizEnteros
m2)
{
matrizEnteros mu = new matrizEnteros(m1.mat.GetLength(0),
m1.mat.GetLength(1));
for (int i = 0; i < m1.mat.GetLength(0); i++)
{
for (int j = 0; j < m1.mat.GetLength(1); j++)
{
mu.mat[i, j] = m1.mat[i, j] * m2.mat[i, j];
}
}
return mu;
}
}
class Program
{
static void Main(string[] args)
{
int n, m;
Console.WriteLine("Introduzca numero de filas de la matriz:");
n = int.Parse(Console.ReadLine());
Console.WriteLine("Introduzca numero de columnas de la matriz:");
m = int.Parse(Console.ReadLine());

matrizEnteros m1 = new matrizEnteros(n,m);


Console.WriteLine("Introduzca los elementos de la primera matriz: ");
m1.Leer(n,m);
matrizEnteros m2 = new matrizEnteros(n, m);
Console.WriteLine("Introduzca los elementos de la segunda matriz: ");
m2.Leer(n, m);
Console.WriteLine("Primera matriz");
m1.Mostrar(n,m);
Console.WriteLine("Segunda matriz");
m2.Mostrar(n, m);

matrizEnteros su;
su = m1 + m2;
Console.WriteLine("El resultado de la suma de matrices es: ");
su.Mostrar(n,m);
matrizEnteros mul;
mul = m1 * m2;
Console.WriteLine("El resultado de la multiplicacin de matrices es:
");
mul.Mostrar(n, m);
Console.ReadKey();
Programacin II
Programacin Orientada a Objetos
Lic. Viviana Pardo

}
}
}

Você também pode gostar