课后习题 3-13 检测程序中的括号匹配

Stack.h

#pragma once
#include<iostream>
using namespace std;

class SNode {
public:
    char data;
    SNode* next;
};

class Stack {
public:
    SNode* top;
    
    Stack() {
        top = nullptr;
    }

    void push(char elem) {
        SNode* s;
        s = new SNode();
        s->data = elem;
        s->next = top;
        top = s;
    }

    bool pop(char& elem) {
        bool res = true;
        if (top != nullptr) {
            elem = top->data;
            SNode* p = top;
            top = top->next;
            delete p;
        }
        else {
            res = false;
        }
        return res;
    }

};

MyTool.h

#pragma once
#include"Stack.h"
#include<fstream>

class MyTool {
public:
    static int match(char* fname) {
        Stack s;
        char ch;
        char temp;
        ifstream ifstr(fname,ios::out|ios::in);
        if (!ifstr) {
            cerr << "FILE_ERROR" << endl;
            exit(1);
        }
        while(ifstr>>ch){
            if (ch == '\'') {
                while (ifstr >> ch) {
                    if (ch == '\'') {
                        break;
                    }
                }
                if (!ifstr) {
                    return 0;
                }
             }
            if (ch == '\"') {
                while (ifstr >> ch) {
                    if (ch == '\"') {
                        break;
                    }
                }
                if (!ifstr) {
                    return 0;
                }
            }

            switch (ch) {
            case '(':
            case '[':
            case '{':
                s.push(ch);
                break;
            case ')':
                if (s.top == nullptr) {
                    return 0;
                }
                else {
                    if (s.top->data == '(') {
                        s.pop(temp);
                    }
                    else {
                        return 0;
                    }
                }
                break;
            case ']':
                if (s.top == nullptr) {
                    return 0;
                }
                else {
                    if (s.top->data == '[') {
                        s.pop(temp);
                    }
                    else {
                        return 0;
                    }
                }
                break;
            case '}':
                if (s.top == nullptr) {
                    return 0;
                }
                else {
                    if (s.top->data == '{') {
                        s.pop(temp);
                    }
                    else {
                        return 0;
                    }
                }
                break;

        }

        }
        if (s.top == nullptr) {
            return 1;
        }
        else {
            return 0;
        }
    }
};

main.cpp

#include"MyTool.h"

int main() {
    char fname[50] = { '\0' };
    int res;
    cout << "Input file name:" << endl;
    gets_s(fname);
    res = MyTool::match(fname);
    cout << res << endl;
    return 0;
}

猜你喜欢

转载自www.cnblogs.com/SlowIsFast/p/12651907.html
今日推荐