Getting started with the basics of programming exercises_4
1、编写一个删除C语言程序中所有的注释语句的程序。要正确处理带引号的字符串与字符串常量,C语言中程序注释不允许嵌套。
#include<stdio.h>
void rcomment(int c);
void in_comment(void);
void echo_quote(int c);
// remove all comments from a valic C program
int main(void)
{
int c;
while((c = getchar()) != EOF)
rcomment(c); return 0;
}
// rcomment: read each character,remove the comments
void rcomment(int c)
{
int d; if(c == '/')
if((d = getchar()) == '*')
in_comment(); // beginning comment
else if(d == '/')
{
putchar(c); // another slash
rcomment(d);
}
else
{
putchar(c); // not a comment
putchar(d);
}
else if(c == '\'' || c == '"')
echo_quote(c); // quote begins
else
putchar(c); // not a comment
}
// in_comment: inside of a valid comment
void in_comment(void)
{
int c, d; c = getchar(); // prev character
d = getchar(); // curr character
while(c != '*' || d != '/') // here can be more readable
{
c = d;
d = getchar();
}
}
// echo_quote: echo characters within quotes
void echo_quote(int c)
{
int d; putchar(c);
while((d = getchar()) != c) // search for end
{
putchar(d);
if(d == '\\')
putchar(getchar()); // ignore escape seq
}
}
2、小型词法分析器
编写程序,查找C语言程序中的基本语法错误,如圆括号、方括号以及花括号不配对等。要正确处理引号(包括单引号、双引号)、转义字符序列与注释。
#include<stdio.h>
int brace, brack, paren;
void in_quote(int c);
void in_comment(void);
void search(int c);
// rudimentary syntax checker for C programs
int main(void)
{
int c;
extern int brace, brack, paren;
while((c = getchar()) != EOF)
{
if(c == '/')
{
if((c = getchar()) == '*')
in_comment(); // inside comment
else
search(c);
}
else if(c == '\'' || c == '"')
in_quote(c); // inside quote
else
search(c); if(brace < 0) // output errors
{
printf("Unbalanced braces\n");
brace = 0;
}
else if(brack < 0)
{
printf("Unbalanced brackets\n");
brack = 0;
}
else if (paren < 0)
{
printf("Unbalanced parentheses\n");
}
} if(brace > 0) // output errors
printf("Unbalanced braces\n");
if(brack > 0)
printf("Unbalanced brackets\n");
if(paren > 0)
printf("Unbalanced parentheses\n"); return 0;
} // search: search for rudimentary syntax errors
void search(int c)
{
extern int brace, brack, paren; if(c == '{')
++brace;
else if(c == '}')
--brace;
else if(c == '[')
++brack;
else if(c == ']')
--brack;
else if(c == '(')
++paren;
else if(c == ')')
--paren;
} // in_comment: inside of a valid comment
void in_comment(void)
{
int c, d; c = getchar(); // pre character
d = getchar(); // curr character
while(c != '*' || d != '/')
{
c = d;
d = getchar();
}
} // in_quote: inside quote
void in_quote(int c)
{
int d; while((d = getchar()) != c) // search end quote
if(d == '\\')
getchar(); // ignore escape seq
}
Getting started with the basics of programming exercises_4的更多相关文章
- Getting started with the basics of programming exercises_5
1.编写函数,把由十六进制数字组成的字符串转换为对应的整型值 编写函数htoi(s),把由十六进制数字组成的字符串(包含可选的前缀0x或0X)转换为与之等价的整型值.字符串中允许包含的数字包括:0~9 ...
- Getting started with the basics of programming exercises_3
1.编写一个程序删除每个输入行末尾的空格及制表符并删除完全是空白符的行 #include<stdio.h> #define MAXLINE 1000 // maximum input li ...
- Getting started with the basics of programming exercises_2
1.编写简单power函数 #include<stdio.h> int power(int m, int n); // test power function int main(void) ...
- Getting started with the basics of programming exercises_1
1.编写一个将输入复制到输出的程序,并将其中连续的多个空格用一个空格代替 使用if 结构: #include<stdio.h> #define NONBLANK 'a'; // repal ...
- Beginning C# Programming with Unity
Welcome to the wonderful world of programming! In this book you’ll learn the basics of programming u ...
- C语言学习书籍推荐《Practical C++ Programming》下载
下载链接 :点我 C++ is a powerful, highly flexible, and adaptable programming language that allows software ...
- How do I learn machine learning?
https://www.quora.com/How-do-I-learn-machine-learning-1?redirected_qid=6578644 How Can I Learn X? ...
- LINQ Query Expressions
https://msdn.microsoft.com/en-us/library/bb397676(v=vs.100).aspx Language-Integrated Query (LINQ) is ...
- 【译】微软的Python入门教程(一)
Getting started with Python(Python入门) Overview 概述 The series of videos on Channel 9 is designed to h ...
随机推荐
- Leetcode606.Construct String from Binary Tree根据二叉树创建字符串
你需要采用前序遍历的方式,将一个二叉树转换成一个由括号和整数组成的字符串. 空节点则用一对空括号 "()" 表示.而且你需要省略所有不影响字符串与原始二叉树之间的一对一映射关系的空 ...
- expect.js
前言 1> 借鉴里面的应用思想,使用断言提高代码的健壮性及维护性 2> 实现方式--不采用直接嵌入expect的方式,统一进行重写(提取常用断言方法,重新构造API) 官网介绍 https ...
- Laravel(PHP)使用Swagger生成API文档不完全指南 - 基本概念和环境搭建 - 简书
在PHPer中,很多人听说过Swagger,部分人知道Swagger是用来做API文档的,然而只有少数人真正知道怎么正确使用Swagger,因为PHP界和Swagger相关的资料实在是太少了.所以鄙人 ...
- linux下监测进程是否存在
因为有的统计脚本需要执行很久,而有不能总去人工的检查进程是否在跑,所以就用shell脚本来循环监测进程是否存在 尝试了网上说的$?表示上一条命令返回值总是达不到预期的结果,后来直接改成用一个变量记录返 ...
- windows上安装Anaconda和python的教程详解
一提到数字图像处理编程,可能大多数人就会想到matlab,但matlab也有自身的缺点: 1.不开源,价格贵 2.软件容量大.一般3G以上,高版本甚至达5G以上. 3.只能做研究,不易转化成软件. 因 ...
- iOS常量(const)、enum以及宏(#define)
http://www.cocoachina.com/ios/20160530/16483.html 本文投稿文章,作者:SuperMario_Nil(简书) 前言:本文主要梳理iOS中如何使用常量.e ...
- php表单和缩略图处理类是什么样呢
<?php//封装一个表单验证类//中文验证.邮箱验证.电话号码.手机.QQ.身份证.(由字母.数字.下划线组成,不能以数字开头)header('content-type:text/html;c ...
- 使用php封装APP接口
php封装APP接口 我们先来介绍Json的封装方法 json_encode函数传递中文的话,输出后是乱码的,针对这个问题我觉得有必要做一个解释: 其实json_encode对中文那不是乱码,只是js ...
- laravel 图片上传 intervention/image
1. composer require intervention/image 2). 修改 app/config/app.php 添加 ServiceProvider: // 将下面代码添加到 pro ...
- SDUT-3375_数据结构实验之查找三:树的种类统计
数据结构实验之查找三:树的种类统计 Time Limit: 400 ms Memory Limit: 65536 KiB Problem Description 随着卫星成像技术的应用,自然资源研究机 ...