c_水程序
*
** This program reads input lines from the standard input and prints
** each input line, followed by just some portions of the lines, to
** the standard output.
**
** The first input is a list of column numbers, which ends with a
** negative number. The column numbers are paired and specify
** ranges of columns from the input line that are to be printed.
** For example, - indicates that only columns through
** and columns through will be printed.
*/ #include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define MAX_COLS 20 /* max # of columns to process */
#define MAX_INPUT 1000 /* max len of input & output lines */ int read_column_numbers( int columns[], int max );
void rearrange( char *output, char const *input,
int n_columns, int const columns[] ); int
main( void )
{
int n_columns; /* # of columns to process */
int columns[MAX_COLS]; /* the columns to process */
char input[MAX_INPUT]; /* array for input line */
char output[MAX_INPUT]; /* array for output line */ /*
** Read the list of column numbers
*/
n_columns = read_column_numbers( columns, MAX_COLS ); /*
** Read, process and print the remaining lines of input.
*/
while( gets( input ) != NULL ){
printf( "Original input : %s\n", input );
rearrange( output, input, n_columns, columns );
printf( "Rearranged line: %s\n", output );
} return EXIT_SUCCESS;
} *
** Read the list of column numbers, ignoring any beyond the specified
** maximum.
*/
int
read_column_numbers( int columns[], int max )
{
int num = ;
int ch; /*
** Get the numbers, stopping at eof or when a number is < 0.
*/
while( num < max && scanf( "%d", &columns[num] ) ==
&& columns[num] >= )
num += ; /*
** Make sure we have an even number of inputs, as they are
** supposed to be paired.
*/
if( num % != ){
puts( "Last column number is not paired." );
exit( EXIT_FAILURE );
} /*
** Discard the rest of the line that contained the final
** number.
*/
while( (ch = getchar()) != EOF && ch != '\n' )
; return num;
} *
** Process a line of input by concatenating the characters from
** the indicated columns. The output line is then NUL terminated.
*/
void
rearrange( char *output, char const *input,
int n_columns, int const columns[] )
{
int col; /* subscript for columns array */
int output_col; /* output column counter */
int len; /* length of input line */ len = strlen( input );
output_col = ; /*
** Process each pair of column numbers.
*/
for( col = ; col < n_columns; col += ){
int nchars = columns[col + ] - columns[col] + ; /*
** If the input line isn't this long or the output
** array is full, we're done.
*/
if( columns[col] >= len ||
output_col == MAX_INPUT - )
break; /*
** If there isn't room in the output array, only copy
** what will fit.
*/
if( output_col + nchars > MAX_INPUT - )
nchars = MAX_INPUT - output_col - ; /*
** Copy the relevant data.
*/
strncpy( output + output_col, input + columns[col],
nchars );
output_col += nchars;
} output[output_col] = '\0';
}
1:首先关注程序的注释,我们在写程序的时候通常会加入很多注释,我们将其注释掉,并不到代表着我们将他从源代码中移除了,只不过是不起作用了而已,如果注释嵌套注释,可能还会出现问题,要从逻辑上删除一段c代码,最好使用#if
eg:
#if 0
statement
#endif这样我们的if条件表达式默认为false,之间的程序段可以有效的从程序中移除了
2:
预处理命令
我们上面代码的前5行都是预处理命令,他们都是由预处理器解释的,预处理器负责读入源代码,咱们我们将经过预处理后的代码递交给编译器,
stdio。h头文件使我们可以访问标准I/O库,
stdlib.h定义了EXIT_SUCCESS和EXIT_FAILURE
3:
gets函数负责从标准输入读取一行文本并将它传递到他的参数中,每一行字符串以一个换行符结尾,gets函数具有自动丢弃换行符,并在末尾自动加上一个NULL
的功能,当gets函数被调用但是并不存在输入行的时候,表示达到了输入的末尾,返回NULL
string 类型不存在与c语言,这一点不要同c++与java弄混
NULL是在stdio头文件中定义的
4:
在函数声明的数组传参中,我们对于数组的定义并未指定其长度,数组在接受传参的时候,无论传递给他的有多长,这个函数都照收不误,这是伟大的特性,但是我门不知道数组的长度,如果我们想知道数组的长度,我们需要额外再定义一个变量专门传递数组的长度
5:标准并未硬性规定c编译器对数组下标进行有效的检查,所以我们在程序中有的时候需要特别判断一下数组的越界与否的情况,如果在没判断的情况下超出了范围,那么多出来的数就会存放在数组紧随其后的位置,这样就占用了野地址,极有可能是其他变量的地址,
6:
scanf()函数的一些特性,我们在按照格式化读取变量的时候,如果按照%d读取的话,我们每次都是读取一个十进制的数,如果转换失败,函数都会返回0,这样return回去就会使得整个程序终止,如果可以合法的转化过去,这个数就会合理的转化为二进制存储在数组中,然后,scanf函数返回1,
7:puts()函数是gets函数的对应函数,puts具有自动加换行的功能
8:
exit_FAILURE这个值是被返回给操作系统,提示出现了错误
9:
78 while( (ch = getchar()) != EOF && ch != '\n' )
79 ;
80
这段代码中我们申明了ch为int类型,为什么为int类型是因为EOF为-1为int类型,这事在stdio中北定义的
因为char类型其实也是就是小的整数,所以这里转化的时候不会出现错误
while后面加上一个分号称为空语句,当我们不需要循环中实现什么操作的时候,我们可以这么是实现
10:
rearrange函数
当数组名作为实参的时候,传递给函数的其实是一个只想首地址的指针,也就是数组在内存中的位置,正是因为我们传的只是一个地址而不是一份拷贝,所以我们在对形参进行操作的时候就会对主函数中的数组的变化产生影响,如果我们不希望产生这种影响,我们可以讲形参定义为const类型,
const类型有两个功能:1)声明的意图就是这个参数中的值不能改变
2)他导致编译器去验证是否违背了原则
11:
注意在void类型的函数中,我们不添加结束标志的程序return;并不表示没有,程序在执行结束之后会执行一条隐式的return;
c_水程序的更多相关文章
- 陕西中际现代:基于自适应算法的PLC滴灌控制系统
基于自适应算法的PLC滴灌控制系统 陕西中际现代包装科技有限公司滴灌部 1.介绍 水资源正在成为一种珍贵的资源.城镇的市民使用成千上万立方的水来浇灌花园和绿地.他们依赖于使用固定灌溉计划的控制器.而这 ...
- 网站下载器WebZip、Httrack及AWWWB.COM网站克隆器
动机 闲扯节点,可略读. 下载并试用这些软件并非是为了一己之私,模仿他人网站以图利.鉴于国内网络环境之艰苦,我等屌丝级半罐水程序员,纵有百度如诸葛大神万般协力相助,也似后主般无能不能解决工作和娱乐中 ...
- 三分钟快速上手TensorFlow 2.0 (后续)——扩展和附录
TensorFlow Hub 模型复用 TF Hub 网站 打开主页 https://tfhub.dev/ ,在左侧有 Text.Image.Video 和 Publishers 等选项,可以选取关注 ...
- 微信小程序--试水
应公司需求,接手小程序,在此之前我是一点也没有接触过,对此,拿过小程序文档和官方案例就一顿恶补,在此期间也看过一些小程序建立模型的视频,终于对小程序知晓一二,拿过项目开始研究.好了废话不多说,总结一下 ...
- C_使用clock()函数获取程序执行时间
clock():捕捉从程序开始运行到clock()被调用时所耗费的时间.这个时间单位是clock tick ,即“时钟打点”. 常数CLK_TCK:机器时钟每秒所走的时钟打点数. #include & ...
- 微信小程序自定义数据分析试水
昨晚收到小程序自定义分析的内测邀请,简单试用了一下.说明挺长的,大概是这个意思: 一.定义一系列事件,对其进行统计 事件可以对页面中的这些事件进行追踪 click enterPage leavePag ...
- uva 110 Meta-Loopless Sorts 用程序写程序 有点复杂的回溯水题
题目要求写一个直接用比较排序的pascal程序,挺有趣的一题. 我看题目数据范围就到8,本来以为贪个小便宜,用switch输出. 然后发现比较次数是阶乘级别的,8的阶乘也是挺大的,恐怕会交不上去. 于 ...
- [水煮 ASP.NET Web API2 方法论](1-1)在MVC 应用程序中添加 ASP.NET Web API
问题 怎么样将 Asp.Net Web Api 加入到现有的 Asp.Net MVC 项目中 解决方案 在 Visual Studio 2012 中就已经把 Asp.Net Web Api 自动地整合 ...
- [水煮 ASP.NET Web API2 方法论](1-2)在 WebForm 应用程序中添加 ASP.NET Web API
问题 怎么样将 Asp.Net Web Api 加入到 Asp.Net Web From 应用程序中 解决方案 在 Visual Studio 2013 中,创建新的 Web From,可以直接在&q ...
随机推荐
- Runner站立会议01
开会时间:9.10-9.30 地点:二教 今天做了什么:学了文本输入,button按钮,界面转换(意图) 遇到什么困难:界面转换时,出现问题,没有正常跳转 明天打所作什么:解决今天遇到的问题,学了第二 ...
- Java——标签组件:JLabel
使用一个标签 import java.awt.Color; import java.awt.Dimension; import java.awt.Point; import javax.swing.J ...
- 注释(Annotation)
J2SE 5.0提供了很多新的特征.其中一个很重要的特征就是对元数据(Metadata)的支持.在J2SE 5.0中,这种元数据称为注释(Annotation).通过使用注释,程序开发人员可以在不改变 ...
- 云服务程序在启动的时候执行Powershell脚本
如果在云服务程序启动时候,需要执行Powershell脚本,我们需要将脚本嵌入到程序中,并且编写一个cmd来执行这个脚本,具体如下: 1.编写测试的Powershell脚本:每隔10分钟 检测dns ...
- 设计模式学习——观察者模式(Observer Pattern)
0. 前言 观察者模式在许多地方都能够用到,特别是作为MVC模式的一部分,在MVC中,模型(M):存放数据,视图(V):显示数据.当模型中的数据发生改变时,视图会得到通知,这是典型的观察者模式. 1. ...
- WAMPSERVER 64位 win7下 php 5.5.12通过 PECL 安装 zip扩展
通过phpinfo()查看 php5.5.12默认集成了zip模块 不过版本是1.11.0 http://pecl.php.net/package/zip 通过PECL 查看 最新版本是1.12.4 ...
- LevelDB库简介
LevelDB库简介 一.LevelDB入门 LevelDB是Google开源的持久化KV单机数据库,具有很高的随机写,顺序读/写性能,但是随机读的性能很一般,也就是说,LevelDB很适合应用在查询 ...
- 程序日志输出实现-Log4j
学习开发的过程中,我们都应该用过System.out.println():来做一些调试工作,有时候确实很有用有没有.但是这种简单粗暴的方式让程序中到处存在着sysout.这种方式难免会有性能的影响,维 ...
- Orchard源码分析(4.4):Orchard.Caching.CacheModule类
概述 CacheModule也是一个Autofac模块. 一.CacheModule类 CacheModule将DefaultCacheManager注册为ICacheManager: ...
- Google地图实现
API地址:https://developers.google.com/maps/documentation/javascript/tutorial <div id="map" ...