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

C/C++ 学习笔记[03].去掉C风格的注释

网上搜索了一大堆去掉/*和*/之间注释的代码,就像<The C Programming Language>练习1-23里有人说的一样
大部分都会被以下的程序broken,这个功能看起来简单,实际上很有难度.网上实现的代码,除了我找到的一个用文件指针实现的没有问题外,其余的都存在各种bug,不信的话就用以下的程序测试一下:-),当然这个程序也不够完善.
还是有限状态自动机实现起来更严谨也容易理解得多:
/* krx123tp.c - a test program to serve as input to krx123*.c
*
* This is a shameless copy of Ben Pfaff's solution, to which I have
* added a few extra statements to further test the candidate programs
* for this exercise. As Ben says, this program already contains lots
* of examples of comments and not-quite-comments. I've just made it
* a little tougher.
*
*/
/* K&R2 1-23: Write a program to remove all comments from a C program.
Don't forget to handle quoted strings and character constants
properly. C comments do not nest.
This solution does not deal with other special cases, such as
trigraphs, line continuation with \, or <> quoting on #include,
since these aren't mentioned up 'til then in K&R2. Perhaps this is
cheating.
Note that this program contains both comments and quoted strings of
text that looks like comments, so running it on itself is a
reasonable test. It also contains examples of a comment that ends
in a star and a comment preceded by a slash. Note that the latter
will break C99 compilers and C89 compilers with // comment
extensions.
Interface: The C source file is read from stdin and the
comment-less output is written to stdout. **/
#include <stdio.h>
int
main(void)
{
/* State machine's current state. */
enum {
PROGRAM,
SLASH,
COMMENT,
STAR,
QUOTE,
LITERAL
} state;
/* If state == QUOTE, then ' or ". Otherwise, undefined. */
int quote;
state = PROGRAM;
for (;;) {
int c = getchar();
if (c == EOF)


相关文档:

c程序设计语言习题1 9

联系1-9编写一个将输入复制到输出的程序,并将其中连续的多个空格用一个空格代替。
#include "stdio.h"
main(){
int c;
int flag;
flag=0;//是否空格标志
while ((c=getchar())!=EOF){
if (c!=32) {
putchar(c);
flag=0;
}else if(flag==0){
flag=1;
putchar(c);
}

/* ......

Java C++ 区别

JAVA和C++都是面向对象语言。也就是说,它们都能够实现面向对象思想(封装,继乘,多态)。
C++能被操作系统(Window,Linux)直接运行,而Java不行,需要有个JVM。
 java语言给开发人员提供了更为简洁的语法;取消了指针带来更高的代码质量;完全面向对象,独特的运行机制是其具有天然的可移植性。
1.指针
JAVA ......

C++与JAVA

JAVA和C++都是面向对象语言。也就是说,它们都能够实现面向对象思想(封装,继乘,多态)。
C++能被操作系统(Window,Linux)直接运行,而Java不行,需要有个JVM。
 java语言给开发人员提供了更为简洁的语法;取消了指针带来更高的代码质量;完全面向对象,独特的运行机制是其具有天然的可移植性。
1.指针
JAVA ......

使用C语言扩展Python(二)

在上一篇中我们已经使用c语言实现了一个最简单的扩展模块,这一篇中将在其基础上进行功能的丰富。首先来考虑如何从外部的Python向C模块传递进参数,foo_bar2展示了如何向C模块传递整数,浮点数,字符串三个参数,其中"ids"指明了传入参数的数据类型。PyArg_ParseTuple负责对args进行解析,若解析失败则返回0.代码#include&n ......

C的位运算符

与 &:  任何位用&运算与0结合结果为0,与1结合结果为其本身;
或 | :  任何位用|运算与0结合结果为其本身,与1结合结果为1;
异或  ^ :  任何位用^运算与0结合结果为其本身,与1结合则取反; ......
© 2009 ej38.com All Rights Reserved. 关于E健网联系我们 | 站点地图 | 赣ICP备09004571号