Как вычесть два больших числа, представленных в виде строк в языке Си#
мне нужна помощь с этой программой
вычитание 2 больших чисел с помощью строки
использование системы;
использование системы.Коллекции.Общий;
использование System.Linq;
использование System.Text;
использование системы.Нарезание резьбы.Задачи;
программа вычитания пространства имен
{
классная арифметика
{
static void Main(string[] args)
{
const int длина = 1000;
Строка В1;
Строки Дюйм2;
растянуться;
int[] Top = new int[длина];
int[] Bottom = new int[длина];
int[] Dif = новый int[длина - 1];
StringBuilder temp;
int заимствование = 0;
int TopLen = 0;
int BottomLen = 0;
int LenDif = 0;
int SumLen = 0;
int TopIn = 0;
int BottomIn = 0;
Приставка.WriteLine("выполнение вычитания\n");
Приставка.Напишите("введите первое число: ");
In1 = Консоль.Линия чтения();
Приставка.Напишите("введите второе число: ");
In2 = Консоль.Линия чтения();
//ПОЛУЧЕНИЕ БОЛЬШЕГО ЧИСЛА СВЕРХУ
// если(In1 < In2) //первый короче второго
{
for (int i = 0; i < TopLen; i++)
{
если (In1[i] < In2[i]) ;
Top[i] = In2[i] - '0';
}
LenDif = TopLen - BottomLen;
for (int i = 0; i < LenDif; i++)
{
Bottom[i] = 0; // pad bottom с нулями слева
}
for (int i = 0; i < BottomLen; i++)
{
Bottom[i + LenDif] = In1[i] - '0';
}
}
еще
// if (In2 < In1 )
{
TopLen = In1.Length;
BottomLen = In2.Length;
for (int i = 0; i < TopLen; i++)
{
Top[i] = In1[i] - '0';
}
LenDif = TopLen - BottomLen;
for (int i = 0; i < LenDif; i++)
{
Дно[i] = 0;
}
for (int i = 0; i < BottomLen; i++)
{
Bottom[i + LenDif] = In2[i] - '0';
}
}
// начните вычитание слева и вернитесь вправо
//---------------ЗАНИМАЮСЬ АРИФМЕТИКОЙ---------------------------
for (int i = TopLen -1; i >= 0; i--)
{
//два входа одинаковы
если (In1[i] == In2[i])
{
Out = 0; // выяснить.....
}
//одно из чисел равно нулю
еще
{
если (BottomIn == 0)
{
Out = Top[i];
}
}
еще
{
если (TopIn == 0)
{
Bottom[i] = Out;
}
}
}
for (int i = TopLen -1 -BottomLen)
{
//две цифры плюс заимствование
if(i > TopLen -1 -BottomLen)
{
Dif[i] = Top[i] - Bottom[i];
if (Top[i] == 0)
{
заимствование = 1;
Top[i] = 10;
Top[i] -= 9;
}
еще
{
брать = 0;
}
}
Что я уже пробовал:
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace SUBTRACTION PROGRAM { class Arithmetic { static void Main(string[] args) { const int LENGTH = 1000; String In1; String In2; String Out; int[] Top = new int[LENGTH]; int[] Bottom = new int[LENGTH]; int[] Dif = new int[LENGTH - 1]; StringBuilder temp; int borrow = 0; int TopLen = 0; int BottomLen = 0; int LenDif = 0; int SumLen = 0; int TopIn = 0; int BottomIn = 0; Console.WriteLine("Doing Subtraction\n"); Console.Write("Enter the First Number: "); In1 = Console.ReadLine(); Console.Write("Enter the Second Number: "); In2 = Console.ReadLine(); //GETTING LARGER NUMBER ON TOP // if(In1 < In2) //first shorter than second { for (int i = 0; i < TopLen; i++) { if (In1[i] < In2[i]) ; Top[i] = In2[i] - '0'; } LenDif = TopLen - BottomLen; for (int i = 0; i < LenDif; i++) { Bottom[i] = 0; // pad bottom with zeros on left } for (int i = 0; i < BottomLen; i++) { Bottom[i + LenDif] = In1[i] - '0'; } } else // if (In2 < In1 ) { TopLen = In1.Length; BottomLen = In2.Length; for (int i = 0; i < TopLen; i++) { Top[i] = In1[i] - '0'; } LenDif = TopLen - BottomLen; for (int i = 0; i < LenDif; i++) { Bottom[i] = 0; } for (int i = 0; i < BottomLen; i++) { Bottom[i + LenDif] = In2[i] - '0'; } } // start subtraction from the left and work back to right //---------------DOING ARITHMETIC--------------------------- for (int i = TopLen -1; i >= 0; i--) { //two inputs are same if (In1[i] == In2[i]) { Out = 0; // figure out..... } //one of the numbers is zero else { if (BottomIn == 0) { Out = Top[i]; } } else { if (TopIn == 0) { Bottom[i] = Out; } } } for (int i = TopLen -1 -BottomLen) { //two digits plus borrow if(i > TopLen -1 -BottomLen) { Dif[i] = Top[i] - Bottom[i]; if (Top[i] == 0) { borrow = 1; Top[i] = 10; Top[i] -= 9; } else { borrow = 0; } }
Patrice T
опишите возникшую у вас проблему.
Показать пример ввода и вывода фактических и ожидаемых данных.
Gerry Schmitz
Выглядит почти как ассемблер. Я думаю, что вы будете делать хорошо. (но вы не инициализировали "TopLen")