计算机上级复试资料

7. 入门并实践STL——stack篇

2019-03-05  本文已影响0人  zju_dream

stack

1. How to use?

#include <stack>
using namespace std;

2. stack的定义

stack<typename> name;

3. 元素的访问

4. 常用函数解析

  1. push(x): O(1)
  2. top(): O(1)
  3. pop(): O(1)
  4. empty(): O(1)
  5. size(): O(1)

5. 常见用途

6. 习题

两道习题

  1. 简单计算器
#include<iostream>

#include<string>

#include<stack>

#include<queue>

using namespace std;

int N;



int charTpri(char ch) {

        int pri;

        switch (ch) {

        case '+':

               pri = 1;

               break;

        case '-':

               pri = 2;

               break;

        case '*':

               pri = 3;

               break;

        case '/':

               pri = 4;

               break;

        }

        return pri;

}



int main() {

        stack<int> operators;

        stack<double> nums;

        string str = "";

        

        while (getline(cin, str), str != "0") {

               while (!operators.empty()) operators.pop();

               while (!nums.empty()) nums.pop();



               int num = 0;

               // +:1  -:2  *:3  /:4. The bigger, the more prior



               operators.push(0);

               for (int i = 0; i < str.length(); i++) {

                       if (str[i] == ' ') {

                              if (num != 0) {

                                      nums.push(num);

                              }

                              // the previous char is operator

                              else {



                              }

                              // reinit

                              num = 0;

                       }

                       else if (str[i] >= '0' && str[i] <= '9') {

                              num = num * 10 + (str[i] - '0');

                              if (i == str.length() - 1) {

                                      nums.push(num);

                              }



                       }

                       else {

                              int top_pri = operators.top();

                              int cur_pri = charTpri(str[i]);

                              while (top_pri >= cur_pri) {



                                      float num2 = nums.top();

                                      //

                                      nums.pop();

                                      float num1 = nums.top();

                                      

                                      nums.pop();

                                      float res = 0;

                                      if (top_pri == 1) {

                                             res = num1 + num2;

                                      }

                                      else if (top_pri == 2) {

                                             res = num1 - num2;

                                      }

                                      else if (top_pri == 3) {

                                             res = num1 * num2;

                                      }

                                      else {

                                             res = num1 / num2;

                                      }

                                      nums.push(res);

                                      operators.pop();

                                      top_pri = operators.top();

                              }



                              operators.push(cur_pri);



                       }

               }



               while (operators.top() != 0) {

                       int top_pri = operators.top();

                       operators.pop();

                       float num2 = nums.top();

                       nums.pop();

                       float num1 = nums.top();

                       nums.pop();

                       float res = 0;

                       if (top_pri == 1) {

                              res = num1 + num2;

                       }

                       else if (top_pri == 2) {

                              res = num1 - num2;

                       }

                       else if (top_pri == 3) {

                              res = num1 * num2;

                       }

                       else {

                              res = num1 / num2;

                       }
                       nums.push(res);

               }

               printf("%.2f\n", nums.top());

               //getline(cin, str);

        }

        return 0;

}


  1. 括号匹配
#include <stack>

#include <iostream>

#include <string>



using namespace std;

bool matching(char ch1, char ch2) {

        if ((ch1 == '[' && ch2 == ']') ||

               (ch1 == '(' && ch2 == ')') ||

               (ch1 == '{' && ch2 == '}')) return true;

        else return false;

}

stack<char> op;



int main() {

        int N;

        cin >> N;

        // 需要用不带参数的get()方法接收掉换行符

        cin.ignore();

        for (int i = 0; i < N; i++) {

               bool flag = false;

               string str;

               getline(cin, str);

               while (!op.empty()) {

                       op.pop();

               }

               for (int j = 0; j < str.length(); j++) {

                       if (str[j] == '[' || str[j] == '{' || str[j] == '(') {

                              op.push(str[j]);

                       }

                       else if (str[j] == ']' || str[j] == '}' || str[j] == ')') {

                              if (op.empty()) {

                                      flag = false;

                                      break;

                                      //printf("%s\n", "no");



                              }

                              else {

                                      char ch = op.top();

                                      op.pop();

                                      if (matching(ch, str[j])) flag = true;

                                      else {

                                             flag = false;

                                             break;

                                      }

                              }

                       }

                       else {

                              continue;

                       }

               }

               //

               if (flag == true && op.empty()) cout << "yes" << endl;

               else cout << "no" << endl;



        }

        return 0;

}


上一篇下一篇

猜你喜欢

热点阅读