Pelajaran Hidup

Inspirasi dari sebuah pengalaman dan pelajaran hidup..Semua butuh sebuah pembelajaran

Program Stack dengan C++

//achicha_01@yahoo.com
#include
#include
#define maks 5
#include
#include

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< banyak =" 0;" i="0;" maks="" int="" return="" s="=" 0="" 1="" void="" cout=""><< "\nIsi stack : "; for (int i=0;i< i="banyak;">=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 A[banyak]= '0';
banyak--;
return temp;
}

void Stack::PrintMatchedPairs(char *expr){
int A[maks];
int j, length=strlen(expr);
for(int i=1;i<=length;i++){
if(expr[i-1]=='(');
else if (expr[i-1]==')')
cout << j << ' ' << i << endl;
}
}

int main(int argc, char *argv[])
{
Stack stack;

for (char c='a'; c<'d'; c++){
stack.push(c);
stack.cetak();
}
char expr[maks];
cout << "Type an expression of length at most " << maks << endl;
cin.getline(expr,maks);
cout << "The pairs of maytching parentheses in " << endl;
puts(expr);
cout << "are" << endl;
// PrintMatchedpairs(*expr);
char p = stack.pop();
stack.cetak();
cout << "\n\nCetak pakai overloading : " << stack;

system("PAUSE");
return EXIT_SUCCESS;
}

0 komentar:

Posting Komentar