Урок 15 Задача 16
Primary tabs
Напишите функцию, которая принимает на вход целое число N и возвращается строку, содержащую арифметическое выражение вида:
a1 # a2 # ..... # aNГде:
a1, a2,....., aN -- случайные числа из диапазона от 1 до 100
# - один из случайных знаков (*, +, -)
var
n: integer;
w: string;
function preobr (a: integer): string;
var
s: string;
begin
if a = 1 then
s:='1'
else if a = 2 then
s:='2'
else if a = 3 then
s:='3'
else if a = 4 then
s:='4'
else if a = 5 then
s:='5'
else if a = 6 then
s:='6'
else if a = 7 then
s:='7'
else if a = 8 then
s:='8'
else if a = 9 then
s:='9'
else if a = 0 then
s:='0';
result:= s;
end;
function simvol (c: integer): string;
var
simv: string;
begin
if c = 1 then
simv:= '*'
else if c = 2 then
simv:= '+'
else
simv:= '-';
result:= simv;
end;
function chislo (a: integer): string;
var
b, d, x, h, i: integer;
ch: string;
begin
b:= a;
d:=1;
h:=1;
while not (b div 10 = 0) do
begin
b:= b div 10;
d:= d*10;
h:= h+1;
end;
for i:=1 to h do
begin
x:= a div d;
a:= a mod d;
d:= d div 10;
ch:= ch + preobr (x);
end;
result:= ch;
end;
function stroka (m: integer): string;
var
i, a: integer;
str: string;
begin
randomize ();
for i:=1 to m do
begin
a:= random (100)+1;
str:= str + chislo (a);
if not (i=m) then
begin
a:= random(3)+1;
str:= str + simvol (a);
end;
end;
result:= str;
end;
begin
write ('Введите число: ');
readln (n);
write (stroka(n));
readln ();
end.Вывод в консоли:
Введите число: 7 59*29+1-73*83-58+11
- Log in to post comments
- 671 reads