{

}

nama : Dwi Kusrianto p. Nim : j2f008098 nama file: kalkulator.pas deskripsi: kalkulator infix

program kalkulator_stack; uses wincrt; const maxStack type = 80;

larik = array[1..maxStack] of char; realStack = record top : integer; element :array[1..maxStack] of real; end; charStack = record top : integer; element : larik; end; operands : realStack; operators : charStack; karakter : larik; banyakKarakter : integer; lagi : char;

var

procedure bacaKar (var karakter : larik; var akhir : integer); var i: integer; begin writeln ('P-R-O-G-R-A-M- -K-A-L-K-U-L-A-T-O-R'); writeln('tulis ekspresi Aritmatika tanpa diakhiri tanda ("=")'); writeln; i:=0; while not eoln do begin i:=i+1; read(karakter[i]); end; readln; akhir:=i;

end;

procedure clearReal(var stack: realStack); begin stack.top:=0; end;

function emptyReal (stack: realStack):boolean; begin emptyReal:=stack.top=0; end;

function fullReal (stack: realStack) : boolean; begin fullReal:=stack.top=maxStack; end;

{prosedur pushreal} procedure pushReal(var stack: realStack; newElement: real ); begin stack.top:=stack.top+1; stack.element[stack.top]:=newElement; end;

{prosedur popreal} procedure popReal(var stack: realStack; var poppedElement:real ); begin poppedElement:=stack.element[stack.top]; stack.top:= stack.top-1; end;

{prosedur clear Char} procedure clearChar(var stack: charStack); begin stack.top:=0; end;

{fungsi empty char} function emptyChar(stack: charStack):boolean; begin emptyChar:=stack.top=0; end;

function fullChar(stack:charStack):boolean; begin fullChar:=stack.top=maxStack; end;

{prosedur push Char} procedure pushChar(var stack: charStack; newElement: char ); begin stack.top:=stack.top+1; stack.element[stack.top]:=newElement; end;

{prosedur popchar} procedure popchar(var stack: charStack; var poppedElement:char ); begin poppedElement:=stack.element[stack.top]; stack.top:= stack.top-1; end;

{prosedur konversi} procedure konversi(karakter: larik; i:integer; var hasil: real ); begin case karakter[i] of '1': hasil :=1; '2': hasil :=2; '3': hasil :=3; '4': hasil :=4; '5': hasil :=5; '6': hasil :=6;

end; end;

'7': '8': '9': '0':

hasil hasil hasil hasil

:=7; :=8; :=9; :=0;

{prosedur ubahBilAsli} procedure ubahKeBilAsli(var stack: realStack; i:integer ); var basis,j : integer; angka, poppedReal :real; begin basis:= 1; angka:= 0; for j:=1 to i-1 do begin popReal(operands, poppedReal); angka:=angka+basis*poppedReal; basis:=basis*10; end; pushReal(operands, angka);

end;

function derajat(op:char):integer; begin case op of '*','/' : derajat:=2; '+','-' : derajat:=1; end; end;

{prosedur kalibagitambahkurang} procedure kaliBagiTambahKurang(var operands:realStack; var operators:charStack ); var x,y,z : real; stackOperandPenuh, stackOperatorKosong, stackOperandKosong :boolean; poppedReal : real; poppedChar : char;

begin stackOperandKosong:=emptyReal(operands); if not stackOperandKosong then popReal(operands,poppedReal); x:=poppedReal; stackOperandKosong:=emptyReal(operands); if not stackOperandKosong then popReal(operands,poppedReal); y:=poppedReal; stackOperatorKosong:=emptyChar(operators); if not stackOperatorKosong then popChar(operators,poppedChar); case poppedChar of '*': z:=x*y; '/': z:=x/y; '+': z:=x+y; '-': z:=x-y; end; stackOperandPenuh:= fullReal(operands); if not stackOperandPenuh then pushReal(operands, z); end;

{prosedur tanpakurung} procedure tanpaKurung(var operands: realStack; var operators: charStack; var z: real); type simpanReal = record penanda : integer; isi : real; end; simpanChar = record drjt, penanda : integer; isi : char; end; var tempReal tempChar i, j, iTempReal, iTempOp stackOperatorKosong, stackOperandKosong x, y, poppedReal poppedChar begin : array[1..maxStack] of simpanReal; : array[1..maxStack] of simpanChar;

: integer; : boolean; : real; : char;

