9月30

来源:互联网 发布:网络信息安全宣传周 编辑:程序博客网 时间:2024/05/20 17:08

char_stack.h

 

/* File: char_stack.h      

  Header file for a very basic array-based implementation of a stack.

*/
#ifndef _CHAR_STACK_H_
#define _CHAR_STACK_H_

const int nDefaultSize = 1024;
const int nCapacitySize = 102400;
const char cErrorChar = 0;

class char_stack
{
  public:
    char_stack();
    void push( char item );
    char pop();
    char top();
    bool empty();
    int size();

  ~char_stack();

  private:
 
    int extend(); 

  private:
    char *m_array;
    int m_nCapacitySize;
    int m_nMaxSize;
    int m_nCurSize;
};

#endif

 

char_stack.cpp

 

#include "char_stack.h"
#include <string.h>

char_stack::char_stack()
{
    m_nCapacitySize = nCapacitySize;
    m_array = new char[nDefaultSize];
    m_nMaxSize = nDefaultSize;
    m_nCurSize = 0;
}

char_stack::~char_stack()
{
    if (m_array != NULL)
    {
        delete []m_array;
    }
}

void char_stack::push( char item )
{
    if (m_nCurSize >= m_nMaxSize-1)
    {
        if (extend() < 0)
        {
           return;
        }
    }
    m_array[m_nCurSize++] = item;
}

char char_stack::pop()
{
    if (empty())
    {
        return cErrorChar;
    }
    return m_array[--m_nCurSize];
}

char char_stack::top()
{
    if (empty())
    {
        return cErrorChar;
    }
    return m_array[m_nCurSize-1];
}

bool char_stack::empty()
{
    return 0==m_nCurSize;
}

int char_stack::size()
{
    return m_nCurSize;
}

int char_stack::extend()
{
    if (m_nCurSize < m_nMaxSize-1)
        return 0;
    if (m_nCurSize >= m_nCapacitySize)
        return -1;
       
    int nNewMaxSize = (m_nMaxSize==0 ? 1024:m_nMaxSize<<1);
    char *cNewArray = new char[nNewMaxSize];
    memset(cNewArray, 0, sizeof(cNewArray));
    memcpy(cNewArray, m_array, m_nMaxSize);

    if (NULL != m_array)
        delete []m_array;
       
    m_array = cNewArray;
    m_nMaxSize = nNewMaxSize;
    return 0;
}

 

 

gscheck.cpp

 

#include "char_stack.h"
#include <iostream>
#include <string>
#include <string.h>
#include <stdio.h>

using namespace std;

void printErrorLog(int nLine, string strError)
{
    cout << "Error on line " << nLine << ": ";
    cout << strError << endl;
}
void printErrorLine(char* buf, int nLen, int nIndex)
{
    string str;
    str.append(buf, nIndex);
    str.append(1, '\n');
    str.append(buf+nIndex, nLen-nIndex);
    cout << str << endl;
}

char getMatchChar(char c)
{
    if (c == '{')
    {  
        return '}';
    }
    else if (c == '[')
    {
        return ']';
    }
    else if (c == '(')
    {

        return ')';
    }
    return 0;
}

void test()
{
    char_stack cstack;
    printf("size=%d\n", cstack.size());
    cstack.push('a');
    cstack.push('b');
    cstack.push('c');
    printf("size=%d\n", cstack.size());
    printf("c = %c\n", cstack.pop());
    printf("c = %c\n", cstack.pop());
    printf("c = %c\n", cstack.pop());
}

int main()
{
    //test();

    char_stack cstack;
    int nLine = 0;
    char buf[1024];
    int nErrorFlag = false;
    while (cin.getline(buf, 1024))
    {
        nLine++;
        int nLength = cin.gcount();
       
       //cout << "getLine:" << buf << endl;
       //cout << "nLen:" << nLength << endl;

        for (int i=0; i<nLength; ++i)
        {
            if (buf[i] == '(' || buf[i] == '{' || buf[i] == '[')
            {
                cstack.push(buf[i]);
                //cout << "push" << buf[i] << endl;
            }
            else if (buf[i] == ')' || buf[i] == '}' || buf[i] == ']')
            {
                if (cstack.empty())
                {
                    string strError = "Too many ";
                    strError.append(1, buf[i]);
                    printErrorLog(nLine, strError);
                    printErrorLine(buf, nLength, i);
                    nErrorFlag = true;
                    break;
                }
                char cPut = cstack.pop();
                char cMatch = getMatchChar(cPut);
               // cout << "pop" << cPut << "read:" << buf[i] << endl;
                if (buf[i] != cMatch)
                {
                    string strError = "Read ";
                    strError.append(1, buf[i]);
                    strError += " expected ";
                    strError.append(1, cMatch);
                    printErrorLog(nLine, strError);
                    printErrorLine(buf, nLength, i);
                    nErrorFlag = true;
                    break;
                   
                }
            }
        }


        memset(buf, 0, sizeof(buf));
        char c = cin.peek();
        if (c == EOF || nErrorFlag)
        {
            break;
        }
    }


    if (!cstack.empty())
    {
        char cPut = cstack.pop();
        string strError = "Too many ";
        strError.append(1, cPut);
        printErrorLog(nLine, strError);
    }
    else if (!nErrorFlag)
    {
        cout << "No Errors Found"<< endl;
    }
   
    return 0;
}

 

 

 

0 0