删除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 =
相关文档:
在做ACM题时,经常都会遇到一些比较大的整数。而常用的内置整数类型常常显得太小了:其中long 和 int
范围是[-2^31,2^31),即-2147483648~2147483647。而unsigned范围是[0,2^32),即
0~4294967295。也就是说,常规的32位整数只能够处理40亿以下的数。
那遇到比40亿要大的数怎么办呢?这时就要用到C++的64位扩展 ......
1、在C文件中调用C++文件中定义的文件
直接的方法是在C++ 文件的头部添加如下代码段:
extern "C"
{
int API(int A);
}
2、C++接口的方法
在C++中调用C的函数,在C头文件中加入如下代码:
#ifdef __cplusplus // 开始
exte ......
c指针的运算有时候还是很迷惑人的。
例如:
struct student {
int num;
int score;
int length;
};
struct student *pt;
pt = (struct student *) malloc(sizeof(struct student));
pt->num = 1;
pt->score = 90;
pt->length = 3 * sizeof(int);
printf("pt length:%d\n", *pt);
pt = (int ......
一. 何谓可变参数
int printf( const char* format, ...);
这是使用过C语言的人所再熟悉不过的printf函数原型,它的参数中就有固定参数format和可变参数(用”…”表示)。
而我们又可以用各种方式来调用printf,如:
printf("%d",value);
printf("%s",str);
printf("the number is %d ,st ......
mmap是linux下的CreateFileMapping,用来映射并同步文件。
这样的话,比如我自定义一种文件格式,把它写入到文件中,现在想修改其中的值,就可以用这个函数,把文件映射到内存中
然后用操作数组的方式,来进行文件的同步。如果不用这个函数就得:
1、定义一个结构体
2、定义结构体数组
3、读取文件(w+)
4、修改文件 ......