项目51-处理C++源代码的程序

来源:互联网 发布:21天学通java百度云 编辑:程序博客网 时间:2024/05/16 17:26
 在CodeBlocks等IDE中都提供了代码格式整理的功能。完成这种功能的程序,操作的数据是用C++写的源代码文件。C++源文件是一种文本文件,可以通过程序进行操作。
集成开发环境(IDE)对对程序进行编译,操作的“数据”是源程序。编译中,要对源程序进行词法检查和语法检查,后续还要进行目标代码生成、代码优化等工作。相关的技术将在《编译原理》课中学习。这些技术可以用在很多领域,当然也能够让我们对于编程语言有更深的了解。
  本项目将以C++源程序为操作对象,完成对源程序的一系列处理。各功能可以分别编制一个程序实现(建议用这种简单的方案),也可以将其集成在一起(向着自己做出IDE努力)。

(1)读入一个C++程序,判断其中是否只有一个main()函数,输出“暂时没有发现问题”,或者“没有main()函数”,或者“不能定义多个main()函数”;

#include <fstream>  #include<iostream>  #include<string>  #include<cstdlib>  using namespace std;  int appear(char*s1,char*s2);  int main( )  {      char line[256];      char main_fun[8]="main()";      int main_num=0;     ifstream sourceFile("source.cpp",ios::in);       if(!sourceFile)     {          cerr<<"source code read error!"<<endl;          exit(1);      }      while(!sourceFile.eof())      {          sourceFile.getline(line,255,'\n');          main_num+=appear(line,main_fun);          if (main_num>1)               break;      }      sourceFile.close();        if(main_num==0)          cout<<"error: no main().";      else if (main_num==1)          cout<<"right: a main() be exist.";      else          cout<<"error: more than one main().";      cout<<endl;      return 0;  }  int appear(char*s1,char*s2)  {      int n=0,flag;      char *p,*q;      for(; *s1!='\0'; s1++)      {          if (*s2==*s1)          {              flag=1;              p=s1 ;              q=s2;              for(; *q!='\0';)             {                  if (*q++!=*p++)                  {                      flag=0;                      break;                  }              }              if (flag==1) n++;          }        }      return(n);  }  

(2)读入一个C++程序,使程序中的所有左花括号“{”和右花括号“}”都单独占一行,新程序保存到另一个.cpp文件中,并在屏幕上显示处理过的程序,显示时加上行号。

#include <fstream>  #include<iostream>  //#include<string>  #include<cstdlib>  using namespace std;  void outprogram(char *filename);  int main( )  {      char ch1,ch2;       ifstream sourceFile("source.cpp",ios::in);     if(!sourceFile)           {          cerr<<"source code read error!"<<endl;          exit(1);      }      ofstream outFile("newsource.cpp",ios::out);       if(!outFile)           {          cerr<<"new source code write error!"<<endl;          exit(1);      }        ch1='\0';      while(!sourceFile.eof())      {          sourceFile.get(ch2);         if((ch2=='{'||ch2=='}')&&(ch1!='\n'))              outFile.put('\n');          else            if((ch1=='{'||ch1=='}')&&(ch2!='\n'))                  outFile.put('\n');          outFile.put(ch2);           ch1=ch2;      }      outFile.close();      sourceFile.close();      cout<<"经过处理后的源程序是:"<<endl;      outprogram("newsource.cpp");      return 0;  }    void outprogram(char *filename)  {      char line[256];      int n = 1;      ifstream inFile(filename, ios::in);       if(!inFile)           {          cerr<<"file open error!"<<endl;          exit(1);      }      while (!inFile.eof())      {          inFile.getline(line,255,'\n');          cout<<n<<'\t'<<line<<endl;          n++;      }      inFile.close();      return;  
(3)读入一个C++程序,输入m、n两个数字,从第m行起的n行代码将作为注释使用(即在这些行前面加上”//”),新程序保存到另一个.cpp文件中,并在屏幕上显示处理过的程序,显示时加上行号。

#include <fstream>  #include<iostream>  #include<cstring>  #include<cstdlib>  using namespace std;  void outprogram(const char *filename);  int main( )  {      char line[256];      int m,n;      ifstream sourceFile("source.cpp",ios::in);    if(!sourceFile)           {          cerr<<"source code read error!"<<endl;          exit(1);      }      ofstream outFile("newsource.cpp",ios::out);      if(!outFile)           {          cerr<<"new source code write error!"<<endl;          exit(1);      }      cout<<"您要将第m行开始的n行代码作为注释,请输入m和n:";      cin>>m>>n;      int n1=0;      while(!sourceFile.eof())      {          sourceFile.getline(line,255,'\n');          n1++;          if(n1>=m&&n1<m+n)              outFile.put('/').put('/');          outFile.write(line,strlen(line));          outFile.write("\n",1);      }      outFile.close();      sourceFile.close();      cout<<"经过处理后的源程序是:"<<endl;      outprogram("newsource.cpp");      return 0;  }    void outprogram(const char *filename)  {      char line[256];      int n = 1;      ifstream inFile(filename, ios::in);        if(!inFile)           {          cerr<<"file open error!"<<endl;          exit(1);      }      while (!inFile.eof())      {          inFile.getline(line,255,'\n');          cout<<n<<'\t'<<line<<endl;          n++;      }      inFile.close();      return;  }  



0 0
原创粉丝点击