stack or the heap, this time the task qw just make a check brackets. if the character is a parenthesis then 1 character will be inserted into the stack and remove the parentheses when you meet the character to the stack, so that we can know whether parentheses akhrir we balanced input. Caran do it easily, eg given manually via a keyboard character is (a + (b / c)) we only use the selection, or if. when character = brackets -> no input characters into the stack and show character. or in c + + can be shown if (chart == '(') push (kar) and when the meet mark parentheses -> no characters pop from the stack: if (chart == ')') pop (kar), if it does not meet the second the character then go directly to the next character. Please try.
#include <except.h>
#include <new.h>
// bad initializers
class BadInitializers {
public:
BadInitializers() {}
};
// insufficient memory
class NoMem {
public:
NoMem() {}
};
// change new to throw NoMem instead of xalloc
void my_new_handler()
{
throw NoMem();
};
new_handler Old_Handler_ = set_new_handler(my_new_handler);
// improper array, find, insert, or delete index
// or deletion from empty structure
class OutOfBounds {
public:
OutOfBounds() {}
};
// use when operands should have matching size
class SizeMismatch {
public:
SizeMismatch() {}
};
// use when zero was expected
class MustBeZero {
public:
MustBeZero() {}
};
// use when zero was expected
class BadInput {
public:
BadInput() {}
};
#endif
// file stack.h
// formula-based stack
#ifndef Stack_
#define Stack_
template<class T>
class Stack {
// LIFO objects
public:
Stack(int MaxStackSize = 10);
~Stack() {delete [] stack;}
int IsEmpty() const {return top == -1;}
int IsFull() const {return top == MaxTop;}
T Top() const;
Stack<T>& Add(const T& x);
Stack<T>& Delete(T& x);
private:
int top; // current top of stack
int MaxTop; // max value for top
T *stack; // element array
};
template<class T>
Stack<T>::Stack(int MaxStackSize)
{// Stack constructor.
MaxTop = MaxStackSize – 1;
stack = new T[MaxStackSize];
top = -1;
}
template<class T>
T Stack<T>::Top() const
{// Return top element.
if (IsEmpty()) throw OutOfBounds(); // Top fails
else return stack[top];
}
template<class T>
Stack<T>& Stack<T>::Add(const T& x)
{// Add x to stack.
if (IsFull()) throw NoMem(); // add fails
stack[++top] = x;
return *this;
}
template<class T>
Stack<T>& Stack<T>::Delete(T& x)
{// Delete top element and put in x.
if (IsEmpty()) throw OutOfBounds(); // delete fails
x = stack[top--];
return *this;
}
#endif
// match parentheses
#include <iostream.h>
#include <string.h>
#include <stdio.h>
const int MaxLength = 100; // max expression length
void cek_tandakurung(char *expr)
{// Parenthesis matching.
Stack<int> s(MaxLength);
int j, length = strlen(expr);
// scan expression expr for ( and )
for (int i = 1; i <= length; i++) {
if (expr[i - 1] == ‘(‘) s.Add(i);
else if (expr[i - 1] == ‘)’)
try {s.Delete(j); // unstack match
cout <<”(buka kurung at “<< j <<”)”<< ‘ ‘ <<”(tutup kurung at “<< i <<”)”<< endl;}
catch (OutOfBounds)
{cout << “tanda kurung kelebihan di no”
<< i << endl;}
}
// remaining ( in stack are unmatched
while (!s.IsEmpty()) {
s.Delete(j);
cout << “tanda kurung kelebihan di no “
<< j << endl;}
}
void main(void)
{
char expr[MaxLength];
cout << “masukan sembarang karekter yang mempunyai tanda kurung max (“
<< MaxLength <<”)”<< endl;
cin.getline(expr, MaxLength);
cout <<”karakter yang anda masukan adalah”
<< endl;
puts(expr);
cout<<endl;
cek_tandakurung(expr);
cout<<”\ntanda kurung seimbang “;
}
Tidak ada komentar:
Posting Komentar