项目51-处理C++源代码的程序
2016-06-18 17:49
399 查看
在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;
}
集成开发环境(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;
}
相关文章推荐
- C语言判断一个数是否是质数(蒙格马利快速幂算法)
- 对C++虚函数、虚函数表的简单理解
- c/c++字符串处理大集合
- 最小生成树之Kruskal和Prim算法——C++实现
- POJ 3687 Labeling Balls-拓扑排序
- C++独孤九剑第六式——洞若观火(深入对象操作)
- 二维数组指针
- 匈牙利算法c++代码
- C语言中可变参数函数实现原理
- C++ switch语句后表达式的类型,vs2015试过
- 用c语言实现求一个字符串长度的函数
- C++14 make_index_sequence原理
- C++面向对象总结(二)--友元函数
- C++工厂模式详解——设计模式(2)
- C++11中的 "enable_if"小记
- c++中的const
- Problem G: C语言习题 n个数逆序
- 对于C++中traits的简单介绍
- Problem E: C语言习题 矩形法求定积分
- Problem D: C语言习题 矩阵元素变换