易截截图软件、单文件、免安装、纯绿色、仅160KB

删除c/c++源程序中的注释

题目:编写一个程序,用于处理c/c++源程序,将源程序中的注释部分去掉
输入:c/c++文件名
输出:处理后的程序源文件
程序伪代码如下:
c1,c2:char
tag:int
a:读入一个字符存入c1
if tag==0   //读入字符不是注释
  if c1=='/'  //可能是注释标记
    读入一个字符存入c2
    if c2=='*'  //为/**/注释的开头
      tag = 2
    else if c2=='/' //为//注释的开头
      tag = 1
    else //不是注释标记
      将c1, c2保存
  else //读入的字符为代码
    保存c1
else if tag==1  //读入的是//注释
  if c1=='\n' //为注释结束标记
    tag=0
else   //读入的是/**/注释tag==2
  if c1=='*'  //可能为注释结束标记
    读入一个字符赋给c2
    if c2=='/' //是/**/注释结尾
      tag=0
  else  //注释没有结束
    nothing
goto a
程序清单如下:
//对源文件进行扫描,删除其中的注释
#include <stdio.h>
void scan(char *filename)
{
int tag=0;//0:读取的字符为代码;1:读取的字符为//注释;2:读取的字符为/**/注释
char temp1, temp2;
FILE *in, *out;
in = fopen(filename,"r");
if(in==NULL)
{
printf("cannot open the input file!");
}
out = fopen("out.c", "w");
if(out==NULL)
{
printf("cannot open the output file!");
}
temp1 = fgetc(in);
while(temp1!=EOF)
{
printf("%c", temp1);
if(0==tag)
{
if('/'==temp1)
{
temp2 = fgetc(in);
if('*'==temp2)
{
tag = 2;
}
else if('/'==temp2)
{
tag = 1;
}
else
{
fputc(temp1, out);
fputc(temp2, out);
}
}
else
{
fputc(temp1, out);
}
}
else if(1==tag)
{
if('\n'==temp1)
{
tag = 0;
}
}
else
{
if('*'==temp1)
{
temp2 =


相关文档:

C/C++函数的借口定义

1、在C文件中调用C++文件中定义的文件
直接的方法是在C++ 文件的头部添加如下代码段:
extern "C"
{
      int API(int A);
}
2、C++接口的方法
在C++中调用C的函数,在C头文件中加入如下代码:
#ifdef __cplusplus          // 开始
exte ......

C/C++ 与运算和或运算转换大小写

// & 与,将指定位置设置为0     | 或,将指定位置设置为1
//注: 只针对纯字母的情况
#include <stdio.h>
#include <string>
int main()
{
char str[6] = "xxing";
std::string str1 = "INGXX";
for(int i = 0; i < 5; i++)
{
str[i] &= 0xdf; ......

初始化函数中的虚函数调用( C++ vs python )

代码+结果,不做解释
当然,对于python没有virtual function一说,估计当作对比一个例子看看吧。
#include <iostream>
using namespace std;
class base
{
public:
virtual void foo() { cout << "base" << endl; }
base() { foo() ;}
};
class derive: public base
{
pub ......

C/C++修改文本文件

mmap是linux下的CreateFileMapping,用来映射并同步文件。
这样的话,比如我自定义一种文件格式,把它写入到文件中,现在想修改其中的值,就可以用这个函数,把文件映射到内存中
然后用操作数组的方式,来进行文件的同步。如果不用这个函数就得:
1、定义一个结构体
2、定义结构体数组
3、读取文件(w+)
4、修改文件 ......
© 2009 ej38.com All Rights Reserved. 关于E健网联系我们 | 站点地图 | 赣ICP备09004571号