Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- Program LAB2_2;
- Uses
- System.SysUtils;
- Const
- MIN = 0;
- MAX = 9;
- MINNUM = 1;
- MAXNUM = 1000000;
- Type
- TArrOI = Array Of Integer;
- Procedure WriteCondition;
- Begin
- Writeln('Данная программа складывает два числа столбиком.');
- Writeln('Условие ввода чисел: числа целые, от 1 до 1000000.');
- End;
- Procedure InputNum(Number: Integer; Var Num: Integer);
- Var
- IsCorrect: Boolean;
- Begin
- Repeat
- IsCorrect := True;
- Write('Введите ', Number, ' число: ');
- Try
- Read(Num);
- Except
- Writeln('Введенные данные не соответствуют условию. Повторите попытку.');
- IsCorrect := False;
- End;
- If IsCorrect And ((Num < MINNUM) Or (Num > MAXNUM)) Then
- Begin
- Writeln('Введенные данные не соответствуют условию. Повторите попытку.');
- IsCorrect := False;
- End;
- Until IsCorrect;
- End;
- Function FindArraySize(Num, Size: Integer): Integer;
- Begin
- Size := 0;
- While Num > MIN Do
- Begin
- Size := Size + 1;
- Num := Num Div 10;
- End;
- FindArraySize := Size;
- End;
- Procedure ConvertToArray(Num, Size: Integer; Var NumArr: TArrOI);
- Var
- I, Ten: Integer;
- Begin
- SetLength(NumArr, Size);
- Ten := 10;
- For I := High(NumArr) DownTo Low(NumArr) Do
- Begin
- NumArr[I] := (Num Mod Ten) Div (Ten Div 10);
- Ten := Ten * 10;
- End;
- End;
- Procedure FindSum(BiggerSize, SmallerSize: Integer;
- BiggerNum, SmallerNum: TArrOI; Var Sum: TArrOI);
- Var
- I: Integer;
- Begin
- SetLength(Sum, BiggerSize);
- For I := High(Sum) Downto 0 Do
- Begin
- If (I > (BiggerSize - SmallerSize - 1)) Then
- Sum[I] := BiggerNum[I] + SmallerNum[I - (BiggerSize - SmallerSize)]
- Else
- Sum[I] := BiggerNum[I];
- End;
- End;
- Procedure AddOnes(Var Sum: TArrOI);
- Var
- I, One: Integer;
- Begin
- One := 0;
- For I := High(Sum) Downto 1 Do
- Begin
- Sum[I] := Sum[I] + One;
- If Sum[I] > MAX Then
- Begin
- One := 1;
- Sum[I] := Sum[I] - 10;
- End
- Else
- One := 0;
- End;
- Sum[0] := Sum[0] + One;
- End;
- Procedure AddPosition(Var Sum: TArrOI);
- Var
- I: Integer;
- Begin
- If Sum[0] > MAX Then
- Begin
- SetLength(Sum, (High(Sum) + 2));
- For I := (High(Sum) - 1) Downto 0 Do
- Sum[I + 1] := Sum[I];
- Sum[1] := Sum[0] - 10;
- Sum[0] := 1;
- End;
- End;
- Procedure OutputArray(Arr: TArrOI);
- Var
- I: Integer;
- Begin
- For I := Low(Arr) To High(Arr) Do
- Write(Arr[I]);
- End;
- Procedure OutputSum(FstNumArr, SecNumArr, Sum: TArrOI);
- Var
- I: Integer;
- Begin
- OutputArray(FstNumArr);
- Write(' + ');
- OutputArray(SecNumArr);
- Write(' = ');
- OutputArray(Sum);
- End;
- Var
- FstNum, SecNum, FstSize, SecSize: Integer;
- FstNumArr, SecNumArr, Sum: TArrOI;
- Begin
- WriteCondition;
- InputNum(1, FstNum);
- InputNum(2, SecNum);
- FstSize := FindArraySize(FstNum, FstSize);
- SecSize := FindArraySize(SecNum, SecSize);
- ConvertToArray(FstNum, FstSize, FstNumArr);
- ConvertToArray(SecNum, SecSize, SecNumArr);
- If (FstSize > SecSize) Then
- FindSum(FstSize, SecSize, FstNumArr, SecNumArr, Sum)
- Else
- FindSum(SecSize, FstSize, SecNumArr, FstNumArr, Sum);
- AddOnes(Sum);
- AddPosition(Sum);
- OutputSum(FstNumArr, SecNumArr, Sum);
- Readln(FstSize);
- End.
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement