栈的应用

1.判断一个字符串中左右括号是否匹配
思路:使用一个栈。遇到左括号时,执行入栈操作,将其压入栈中。当遇到第一个右括号时,判断栈顶元素是否与其匹配,若匹配则出栈,继续搜索下一个右括号;若不匹配则说明整个字符串左右括号不匹配。以此类推,直到搜索完全部字符,若搜索完成后栈中还有元素则说明不匹配。
程序:

typedef char Stack_entry;  //栈中数据类型为char字符型
#include "stack.cpp"  //调用栈
int main()
{
   Stack openings;  //定义一个对象
   char symbol;     //存放符号
   bool is_matched = true;

   while (is_matched && (symbol = cin.get()) != '\n') { //搜索一个字符
      if (symbol == '{' || symbol == '(' || symbol == '[')  //如果属于左括号则入栈
         openings.push(symbol);
      if (symbol == '}' || symbol == ')' || symbol == ']') { //如果属于右括号则检测是否与栈顶元素匹配
         if (openings.empty()) {   //判断栈是否为空
            cout << "Unmatched closing bracket " << symbol
                 << " detected." << endl;  //栈为空时说明不匹配
            is_matched = false;
         }

         else {   //栈不为空时检测是否与栈顶元素匹配
            char match;
            openings.top(match);
            openings.pop();
            is_matched = (symbol == '}' && match == '{')
                        || (symbol == ')' && match == '(')
                        || (symbol == ']' && match == '[');
            if (!is_matched)  //如果不匹配则整个字符串不匹配
            {
                cout << "Bad match " << match << symbol << endl;
                is_matched = false;
            }
        }
    }
   }
    if(!openings.empty())  //如果栈为空则返回不匹配信息
    {
        cout << "Unmatched opening bracket(s) detected." << endl;
        is_matched = false;
    }
    if(is_matched == true)  //如果匹配则返回“matched”
        cout << "matched!" << endl;
}

2.简单的计算器
思路:用一个栈,解决简单的+、-、*、/问题。将表达式写成后缀表达式,如:3+5= 用后缀表达式表示为:?3?5+=,在程序中’?’表示入栈,‘q’表示结束程序。
程序:
commands.cpp

typedef double Stack_entry;
#include "stack.cpp"

void introduction() //说明程序作用
{
    cout << "Reverse Polish Calculator Program." << "\n----------------------------------" << endl << endl;
}

void instructions() //说明输入规则
{
    cout << "User commands are entered to read in and operate on integers." << endl;
    cout << "The valid commands are as follows:" << endl << "[Q]uit." << endl;
    cout << "[?] to enter an integer onto a stack." << endl << "[=] to print the top integer in the stack." << endl;
    cout << "[+] [-] [*] [/] are arithmetic operations." << endl << "These operations apply to the top pair of stacked integers." << endl;
}

char get_command() //检测输入的字符是否有效
{
   char command;
   bool waiting = true;
   cout << "Select command and press <Enter>:";

   while (waiting) {
      cin >> command;
      command = tolower(command);
      if (command == '?' || command == '=' || command == '+' ||
          command == '-' || command == '*' || command == '/' ||
          command == 'q' ) waiting = false;  //当输入合法时给waiting赋false,跳出循环,执行操作


      else {
         cout << "Please enter a valid command:"   << endl
              << "[?]push to stack   [=]print top" << endl
              << "[+] [-] [*] [/]   are arithmetic operations" << endl
              << "[Q]uit." << endl;  //当输入不合法时,提示重新输入
      }
   }
   return command;
}


bool do_command(char command, Stack &numbers)  //根据输入字符执行相应操作
{
   double p, q;
   switch (command) {
   case '?':  //'?'表示一个元素将入栈
      cout << "Enter a real number: " << flush;  //提示输入一个数据
      cin >> p;
      if (numbers.push(p) == overflow) //检测栈是否已满
         cout << "Warning: Stack full, lost number" << endl;
      break;

   case '=': //'='表示将栈顶元素读出
      if (numbers.top(p) == underflow)
         cout << "Stack empty" << endl;
      else
         cout << p << endl;
      break;

   case '+': //'+'表示取出栈顶两个数据相加
      if (numbers.top(p) == underflow)
         cout << "Stack empty" << endl;
      else {
         numbers.pop();
         if (numbers.top(q) == underflow) {
            cout << "Stack has just one entry" << endl;
            numbers.push(p);
         }  //若栈中只有一个元素则提示只有一个元素,并将读出的返回

         else {
            numbers.pop();
            if (numbers.push(q + p) == overflow)
               cout << "Warning: Stack full, lost result" << endl;
         } //检测是否栈已满
      }
      break;
    case '-': //'-'表示取出栈顶两个数据相减
      if (numbers.top(p) == underflow)
         cout << "Stack empty" << endl;
      else {
         numbers.pop();
         if (numbers.top(q) == underflow) {
            cout << "Stack has just one entry" << endl;
            numbers.push(p);
         }

         else {
            numbers.pop();
            if (numbers.push(q - p) == overflow)
               cout << "Warning: Stack full, lost result" << endl;
         }
      }
      break;
    case '*': //'*'表示取出栈顶两个数据相乘
      if (numbers.top(p) == underflow)
         cout << "Stack empty" << endl;
      else {
         numbers.pop();
         if (numbers.top(q) == underflow) {
            cout << "Stack has just one entry" << endl;
            numbers.push(p);
         }

         else {
            numbers.pop();
            if (numbers.push(q * p) == overflow)
               cout << "Warning: Stack full, lost result" << endl;
         }
      }
      break;
    case '/': //'/'表示取出栈顶两个数据相除
      if (numbers.top(p) == underflow)
         cout << "Stack empty" << endl;
      else {
         numbers.pop();
         if (numbers.top(q) == underflow) {
            cout << "Stack has just one entry" << endl;
            numbers.push(p);
         }

         else {
            numbers.pop();
            if (numbers.push(q / p) == overflow)
               cout << "Warning: Stack full, lost result" << endl;
         }
      }
      break;
    case 'q': //'q'表示结束程序
      cout << "Calculation finished.\n";
      return false;
   }
   return true;
}

主函数main.cpp

#include "commands.cpp"

int main()
{
   Stack stored_numbers;  //定义一个栈存数据
   introduction();  //说明程序作用
   instructions();  //说明输入规则
   while (do_command(get_command(), stored_numbers)); //当do_command(get_command(), stored_numbers)值为真时一直执行
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
LIN协议是一种用于低成本、低速率串行网络的通信协议,主要应用于车辆电子系统中。以下是关于LIN协议应用的一些分享。 首先,LIN协议在车辆电子系统中常用于连接各种低速率从设备,如门控制单元、窗户控制单元、座椅控制单元等。通过LIN总线,这些从设备可以与车辆主控制单元进行通信和控制。相比于其他高速率通信协议,如CAN协议,LIN协议更适用于这些低速率从设备的通信需求。 其次,LIN协议具有较低的成本优势。由于LIN协议使用的是普通的串行通信线路,不需要专用的硬件设备支持,因此整体成本相对较低。这使得LIN协议在汽车电子系统中得到广泛的应用,特别是在非关键性应用中。 此外,LIN协议还具有较低的功耗。由于LIN协议使用的是低速率通信,通信过程中的能耗相对较低。这在需要长时间运行的系统中尤为重要,例如关闭引擎后仍需要继续运行的车辆电子系统。 最后,LIN协议还支持多主从架构。这意味着在LIN总线上可以连接多个主设备和从设备,实现复杂的通信和控制功能。这种灵活性使得LIN协议在车辆电子系统中能够满足不同设备之间的通信需求。 总之,LIN协议在车辆电子系统中应用广泛,并且具有成本低、功耗低和多主从支持等优势。随着车辆电子化的发展,LIN协议在汽车行业中的应用前景将会更加广阔。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

格格不入ち

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值