?? wordany.cpp
字號:
#ifndef __WORDANY_CPP
#define __WORDANY_CPP
#include "WordAny.h"
string ImToken[TOTAL]={"program","const","var","procedure","begin","if","while","call","read","write","end","then","else","do","odd"};
void GetChar(char &ch,int &i,string passage)
{
if(i<passage.length())
ch=passage[i];
else
ch=0;
i++;
}
void GetBC(char &ch,int &i,string passage)
{
while(ch==' ')
GetChar(ch,i,passage);
}
void Concat(string &strTokenFile,char ch)
{
strTokenFile+=ch;
}
bool IsLetter(char ch)
{
if(ch<='z' && ch>='a')
return true;
else if(ch<='Z' && ch>='A')
return true;
else
return false;
}
bool IsDigit(char ch)
{
if(ch<='9' && ch>='0')
return true;
else
return false;
}
int Reserve(const string strTokenFile)
{
for(int i=0;i<14;i++)
if(strTokenFile.compare(ImToken[i])==0)
return i+1;
return 0;
}
void Retract(int &i,char &ch)
{
i--;
ch=' ';
}
void WordAnaly(string passage,int &i,int &j,Word *Token,const int row)
{
string strTokenFile;
char ch;
int code;
strTokenFile="";
GetChar(ch,i,passage);
GetBC(ch,i,passage);
if(IsLetter(ch))
{
while(IsLetter(ch) || IsDigit(ch))
{
Concat(strTokenFile,ch);
GetChar(ch,i,passage);
}
Retract(i,ch);
code=Reserve(strTokenFile);
Token[j].ChanCont(strTokenFile);
Token[j].ChanXY(i,row);
if(code==0)
Token[j].ChanWType($id);
else
Token[j].ChanWType(code);
j++;
}
else if(IsDigit(ch))
{
while(IsDigit(ch))
{
Concat(strTokenFile,ch);
GetChar(ch,i,passage);
}
if(IsLetter(ch))
{
cout<<"錯誤的標識符,第"<<row<<"行"<<endl;
exit(0);
}
Retract(i,ch);
Token[j].ChanCont(strTokenFile);
Token[j].ChanWType($integer);
Token[j].ChanXY(i,row);
j++;
}
else if(ch=='=')
{
Concat(strTokenFile,ch);
Token[j].ChanCont(strTokenFile);
Token[j].ChanWType($lop);
Token[j].ChanXY(i,row);
j++;
}
else if(ch=='+' || ch=='-')
{
Concat(strTokenFile,ch);
Token[j].ChanCont(strTokenFile);
Token[j].ChanWType($aop);
Token[j].ChanXY(i,row);
j++;
}
else if(ch=='*' || ch=='/')
{
Concat(strTokenFile,ch);
Token[j].ChanCont(strTokenFile);
Token[j].ChanWType($mop);
Token[j].ChanXY(i,row);
j++;
}
else if(ch=='<')
{
Concat(strTokenFile,ch);
GetChar(ch,i,passage);
if(ch=='>' || ch=='=')
Concat(strTokenFile,ch);
else
Retract(i,ch);
Token[j].ChanCont(strTokenFile);
Token[j].ChanWType($lop);
Token[j].ChanXY(i,row);
j++;
}
else if(ch=='>')
{
Concat(strTokenFile,ch);
GetChar(ch,i,passage);
if(ch=='=')
Concat(strTokenFile,ch);
else
Retract(i,ch);
Token[j].ChanCont(strTokenFile);
Token[j].ChanWType($lop);
Token[j].ChanXY(i,row);
j++;
}
else if(ch==';')
{
Concat(strTokenFile,ch);
Token[j].ChanCont(strTokenFile);
Token[j].ChanWType($);
Token[j].ChanXY(i,row);
j++;
}
else if(ch==':')
{
Concat(strTokenFile,ch);
GetChar(ch,i,passage);
if(ch=='=')
Concat(strTokenFile,ch);
else
{
cout<<"錯誤的輸入!!"<<":"<<"后應該為"<<"\"=\",第"<<row<<"行"<<endl;
exit(0);
}
Token[j].ChanCont(strTokenFile);
Token[j].ChanWType($equal);
Token[j].ChanXY(i,row);
j++;
}
else if(ch==',')
{
Concat(strTokenFile,ch);
Token[j].ChanCont(strTokenFile);
Token[j].ChanWType($and);
Token[j].ChanXY(i,row);
j++;
}
else if(ch=='(')
{
Concat(strTokenFile,ch);
Token[j].ChanCont(strTokenFile);
Token[j].ChanWType($leftbrack);
Token[j].ChanXY(i,row);
j++;
}
else if(ch==')')
{
Concat(strTokenFile,ch);
Token[j].ChanCont(strTokenFile);
Token[j].ChanWType($rightbrack);
Token[j].ChanXY(i,row);
j++;
}
else if(ch==0)
ch=' ';
else
{
cout<<"Wrong Input!!,第"<<row<<"行"<<endl;
exit(0);
}
}
#endif
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -