.NET 2.x Длинная арифметика вычитание что не верно? - C#

Узнай цену своей работы

Формулировка задачи:

Начал изучать длинную арифметику но не как не могу дописать вычитание Что не верно? Если первое число больше второго то все ок. А если на оборото то уже баг
   int[] mass = new int[1000];
 
//................
 
        public static Long_Int operator -(Long_Int a, Long_Int b)
        {
            int[] _mass = new int[1000];
            for (int i = a.Length - 1; i > 0; --i)
            {
                if (a.mass[i] >= b.mass[i])
                {
                    _mass[i] += (a.mass[i] - b.mass[i]);
                }
                else
                {
                    _mass[i] += (a.mass[i] - b.mass[i]) + 10;
                    _mass[i - 1] -= 1;
                }
            }
            return new Long_Int(_mass);
        }

Сам класс выглядит так

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
 
namespace задача3
{
    class Long_Int
    {
        int[] mass = new int[1000];
 
        public Long_Int(string Dec)
        {
//лучше придумать не смог
            int i = 1000;
            while (Dec != "")
            {
                string s = Dec.Substring(Dec.Length - 1, 1);
                this.mass[--i] = Convert.ToInt32(s);
                Dec = Dec.Substring(0, Dec.Length - 1);
            }
        }
        public Long_Int(int[] mass)
        {
            for (int i = 0; i < mass.Length; i++)
                this.mass[i] = mass[i];
        }
 
        public override string ToString()
        {
//лучше придумать не смог
            string s = "";
            try
            {
                int i = 0;
                while (mass[i] == 0 && i < 999) i++;
                for (int j = i; j < mass.Length; j++)
                {
                    s += mass[j];
                }
                if (s == "") s = "0";
            }
            catch
            {
                throw new Exception("Число пустое");
            }
            return s;
        }
 
        public static Long_Int operator +(Long_Int a, Long_Int b)
        {
            int[] _mass = new int[1000];
            for (int i = a.Length - 1; i > 0; --i)
            {
                _mass[i] = (a.mass[i] + b.mass[i]) % 10;
                _mass[i - 1] = (a.mass[i] + b.mass[i]) / 10;
            }
            return new Long_Int(_mass);
        }
 
        public static Long_Int operator -(Long_Int a, Long_Int b)
        {
            int[] _mass = new int[1000];
            for (int i = a.Length - 1; i > 0; --i)
            {
                if (a.mass[i] >= b.mass[i])
                {
                    _mass[i] += (a.mass[i] - b.mass[i]);
                }
                else
                {
                    _mass[i] += (a.mass[i] - b.mass[i]) + 10;
                    _mass[i - 1] -= 1;
                }
            }
            return new Long_Int(_mass);
        }
 
        public static Long_Int operator *(Long_Int a, Long_Int b)
        {
            int[] _mass = new int[1000];
//пусто но реализовать не сложно
            return new Long_Int(_mass);
        }
 
        public static Long_Int operator *(Long_Int a, Int32 b)
        {
            int[] _mass = new int[1000];
//пусто но реализовать не сложно
            return new Long_Int(_mass);
        }
 
        public int Length
        {
            get
            {
                return mass.Length;
            }
        }
 
    }
}

Решение задачи: «.NET 2.x Длинная арифметика вычитание что не верно?»

textual
Листинг программы
function substract(longInt a, longInt b)
{
  if (b > a) Result := -(b - a);
  else Result := a - b;
}

ИИ поможет Вам:


  • решить любую задачу по программированию
  • объяснить код
  • расставить комментарии в коде
  • и т.д
Попробуйте бесплатно

Оцени полезность:

9   голосов , оценка 4.222 из 5
Похожие ответы