logo elektroda
logo elektroda
X
logo elektroda
REKLAMA
REKLAMA
Adblock/uBlockOrigin/AdGuard mogą powodować znikanie niektórych postów z powodu nowej reguły.

Program w Pascalu: operacje arytmetyczne na liczbach binarnych w tablicach

bobek122 18 Paź 2005 19:44 2997 3
REKLAMA
  • #1 1908886
    bobek122
    Poziom 21  
    Posty: 624
    Pomógł: 4
    Ocena: 18
    Napisać program wykonujący przynajmniej jedno z działań (dodawanie, odejmowanie, mnożenie lub dzielenie) na liczbach w systemie dwójkowym. Użyj do reprezentacji liczb binarnych tablic długości 10. Użytkownik podaje liczby po uruchomieniu programu (liczby maksymalnie 10-cyfrowe). Program wypisuje te liczby i wynik działania. Prosze o pomoc!!!
  • REKLAMA
  • Pomocny post
    #2 1911303
    Łuk_F
    Poziom 17  
    Posty: 257
    Pomógł: 34
    Ocena: 12
    Witam


    Mam rozwiązanie twojego problemu ale w osobnych plikach. Jest tam dodawanie, odejmowanie, mnożenie i dzilenie we wszystkich kodach (NKB, U1, U2, BDC). Musisz przejrzeć te programy i wybrać odpowiednie procedury.
    Załączniki:
    • operacje.rar (5.22 KB) Musisz być zalogowany, aby pobrać ten załącznik.
  • REKLAMA
  • #4 1927481
    bobek122
    Poziom 21  
    Posty: 624
    Pomógł: 4
    Ocena: 18
    hmmm jakos nieradze sobie z tym co mi przesłałes... moze mi pomozesz?

    Dodano po 56 [minuty]:

    a moze chociaz ktos wie jak dodac takowe liczy w tym systemie? zamieszczam kod ktory moze ktos potrafi przekształcic?

    program Dodawanie_Odejmowanie;

    type tabA=array[1..16] of integer;
    tabB=array[1..4] of integer;
    var
    tab:tabA;
    tab1:tabB;
    a,i,j,x,y,z:integer;
    roz,zypis,dzialanie:integer;
    ciag:string[16];

    procedure DECnaNKB(var a:integer; var tab:tabA);

    var b,c:integer;
    i:byte;

    begin
    i:=0;
    repeat
    begin
    i:=i+1;
    b:=a div 2;
    c:=a mod 2;
    a:=b;

    if c=0 then tab[i]:=0 else tab[i]:=1;

    end;

    until b=0;
    for j:=i+1 to 16 do tab[j]:=0;

    end;

    procedure NKBnaDEC( var a:integer; tab:tabA);
    var
    i:integer;
    s:integer;
    begin
    s:=1;
    a:=0;
    for i:=16 downto 1 do
    begin
    if tab[i]=1 then
    a:=a+s;
    s:=s*2;
    end;
    end;

    procedure DECnaBCD(var tab:tabA; var tab1:tabB);
    var i,j,b,c,d,f:integer;
    begin
    for i:=1 to 16 do
    tab[i]:=0;
    for i:=1 to 4 do
    tab1[i]:=0;
    i:=4;
    d:=f;

    repeat
    begin
    b:=a div 10;
    c:=a mod 10;
    a:=b;
    tab1[i]:=c;
    i:=i-1;
    end;
    until b=0;

    for j:=1 to 4 do
    begin
    if tab1[j]<>0 then
    begin
    i:=4*j;
    repeat
    begin

    b:=tab1[j] div 2;
    c:=tab1[j] mod 2;
    tab1[j]:=b;
    if c=0 then tab[i]:=0 else tab[i]:=1;
    i:=i-1;
    end;
    until b=0;

    end;
    end;

    end;

    procedure BCDnaDEC(var a:integer; tab:tabA);
    var s,i:byte;
    begin
    s:=1;
    a:=0;
    for i:=4 downto 1 do
    begin
    if tab[i]=1 then
    a:=a+s;
    s:=s*2;
    end;
    S:=1;
    a:=a*10;
    for i:=8 downto 5 do
    begin
    if tab[i]=1 then
    a:=a+s;
    s:=s*2;
    end;
    a:=a*10;
    s:=1;
    for i:=12 downto 9 do
    begin
    if tab[i]=1 then
    a:=a+s;
    s:=s*2;
    end;
    a:=a*10;
    s:=1;
    for i:=16 downto 13 do
    begin
    if tab[i]=1 then
    a:=a+s;
    s:=s*2;
    end;

    end;




    procedure DECnaZM(a:integer; var tab:tabA);
    begin
    DECnaNKB(a,tab);
    if a<0 then
    tab[1]:=1;
    end;

    procedure ZMnaDEC(var a:integer; tab:tabA);
    begin
    if tab[1]=1 then
    begin
    tab[1]:=0;
    NKBnaDEC(a,tab);
    a:=a*(-1);
    end
    else
    NKBnaDEC(a,tab);
    end;

    procedure DECnaU1(a:integer;var tab:tabA);
    var
    i:integer;
    begin
    if a>0 then
    begin
    DECnaNKB(a,tab);
    tab[1]:=0;
    end
    else
    begin
    DECnaNKB(a,tab);
    tab[1]:=1;
    for i:=2 to 16 do
    if tab[i]=1 then
    tab[i]:=0
    else tab[i]:=1;
    end;
    end;

    procedure U1wDEC(var a:integer;tab:tabA);
    var
    i:byte;
    begin
    if tab[1]=1 then
    begin
    for i:=1 to 16 do
    if tab[i]=1 then
    tab[i]:=0
    else tab[i]:=1;
    NKBnaDEC(a,tab);
    a:=a*(-1);
    end
    else NKBnaDEC(a,tab);
    end;

    procedure DECnaU2(a:integer;var tab:tabA);
    var z,i:byte;
    begin
    if a>=0 then
    DECnaNKB(a,tab)
    else
    begin
    i:=16;
    DECnaU1(a,tab);
    repeat
    z:=tab[i];
    if z=1 then tab[i]:=0
    else tab[i]:=1;
    i:=i-1;
    until (i=2) or (z=0);
    end;
    end;

    procedure U2naDEC(var a:integer;tab:tabA);
    var i,z:byte;
    begin
    if tab[1]=0 then
    NKBnaDEC(a,tab)
    else
    begin
    for i:=1 to 16 do
    if tab[i]=1 then tab[i]:=0
    else tab[i]:=1;
    i:=16;
    repeat
    z:=tab[i];
    if z=1 then tab[i]:=0
    else tab[i]:=1;
    i:=i-1;
    until (i=2) or (z=0);
    NKBnaDEC(a,tab);
    a:=a*(-1);
    end;
    end;

    begin
    roz:=0;
    writeln('Kalkulator wzkonujacz dodawanie i odejmowanie liczb binarnych');
    writeln('Wzbiery zypis w jakim podasy liczbe');
    writeln('1 NKB');
    writeln('2 ZM');
    writeln('3 U1');
    writeln('4 U2');
    writeln('5 BCD');
    readln(zypis);

    if zypis=1 then begin
    for j:=1 to 2 do begin
    writeln;
    writeln('podaj ',j ,'-a liczbe w zypiszie NKB');
    readln(ciag);
    for i:=1 to 16 do
    tab[i]:=ord(ciag[i])-48;
    NKBnaDEC(a,tab);
    if j=1 then x:=a else y:=a;
    z:=1;
    end;
    end

    else
    if zypis=2 then begin
    for j:=1 to 2 do begin
    writeln('podaj ',j,'-a liczbe w zypiszie Z-M ');
    readln(ciag);
    for i:=1 to 16 do
    tab[i]:=ord(ciag[i])-48;
    ZMnaDEC(a,tab);
    if j=1 then x:=a else y:=a;
    z:=2;
    end;
    end

    else if zypis=3 then begin
    for j:=1 to 2 do begin
    writeln('podaj ',j,' liczbe w zypiszie U1');
    readln(ciag);
    for i:=1 to 16 do
    tab[i]:=ord(ciag[i])-48;
    U1wDEC(a,tab);
    if j=1 then x:=a else y:=a;
    end;

    z:=3;
    end

    else if zypis=4 then begin
    for j:=1 to 2 do begin
    writeln('podaj',j,'-a liczbe w zypiszie U2');
    readln(ciag);
    for i:=1 to 16 do
    tab[i]:=ord(ciag[i])-48;
    U2naDEC(a,tab);
    if j=1 then x:=a else y:=a;
    end;
    z:=4;
    end

    else begin
    for j:=1 to 2 do begin
    writeln('podaj',j,'-a liczbe w zypiszie BCD');
    readln(ciag);
    for i:=1 to 16 do
    tab[i]:=ord(ciag[i])-48;
    BCDnaDEC(a,tab);
    if j=1 then x:=a else y:=a;
    end;
    z:=5;
    end;

    for i:=1 to 16 do
    tab[i]:=0;
    writeln('Wzbiery dyialanie');
    writeln('1 - dodawanie');
    writeln('2 - odejmowanie');
    readln(dzialanie);
    if dzialanie=1 then
    begin
    roz:=x+y;
    end
    else
    begin
    roz:=x-y;
    end;

    readln;
    a:=roz;
    writeln('roz: ');
    if (z=1) and (a>0) then
    begin DECnaNKB(a,tab); for i:=16 downto 1 do write(tab[i]); end
    else
    if z=2 then
    begin DECnaZM(a,tab); for i:=16 downto 1 do write(tab[i]); end
    else
    if z=3 then
    begin DECnaU1(a,tab); for i:=16 downto 1 do write(tab[i]); end
    else
    if z=4 then
    begin DECnaU2(a,tab); for i:=16 downto 1 do write(tab[i]); end
    else
    if (z=5) and (a>0) then
    begin DECnaBCD(tab,tab1); for i:=1 to 16 do write(tab[i]); end
    else writeln('nie ma roywiayania w wzbranzm zypisyie !!!');


    readln;
    end.
REKLAMA