Pascal

Modified: Wednesday, 22-12-2021 07:00 AM

Đọc số thành chữ



program DocSoSangChu;//Pham vi <=999.999.999.999
var SoInp:string;
//Nhap day so
procedure Input;
var i:byte;
    s:string;
    b:boolean;
begin
    repeat
        b := true;
        write('Nhap day so can chuyen: ');
        readln(s);
        //Loai bo so 0 khong co nghia
        while s[1]='0' do
        delete(s,1,1);
        for i:=1 to length(s) do
        if not (s[i] in ['0'..'9']) then b:=false;
    until b=true;
    SoInp := s;
end;
//Doc so
procedure ReadN;
var i,j:byte;
    s1,s2,ss:string;
begin
    ss := '';
    j  := 0;
    for i:=length(SoInp) downto 1 do
    begin
        inc(j);
        j := j mod 12;
        case (SoInp[i]) of
        '0':
        begin
            if not (j in [1,4,7,10]) then
            s1:=' KHONG'
            else s1 := '';
            if (j in [2,5,8,11]) then s1:='';
        end;
        '1':
        begin
            if j in [2,5,8,11] then s1 := ' MUOI'
            else s1 := ' MOT';
        end;
        '2': s1:=' HAI';
        '3': s1:=' BA';
        '4': s1:=' BON';
        '5':
        begin
            if ((j in [1,4,7,10])and(SoInp[i-1]<>'0')) then
            s1 := ' LAM'
            else s1 := ' NAM';
        end;
        '6': s1:=' SAU';
        '7': s1:=' BAY';
        '8': s1:=' TAM';
        '9': s1 := ' CHIN';
        end;
        case j of
        1:s2:='';
        2,5,8,11:
        begin
            if SoInp[i]<>'0' then
            begin
                if SoInp[i]<>'1' then s2 := ' muoi'
                else s2 := '';
            end
            else if SoInp[i+1]<>'0' then
            s2:=' le'
            else s2 := '';
        end;
        3,6,9,0:s2 := ' tram';
        4:s2 := ' ngan';
        7:s2 := ' trieu';
        10:s2 := ' ty';
        end;
    ss := s1+s2+ss;
    end;
    write(ss);
end;

//Chuong trinh chinh
begin
    InPut;
    ReadN;
    readln;
end.