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 ...
随机推荐
- python 索引上的合并
- 计蒜客 Red Black Tree(树形DP)
You are given a rooted tree with n nodes. The nodes are numbered 1..n. The root is node 1, and m of ...
- NOIP模拟17.10.12
T1 临江仙 旧梦 题目背景 闻道故园花陌,今年奼紫嫣红.扬帆直渡水千重.东君何解意,送我一江风. 还是昔时庭院,终得醉卧花丛.残更惊醒月明中.流光如旧岁,多少梦成空. 题目描述 #define go ...
- 【洛谷P2907】 【USACO08OPEN】农场周围的道路 水模拟分治
P2907 [USACO08OPEN]农场周围的道路Roads Around The Farm 题目描述 Farmer John's cows have taken an interest in ex ...
- 【cml】wosi-demo
推荐一个cml项目,https://github.com/Bowen7/wosi-demo 呃呃,运行了项目只能够说开发者好牛逼,数据处理很厉害, 这个是最后的结果,然后要进行效果查看,估计你得等明天 ...
- 7. 18 test 砍树题解
(题面保密,内部人员可览) 首先观察题面,可得出如下公式 ∑(ceil(a[i] /d)*d−a[i])≤k 其中,ceil(a[i] /d)表示在需要被砍伐之前所经过的轮数,ceil函数是为了保证一 ...
- vue 根据数组中某一项的值进行排序
一.前言 我在vue项目中遇到了一个表格排序的需求,根据某一项的值的大小从大到小调整数组顺序. 二.代码 表格大概是这个样子,样式和图片在代码中简化了. <table class="r ...
- [case49]聊聊flink的checkpoint配置
序 本文主要研究下flink的checkpoint配置 实例 StreamExecutionEnvironment env = StreamExecutionEnvironment.getExecut ...
- 足迹地图 搜索jvectormap
https://blog.wangjunfeng.com/foot_print/
- Ceph 之Multisite 下的bucket reshard
目录 一.背景和问题 二.bucket reshard 过程 主集群信息汇总 Multisite 下手动reshard References 一.背景和问题 默认情况下只有当单个bucket承载的ob ...