z:=0; iTempReal:=1;

stackOperandKosong:=emptyReal(operands); while (not stackOperandKosong) do begin popReal(operands, poppedReal); tempReal[iTempReal].isi:=poppedReal; tempReal[iTempReal].penanda:=1; iTempReal:=iTempReal+1; stackOperandKosong:=emptyReal(operands); end; iTempReal:=iTempReal-1; iTempOp:=1; stackOperatorKosong:=emptyChar(operators); while (not stackOperatorKosong) do begin popChar(operators, poppedChar); tempChar[iTempOp].isi:=poppedChar; tempChar[iTempOp].drjt:=derajat(poppedChar); tempChar[iTempOp].penanda:=1; iTempOp:=iTempOp+1; stackOperatorKosong:=emptyChar(operators); end; iTempOp:=iTempOp-1; for i:=iTempOp downto 1 do begin if ((tempChar[i].drjt=2) and (tempReal[i+1].penanda=1) and (tempReal[i].penanda=1)) then begin x:=tempReal[i+1].isi; tempReal[i+1].penanda:=0; y:=tempReal[i].isi; if tempChar[i].isi='*' then z:=x*y else z:=x/y; tempReal[i].isi:=z; tempChar[i].penanda:=0; end; end; for i:=iTempOp downto 1 do begin if tempChar[i].penanda<>0 then begin x:=tempReal[i+1].isi; tempReal[i+1].penanda:=0; if tempReal[i].penanda<>0 then y:=tempReal[i].isi else begin for j:=i downto 1 do if tempReal[j].penanda<>0 then y:=tempReal[j].isi; end;

if tempChar[i].isi='+' then z:=x+y else z:=x-y; tempReal[i].isi:=z; end; end; end;

{prosedur hitung} procedure hitung (karater:larik; var operands:realStack; var operators:charStack; banyakKarakter:integer ); var i, posisiOperator :integer; hasilKonversi, hasil :real; operator : set of char; stackOperatorPenuh, stackOperandPenuh, stackOperandkosong, stackOperatorKosong : boolean; poppedReal :real; poppedChar :char; begin operator:=['+','/','-','*']; i:=1; posisiOperator:=0; while i<=banyakKarakter do begin if karakter[i]=')' then begin posisiOperator:=i-posisiOperator; if posisiOperator>2 then ubahKeBilAsli(operands, posisiOperator); posisiOperator:=i; kaliBagiTambahKurang(operands, operators); i:=i+1; end else begin if karakter[i]='(' then begin i:=i+1; {index} posisiOperator:=posisiOperator+1; end else begin if karakter[i] in operator then begin stackOperatorPenuh:=fullChar(operators); if not stackOperatorPenuh then pushChar(operators, karakter[i]); posisiOperator:=i-posisiOperator; if posisiOperator>2 then

ubahKeBilAsli(operands, posisiOperator); posisiOperator:=i; i:=i+1; end else begin

konversi(karakter, i, hasilKonversi); stackOperandPenuh:=fullReal(operands); if not stackOperandPenuh then pushReal(operands, hasilKonversi); i:=i+1;

end;

end;

end; end; if karakter[banyakKarakter]<>')' then begin posisiOperator:=banyakKarakter-posisiOperator+1; ubahKeBilAsli(operands, posisiOperator); end; if operands.top>1 then begin tanpaKurung(operands, operators, hasil); stackOperandPenuh:=fullReal(operands); if not stackOperandPenuh then pushReal(operands, hasil); end;

end;

{prosedur cetak} procedure cetak(operands: realStack); var stackOperandKosong: boolean; poppedReal : real; begin stackOperandKosong:=emptyReal(operands); if not stackOperandKosong then popReal(operands, poppedReal); writeln('Hasil = ',poppedReal:8:2); write('hendak menghitung lagi? Y(ya)/T(idak): ');

end;

{ PROGRAM UTAMA } begin clrscr; repeat clrscr; writeln('PROGRAM KALKULATOR'); writeln('Deskripsi : Kalkulator Infix Dengan Input Bilangan Bulat'); writeln; bacaKar(karakter, banyakKarakter); clearChar(operators);

clearReal(operands); hitung(karakter, operands, operators, banyakKarakter); cetak(operands); lagi:=readkey; until upcase(lagi)<>'Y'; end. { AKHIR PROGRAM }

Sign up to vote on this title
UsefulNot useful