运用链表建立堆栈 完成对C语言源代码的配对检查

//请编写程序检查C语言原程序中下列符号是否匹配:/* */ ()[] {}
//输入为C语言源程序
#include <iostream>
#include<fstream>
#include<string>
using namespace std;
typedef struct Node *PtrToNode;
struct Node {
    string c;
    PtrToNode next;
};
class stack {
    PtrToNode top;
public:
    stack();
    void push(char c);
    void pop();
    void showstack();
    bool ISNULL();
    void empty();
    string showtop();
    string showtwotop();
};
stack::stack()
{
    //top = (PtrToNode)malloc(sizeof(struct Node));
    PtrToNode top = new Node;
    top->next = NULL;
    top->c = "top";
}
void stack::push(char c)
{
    PtrToNode p1=new Node;
    //p1 = (PtrToNode)malloc(sizeof(struct Node));
    string s = "a";
    s[0] = c;
    p1->c = s;
    p1->next = top;
    top = p1;
}
void stack::pop()
{
    if (top->next == NULL)
        return;
    else {
        PtrToNode temp;
        temp = top;
        top = top->next;
        delete  temp;
    }
}
string stack::showtop()
{
    return top->c;
}
string stack::showtwotop()
{
    return  top->c + top->next->c;
}
void stack::showstack()
{
    PtrToNode temp;
    temp = top;
    while (temp)
    {
        cout << temp->c<<endl;
        temp = temp->next;
    }
}
bool stack::ISNULL()
{
    if (top->next == NULL)
        return 1;//1为空
    else    
        return 0;
}
void stack::empty()
{
    PtrToNode temp;
    if (top->next != NULL)
    {
        while (top != NULL)
        {
            temp = top;
            top = top->next;
            delete temp;
        }
    }
}
void main()
{
    ifstream file;
    string str,temp;
    file.open("D:\\大三课程\\大三下\\算法与数据结构\\DS第三章6\\示例.txt",ios::out);//文件长度不超过100字符
    getline(file, str,'#');
    cout << str;
    file.close();
    stack examine;
    int len = str.length();
    for (int i = 0; i < len; i++)
    {
        if (str[i] == '(' ||  str[i] == '['  || str[i] == '{'){
            examine.push(str[i]);
        }
        if (str[i] == ')' ) {
            if (examine.showtop() == "(")
                examine.pop();
            else
                examine.push(str[i]);
        }
        if (str[i] == ']')
        {
            if (examine.showtop() == "[")
                examine.pop();
            else
                examine.push(str[i]);
        }
        if (str[i] == '}')
        {
            if (examine.showtop() == "{")
                examine.pop();
            else
                examine.push(str[i]);
        }
        if (str[i] == '/'&&str[i + 1] == '*')
        {
            examine.push(str[i]);
            examine.push(str[i + 1]);
        }
        if (str[i] == '*'&&str[i + 1] == '/')
        {
            if (examine.showtwotop()=="*/")
            {
                examine.pop();
                examine.pop();
            }
            else
            {
                examine.push(str[i]);
                examine.push(str[i + 1]);
            }
        }
    }
    if (examine.ISNULL())
    {
        cout << "已经完美匹配" << endl;
    }
    else
    {
        cout << "剩余未匹配的字符为" << endl;
        examine.showstack();
        examine.empty();
    }
    system("pause");
}

1.注意在分配内存的时候采用new形式  采用malloc形式报错了

2.删除的时候采用delete [] 形式

3.做这道题的顺序表版本的时候出现指针作为函数返回值,但是在主程序中不显示出来的问题

 

posted on 2018-04-01 19:16  骑毛驴的关关  阅读(317)  评论(0编辑  收藏  举报

导航