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 ...
随机推荐
- Ubuntu PostgreSQL安装和配置
一.安装 1.安装 使用如下命令,会自动安装最新版,这里为9.5 sudo apt-get install postgresql 安装完成后,默认会: (1)创建名为"postgres&qu ...
- winSocket数据传输
服务器端: #include <WINSOCK2.H> #include <stdio.h> #pragma comment(lib,"ws2_32.lib" ...
- JQM弹出对话框
<div data-role="page" id="pageone"> <div data-role="header"&g ...
- jquery.cookie使用方法
jquery.cookie 使用方法 一个轻量级的 cookie 插件,可以读取.写入.删除 cookie . jquery.cookie.js 的配置 首先包含 jQuery 的库文件,在后面包含 ...
- How to get http response.
public class HttpWebResponseUtility { public static string CreateGetHttpResponse(string url) { var r ...
- Semantic ui 学习笔记 持续更新
这个semantic 更新版本好快~ 首先是代码的标识<code></code> 具体样式就是红框这样的 圈起来代码感觉不错 不过要在semantic.css里在加上如下样式~ ...
- Java多线程编程核心技术---单例模式与多线程
立即加载/饿汉模式 立即加载就是使用类的时候已经将对象创建完毕. public class MyObject { //立即加载方式==饿汉模式 private static MyObject myOb ...
- C# Emit动态代理生成一个实体对象
/// <summary> /// 使用Emit动态代理收集实体信息 /// </summary> /// <typeparam name="T"&g ...
- jsp应用
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding= ...
- 真正理解linux的inode?
linux 在整个架构上可以看作是三层: 1.底层代码, (引导层strip) 跟硬件沟通的那一层的代码(可能是汇编+c), 驱动底层的; strain: n./v. 拉紧, 张力, 气质, 风格, ...