Перегрузки операций для математических векторов в двумерном пространстве - C#
Формулировка задачи:
Доброго времени суток) Написана программа для перегрузки операций(для математических векторов в двумерном пространстве):
+ (сума векторов)
- (разность векторов)
* (умножение на скаляр)
* (скалярное произведение)
/ (деление на скаляр)
Помогите пожалуйста переделать для n-мерного пространства, что бы пользователь сам вводил это n. Понимаю, что все это делается через цикл, уже пару дней мучаюсь с этим, но что-то ничего не работает..)
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace _3._5
{
public class Vector
{
private double x, y, k = -9999;
public Vector(double k)
{
this.k = k;
}
public Vector(double x, double y)
{
this.x = x;
this.y = y;
}
public static Vector operator +(Vector A, Vector B)
{
return new Vector(A.x + B.x, A.y + B.y);
}
public static Vector operator -(Vector A, Vector B)
{
return new Vector(A.x - B.x, A.y - B.y);
}
public static Vector operator *(Vector A, Vector B)
{
return new Vector(A.x * B.x + A.y * B.y);
}
public static Vector operator *(Vector A, double s)
{
return new Vector(A.x * s, A.y * s);
}
public static Vector operator /(Vector A, double s)
{
return new Vector(A.x / s, A.y / s);
}
public static implicit operator string(Vector p)
{
if (p.k == -9999) return "(" + p.x + "; " + p.y + ")";
else return p.k + " ";
}
}
class Program
{
static void Main(string[] args)
{
Console.WriteLine("Координаты вектора А(x1;y1) и координаты вектора В(х2; у2):");
Console.WriteLine("Введите х1:");
int x1 = int.Parse(Console.ReadLine());
Console.WriteLine("Введите у1:");
int y1 = int.Parse(Console.ReadLine());
Console.WriteLine("Введите х2:");
int x2 = int.Parse(Console.ReadLine());
Console.WriteLine("Введите у1:");
int y2 = int.Parse(Console.ReadLine());
Console.WriteLine("Введите скаляр:");
int s = int.Parse(Console.ReadLine());
Vector p1 = new Vector(x1, y1);
Vector p2 = new Vector(x2,y2);
Vector p3 = p1 + p2;
Vector p4 = p1 - p2;
Vector p5 = p1 * p2;
Vector p6 = p2 * s;
Vector p7 = p2 / s;
Console.WriteLine("Ваши координаты векторов:");
Console.WriteLine("А" + p1 + " В" + p2 + " Скаляр = " + s);
Console.WriteLine("Сумма векторов = " + p3);
Console.WriteLine("Разность векторов = " + p4);
Console.WriteLine("Скалярное произведение векторов = " + p5);
Console.WriteLine("Произведение второго вектора на скаляр = " + p6);
Console.WriteLine("Деления второго вектора на скаляр = " + p7);
}
}
}Решение задачи: «Перегрузки операций для математических векторов в двумерном пространстве»
textual
Листинг программы
using System;
using System.Linq;
namespace ConsoleApplication8
{
class Program
{
static void Main(string[] args)
{
Vector vector1=new Vector(new Elements(1,2,3,4,3));
Vector vector2=new Vector(new Elements(4,4,4,5,7));
Vector vector3 = vector1 + vector2;
Console.WriteLine(vector3.GetContainer);
}
}
class Vector
{
private Elements container;
public Vector(Elements data)
{
container = data;
}
public static Vector operator +(Vector a, Vector b)
{
if(a.container.Getlength!=b.container.Getlength)
throw new Exception("Длина векторов не совпадает!");
var arr1=new int[a.container.Getlength];
var arr2=new int[a.container.Getlength];
Array.Copy(a.container.GetElements,arr1,a.container.Getlength);
Array.Copy(b.container.GetElements,arr2,b.container.Getlength);
arr1 = arr1.Select((element, index) => element + arr2[index]).ToArray();
return new Vector(new Elements(arr1));
}
public Elements GetContainer
{
get { return container; }
}
}
class Elements
{
private int[] container;
public Elements(params int[]elements)
{
container=new int[elements.Length];
Array.Copy(elements,container,elements.Length);
}
public int Getlength
{
get { return container.Length; }
}
public int[] GetElements
{
get { return container; }
}
public override string ToString()
{
return string.Join(" ", container);
}
}
}