示例#1
0
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
0
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
0
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;
}
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;
}