本文整理汇总了C++中Stack::IsEmpty方法的典型用法代码示例。如果您正苦于以下问题:C++ Stack::IsEmpty方法的具体用法?C++ Stack::IsEmpty怎么用?C++ Stack::IsEmpty使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类Stack
的用法示例。
在下文中一共展示了Stack::IsEmpty方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C++代码示例。
示例1: checkBrackets
bool checkBrackets(char * text)
{
Stack<char> st;
int size = strlen(text);
for (int i = 0; i < size; i++)
if (text[i] == '(' || text[i] == '{' || text[i] == '[' || text[i] == '<')
st.Push(text[i]);
else if (text[i] == ')' || text[i] == '}' || text[i] == ']' || text[i] == '>')
{
if (st.IsEmpty())
return false;
if (((char)st.Peek() == '(' && text[i] == ')') ||
((char)st.Peek() == '{' && text[i] == '}') ||
((char)st.Peek() == '[' && text[i] == ']') ||
((char)st.Peek() == '<' && text[i] == '>'))
{
st.Pop();
}
else return false;
}
if (st.IsEmpty())
return true;
else
return false;
}
示例2: ToPostFix
void ToPostFix(char *indata, char *postfix)
{
int len = strlen(indata);
// now transform the infix into postfix
Stack<char> pst;
int i = 0;
int j = 0;
for(i = 0; i < len; i++)
{
cout << "Token " << indata[i] << endl;
if(indata[i] >= '0' && indata[i] <= '9')
postfix[j++] = indata[i];
else
{
switch(indata[i])
{
case '+': case '-':
while(pst.Top()!='(' && !(pst.IsEmpty()))
{
postfix[j++] = pst.Top();
pst.Pop();
}
pst.Push(indata[i]);
break;
case '*': case '/':
while(pst.Top()!='(' && pst.Top()!='+' && pst.Top()!='-' && !(pst.IsEmpty()))
{
postfix[j++] = pst.Top();
pst.Pop();
}
pst.Push(indata[i]);
break;
case '(':
pst.Push(indata[i]);
break;
case ')':
while(pst.Top()!='(' && !(pst.IsEmpty()))
{
postfix[j++] = pst.Top();
pst.Pop();
}
pst.Pop();
break;
}
}
pst.Show();
}
while(!(pst.IsEmpty()))
{
postfix[j++] = pst.Top();
pst.Pop();
}
postfix[j] = 0;
}
示例3: Count
ExpType Postfix::Count(const string& poststring, map<char, ExpType> values)
{
if (poststring == "")
throw
exception("String is empty");
Stack<ExpType> result;
char tmp;
ExpType leftOperand;
ExpType rightOperand;
for (int i = 0; i < poststring.length(); i++)
{
tmp = poststring[i];
if (poststring[poststring.length() - 1] == '=')
values[poststring[0]] = 0;
if (((tmp >= 'a') && (tmp <= 'z')) || ((tmp >= 'A') && (tmp <= 'Z')))
{
if (!values.count(tmp))
{
cout << "Enter " << tmp << ": ";
cin >> values[tmp];
}
result.Push(values[tmp]);
continue;
}
if (result.IsEmpty())
throw
exception("There is no result");
rightOperand = result.Pop();
if ((result.IsEmpty()) && (tmp == '-'))
{
result.Push(-rightOperand);
continue;
}
if (result.IsEmpty())
throw
exception("There is no result");
leftOperand = result.Pop();
switch (tmp)
{
case '+':
result.Push(leftOperand + rightOperand);
break;
case '-':
result.Push(leftOperand - rightOperand);
break;
case '*':
result.Push(leftOperand * rightOperand);
break;
case '/':
if (rightOperand == 0)
throw
exception("You can't divide by 0");
result.Push(leftOperand / rightOperand);
break;
}
}
示例4: main
int main(int argc, char *argv[])
{
//Error if there are not at least two commandline arguments
if(argc < 2)
output_usage_and_exit(argv[0]);
//Open specified file, quit if cannot open
std::ifstream in(argv[1]);
if(!in){
std::cerr << "Couldn't open " << argv[1] << std::endl;
exit (2);
}
//convert the infix expressions to postfix
string infix;
Stack<string> postfix;
while(in>>infix){
//substrings out the infix expression minus the line return and
//semi-colon.
string temp = infix.substr(0,infix.length()-2);
//splits the infix expression on spaces for easier processing. Stores in
//it in vector.
std::vector<string> vec = temp.split(' ');
//function for converting infix expressions to postfix format.
string result = infix2postfix(vec);
//places the resulting string from the the conversion on the stack.
postfix.push(result);
}
//done using the input file
in.close();
//handles if an output file was specified on commandline
if(argc==3){
std::ofstream out(argv[2]);
if(!out){
std::cerr << "Couldn't open " << argv[2] <<std::endl;
}
while(!postfix.IsEmpty()){
out<< postfix.pop() << std::endl;}
}else{
while(!postfix.IsEmpty()){
std::cout << postfix.pop() << std::endl;}
}
//successful
return 0;
}
示例5: GFG_LongestSubstringParen
static int GFG_LongestSubstringParen( char* string, int length )
{
Stack<int> subStringIndices;
int result = 0;
subStringIndices.Enqueue(-1);
for(int i = 0; i < length; i++)
{
if( string[i] == '(' )
{
subStringIndices.Enqueue(i);
}
else if( string[i] == ')' )
{
delete subStringIndices.Dequeue();
if( !subStringIndices.IsEmpty() )
{
int newLength = i - subStringIndices.Peek()->data;
if( result < newLength) result = newLength;
}
else
{
subStringIndices.Enqueue(i);
}
}
}
return result;
}
示例6: main
void main(void)
{
Stack<int> stack;
stack.PushBack(1);
stack.PushBack(2);
stack.PushBack(3);
stack.PushBack(4);
stack.PushBack(5);
cout << "Stack size : " << stack.Size() << endl;
cout << "Top element : " << stack.Top() << endl;
stack.PopBack();
stack.PopBack();
cout << "Stack size : " << stack.Size() << endl;
stack.Show();
stack.Clear();
if ( stack.IsEmpty() )
{
cout << "Stack is empty" << endl;
}
else
{
cout << "Stack is not empty" << endl;
}
system("@pause");
}
示例7: Lexema
TEST(Corrector, can_pass_correct_word) {
Lexema *word = new Lexema[6];
word[0] = Lexema(Type_Lexems::open_bracet, "(", 0);
word[1] = Lexema(Type_Lexems::digit, "1", 1);
word[2] = Lexema(Type_Lexems::add, "+", 2);
word[3] = Lexema(Type_Lexems::digit, "2", 3);
word[4] = Lexema(Type_Lexems::close_bracket, ")", 4);
word[5] = Lexema(Type_Lexems::terminal, "", 5);
Corrector c;
Stack<Error>* errors;
Error res;
errors = c.CheckExpression(word);
if (errors->IsEmpty())
{
res = Error(-2, "Ура, ошибок нет!");
}
else
{
res = errors->Put();
}
EXPECT_EQ(-2, res.GetPosition());
EXPECT_EQ("Ура, ошибок нет!", res.GetText());
}
示例8: main
// ------ Main (Driver) ------
int main(void)
{
ItemType Item1 = {'A', 4.1};
ItemType Item2 = {'B', 3.1};
ItemType Item3 = {'C', 2.1};
ItemType Item4 = {'D', 1.0};
ItemType tmpItem;
Stack dStack;
std::cout << " Stack created" << std::endl;
std::cout << " Stack is " << (dStack.IsEmpty() ? "" : "Not ") << "Empty" << std::endl;
std::cout << " Adding Items to Stack " << std::endl;
dStack.Push(Item1);
dStack.Push(Item2);
dStack.Push(Item3);
dStack.Push(Item4);
std::cout << " Stack is " << (dStack.IsEmpty() ? "" : "Not ") << "Empty" << std::endl;
std::cout << " Top of the Stack is : " ;
tmpItem = dStack.Top();
std::cout << " Key = " <<tmpItem.key
<< " Data = " <<tmpItem.data
<< std::endl;
std::cout << " Stack contains the following Items:" << std::endl;
int iloop = 0;
while (!dStack.IsEmpty())
{
iloop++;
std::cout << " Item " << iloop << " : " ;
tmpItem = dStack.Pop();
std::cout << " Key = " <<tmpItem.key
<< " Data = " <<tmpItem.data
<< std::endl;
}
return 0;
}
示例9: trans
void trans(char* infix,Stack<T> &stck)
{
stck.Push('#');
for(int i=0;i<strlen(infix);i++)
{
if(infix[i]>='0'&&infix[i]<='9')
cout<<infix[i];
else if(infix[i]==')')
{
for(;stck.Top()!='(';stck.Pop())
cout<<stck.Top();
stck.Pop();
}
else
{
if(infix[i]!='(')
for(;OpPriority(stck.Top())>=OpPriority(infix[i]);stck.Pop())
cout<<stck.Top();
stck.Push(infix[i]);
}
}
for(;!stck.IsEmpty();stck.Pop())
if(stck.Top()!='#')
cout<<stck.Top();
cout<<endl;
// if(isdigit(infix))
// cout<<infix;
// else if(IsOperator(infix))
// {
// if(stck.IsEmpty())
// stck.Push(infix);
// else
// {
// char temp=stck.Top();
// if(OpPriority(temp)>=OpPriority(infix))
// {
// if(stck.Top()==')')
// {
// while(stck.Top()!='(')
// {
// cout<<stck.Top();
// stck.Pop();
// }
// }
// else
// {
// while(!stck.IsEmpty())
// {
// cout<<stck.Top();
// stck.Pop();
// }
// stck.Push(infix);
// }
// }
// else
// stck.Push(infix);
// }
// }
}
示例10: printstack
int printstack(Stack &stack) {
// Prints the stack
if ( stack.IsEmpty() ) {
cout << "Stack is empty" << endl;
} else {
cout << "Stack contents: " << stack.PrintStack() << endl;
}
}
示例11: GFG_IsBST_GivenPreOrderTraversal
static bool GFG_IsBST_GivenPreOrderTraversal( int* array, int n )
{
Stack<int> parents;
int prevMin;
bool prevMinInitialised = false;
//Continue pushing parents onto the stack until we discover a right subtree
//If a right subtree is discovered, pop all the left nodes off the stack onto the array until the stack is empty or we find a parent of the right subtree
for(int i = 0; i < n; i++)
{
if( parents.IsEmpty() || parents.Peek()->data > array[i] )
{
parents.Enqueue(array[i]);
}
else //right subtree discovered
{
while( !parents.IsEmpty() && parents.Peek()->data < array[i] )
{
int Value = parents.Dequeue()->data;
//Test to see that the resulting array is sorted
if( prevMinInitialised && prevMin > Value )
{
return false;
}
prevMinInitialised = true;
prevMin = Value;
}
parents.Enqueue(array[i]);
}
}
//Put all of the remaining parents into our array
while( !parents.IsEmpty() ) {
int Value = parents.Dequeue()->data;
//Test to see that the resulting array is sorted
if( prevMinInitialised && prevMin > Value )
{
return false;
}
prevMinInitialised = true;
prevMin = Value;
}
return true;
}
示例12: square
int square(Stack &stack) {
// Checks if the stack has enough values.
if ( stack.IsEmpty() ) {
cout << "Not enough values in the stack!" << endl;
return 1;
}
double temp = stack.Pop();
double out = temp * temp;
stack.Push( out );
}
示例13: duplicate
int duplicate(Stack &stack) {
// Checks if the stack has enough values.
if ( stack.IsEmpty() ) {
cout << "Not enough values in the stack!" << endl;
return 1;
}
double temp = stack.Pop();
stack.Push( temp );
stack.Push( temp );
}
示例14: StackPushTests
bool StackPushTests()
{
Test t("Stack Pushing tests");
Stack s;
for (int i=0;i<10000; i++)
{
s.Push(i);
t.VerifyTrue(s.Top()==i,"Top should be i");
t.VerifyTrue(s.GetSize()==i+1,"Size should be i+1");
t.VerifyTrue(s.Peek()==i-1,"Peeked value should be i-1");
t.VerifyTrue(s.IsEmpty()==false,"Stack should not be empty");
}
return t.isPassed();
}
示例15: main
int main()
{
Stack<int> stack;
std::cout << "Stack created." << std::endl;
if(stack.IsEmpty())
std::cout << "Stack is empty." << std::endl;
else
std::cout << "Stack is not empty." << std::endl;
std::cout << "Pushing elements to the stack..." << std::endl;
stack.Push(1);
stack.Push(2);
stack.Push(3);
stack.Push(4);
stack.Push(5);
std::cout << "Top element of the stack right now: " << stack.Peek() << std::endl;
if(stack.IsEmpty())
std::cout << "Stack is empty." << std::endl;
else
std::cout << "Stack is not empty." << std::endl;
std::cout << std::endl;
std::cout << "Popping the stack:" << std::endl;
while(!stack.IsEmpty())
{
std::cout << stack.Peek() << std::endl;
stack.Pop();
}
if(stack.IsEmpty())
std::cout << "Stack is empty." << std::endl;
else
std::cout << "Stack is not empty." << std::endl;
return 0;
}