📄 1.cpp
字号:
#include <iostream>
#include <fstream>
#include <string>
#include <memory>
using namespace std;
struct Result
{
string str;
string ch;
int pos;
}result[9000];
string ID[5000];
string NUM[1000];
int resultNum = 0, IDNum = 0, NUMNum = 0;
int line = 1;
const int KEYNUM = 54;
const int SPECIALNUM = 32;
string keyWord[KEYNUM] = {"asm", "auto", "bool", "break", "case", "catch", "char", "class",
"const", "continue", "default", "delete", "do", "double", "else", "enum", "extern",
"false", "float", "for", "friend", "goto", "if", "inline", "int", "long", "namespace",
"new", "operator", "private", "protected", "public", "register", "return", "short",
"signed", "sizeof", "static", "struct", "switch", "template", "this", "throw", "true",
"try", "typedef", "typename", "union", "unsigned", "using", "virtual", "void", "volatile", "while"};
string specialWord[SPECIALNUM] = { "+", "++", "-", "--", "*", "/", "%", "!", "?", "=","==", ",", "<",
">", "(", ")", "{", "}", "||", "&&", ";","#","<<",">>","_","[","]" , "\"",".","\'","\:","\\"};
ifstream data("data.txt");
ofstream keylist("keyword.txt");
ofstream speciallist("special.txt");
ofstream idlist("identifier.txt");
ofstream numlist("constnumber.txt");
ofstream resultlist("result.txt");
ofstream errer("errer.txt");
int posInKeyWord(const string& str)
{
for(int i = 0; i < KEYNUM; i++)
{
if( keyWord[i] == str )
return i;
}
return -1;
}
int posInID(const string& str)
{
for(int i = 0; i < IDNum; i++)
{
if( ID[i] == str)
return i;
}
return -1;
}
int posInNUM(const string& str)
{
for(int i =0; i < NUMNum; i++)
{
if(NUM[i] == str)
return i;
}
return -1;
}
int posInSpecialWord(const string& str)
{
for(int i = 0; i < SPECIALNUM; i++)
{
if(specialWord[i] == str)
return i;
}
return -1;
}
void addResult(const string& str, const string& ch, const int& pos)
{
result[resultNum].str = str;
result[resultNum].ch = ch;
result[resultNum].pos = pos;
resultNum++;
if(resultNum>9000)
{
cout<<"You should expand the resultzone."<<endl;
}
}
void identifier(const string& str)
{
if(posInKeyWord(str) != -1)
{
addResult(str, "Keyword", posInKeyWord(str) + 1);
}
else if(posInID(str) == -1)
{
ID[IDNum++] = str;
addResult(str, "Identifier", IDNum);
}
else addResult(str, "Identifier", IDNum);
}
void number(const string& str)
{
if(posInNUM(str) == -1)
NUM[NUMNum++] = str;
addResult(str, "Constnumber", NUMNum);
}
void special(const string& str)
{
addResult(str, "Special", posInSpecialWord(str) + 1);
}
void stringToNumber(ofstream& outfile, const string& str)
{
int dotpos = str.find_first_of('.'), intNum = 0, len = str.length(), i = 0;
float floatNum = 0.0, numAfterDot = 0.0;
if( dotpos == -1)
{
while(i != len)
{
intNum = 10 * intNum + (str[i] - '0');
i++;
}
outfile << intNum << endl;
}
else
{
while(i != dotpos)
{
floatNum = 10 * floatNum + (str[i] - '0');
i++;
}
i = len - 1;
while(i != dotpos)
{
numAfterDot = (numAfterDot + str[i] - '0') / 10;
i--;
}
floatNum += numAfterDot;
outfile << floatNum ;
}
}
void print()
{
int i;
for(i = 0; i < KEYNUM; i++)
{
keylist << i+1 << "---" << keyWord[i] << endl;
}
for(i = 0; i < SPECIALNUM; i++)
{
speciallist << i + 1 << "---" << specialWord[i] << endl;
}
for(i = 0; i < IDNum; i++)
{
idlist << i +1 << "---" << ID[i] << endl;
}
for(i = 0; i < NUMNum; i++)
{
numlist << i + 1 << "---" ;
stringToNumber(numlist, NUM[i]);
numlist << endl;
}
for(i = 0; i < resultNum; i++)
{
if((result[i].str[ 0 ] - '0') * (result[i].str[ 0 ] - '9') <= 0)
{
stringToNumber(resultlist, result[i].str);
resultlist << " --> (" << result[i].ch << ", " << result[i].pos << ")" << endl;
}
else resultlist << result[i].str << " --> (" << result[i].ch << ", " << result[i].pos << ")" << endl;
}
}
int main()
{
char str[20], ch;
int i = 0;
streampos inpos = data.tellg();
while(!data.eof())
{
i = 0;
memset(str, '\0', sizeof(str));
data.get(ch);
inpos = data.tellg();
if(ch == ' ' || ch =='\n' || ch == '\t')
{
while(ch == ' ' || ch == '\n' || ch == '\t')
{
if(ch == '\n')
line++;
data.get(ch);
if(data.eof())
break;
inpos = data.tellg();
}
if(data.eof())
break;
data.seekg(-1, ios::cur);
}
else if( (ch - 'a') * (ch - 'z') <= 0 || (ch - 'A') *(ch - 'Z') <= 0 )
{
while((ch - 'a') * (ch - 'z') <= 0 || (ch - 'A') *(ch - 'Z') <= 0 || (ch - '0') * (ch - '9') <= 0)
{
str[i++] = ch;
data.get(ch);
inpos = data.tellg();
}
identifier(str);
data.seekg(-1,ios::cur);
inpos = data.tellg();
}
else if((ch - '0') * (ch - '9') <= 0)
{
while((ch - '0') * (ch - '9') <= 0 || ch == '.')
{
str[i++] = ch;
data.get(ch);
if(data.eof())
break;
inpos = data.tellg();
}
number(str);
if(data.eof())
break;
data.seekg(-1, ios::cur);
inpos = data.tellg();
}
else if(ch == '+')
{
str[i++] = ch;
data.get(ch);
if(ch == '+')
{
str[i] = ch;
special(str);
}
else{
data.seekg(-1, ios::cur);
special(str);
}
}
else if(ch == '-')
{
str[i++] = ch;
data.get(ch);
if(ch == '-')
{
str[i] = ch;
special(str);
}
else{
data.seekg(-1, ios::cur);
special(str);
}
}
else if(ch == '?')
{
str[i++] = ch;
special(str);
}
else if(ch == '|')
{
str[i++] = ch;
special(str);
}
else if(ch == '&')
{
str[i++] = ch;
special(str);
}
else if(ch == '=')
{
str[i++] = ch;
data.get(ch);
if(ch == '=')
{
str[i] = ch;
special(str);
}
else
{
special(str);
data.seekg( -1, ios::cur);
}
}
else if(ch == '#')
{
str[i++] = ch;
special(str);
}
else if(ch == '_')
{
str[i++] = ch;
special(str);
}
else if(ch == '[')
{
str[i++] = ch;
special(str);
}
else if(ch == ']')
{
str[i++] = ch;
special(str);
}
else if(ch=='\"')
{
str[i++] = ch;
special(str);
}
else if(ch=='.')
{
str[i++] = ch;
special(str);
}
else if(ch=='\'')
{
str[i++] = ch;
special(str);
}
else if(ch=='\:')
{
str[i++] = ch;
special(str);
}
else if(ch=='\\')
{
str[i++] = ch;
special(str);
}
else if(ch == '>')
{
str[i++] = ch;
data.get(ch);
if(ch == '>')
{
str[i] = ch;
special(str);
}
else
{
special(str);
data.seekg(-1, ios::cur);
}
}
else if(ch == '<')
{
str[i++] = ch;
data.get(ch);
if(ch == '<')
{
str[i] = ch;
special(str);
}
else {
special(str);
data.seekg(-1, ios::cur);
}
}
else if (ch == '*' || ch == '/' || ch == '%' || ch == '!' || ch == ',' || ch == '(' || ch == ')' || ch == '{' || ch == '}' || ch == ';')
{
str[0] = ch;
special(str);
}
else
{
errer << "error in line " << line << " : there is something we can't deal with :" <<ch<< endl;
}
}
print();
return 0;
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -