📄 作业.cpp
字号:
#include <iostream>
#include <cstdio>
#include <fstream>
#include <string>
#include <vector>
#include <cstdlib>
#include <iomanip>
#include <bitset>
using namespace std;
string strToken;
vector<string> *words=new vector<string>;
static const string table[5]={"DIM","IF","DO","STOP","END"};
vector<string> tableID;
vector<string> tableConst;
char ch;
int code,value;
inline bool IsLetter()
{
if(ch>='A'&&ch<='Z'||ch>='a'&&ch<='z')
return true;
else
return false;
}
inline bool IsDigit()
{
if(ch>='0'&&ch<='9')
return true;
else
return false;
}
inline void Concat()
{
strToken.insert(strToken.end(),ch);
}
inline int Reserve()
{
int count=1,i;
for(i=0;i<5;i++,count++)
if(strToken==table[i])
return count;
return 0;
}
inline int InsertId()
{ static int count=0;
tableID.insert(tableID.end(),strToken);
count++;
return count;
}
inline int InsertConst()
{
static int count=0;
tableConst.insert(tableConst.end(),strToken);
count++;
return count;
}
int main(void)
{
ifstream myFile("text1.txt");
if(!myFile){
cerr<<"oops!unable to open file--bailing out!\n";
exit(-1);
}
strToken="";
while(myFile>>ch){
strToken="";
if(IsLetter()){
do{
Concat();
myFile.get(ch);
}while(IsLetter()||IsDigit());
myFile.seekg(-1,ios::cur);
code=Reserve();
if(code==0){
value=InsertId();
cout<<strToken<<":<$ID,"<<strToken<<">"<<endl;
continue ;
}else{
cout<<strToken<<":<$"<<strToken<<",->"<<endl;
continue ;
}
}
if(IsDigit()){
do{
Concat();
myFile.get(ch);
}while(IsDigit());
myFile.seekg(-1,ios::cur);
value=InsertConst();
int temp1=atoi(strToken.c_str());
bitset<32> myset(temp1);
cout<<strToken<<":<$INT,"<<myset<<">"<<endl;
continue ;
}
if(ch=='=') {
cout<<ch<<":<$ASSIGN,->"<<endl;
continue ;
}
if(ch=='+') {
cout<<ch<<":<$PLUS,->"<<endl;
continue ;
}
if(ch=='*'){
myFile.get(ch);
if(ch=='*'){
cout<<ch<<ch<<":<$POWER,->"<<endl;
continue ;
}else{
cout<<'*'<<":<$STAR,->"<<endl;
myFile.seekg(-1,ios::cur);
continue ;
}
}
if(ch==';') {
cout<<ch<<":<$SEMICOLON,->"<<endl;
continue ;
}
if(ch=='(') {
cout<<ch<<":<$LPAR,->"<<endl;
continue ;
}
if(ch==')') {
cout<<ch<<":<$RPAR,->"<<endl;
continue ;
}
if(ch=='{') {
cout<<ch<<":<$LBRACE,->"<<endl;
continue ;
}
if(ch=='}'){
cout<<ch<<":<$RBRACE,->"<<endl;
continue ;
}
else
cout<<ch<<" can't be recognized!"<<endl;
}
system("pause");
return 0;
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -