首页 > 试题广场 >

在标准 C++类库中,对栈类(stack)重载了= =、!

[问答题]

在标准 C++类库中,对栈类(stack)重载了= =! =>> =<< =等运算符,以对两个不同的栈进行算术比较运算操作,请构造一个整型栈,以 = =< 运算为例,对两 个栈进行算术比较运算,体会其比较归者规则;运行程序,观察其输出。

推荐

解:

源程序:

#include <stack>
#include <iostream>
using namespace std ;
typedef stack<double> STACK_DOUBLE;
void main()
{
STACK_DOUBLE stack1,stack2;
// Add item 4.0 to Stack1. Stack1 contains 4.0.
cout << "stack1.push(4.0) s1=[4.0]" << endl;
stack1.push(4.0);
//Add item 3.0 to Stack1. Stack1 contains 3.0(top) and 4.0(bottom).
cout << "stack1.push(3.0) s1=[3.0 4.0]" << endl;
stack1.push(3.0);
//Add item 4.0 to Stack2. Stack2 contains 4.0 (top=bottom).
cout << "stack2.push(4.0) s2=[4.0]" << endl;
stack2.push(4.0);
// Compare if Stack1 is smaller than Stack2. Should return False.
cout << "stack1==stack2 is " <<
((stack1==stack2)? "True": "False") << endl;
cout << "stack1<stack2 is " <<
((stack1<stack2)? "True": "False") << endl << endl;
//Add item 6.0 to Stack2. Stack2 contains 6.0(top) and 4.0(bottom).
cout << "stack2.push(6.0) s2=[6.0 4.0]" << endl;
stack2.push(6.0);
//Compare if Stack1 is smaller than Stack2. Should return True.
cout << "stack1==stack2 is " <<
((stack1==stack2)? "True": "False") << endl;
cout << "stack1<stack2 is " <<
((stack1<stack2)? "True": "False") << endl << endl;
//Add item 8.0 to Stack2. Stack2 contains 8.0(top), 6.0 and
//4.0(bottom).
cout << "stack2.push(8.0) s2=[8.0 6.0 4.0]" << endl;
stack2.push(8.0);
// Compare if Stack1 is smaller than Stack2. Should return True.
cout << "stack1==stack2 is " <<
((stack1==stack2)? "True": "False") << endl;
cout << "stack1<stack2 is " <<
((stack1<stack2)? "True": "False") << endl << endl;
// Delete item 8.0 from Stack2.
cout << "stack2.pop() s2=[6.0 4.0]" << endl;
stack2.pop();
// Delete item 6.0 from Stack2.
cout << "stack2.pop() s2=[4.0]" << endl;
stack2.pop();
//Add item 3.0 to Stack2. Stack2 contains 3.0(top) and 4.0(bottom).
cout << "stack2.push(3.0) s2=[3.0 4.0]" << endl;
stack2.push(3.0);
//Compare if Stack1 is smaller than Stack2. Should return False.
cout << "stack1==stack2 is " <<
((stack1==stack2)? "True": "False") << endl;
cout << "stack1<stack2 is " <<
((stack1<stack2)? "True": "False") << endl << endl;
//Delete item 3.0 from Stack2.
cout << "stack2.pop() s2=[4.0]" << endl;
stack2.pop();
// Delete item 4.0 from Stack2.
cout << "stack2.pop() s2=[]" << endl;
stack2.pop();
//Add item 8.0 to Stack2. Stack2 contains 8.0(top=bottom).
cout << "stack2.push(8.0) s2=[8.0]" << endl;
stack2.push(8.0);
//Compare if Stack1 is smaller than Stack2. Should return True.
cout << "stack1==stack2 is " <<
((stack1==stack2)? "True": "False") << endl;
cout << "stack1<stack2 is " <<
((stack1<stack2)? "True": "False") << endl << endl;
}

程序运行输出:

stack1.push(4.0) s1=[4.0]

stack1.push(3.0) s1=[3.0 4.0]

stack2.push(4.0) s2=[4.0]

stack1<stack2 is False

stack2.push(6.0) s2=[6.0 4.0]

stack1<stack2 is True

stack2.push(8.0) s2=[8.0 6.0 4.0]

stack1<stack2 is True

stack2.pop() s2=[6.0 4.0]

stack2.pop() s2=[4.0]

stack2.push(3.0) s2=[3.0 4.0]

stack1<stack2 is False

stack2.pop() s2=[4.0]

stack2.pop() s2=[]

stack2.push(8.0) s2=[8.0]

stack1<stack2 is True



发表于 2018-04-18 20:41:47 回复(0)