(精选)括号匹配C++程序

上传人:日度 文档编号:148267071 上传时间:2020-10-17 格式:DOC 页数:5 大小:23.50KB
返回 下载 相关 举报
(精选)括号匹配C++程序_第1页
第1页 / 共5页
(精选)括号匹配C++程序_第2页
第2页 / 共5页
(精选)括号匹配C++程序_第3页
第3页 / 共5页
(精选)括号匹配C++程序_第4页
第4页 / 共5页
(精选)括号匹配C++程序_第5页
第5页 / 共5页
亲,该文档总共5页,全部预览完了,如果喜欢就下载吧!
资源描述

《(精选)括号匹配C++程序》由会员分享,可在线阅读,更多相关《(精选)括号匹配C++程序(5页珍藏版)》请在金锄头文库上搜索。

1、#include #include #include #include using namespace std;/CBalance类/此类对于一般的C+或者C文件进行括号匹配检查/文件可以有注释,字符串/字符串常量不可以跨行/my.h定义了常用的类库/class CBalanceprivate:ifstream fin; /文件流int nCurrentLine; /正在处理的行号int nErrors; /已发现的错误数struct CSymbol /此类记录遇到的字符和字符的行号char cToken;int nTheLine;enum CommentType SlashSlash , S

2、lashStar;/记录/和/*两中注释方式/CheckBalance()函数的工具类/CheckMatch函数用于比较两个符号是否匹配bool CheckMatch(char char1, char char2, int nLine1, int nLine2);/GetNextSymbol函数用于返回读到的括号char GetNextSymbol(void);/PutBackChar用于把字符重新返还给文件流void PutBackChar(char char1);/SkipComment根据注释符的不同跳过源文件的注释void SkipComment(enum CommentType ty

3、pe);/SkipQuote函数用于跳过源文件中的字符串和字符常量void SkipQuote(char type);/NextChar函数用于得到下一个字符char NextChar(void);public:CBalance(const char* filename); /构造函数int CheckBalance(); /检查fin中的字符是否匹配;class noFile; /定义的异常类,当函数不存在的时候抛出异常CBalance:CBalance(const char* filename)fin.open(filename);if(!fin) throw noFile();nCurr

4、entLine = 1;nErrors = 0;int CBalance:CheckBalance()struct CSymbol node; /符号与行号stack st; /定义的符号栈char LastChar,Match; /LastChar为读入的符号,Match为栈顶的字符while(LastChar = GetNextSymbol()/从文件中读取括号字符,直到文件结束/while_beiginswitch (LastChar) /switch_beigincase (:case :case : /遇到这三种符号要进栈node.cToken = LastChar;node.nTh

5、eLine = nCurrentLine;st.push(node);break;case ):case :case :/遇到这三种括号要进行比较if(st.isEmpty()nErrors+;cout在第nCurrentLine有一个多余LastCharendl;elsenode = st.pop();Match = node.cToken;if(!CheckMatch(Match,LastChar,node.nTheLine,nCurrentLine)nErrors+;break;/switch_end/while_endwhile(!st.isEmpty() /栈中多余的符号nError

6、s+;node = st.pop();cout第node.nTheLine行的符号node.cToken不匹配endl; return nErrors;bool CBalance:CheckMatch(char char1, char char2, int nLine1, int nLine2)if(char1=(&char2!=)|char1=&char2!=|char1=&char2!=)cout发现第nLine2的符号char2与第nLine1的符号char1不匹配endl;return false;return true;char CBalance:GetNextSymbol() ch

7、ar ch;while(ch=NextChar()if(ch=/)ch=NextChar();if(ch=/)SkipComment(SlashSlash);else if(ch=*)SkipComment(SlashStar);else PutBackChar(ch);else if (ch=|ch=)SkipQuote(ch);else if (ch=|ch=|ch=(|ch=|ch=|ch=)return ch;return NULL;char CBalance:NextChar()char ch;if(ch=fin.get()=EOF)return NULL;if(ch=n)nCurr

8、entLine+;return ch;void CBalance:PutBackChar(char char1)fin.putback(char1);if(char1=n) nCurrentLine-;void CBalance:SkipQuote(char type)char ch;while(ch=NextChar()if(ch=type) return;else if (ch=n)nErrors+;cout在第nCurrentLine行缺少匹配的或者endl;else if (ch=)ch = NextChar();void CBalance:SkipComment(enum CommentType type) char ch,flag;if(type=SlashSlash) /对于像/注释一直读到行末尾while (ch=NextChar()&(ch!=n)return;flag=0; /对于像/*这样的注释要不断的读文件while(ch=NextChar()!=NULL)if(flag=*&ch=/)return;flag = ch; int main() try CBalance b(123.txt); /文件要在工程当前目录下 b.CheckBalance(); catch (noFile& file) cout 文件打开失败!endl; return 0;

展开阅读全文
相关资源
相关搜索

当前位置:首页 > 办公文档 > 教学/培训

电脑版 |金锄头文库版权所有
经营许可证:蜀ICP备13022795号 | 川公网安备 51140202000112号