Please note, this is a STATIC archive of website www.tutorialspoint.com from 11 May 2019, cach3.com does not collect or store any user information, there is no "phishing" involved.
Tutorialspoint

Logic Gate

program LogicGate;
//Jonas Yip
uses Math,StrUtils,SysUtils;
var
  First_Input,Second_Input,Output : integer;
  Gate : string;
  B_First , B_Second : integer;
procedure Start;
begin
    writeln('Choose a gate AND, OR, NOT, XOR, NAND, NOR');
    readln(Gate);
    if MatchStr(Gate, ['AND','OR','NOT','XOR','NAND','NOR']) then begin //A Match
      write('First input '); //Validation
      try
      readln(First_Input);
      try except
        Start; //Loops
        end;
      finally //Continues if everything is fine
      B_First := IfThen(((First_Input= 0) or (First_Input = 1)), 1, 0);
      if(Gate <> 'NOT') and (B_First = 1) then begin //NOT gate has one input
              write('Second input ');
              try
                 readln(Second_Input);
              except
                Start; //Restart
              end;
              end;
      //Condition for two inputs

      end;
    end

    else
    Start; //Loops
end;
begin // //
  while true do begin //Indefinite loop
      Start;  //Validation loop
      //if((First_Input <> 0) or (First_Input <> 1)) or ((Second_Input <> 1) or (Second_Input <> 0)) then Start;
      case Gate of //Gate selection
           'AND' : Output := IfThen((First_Input=1) and (Second_Input=1),1,0); //Ternary operator
           'OR' : Output := IfThen((First_Input=1) or (Second_Input=1), 1,0);
           'NOT' : Output := IfThen(First_Input=1,0,1);
           'XOR' : Output := IfThen(((First_Input=1) and (Second_Input=1) or
             (First_Input=0) and (Second_Input=0)),0,1);
           'NAND' : Output := IfThen((First_Input=1) and (Second_Input=1),0,1);
           'NOR' : Output := IfThen((First_Input=0) and (Second_Input=0),1,0);
      else;

      end;
      writeln('Output : ',Output);
      readln;

  end;
end.

Advertisements
Loading...

We use cookies to provide and improve our services. By using our site, you consent to our Cookies Policy.