Notes from The C Programming Language

A decimal point in a constant indicates that it is floating point, however, so $5.0/9.0$ i not truncated because it is the ratio of two floating-point values.

printf specification

  • %3.0f says that a floating-point number is to be printed at least three characters wide, with no decimal point and no fraction dgits.
  • %6.1f describes another number that is to be printed at least six characters wide, with 1 digit after the decimal point.

Width and precision may be omitted from a specification: %6f says that the number is to be at least six characters wide; %.2f specifies two characters after the decimal point, but the width is not constrained.

  • %o for octal
  • %x for hexadecimal
  • %c for character
  • %s for character string
  • %% for % itself

for statement:

#include <stdio.h>

#define LOWER 0			/* lower limit of table */
#define UPPER 300 /* upper limit */
#define STEP 20 /* step size */ /* print Fahrenheit-Celsius table */
main()
{
int fahr; for(fahr = LOWER; fahr <= UPPER; fahr = fahr + STEP)
printf("%3d %6.1f\n", fahr, (5.0/9.0)*(fahr-32));
}

Character input and output

The standard library provides getchar() and putchar() for reading and writing one character at a time. getchar() reads the next input character from a text stream and returns that as its value:

c = getchar();

The function putchar prints a character each time:

putchar(c);

prints the contents of the integer variable c as a character, usually on the screen.

File copy: a program that copies its input to its output one character at a time:

#include <stdio.h>

/* copy input to output; 1st version */
main()
{
int c; c = getchar();
while(c != EOF)
{
putchar(c);
c = getchar();
}
}

EOF is defined in <stdio.h>.

As an expression has a value, which is the left hand side after the assignment, the code can be concise:

#include <stdio.h>

/* copy input to output; 2nd version */
main()
{
int c; while((c = getchar()) != EOF)
putchar(c);
}

The next program counts characters:

#include <stdio.h>

/* count characters in input; 1st version */
main()
{
long nc; nc = 0;
while(getchar() != EOF)
++nc; printf("%ld\n", nc);
}

long integers are at least 32-bits.

It may be possible to cope with even bigger numbers by using a double(double precision float).

#include <stdio.h>

/* count characters in input; 2nd version */
main()
{
double nc; for(nc = 0; getchar() != EOF; ++nc)
;
printf("%.0f\n", nc);
}

printf uses %f for both float and double; %.0f suppresses printing of the decimal point and the fraction part, which is zero.

Counts lines:

#include <stdio.h>

/* count lines in input */
main()
{
int c, nl; nl = 0;
while((c = getchar()) != EOF)
{
if(c == '\n')
++n1;
printf("%d\n", nl);
}
}

Word counting with loose definition that a word is any sequence of characters that does not contain blank, tab or newline. This is a bare-bones version of the UNIX program wc:

#include <stdio.h>

#define IN  1	/* inside a word */
#define OUT 0 /* outside a word*/ /* count lines, words, and characters in input*/
main()
{
int c, nl, nw, nc, state; state = OUT;
nl = nw = nc = 0;
while((c = getchar()) != EOF)
{
++nc;
if(c == '\n')
++nl;
if(c == ' ' || c == '\n' || c == '\t')
state = OUT;
else if(state == OUT)
{
state = IN;
++nw;
}
}
printf("%d %d %d\n", nl, nw, nc);
}

Every time the program encouters the first character of a word, it counts one more word.

Count the number of occurrences of each digit, of white space character(blank, tab, newline), and of all other characters:

#include <stdio.h>

/* count digits, white space, others */
main()
{
int c, i, nwhite, nother;
int ndigit[10]; nwhite = nother = 0;
for(i = 0; i < 10; ++i)
ndigit[i] = 0; while((c = getchar()) != EOF)
{
if(c >= '0' && c <= '9')
++ndigit[c - '0'];
else if(c == ' ' || c == '\n' || c == '\t')
++nwhite;
else
++nother; printf("digits =");
for(i = 0; i < 10; ++i)
printf(" %d", ndigit[i]);
printf(", white space = %d, other = %d\n", nwhite, nother);
}
}

