E
E
evgenyt20002018-10-16 07:35:10
C++ / C#
evgenyt2000, 2018-10-16 07:35:10

Why does it add and subtract vectors incorrectly?

Good morning!
I can’t figure out why the sum is a doubled vector, although there should be a sum and why the difference is 0, most likely the error is the same. And how to fix it?

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


namespace oop
{
    public class Vektor
    {
        public readonly double x, y, z;
        public Vektor(double x, double y, double z)

        {
            this.x = x;
            this.y = y;
            this.z = z;
        }
       
        //summa
        public  Vektor Add(Vektor V)
        {
            return new Vektor(this.x + x, this.y + y, this.z + z);
        }
        //raznost
        public Vektor Razn(Vektor V)
        {
            return new Vektor(x-this.x,y -this.y,z-this.z);
        }
        //dlina
        public double Length()
        {
            return Math.Sqrt(x * x + y * y + z * z);
        }
        //skalarnoe proizvedenie
        public double Skalar(Vektor V)
        {
            return x * this.x + y * this.y + z * this.z;
        }
        //umnozenie na skalar a
        public double Uskalar()
        {
            double a = 1;
            return a*x * this.x + a*y * this.y + a*z * this.z;
        }
        //sravnenie
        public bool Equals(Vektor V)
        {
            if (!(V is Vektor))
            { return false; }
            return base.Equals(V);
        }

            public override string ToString()
        {
            return $"{x} {y} {z}";
        }
       

        public class Demo
        {
            public static void Main()
            {
                Vektor A = new Vektor(10, 7, 9);
                Vektor B = new Vektor(3, 5, 7);
                Console.WriteLine("A vektor: x={0},y={1},z={2}", A.x, A.y, A.z);
                Console.WriteLine("B vektor: x={0},y={1},z={2}", B.x, B.y, B.z);
                Console.WriteLine($"Summa : {A.Add(B)}");
                Console.WriteLine($"Raznost: {A.Razn(B)}");
                Console.WriteLine($"The scalar *****: {A.Skalar(B)}");
                Console.WriteLine("Dlina A  :   {0}    ", A.Length());
                Console.WriteLine("Dlina B  :   {0}  ", B.Length());
                Console.WriteLine($"Umnozenie na skalar-Vektor A: {A.Uskalar()}");
                Console.WriteLine($"Umnozenie na skalar-Vektor B: {B.Uskalar()}");
                Console.WriteLine($"Ravni li: {A.Equals(B)}");
                Console.WriteLine("Press Enter...");
                Console.ReadLine(); // Чтобы программа сразу не закрылась
            }
        }
    }
}

Answer the question

In order to leave comments, you need to log in

1 answer(s)
1
15432, 2018-10-16
@evgenyt2000

You use this.x and x everywhere in your methods, which is the same thing, but you should use Vx passed as an argument.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question