Несколько вариантов: program StrToInt; uses crt; var st : string; num : integer;
function val1 (s : string) : integer; var i, res : integer; s1 : string;
begin s1 := ''; res := 0; for i:=1 to length(s) do case s[i] of '0' : s1 := s1+'0'; '1' : s1 := s1+'1'; '2' : s1 := s1+'2'; '3' : s1 := s1+'3'; '4' : s1 := s1+'4'; '5' : s1 := s1+'5'; '6' : s1 := s1+'6'; '7' : s1 := s1+'7'; '8' : s1 := s1+'8'; '9' : s1 := s1+'9'; end; val (s1, res); if s[1]='-' then res := res * -1; val1 := res; end;
function isnumber (ch : char) : boolean; var res : boolean; begin res := false; case ch of '0' : res := true; '1' : res := true; '2' : res := true; '3' : res := true; '4' : res := true; '5' : res := true; '6' : res := true; '7' : res := true; '8' : res := true; '9' : res := true; end; isnumber := res; end;
function val2 (s : string) : integer; var i, res : integer;
begin res := 0; for i:=1 to length(s) do begin if isnumber(s[i]) then res := res*10 + byte(s[i])-byte('0'); end;
val2 := res; end;
BEGIN write('Input a string: '); readln(st); val(st, num); writeln('Standart function: ', num); num := val1(st); writeln('Function VAL1: ', num); num := val2(st); writeln('Function VAL2: ', num);
#include <iostream>
#include <cstdlib>
using namespace std;
int main()
{
int x[10];
int y[20];
int z[25];
int xs=0, ys=0, zs=0;
// Заполняем массивы случайными числами до 100
srand(time(0));
cout <<"Массив X(10):" <<endl;
for(int i=0; i<10; ++i)
{
x[i] = rand() % 100;
cout <<x[i] <<" ";
}
cout <<endl;
cout <<"Массив Y(20):" <<endl;
for(int i=0; i<20; ++i)
{
y[i] = rand() % 100;
cout <<y[i] <<" ";
}
cout <<endl;
cout <<"Массив Z(25):" <<endl;
for(int i=0; i<25; ++i)
{
z[i] = rand() % 100;
cout <<z[i] <<" ";
}
cout <<endl;
// Считаем суммы элементов массивов
for(int i=0; i<10; ++i)
xs+=x[i];
for(int i=0; i<20; ++i)
ys+=y[i];
for(int i=0; i<25; ++i)
zs+=z[i];
cout <<endl;
// Выводим суммы элементов на экран
cout <<"Сумма элементов массива X(10) = " <<xs <<endl;
cout <<"Сумма элементов массива Y(20) = " <<ys <<endl;
cout <<"Сумма элементов массива Z(25) = " <<zs <<endl;
return 0;
}
program StrToInt;
uses crt;
var
st : string;
num : integer;
function val1 (s : string) : integer;
var
i, res : integer;
s1 : string;
begin
s1 := '';
res := 0;
for i:=1 to length(s) do
case s[i] of
'0' : s1 := s1+'0';
'1' : s1 := s1+'1';
'2' : s1 := s1+'2';
'3' : s1 := s1+'3';
'4' : s1 := s1+'4';
'5' : s1 := s1+'5';
'6' : s1 := s1+'6';
'7' : s1 := s1+'7';
'8' : s1 := s1+'8';
'9' : s1 := s1+'9';
end;
val (s1, res);
if s[1]='-' then res := res * -1;
val1 := res;
end;
function isnumber (ch : char) : boolean;
var res : boolean;
begin
res := false;
case ch of
'0' : res := true;
'1' : res := true;
'2' : res := true;
'3' : res := true;
'4' : res := true;
'5' : res := true;
'6' : res := true;
'7' : res := true;
'8' : res := true;
'9' : res := true;
end;
isnumber := res;
end;
function val2 (s : string) : integer;
var
i, res : integer;
begin
res := 0;
for i:=1 to length(s) do begin
if isnumber(s[i]) then res := res*10 + byte(s[i])-byte('0');
end;
val2 := res;
end;
BEGIN
write('Input a string: ');
readln(st);
val(st, num);
writeln('Standart function: ', num);
num := val1(st);
writeln('Function VAL1: ', num);
num := val2(st);
writeln('Function VAL2: ', num);
END.