Variables and Arithmetic Expression的更多相关文章

  1. [UCSD白板题] Maximize the Value of an Arithmetic Expression

    Problem Introduction In the problem, your goal is to add parentheses to a given arithmetic expressio ...

  2. leetcode-Evaluate the value of an arithmetic expression in Reverse Polish Notation

    leetcode 逆波兰式求解 Evaluate the value of an arithmetic expression in Reverse Polish Notation. Valid ope ...

  3. hihocoder Arithmetic Expression【在线查询】

    Arithmetic Expression   时间限制:2000ms 单点时限:200ms 内存限制:256MB 描述 Given N arithmetic expressions, can you ...

  4. ACM Arithmetic Expression

    Description Given N arithmetic expressions, can you tell whose result is closest to 9? Input Line 1: ...

  5. Arithmetic Expression

    时间限制:2000ms 单点时限:200ms 内存限制:256MB 描述 Given N arithmetic expressions, can you tell whose result is cl ...

  6. 【微软编程一小时】题目1 : Arithmetic Expression

    时间限制:2000ms 单点时限:200ms 内存限制:256MB 描写叙述 Given N arithmetic expressions, can you tell whose result is ...

  7. 【Codeforces 115D】Unambiguous Arithmetic Expression

    Codeforces 115 D 题意:给一个没有括号的表达式,问有多少种添加括号的方法使得这是一个合法的表达式?输入可能有正负号.加减乘除.数字. 思路1: 这是不能过的\(naive\)的\(dp ...

  8. Accessing Scoped Variables

    To permit the JSP page to access the data, the servlet needs to use setAttribute to store the data i ...

  9. [Erlang 0118] Erlang 杂记 V

       我在知乎回答问题不多,这个问题: "对你职业生涯帮助最大的习惯是什么?它是如何帮助你的?",我还是主动回答了一下.    做笔记 一开始笔记软件做的不好的时候就发邮件给自己, ...

随机推荐

  1. PHPsocket、CURL、File_get_contents采集

    1.socket采集.采用最底层的,它只是建立一个长连接,然后我们自己构造http协议字符串去发送请求.例如想获取这个页面内容(http://tv.youku.com/?spm=a2hww.20023 ...

  2. Halcon除法

    今天,用到了Halcon 的除法.求出两个region的面积,area1,area2.我想求出它们的比值area1/area2.但是发现比值是整数,没有保留小数.应该改为这样area1/real(ar ...

  3. ES6语法的数组查询

    setProductId(param){ console.log(param); let prod = this.products.find(item =>{ return item.prodC ...

  4. 基于PaddlePaddle的语义匹配模型DAM,让聊天机器人实现完美回复 |

    来源商业新知网,原标题:让聊天机器人完美回复 | 基于PaddlePaddle的语义匹配模型DAM 语义匹配 语义匹配是NLP的一项重要应用.无论是问答系统.对话系统还是智能客服,都可以认为是问题和回 ...

  5. cdnbest获取,删除,增加,修改域名列表,高级设置api示例

    <?php $uid = 28; $vhost = 'asdfw'; $token = getToken($uid, $vhost); print_r($token); //获取token fu ...

  6. 十四、ChainOfResponsibility 责任链模式

    设计: 代码清单: Trouble: public class Trouble { private int number; public Trouble(int number){ this.numbe ...

  7. KBEngine 编译出现 MSB802 无法找到v140的生成工具

    我用的vs版本是vs2017professional版本,并未安装所有的工具 在编译kbengine源码时候出现 MSB802 无法找到v140的生成工具错误 修复办法在菜单栏选择  工具--> ...

  8. python --数据可视化(一)

    python --数据可视化 一.python -- pyecharts库的使用 pyecharts--> 生成Echarts图标的类库 1.安装: pip install pyecharts ...

  9. 容器101:Docker基础

    Docker如此受欢迎的一个原因是它提供了“一次开发,随处运行”的承诺.Docker提供了一种将应用程序及其运行时依赖性打包到单个容器中的简单方法.它还提供了一个运行时抽象,使容器能够跨不同版本的Li ...

  10. [leetcode]91. Decode Ways解码方法

    A message containing letters from A-Z is being encoded to numbers using the following mapping: 'A' - ...