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;
}
- 9月30
- 9月-月总结
- 7月-9月
- 会议记录(9月2日-9月30日)
- 2007年10月1日(9月29日,9月30日没写)
- 9月
- 9月
- 08-8月、9月
- 2004年9月30日星期三
- 9月11日 0:30 流水账
- 2006年9月30日星期六
- 9月30日skype事件
- 2010年9月30日
- 2011年9月30日
- 2013年9月30日~杂记
- 2014年9月30日
- 2014年9月30日
- 2014年9月30日
- 纯js实现网页返回顶部功能(万能的兼容目前所有浏览器)
- 9月熊猫总结
- 快速克隆网站(Teleport Ultra)
- Maven配置之settings.xml
- thinkphp模块分组后,cli模式无法使用的调整方法
- 9月30
- 20140930 【 数论 -- 本福特定律 】 2014-上海区域赛-网络预选赛 hdoj 5051 Fraction
- zoj 1245 - Triangles
- 小白的内存管理(MRC)学习记录
- C++ char*,const char*,string的相互转换
- Android UI 属性 android:layout_below android:layout_below 心碎了一个下午
- JAVA项目名称前 红色叹号如何解决?
- Python学习笔记——字符串
- Android中的GraphicBuffer同步机制-Fence