📄 fenxiqi.cpp
字号:
#include<stdio.h>
#include<ctype.h>
#include<stdlib.h>
#include<istream.h>
#include<fstream.h>
#include<string.h>
int main(){
printf("Input the file name:");
char name[50],name2[50];
scanf("%s",name);
getchar();
int i=0;
for(i=0;name[i]!='.';i++)
name2[i]=name[i];
name2[i]='\0';
ifstream fpin(name); //读文件数据
strcat(name2,"_scanner_output.txt"); //串连接函数
ofstream fpout(name2);
if(fpin.fail())
exit(0);
if(fpout.fail())
exit(0);
int row=0,row_total[9000]={0},total=0,state=0,b=0,judge=0,judge2=0;
char in,out1,out2,bsf[10]={0},len;
char name1[]="回车",string[100];
while(!fpin.eof()){
in=fpin.get();
if(in==-1){
goto exit;
}
switch(state) {
case 0:
if(isalpha(in) || in=='$' || in=='_'){ //isalpha判断 in是否为英文字母 ,包含在头文件ctype.h中
state=100; //关键字 标识符
fpin.putback(in);
}
else if(isdigit(in)){ //isdigit判断in是否为数字
state=101; //数字
fpin.putback(in);
}
else if(in=='\'')
state=105; //'
else if(in=='\"')
state=106; //"
else if(in=='\\')
state=107; //转义
else if(in=='{'){
state=108; //{
fpin.putback(in);
}
else if(in=='}'){
state=109; //}
fpin.putback(in);
}
else if(in=='('){
state=110; //(
fpin.putback(in);
}
else if(in==')'){
state=111; //)
fpin.putback(in);
}
else if(in=='['){
state=112; //[
fpin.putback(in);
}
else if(in==']'){
state=113; //]
fpin.putback(in);
}
else if(in==';'){
state=114; //;
fpin.putback(in);
}
else if(in=='.'){
state=115; //.
fpin.putback(in);
}
else if(in==','){
state=116; //,
fpin.putback(in);
}
else if(in==' '){
state=117; //空格
fpin.putback(in);
}
else if(in=='\n'){
state=118; //回车
fpin.putback(in);
}
else if(in=='-'){
in=fpin.get();
if(in=='=')
state=102; //-=
else if(in=='-')
state=103; //--
else
state=104; //-
fpin.putback(in);
}
else if(in=='+'){
in=fpin.get();
if(in=='=')
state=119; //+=
else if(in=='+')
state=120; //++
else
state=121; //+
fpin.putback(in);
}
else if(in=='&'){
in=fpin.get();
if(in=='=')
state=122; //&=
else if(in=='&')
state=123; //&&
else
state=124; //&
fpin.putback(in);
}
else if(in=='='){
in=fpin.get();
if(in=='=')
state=125; //==
else
state=126; //=
fpin.putback(in);
}
else if(in=='~'){
state=127;
fpin.putback(in);
}
else if(in=='|'){
in=fpin.get();
if(in=='=')
state=128; //|=
else if(in=='|')
state=129; //||
else {
state=130; //|
}
fpin.putback(in);
}
else if(in=='^'){
in=fpin.get();
if(in=='=')
state=131; //^=
else {
state=132; //^
}
fpin.putback(in);
}
else if(in=='?'){
state=133; //?
fpin.putback(in);
}
else if(in=='/'){
in=fpin.get();
if(in=='=')
state=134; // /=
else if(in=='*')
state=135; // /*
else if(in=='/')
state=136; // //
else {
state=137; // /
}
fpin.putback(in);
}
else if(in=='>'){
in=fpin.get();
if(in=='>'){
in=fpin.get();
if(in=='='){
state=138; //>>=
fpin.putback(in);
}
else if(in=='>'){
in=fpin.get();
if(in=='=')
state=139; // >>>=
else
state=140; // >>>
fpin.putback(in);
}
else {
state=141; //>>
fpin.putback(in);
}
}
else if(in=='='){
state=142; //>=
fpin.putback(in);
}
else {
state=143; //>
fpin.putback(in);
}
}
else if(in=='<'){
in=fpin.get();
if(in=='=')
state=144; // <=
else if(in=='<'){
in=fpin.get();
if(in=='=')
state=145; // <<=
else
state=146; // <<
}
else {
state=147; // <
}
fpin.putback(in);
}
else if(in=='%'){
in=fpin.get();
if(in=='=')
state=148; // %=
else
state=149; //%
fpin.putback(in);
}
else if(in=='*'){
in=fpin.get();
if(in=='=')
state=150; // *=
else
state=151; // *
fpin.putback(in);
}
else if(in=='!'){
in=fpin.get();
if(in=='=')
state=152; // !=
else
state=153; // !
fpin.putback(in);
}
else if(in==':'){
state=154; // :
fpin.putback(in);
}
else if(in=='\t'){
state=155; // :
fpin.putback(in);
}
else{
printf("Error! line=%d,content=%c.\n",row+1,in);
fpout<<in<<' '<<"0x100(错误的单词)"<<endl;
state=0;
}
break;
case 100:
b=0;
while(isalnum(in) || in=='$' || in=='_'){
bsf[b++]=in;
in=fpin.get();
}
fpin.putback(in);
bsf[b]='\0';
b=0;
if(strcmp(bsf,"abstract")==0 || strcmp(bsf,"boolean")==0 ||
strcmp(bsf,"break")==0){//1
fpout<<bsf<<' '<<"0x103(关键字)"<<endl;
state=0;
row_total[row]++;
total++;
}
else if(strcmp(bsf,"break")==0 || strcmp(bsf,"case")==0 ||
strcmp(bsf,"catch")==0){//2
fpout<<bsf<<' '<<"0x103(关键字)"<<endl;
state=0;
row_total[row]++;
total++;
}
else if(strcmp(bsf,"char")==0 || strcmp(bsf,"class")==0 ||
strcmp(bsf,"const")==0){//3
fpout<<bsf<<' '<<"0x103(关键字)"<<endl;
state=0;
row_total[row]++;
total++;
}
else if(strcmp(bsf,"continue")==0 || strcmp(bsf,"default")==0 ||
strcmp(bsf,"do")==0){//4
fpout<<bsf<<' '<<"0x103(关键字)"<<endl;
state=0;
row_total[row]++;
total++;
}
else if(strcmp(bsf,"double")==0 || strcmp(bsf,"else")==0 ||
strcmp(bsf,"extends")==0){//5
fpout<<bsf<<' '<<"0x103(关键字)"<<endl;
state=0;
row_total[row]++;
total++;
}
else if(strcmp(bsf,"final")==0 || strcmp(bsf,"finally")==0 ||
strcmp(bsf,"float")==0){//6
fpout<<bsf<<' '<<"0x103(关键字)"<<endl;
state=0;
row_total[row]++;
total++;
}
else if(strcmp(bsf,"for")==0 || strcmp(bsf,"goto")==0 ||
strcmp(bsf,"if")==0){//7
fpout<<bsf<<' '<<"0x103(关键字)"<<endl;
state=0;
row_total[row]++;
total++;
}
else if(strcmp(bsf,"implements")==0 || strcmp(bsf,"import")==0 ||
strcmp(bsf,"instanceof")==0){//8
fpout<<bsf<<' '<<"0x103(关键字)"<<endl;
state=0;
row_total[row]++;
total++;
}
else if(strcmp(bsf,"int")==0 || strcmp(bsf,"interface")==0 ||
strcmp(bsf,"long")==0){//9
fpout<<bsf<<' '<<"0x103(关键字)"<<endl;
state=0;
row_total[row]++;
total++;
}
else if(strcmp(bsf,"native")==0 || strcmp(bsf,"new")==0 ||
strcmp(bsf,"null")==0){//10
fpout<<bsf<<' '<<"0x103(关键字)"<<endl;
state=0;
row_total[row]++;
total++;
}
else if(strcmp(bsf,"package")==0 || strcmp(bsf,"private")==0 ||
strcmp(bsf,"protected")==0){//11
fpout<<bsf<<' '<<"0x103(关键字)"<<endl;
state=0;
row_total[row]++;
total++;
}
else if(strcmp(bsf,"public")==0 || strcmp(bsf,"return")==0 ||
strcmp(bsf,"short")==0){//12
fpout<<bsf<<' '<<"0x103(关键字)"<<endl;
state=0;
row_total[row]++;
total++;
}
else if(strcmp(bsf,"static")==0 || strcmp(bsf,"super")==0 ||
strcmp(bsf,"switch")==0){//13
fpout<<bsf<<' '<<"0x103(关键字)"<<endl;
state=0;
row_total[row]++;
total++;
}
else if(strcmp(bsf,"synchronized")==0 || strcmp(bsf,"this")==0 ||
strcmp(bsf,"throw")==0){//14
fpout<<bsf<<' '<<"0x103(关键字)"<<endl;
state=0;
row_total[row]++;
total++;
}
else if(strcmp(bsf,"throws")==0 || strcmp(bsf,"transient")==0 ||
strcmp(bsf,"try")==0){//15
fpout<<bsf<<' '<<"0x103(关键字)"<<endl;
state=0;
row_total[row]++;
total++;
}
else if(strcmp(bsf,"void")==0 || strcmp(bsf,"volatile")==0 ||
strcmp(bsf,"while")==0){//16
fpout<<bsf<<' '<<"0x103(关键字)"<<endl;
state=0;
row_total[row]++;
total++;
}
else if(strcmp(bsf,"false")==0 || strcmp(bsf,"true")==0){//17
fpout<<bsf<<' '<<"0x105(布尔型)"<<endl;
state=0;
row_total[row]++;
total++;
}
else{
fpout<<bsf<<' '<<"0x104(标识符)"<<endl;
state=0;
row_total[row]++;
total++;
}
break;
case 101:
if(in=='0'){
b=0;
in=fpin.get();
if(in=='x' || in=='X'){
out1=in;
do{
in=fpin.get();
bsf[b++]=in;
}while(isdigit(in) || (in>=65 && in<=70) || (in>=97 && in<=102));
bsf[b-1]='\0';
b=0;
fpin.putback(in);
fpout<<'0'<<out1<<bsf<<' '<<"0x107(整型)"<<endl;
state=0;
row_total[row]++;
total++;
judge=0;
}
else{
judge=1;
fpin.putback(in);
}
}
else
judge=1;
if(judge){
b=0;
while(isdigit(in)){
bsf[b++]=in;
in=fpin.get();
if(in==-1 || fpin.eof())
break;
}
if(in=='.'){
bsf[b++]=in;
do{
in=fpin.get();
if(in==-1 || fpin.eof())
break;
bsf[b++]=in;
}while(isdigit(in));
if(in=='e' || in=='E'){
goto A;
}
else{
fpin.putback(in);
bsf[b-1]='\0';
b=0;
fpout<<bsf<<' '<<"0x108(浮点型)"<<endl;
state=0;
row_total[row]++;
total++;
}
}
else if(in=='e' || in=='E'){
bsf[b++]=in;
A: in=fpin.get();
if(in=='+' || in=='-'){
bsf[b++]=in;
}
else if(isdigit(in))
fpin.putback(in);
else{
bsf[b]='\0';
b=0;
printf("Error! line=%d,content=%s.\n",row+1,bsf);
fpout<<bsf<<' '<<"0x100(错误的单词)"<<endl;
state=0;
}
if(state!=0){
do{
in=fpin.get();
if(in==-1 || fpin.eof())
break;
bsf[b++]=in;
}while(isdigit(in));
fpin.putback(in);
bsf[b-1]='\0';
b=0;
fpout<<bsf<<' '<<"0x108(浮点型)"<<endl;
state=0;
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -