?? glossary.cpp
字號(hào):
/******************************************************************************
* glossary.cpp
*
* 2008-03-28 20:41
* 周鑫(zhouxin63766@yahoo.com.cn)
*
******************************************************************************/
#include <QObject>
#include <QFile>
#include <QMessageBox>
#include <cctype>
#include "glossary.h"
Glossary::Glossary()
{
_lineNo = 1;
fileEnd = false;
sourceFile = NULL;
}
Glossary::~Glossary()
{
if ( sourceFile != NULL )
{
sourceFile->close();
delete sourceFile;
sourceFile = NULL;
}
}
bool Glossary::openFile( const QString &fileName )
{
if ( sourceFile != NULL )
{
sourceFile->close();
delete sourceFile;
sourceFile = NULL;
}
sourceFile = new QFile( fileName );
if ( sourceFile == NULL || !(sourceFile->open( QIODevice::ReadOnly )) )
{
return false;
}
_lineNo = 1;
fileEnd = false;
tokenBuffer.clear();
return true;
}
Token Glossary::judgeValidToken( QString &lexeme )
{
int i;
lexeme = lexeme.toUpper();
for ( i = 0; i < TABLENGTH; i++ )
{
if ( lexeme == TokenTab[i].lexeme )
{
return TokenTab[i];
}
}
Token token;
token.type = ERRTOKEN;
token.lexeme = lexeme;
token.value = 0.0;
token.pFunction = NULL;
return token;
}
Token Glossary::getToken()
{
Token token;
char ch;
tokenBuffer.clear();
token.lexeme = tokenBuffer;
while ( 1 )
{
if ( sourceFile->atEnd() || fileEnd )
{
token.type = NONTOKEN;
fileEnd = true;
return token;
}
sourceFile->getChar( &ch );
if ( ch == '\n' )
{
_lineNo++;
}
// space 包括 空格、TAB和換行
else if ( !isspace(ch) )
{
break;
}
}
tokenBuffer.append( ch );
// 保留字或參數(shù)
if ( isalpha(ch) )
{
while ( 1 )
{
if ( sourceFile->atEnd() )
{
fileEnd = true;
break;
}
else
{
sourceFile->getChar( &ch );
if ( isalnum(ch) )
{
tokenBuffer.append( ch );
}
else
{
break;
}
}
}
sourceFile->ungetChar( ch );
token = judgeValidToken( tokenBuffer );
token.lexeme = tokenBuffer;
return token;
}
// 數(shù)字
else if( isdigit(ch) )
{
while ( 1 )
{
if ( sourceFile->atEnd() )
{
fileEnd = true;
break;
}
sourceFile->getChar( &ch );
if ( isdigit(ch) )
{
tokenBuffer.append( ch );
}
else
{
break;
}
}
if ( ch == '.' )
{
tokenBuffer.append( ch );
while ( 1 )
{
if ( sourceFile->atEnd() )
{
fileEnd = true;
break;
}
sourceFile->getChar( &ch );
if ( isdigit(ch) )
{
tokenBuffer.append( ch );
}
else
{
break;
}
}
}
sourceFile->ungetChar( ch );
token.type = CONSTANT;
token.value = tokenBuffer.toDouble();
return token;
}
// 其它符號(hào)
else
{
switch ( ch )
{
case ';':
token.type = SEMICO;
break;
case '(':
token.type = LBRACKET;
break;
case ')':
token.type = RBRACKET;
break;
case ',':
token.type = COMMA;
break;
case '+':
token.type = PLUS;
break;
case '*':
token.type = MUL;
break;
case '^':
token.type = POWER;
break;
case '-':
if ( sourceFile->atEnd() )
{
fileEnd = true;
break;
}
sourceFile->getChar( &ch );
if ( ch == '-' )
{
while ( ch != '\n' && !sourceFile->atEnd() )
{
sourceFile->getChar( &ch );
}
sourceFile->ungetChar( ch );
return getToken();
}
else
{
sourceFile->ungetChar( ch );
token.type = MINUS;
break;
}
case '/':
if ( sourceFile->atEnd() )
{
fileEnd = true;
break;
}
sourceFile->getChar( &ch );
if( ch == '/' )
{
while(ch != '\n'&& !sourceFile->atEnd() )
{
sourceFile->getChar( &ch );
}
sourceFile->ungetChar( ch );
return getToken();
}
else
{
sourceFile->ungetChar( ch );
token.type = DIV;
break;
}
default:
token.type = ERRTOKEN;
break;
}
}
return token;
}
?? 快捷鍵說明
復(fù)制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號(hào)
Ctrl + =
減小字號(hào)
Ctrl + -