class1.h
资源名称:lp-scaner.rar [点击查看]
上传用户:jiashang
上传日期:2013-07-26
资源大小:12k
文件大小:10k
源码类别:
文本生成
开发平台:
Visual C++
- #include <stdlib.h>
- #include <stdio.h>
- #include <iostream.h>
- #include <string.h>
- #include <fstream.h>
- int flag=0; //标志位
- char cch;
- char bchar;
- char str[100];
- FILE *fp;
- void Pushback(char c)
- {
- bchar=c;
- flag=1;
- }
- /////////////////////////////////////////////////////////////////
- char GetC()
- {
- if(flag==1)
- {
- flag=0;
- return bchar;
- }
- char t;
- t=getc(fp);//继续取出字符
- if(t=='n'||t=='t'||t=='r') t=' ';
- return t;
- }
- /////////////////////////////////////////////////////////////////////////
- int SearchKey() //搜索关键字
- {
- char StrToken[32][10]={"auto","break","case","char","const",
- "continue","default","do","double","else","enum","extern",
- "float","for","goto","if","int","long","register","return",
- "short","signed","sizeof","static","struct","switch",
- "typedef","union","unsigned","void","volatile","while"};
- for(int i=0;i<32;i++)
- {
- if(strcmp(StrToken[i],str)==0) //比较关键字
- return i;
- }
- return 0;
- }
- ///////////////////////////////////////////////////////////////////////////////////
- bool OpenFile(char fname[20])//读取文件
- {
- cout<<"输入文件位置及名称(如: e:\a.c):"<<endl;
- cin>>fname;
- if((fp=fopen(fname,"r"))==NULL)
- {
- cout<<"文件路径或者文件名不正确导致了文件打开错误!!!"<<endl;
- return false;
- }
- else return true;
- }//OpenFile*/
- /*void ShowFile() //显示源文件
- {
- char charfile;
- char fname[20];
- if( OpenFile(fname) ==true )
- {
- cout<<"*********************************源程序**********************************"<<endl;
- while(charfile!=EOF)
- {
- cout<<charfile<<endl;
- }
- fclose(fp);
- printf("/n");
- }
- }//*/
- /*bool ReadFile(ifstream& infile)//读取源文件
- {
- const int MAXLENTH=31;
- char SFile[MAXLENTH];
- cout<<"输入正确路径和文件名(包括扩展名):"<<endl;
- cin.getline(SFile,MAXLENTH,'n');
- infile.open(SFile,ios::binary);
- if(infile.fail())
- {
- cout<<"The file doesn't exist,Can't open it!"<<endl;
- exit(1);
- return false;
- }
- else
- {
- cout<<"FILE Opened"<<endl;
- return true;
- }
- }//ReadFile*/
- bool SaveFile(ofstream& outfile)//保存文件
- {
- const int MAXLENTH1=31;
- char FFile[MAXLENTH1];
- cout<<"Please enter a name for final file:"<<endl;
- cin.getline(FFile,MAXLENTH1,'n');
- outfile.open(FFile,ios::app);
- //cout<<"File has beeen created and opened"<<endl;
- //return true;
- if(outfile.fail())
- {
- cout<<"Cann't creat&open file"<<endl;
- return false;
- }
- else
- {
- cout<<"File has beeen created and opened"<<endl;
- return true;
- }//*/
- }//SaveFile
- void CloseFile(char fname[20])
- {
- fp=fopen(fname,"r");
- fclose(fp);
- cout<<"文件 "<<fname<<" 已经关闭!"<<endl;
- }//CloseFile
- ////////////////////////////////////////////////////////////////////////////////////////
- ////////////////////////////////////////////////////////////////////////////////////////
- /////////////////////////////////////////////////////////////////////////////////////////
- //////////////////////////////////////////////////////////////////////////////////////////
- void GetNextWord()//处理字符,并且取下一个
- {
- cch=GetC();
- while(cch==' ')// 除去空格
- {
- cch=GetC();
- }
- int k=0;
- str[0]=cch;
- //////////////////////////////////////////////////////////////////////////////////
- if((cch>='a'&&cch<='z')||(cch>='A'&&cch<='Z'))//字符是字母
- {
- str[++k]=cch;//当前字符存入str数组中
- cch=GetC();
- while((cch>='a'&&cch<='z')||(cch>='A'&&cch<='Z')||(cch>='0'&&cch<='9'))
- {
- str[k++]=cch;
- cch=GetC();
- }
- str[k]=' ';
- Pushback(cch);//是 的话回退
- if(SearchKey()==0)
- cout<<"字符(串):"<<str;
- else cout<<"关键字:"<<str;//是关键字的话输出<<SearchKey()
- }
- //////////////////////////////////////////////////////////////
- else if(cch<='9'&&cch>='0')//数字的识别
- //else if1
- {
- while(cch<='9'&&cch>='0')
- cch=GetC();
- if(cch=='E'||cch=='D')
- cch=GetC();
- if(cch=='+'||cch=='-')
- {
- cch=GetC();
- if(cch<='9'&&cch>='0')
- cch=GetC();
- while(cch<='9'&&cch>='0')
- cch=GetC();
- Pushback(cch);
- cout<<"400";
- }
- else if(cch<='9'&&cch>='0')
- {
- while(cch<='9'&&cch>='0')
- cch=GetC();
- Pushback(cch);
- cout<<"400";
- }
- else if(cch=='.')//else if2
- {
- while(cch<='9'&&cch>='0')
- cch=GetC();
- if(cch=='E'||cch=='D')
- cch=GetC();
- if(cch=='+'||cch=='-')
- {
- cch=GetC();
- if(cch<='9'&&cch>='0')
- cch=GetC();
- while(cch<='9'&&cch>='0')
- cch=GetC();
- Pushback(cch);
- cout<<"400";
- }
- else if(cch<='9'&&cch>='0')
- {
- while(cch<='9'&&cch>='0')
- cch=GetC();
- Pushback(cch);
- cout<<"400";
- }
- else Pushback(cch);
- cout<<"400";
- }//else if2
- }//else if1
- ////////////////////////////////////////////////////////////////////////////////////////
- else //判断运算符
- {
- switch(cch)
- {
- case '+':
- {
- cch=GetC();
- switch(cch)
- {
- case '+':cout<<"1"<<"++";break;
- case '-':cout<<"2"<<"+=";break;
- default:Pushback(cch);cout<<"3"<<"+";break;
- }
- };break;
- case '-':
- {
- cch=GetC();
- switch(cch)
- {
- case '-':cout<<"4"<<"--";break;
- case '=':cout<<"5"<<"-=";break;
- case '>':cout<<"6"<<"->";break;
- default:Pushback(cch);cout<<"7"<<"-";break;
- }
- };break;
- case '*':
- {
- cch=GetC();
- switch(cch)
- {
- case'=':cout<<"12"<<"*=";break;
- case'/':cout<<"右注释:"<<"*/";break;
- default:Pushback(cch);cout<<"乘法:"<<"*";
- }
- };break;
- case '/':
- {
- cch=GetC();
- switch(cch)
- {
- case'=':cout<<"除以等于:"<<"/=";break;
- case'*':cout<<"左注释:"<<"/*";break;
- case'n':cout<<"换行符:"<<"/n";break;
- default:Pushback(cch);cout<<"除法:"<<"/";
- }
- };break;
- case '<':
- {
- cch=GetC();
- switch(cch)
- {
- case'=':cout<<"12"<<"<=";break;
- case'<':cch=GetC();
- if(cch=='=')cout<<"13"<<"<<=";
- else
- {
- Pushback(cch);
- cout<<"14"<<"<<";
- };
- default:Pushback(cch);cout<<"左尖括号:"<<"<";
- }
- };break;
- case '>':
- {
- cch=GetC();
- switch(cch)
- {
- case'=':cout<<"16"<<">=";break;
- case'>':cch=GetC();
- if(cch=='=')cout<<"17"<<">>=";
- else
- {
- Pushback(cch);
- cout<<"18"<<">>";break;}
- default:Pushback(cch);cout<<"右尖括号:"<<">";
- }
- };break;
- /* case '"':
- {
- cch=GetC();
- switch(cch)
- {
- case'=':cout<<"16"<<">=";break;
- case'>':cch=GetC();
- if(cch=='=')cout<<"17"<<">>=";
- else
- {
- Pushback(cch);
- cout<<"18"<<">>";break;}
- default:Pushback(cch);cout<<"右尖括号:"<<">";
- }
- };break;//*/
- ////////////////////////////////////////////////////////////////////////
- case '=':
- {
- cch=GetC();
- if(cch=='=') cout<<"20"<<"==";
- else
- {
- Pushback(cch);
- cout<<"21"<<"=";
- }
- };break;
- case '%':
- {
- cch=GetC();
- if(cch=='=') cout<<"22"<<"%=";
- else
- {
- Pushback(cch);
- cout<<"23"<<"%";
- }
- };break;
- case '!':
- {
- cch=GetC();
- if(cch=='=') cout<<"不等于:"<<"!=";
- else
- {
- Pushback(cch);
- cout<<"感叹号:"<<"!";
- }
- };break;
- case '&':
- {
- cch=GetC();
- switch(cch)
- {
- case '&':cout<<"26"<<"&&";break;
- case '=':cout<<"27"<<"&=";break;
- default:Pushback(cch);cout<<"28"<<"&";
- }
- };break;
- //////////////////////////////////////////////////////////////////////////
- case '#':cout<<"#号:"<<"#"<<endl;
- {
- // for(k=0;k<100;k++)
- // str[0]=' ';
- cch=GetC();
- if((cch>='a'&&cch<='z')||(cch>='A'&&cch<='Z'))//字符是字母
- {
- str[++k]=cch;//当前字符存入str数组中
- cch=GetC();
- while((cch>='a'&&cch<='z')||(cch>='A'&&cch<='Z'))
- {
- str[k++]=cch;
- cch=GetC();
- str[k]=' ';
- Pushback(cch);//是 的话回退
- cout<<"#头文件名:"<<str;break;
- }
- }
- };break;
- case '^':
- cch=GetC();
- if(cch=='=')cout<<"29"<<"^=";
- else
- {
- Pushback(cch);cout<<"30"<<"^";
- };break;
- case '|':
- cch=GetC();
- switch(cch)
- {
- case'|':cout<<"31"<<"||";break;
- case'=':cout<<"32"<<"|=";break;
- default:Pushback(cch);cout<<"33"<<"|";
- };break;
- case '(':cout<<"左小括号:"<<"(";break;
- case ')':cout<<"右小括号:"<<")";break;
- case '[':cout<<"左中括号:"<<"[";break;
- case ']':cout<<"右中括号:"<<"]";break;
- case '{':cout<<"左大括号:"<<"{";break;
- case '}':cout<<"右大括号:"<<"}";break;
- case '~':cout<<"40"<<"~";break;
- case '.':cout<<" 点 :"<<".";break;
- case '"':cout<<"双引号:"<<""";break;
- case ';':cout<<"分号:"<<";";break;
- case ',':cout<<"逗号:"<<",";break;
- //case '#':cout<<"#";break;
- case '?':
- {
- cch=GetC();
- if(cch==':')cout<<"44"<<"?:";
- else cerr<<"error!"<<endl;
- }break;
- }//switch
- }//else
- }//GetNextWord
- /*string GetText()
- {
- string file_name1; //
- cout<<"请输入源文件名(包括路径和后缀名):";
- cin>>file_name1;
- ifstream infile(file_name1.c_str(),ios::in);
- if (!infile)
- {
- cerr<<"哦!无法打开文件 "<<file_name1.c_str()<<" !!!"<<endl;
- exit(-1);
- }
- cout<<endl;
- char f[1000];
- infile.getline(f,1000,EOF);
- infile.close();
- return f;
- }//*/
English
