符号配对

符号配对


一、目的

-掌握栈的使用
-掌握getline的用法


二、实验内容与设计思想

符号配对

假设表达式中允许包含3种括号:圆括号、方括号和大括号。即(,[,'{'。编写一个算法判断表达式中的括号是否正确配对,
要求利用栈的结构实现。

输入要求

输入一行带上述括号字符串

输出要求

若匹配,输出yes。若不匹配,输出当前栈顶元素再换行输出no,并。若栈顶为空,则输出no。

输入样例1:
([1+2])
输出样例1:
yes
输入样例2:
([
输出样例:
[
no


题目分析:

代码重点在于判断条件的重复


函数相关伪代码

1.getline(cin, s);
2.for (char c : s) {
|if (c == '(' || c == '[' || c == '{') 
|| s1.push(c);
| else if (c == ')') {
||if (s1.empty() || s1.top() != '(') {
|||if (!s1.empty()) {
||||cout << s1.top() << endl;
|||end
|||cout << "no" << endl;
||| return 0;
||end
||else 
|||s1.pop();
| else if (c == ']') {
|| if (s1.empty() || s1.top() != '[') {
|||if (!s1.empty()) {
||||cout << s1.top() << endl;
|||end
|||cout << "no" << endl;
|||return 0;
||end
||else 
|||s1.pop();
|else if (c == '}') {
||if (s1.empty() || s1.top() != '{') {
||| if (!s1.empty()) {
||||cout << s1.top() << endl;
|||end
|||cout << "no" << endl;
|||return 0;
||end
||else 
||| s1.pop();
end
 if (s1.empty()) 
|cout << "yes" << endl;
else 
|cout << s1.top() << endl;
|cout << "no" << endl;


函数代码

#include <iostream>
#include <stack>
#include <string>
using namespace std;

int main() {
    string s;
    getline(cin, s);
    stack<char> s1;
    for (char c : s) {
        if (c == '(' || c == '[' || c == '{') {
            s1.push(c);
        }
        else if (c == ')') {
            if (s1.empty() || s1.top() != '(') {
                if (!s1.empty()) {
                    cout << s1.top() << endl;
                }
                cout << "no" << endl;
                return 0;
            }
            else {
                s1.pop();
            }
        }
        else if (c == ']') {
            if (s1.empty() || s1.top() != '[') {
                if (!s1.empty()) {
                    cout << s1.top() << endl;
                }
                cout << "no" << endl;
                return 0;
            }
            else {
                s1.pop();
            }
        }
        else if (c == '}') {
            if (s1.empty() || s1.top() != '{') {
                if (!s1.empty()) {
                    cout << s1.top() << endl;
                }
                cout << "no" << endl;
                return 0;
            }
            else {
                s1.pop();
            }
        }
    }
    if (s1.empty()) {
        cout << "yes" << endl;
    }
    else {
        cout << s1.top() << endl;
        cout << "no" << endl;
    }
    return 0;
}

三、实验使用环境

以下请根据实际情况编写

  • 操作系统:Windows 11专业版
  • 编程语言:C++
  • 开发工具:[Visual Stdio 2022]

四、实验步骤和调试过程

符号配对

本机运行截图

运行分析

输入第一行字符串,对其进行判断是否正确配对
最后输出要按照格式要求,如果不配对,输出当前栈顶,即最后检查的不配对字符,还要换行输出no,配对则直接输出yes

五、实验小结

遇到的问题及解决方法:

  1. 问题:判断逻辑有问题
  • 解决方法:修改代码

实验体会和收获:

以上代码是一个括号匹配的检查程序,用于判断输入的字符串中的括号是否匹配。
程序首先要读取一行输入(用getline,也可以是cin),循环字符串(可以采取for(i=0;i<s.length();i++)的方式来比较,也可以直接for(char c:s)s按顺序一个个赋值),然后要使用一个栈stacks1(我们输入的之中有数字和字符,但栈是来存括号,要定义类型为char)来存储遇到的左括号('(‘、’{‘、'['),当遇到右括号时,程序会检查栈顶的左括号是否与之匹配。如果匹配,则弹出栈顶的左括号;如果不匹配,则输出错误信息并结束程序(因为代码里面夹杂着数字,我需要去准确判断c的数值,所以我要写三个类似的判断条件来进行(只是进入的条件不同,其它差不多),比较繁琐,但思路简单)。最后,如果栈为空,说明所有括号都匹配,输出“yes”;否则输出栈顶的左括号和“no”。


posted @ 2025-05-17 22:48  穗和  阅读(20)  评论(0)    收藏  举报