class1.h
上传用户:jiashang
上传日期:2013-07-26
资源大小:12k
文件大小:10k
源码类别:

文本生成

开发平台:

Visual C++

  1. #include <stdlib.h>
  2. #include <stdio.h>
  3. #include <iostream.h>
  4. #include <string.h>
  5. #include <fstream.h>
  6. int flag=0;  //标志位
  7. char cch;
  8. char bchar;
  9. char str[100];
  10. FILE *fp;
  11. void Pushback(char c)
  12. {
  13. bchar=c;
  14. flag=1;
  15. }
  16. /////////////////////////////////////////////////////////////////
  17. char GetC()
  18. {
  19.   if(flag==1)
  20.   {
  21.   flag=0;
  22.   return bchar;
  23.   }
  24.   char t;
  25.   t=getc(fp);//继续取出字符
  26.   if(t=='n'||t=='t'||t=='r') t=' ';
  27.   return t;
  28. }
  29. /////////////////////////////////////////////////////////////////////////
  30. int SearchKey()  //搜索关键字
  31. {
  32. char StrToken[32][10]={"auto","break","case","char","const",
  33.  "continue","default","do","double","else","enum","extern",
  34.  "float","for","goto","if","int","long","register","return",
  35.          "short","signed","sizeof","static","struct","switch",
  36.  "typedef","union","unsigned","void","volatile","while"};
  37. for(int i=0;i<32;i++)
  38. {
  39. if(strcmp(StrToken[i],str)==0)   //比较关键字    
  40. return i;   
  41. }
  42. return 0;
  43. }
  44. ///////////////////////////////////////////////////////////////////////////////////
  45. bool OpenFile(char fname[20])//读取文件
  46. {
  47.     cout<<"输入文件位置及名称(如: e:\a.c):"<<endl;
  48.     cin>>fname;
  49. if((fp=fopen(fname,"r"))==NULL)   
  50. {
  51. cout<<"文件路径或者文件名不正确导致了文件打开错误!!!"<<endl;
  52. return false;
  53. }
  54.     else return true;
  55. }//OpenFile*/
  56. /*void ShowFile() //显示源文件
  57. {
  58. char charfile;
  59. char fname[20];
  60. if( OpenFile(fname) ==true )
  61. {
  62. cout<<"*********************************源程序**********************************"<<endl;
  63.     while(charfile!=EOF) 
  64. {
  65. cout<<charfile<<endl;
  66. }
  67.     fclose(fp);
  68. printf("/n");
  69. }
  70. }//*/
  71. /*bool ReadFile(ifstream& infile)//读取源文件
  72. {
  73. const int MAXLENTH=31;
  74. char SFile[MAXLENTH];
  75. cout<<"输入正确路径和文件名(包括扩展名):"<<endl;
  76. cin.getline(SFile,MAXLENTH,'n');
  77. infile.open(SFile,ios::binary);
  78. if(infile.fail())
  79. {
  80. cout<<"The file doesn't exist,Can't open it!"<<endl;
  81. exit(1);
  82. return false;
  83. }
  84.     else
  85. {
  86. cout<<"FILE Opened"<<endl;
  87. return true;
  88. }
  89. }//ReadFile*/
  90. bool SaveFile(ofstream& outfile)//保存文件
  91. {
  92. const int MAXLENTH1=31;
  93. char FFile[MAXLENTH1];
  94. cout<<"Please enter a name for final file:"<<endl;
  95. cin.getline(FFile,MAXLENTH1,'n');
  96. outfile.open(FFile,ios::app);
  97. //cout<<"File has beeen created and opened"<<endl;
  98. //return true;
  99. if(outfile.fail())
  100. {
  101. cout<<"Cann't creat&open file"<<endl;
  102. return false;
  103. }
  104. else 
  105. {
  106. cout<<"File has beeen created and opened"<<endl;
  107. return true;
  108. }//*/
  109. }//SaveFile
  110. void CloseFile(char fname[20])
  111. {
  112. fp=fopen(fname,"r");
  113.     fclose(fp);
  114. cout<<"文件 "<<fname<<" 已经关闭!"<<endl;
  115. }//CloseFile
  116. ////////////////////////////////////////////////////////////////////////////////////////
  117. ////////////////////////////////////////////////////////////////////////////////////////
  118. /////////////////////////////////////////////////////////////////////////////////////////
  119. //////////////////////////////////////////////////////////////////////////////////////////
  120. void GetNextWord()//处理字符,并且取下一个
  121. {    
  122. cch=GetC();
  123. while(cch==' ')// 除去空格
  124. {
  125. cch=GetC();
  126. }
  127. int k=0;
  128. str[0]=cch;
  129. //////////////////////////////////////////////////////////////////////////////////
  130. if((cch>='a'&&cch<='z')||(cch>='A'&&cch<='Z'))//字符是字母
  131. {
  132. str[++k]=cch;//当前字符存入str数组中
  133. cch=GetC();
  134. while((cch>='a'&&cch<='z')||(cch>='A'&&cch<='Z')||(cch>='0'&&cch<='9'))
  135. {
  136. str[k++]=cch;
  137. cch=GetC();
  138. }
  139. str[k]='';
  140. Pushback(cch);//是的话回退
  141. if(SearchKey()==0)
  142. cout<<"字符(串):"<<str;
  143. else cout<<"关键字:"<<str;//是关键字的话输出<<SearchKey()
  144. }
  145. //////////////////////////////////////////////////////////////
  146. else if(cch<='9'&&cch>='0')//数字的识别 
  147. //else if1
  148. {
  149. while(cch<='9'&&cch>='0')
  150. cch=GetC();
  151. if(cch=='E'||cch=='D')
  152. cch=GetC();
  153. if(cch=='+'||cch=='-')
  154. {
  155. cch=GetC();
  156. if(cch<='9'&&cch>='0')
  157. cch=GetC();
  158. while(cch<='9'&&cch>='0')
  159. cch=GetC();
  160. Pushback(cch);
  161. cout<<"400";
  162. }
  163. else if(cch<='9'&&cch>='0')
  164. {
  165. while(cch<='9'&&cch>='0')
  166. cch=GetC();
  167. Pushback(cch);
  168. cout<<"400";
  169. }
  170. else if(cch=='.')//else if2
  171. {
  172. while(cch<='9'&&cch>='0')
  173. cch=GetC();
  174. if(cch=='E'||cch=='D')
  175. cch=GetC();
  176. if(cch=='+'||cch=='-')
  177. {
  178. cch=GetC();
  179.     if(cch<='9'&&cch>='0')
  180.     cch=GetC();
  181.     while(cch<='9'&&cch>='0')
  182.     cch=GetC();
  183.     Pushback(cch);
  184.     cout<<"400";
  185. }
  186.     else if(cch<='9'&&cch>='0')
  187. {
  188. while(cch<='9'&&cch>='0')
  189.     cch=GetC();
  190.     Pushback(cch);
  191.     cout<<"400";
  192. }
  193.     else Pushback(cch);
  194.     cout<<"400";
  195. }//else if2
  196. }//else if1
  197. ////////////////////////////////////////////////////////////////////////////////////////
  198. else //判断运算符
  199. {
  200. switch(cch)
  201. {
  202.     case '+':
  203. {
  204. cch=GetC();
  205.         switch(cch)
  206. {
  207.             case '+':cout<<"1"<<"++";break;
  208.         case '-':cout<<"2"<<"+=";break;
  209.         default:Pushback(cch);cout<<"3"<<"+";break;
  210. }
  211. };break;
  212.   
  213. case '-':
  214. {
  215. cch=GetC();
  216.     switch(cch)
  217. {
  218.     case '-':cout<<"4"<<"--";break;
  219.     case '=':cout<<"5"<<"-=";break;
  220.                         case '>':cout<<"6"<<"->";break;
  221.     default:Pushback(cch);cout<<"7"<<"-";break;
  222. }
  223. };break;
  224. case '*':
  225. {
  226. cch=GetC();
  227. switch(cch)
  228. {
  229. case'=':cout<<"12"<<"*=";break;
  230. case'/':cout<<"右注释:"<<"*/";break;
  231. default:Pushback(cch);cout<<"乘法:"<<"*";
  232. }
  233. };break;
  234.  
  235. case '/':
  236. {
  237. cch=GetC();
  238. switch(cch)
  239. {
  240. case'=':cout<<"除以等于:"<<"/=";break;
  241. case'*':cout<<"左注释:"<<"/*";break;
  242.                         case'n':cout<<"换行符:"<<"/n";break;
  243. default:Pushback(cch);cout<<"除法:"<<"/";
  244. }
  245. };break;
  246.  
  247. case '<':
  248. {
  249. cch=GetC();
  250. switch(cch)
  251. {
  252. case'=':cout<<"12"<<"<=";break;
  253. case'<':cch=GetC();
  254.   if(cch=='=')cout<<"13"<<"<<=";
  255.   else 
  256.   {
  257.   Pushback(cch);
  258.       cout<<"14"<<"<<";
  259.   };
  260. default:Pushback(cch);cout<<"左尖括号:"<<"<";
  261.  
  262. }
  263. };break;
  264.  
  265. case '>':
  266. {
  267. cch=GetC();
  268.     switch(cch)
  269. {
  270. case'=':cout<<"16"<<">=";break;
  271. case'>':cch=GetC();
  272.   if(cch=='=')cout<<"17"<<">>=";
  273.   else 
  274.   {
  275.   Pushback(cch);
  276.   cout<<"18"<<">>";break;}
  277. default:Pushback(cch);cout<<"右尖括号:"<<">";
  278. }
  279. };break;
  280.            /* case '"':
  281. {
  282. cch=GetC();
  283.     switch(cch)
  284. {
  285. case'=':cout<<"16"<<">=";break;
  286. case'>':cch=GetC();
  287.   if(cch=='=')cout<<"17"<<">>=";
  288.   else 
  289.   {
  290.   Pushback(cch);
  291.   cout<<"18"<<">>";break;}
  292. default:Pushback(cch);cout<<"右尖括号:"<<">";
  293. }
  294. };break;//*/
  295. ////////////////////////////////////////////////////////////////////////
  296. case '=':
  297. {
  298. cch=GetC();
  299. if(cch=='=') cout<<"20"<<"==";
  300. else 
  301. {
  302. Pushback(cch);
  303. cout<<"21"<<"=";
  304. }
  305. };break;
  306. case '%':
  307. {
  308. cch=GetC();
  309. if(cch=='=') cout<<"22"<<"%=";
  310. else
  311. {
  312. Pushback(cch);
  313. cout<<"23"<<"%";
  314. }
  315. };break;
  316. case '!':
  317. {
  318. cch=GetC();
  319. if(cch=='=') cout<<"不等于:"<<"!=";
  320. else 
  321. {
  322. Pushback(cch);
  323. cout<<"感叹号:"<<"!";
  324. }
  325. };break;
  326. case '&':
  327. {
  328. cch=GetC();
  329.     switch(cch)
  330. {
  331.     case '&':cout<<"26"<<"&&";break;
  332. case '=':cout<<"27"<<"&=";break;
  333. default:Pushback(cch);cout<<"28"<<"&";
  334. }
  335. };break;
  336. //////////////////////////////////////////////////////////////////////////
  337. case '#':cout<<"#号:"<<"#"<<endl;
  338. {
  339. // for(k=0;k<100;k++)
  340. // str[0]='';
  341. cch=GetC();
  342. if((cch>='a'&&cch<='z')||(cch>='A'&&cch<='Z'))//字符是字母
  343. {
  344. str[++k]=cch;//当前字符存入str数组中
  345.                 cch=GetC();
  346.     while((cch>='a'&&cch<='z')||(cch>='A'&&cch<='Z'))
  347. {
  348. str[k++]=cch;
  349.                 cch=GetC();
  350.     str[k]=' ';
  351.                     Pushback(cch);//是的话回退
  352.     cout<<"#头文件名:"<<str;break;
  353. }
  354.             
  355. };break;
  356. case '^':
  357. cch=GetC();
  358. if(cch=='=')cout<<"29"<<"^=";
  359. else
  360. {
  361. Pushback(cch);cout<<"30"<<"^";
  362. };break;
  363. case '|':
  364.     cch=GetC();
  365. switch(cch)
  366. {
  367. case'|':cout<<"31"<<"||";break;
  368. case'=':cout<<"32"<<"|=";break;
  369. default:Pushback(cch);cout<<"33"<<"|";
  370. };break;
  371. case '(':cout<<"左小括号:"<<"(";break;
  372. case ')':cout<<"右小括号:"<<")";break;
  373. case '[':cout<<"左中括号:"<<"[";break;
  374.             case ']':cout<<"右中括号:"<<"]";break;
  375.             case '{':cout<<"左大括号:"<<"{";break;
  376. case '}':cout<<"右大括号:"<<"}";break;
  377. case '~':cout<<"40"<<"~";break;
  378. case '.':cout<<" 点 :"<<".";break;
  379.             case '"':cout<<"双引号:"<<""";break;
  380.             case ';':cout<<"分号:"<<";";break;
  381.             case ',':cout<<"逗号:"<<",";break;
  382. //case '#':cout<<"#";break;
  383.             case '?':
  384. {
  385. cch=GetC();
  386.     if(cch==':')cout<<"44"<<"?:";
  387. else cerr<<"error!"<<endl;
  388. }break;
  389. }//switch
  390. }//else
  391. }//GetNextWord
  392. /*string GetText()  
  393. {   
  394.     string file_name1; //
  395. cout<<"请输入源文件名(包括路径和后缀名):";
  396. cin>>file_name1;
  397. ifstream infile(file_name1.c_str(),ios::in);
  398. if (!infile)
  399. {
  400. cerr<<"哦!无法打开文件 "<<file_name1.c_str()<<" !!!"<<endl;
  401.         exit(-1);
  402. }
  403.     cout<<endl;
  404.     char f[1000];
  405. infile.getline(f,1000,EOF);
  406. infile.close();
  407. return f;
  408. }//*/
  409.