Skip to content

Praktikum 7

Desember 13, 2010

#include
#include
#include
#include
#define maks 5

using namespace std;

class Stack{
friend ostream& operator<<(ostream&, const Stack&);
public:
Stack();
int penuh(int);
int kosong(int);
void cetak();
void push(char);
char pop();
void PrintMatchedPairs(char *expr);
private:
char A[maks];
int banyak;
};

ostream& operator<<(ostream& out, const Stack& s)
{
cout << “\nIsi stack : “;
for (int i=0;i< s.banyak;i++)
out << s.A[i] << ” “;
}

Stack::Stack(){
banyak = 0;
for (int i=0; i<maks; i++)
A[i] = ’0′;
}

int Stack::penuh(int s)
{
return s == maks ? 1 : 0;
}

int Stack::kosong(int s)
{
return s == 0 ? 1 : 0;
}

void Stack::cetak()
{
cout << “\nIsi stack :”;
for (int i=0;i< banyak;i++)
cout << A[i] << ” “;
}

void Stack::push(char x)
{
cout << “\nElemen masuk :” << x;
if (penuh(banyak)) cout <=0; i–)
A[i+1] = A[i];
A[0] = x;
banyak++; }
}

char Stack::pop()
{
cout <<”\nPop stack, elemen yang di-pop :” << A[0];
char temp=A[0];
for (int i=0;i<banyak; i++) A[i] = A[i+1];
A[banyak]= ’0′;
banyak–;
return temp;
}

void Stack::PrintMatchedPairs(char *expr)
{
Stack A;
//int A[maks];
int j, length = strlen(expr);
for (int i=1; i<=length;i++){
if(expr[i-1]==’(‘)A.push(i);
else if (expr[i-1]==’)’)
cout << j << ‘ ‘ << endl;}
// while (!A.kosong()){
// A.pop(j);
// cout << “No match for left parenthesis at ” << j << endl;}
}

int main(int argc, char *argv[])
{
Stack stack;
char *rumus = {“(a+b))”};
for (char c=’a’; c<’d’; c++){
stack.push(c);
stack.cetak();
}
char expr[maks];
cout << “\nType an expression of length at most ” << maks << endl;
cin.getline(expr, maks);
cout << “The pairs of matching parentheses in” << endl;
puts(expr);
cout << “are” << endl;
//stack.PrintMatchedPairs(rumus);
char p = stack.pop();
stack.cetak();
cout << “\n\nCetak pakai overloading : ” << stack;

system(“PAUSE”);
return EXIT_SUCCESS;
}

From → Praktikum

Tinggalkan sebuah Komentar

Tinggalkan Balasan

Isikan data di bawah atau klik salah satu ikon untuk log in:

Logo WordPress.com

You are commenting using your WordPress.com account. Logout / Ubah )

Gambar Twitter

You are commenting using your Twitter account. Logout / Ubah )

Foto Facebook

You are commenting using your Facebook account. Logout / Ubah )

Foto Google+

You are commenting using your Google+ account. Logout / Ubah )

Connecting to %s

%d blogger menyukai ini: