本文整理汇总了C++中LinkedStack::top方法的典型用法代码示例。如果您正苦于以下问题:C++ LinkedStack::top方法的具体用法?C++ LinkedStack::top怎么用?C++ LinkedStack::top使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类LinkedStack
的用法示例。
在下文中一共展示了LinkedStack::top方法的4个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C++代码示例。
示例1: eval
void eval(LinkedStack& op, LinkedStack& num) {
int numtop = num.top2();
num.pop();
if (op.top() == "+")
numtop = add(num.top2(),numtop);
else if (op.top() == "-")
numtop = subtract(num.top2(),numtop);
else if (op.top() == "*")
numtop = multiply(num.top2(),numtop);
else if (op.top() == "/")
numtop = divide(num.top2(),numtop);
op.pop(), num.pop();
num.push(numtop);
}
示例2: expressionMain
void expressionMain() {
LinkedStack expressionNum;
LinkedStack expressionOp;
int choice = 1, num;
string op, expression = "", numS;
stringstream ss;
cout << "Please enter your expression one character at a time." << endl;
cout << "num:";
num = enterNum();
ss << num;
ss >> numS;
expression += numS;
expressionNum.push(num);
while (choice != 3) {
switch (choice) {
case 1: cout << "op:";
op = enterOp();
cout << "num:";
num = enterNum();
ss.clear();
ss << num;
ss >> numS;
expression += op + numS;
if (!expressionOp.empty() && opSize(expressionOp.top()) >= opSize(op)) {
eval(expressionOp,expressionNum);
if (!expressionOp.empty() && opSize(expressionOp.top()) >= opSize(op)) {
eval(expressionOp,expressionNum);
}
expressionOp.push(op);
expressionNum.push(num);
}
else {
expressionOp.push(op);
expressionNum.push(num);
}
cout << "Expression: " << expression << endl;
cout << "More(1),Quit(2)?: ";
cin >> choice;
break;
case 2: while (!expressionOp.empty())
eval(expressionOp,expressionNum);
cout << "Answer: " << expressionNum.top2() << endl;
choice = 3;
}
}
}
示例3: is_balanced
bool is_balanced(string brackets)
{
if(brackets == "") return true;
LinkedStack ls;
for (unsigned i = 0; i < brackets.size() ; i++)
{
string c = brackets.substr(i,i+1);
if(c == "[" || c == "(" || c == "{")
{
ls.push(c);
}
else if(c == "]" )
{
if(ls.top() == "[")
{
ls.pop();
}
else return false;
}
else if(c == "}")
{
if(ls.top() == "{")
{
ls.pop();
}
}
else if( c == ")" )
{
if(ls.top() == "(")
{
ls.pop();
}
else return false;
}
else
{
if(ls.isEmpty()) return true;
}
}
if(ls.isEmpty()) return true;
return false;
}
示例4: isHtmlMatched
bool isHtmlMatched(const vector<string>& tags) {
LinkedStack<string> S;
typedef vector<string>::const_iterator Iter;
for (Iter p = tags.begin(); p != tags.end(); ++p) {
if (p->at(1) != '/')
S.push(*p);
else {
if (S.empty()) return false;
string open = S.top().substr(1);
string close = p->substr(2);
if (open.compare(close) != 0) return false;
else S.pop();
}
}
if (S.empty()) return true;
else return false;
}