Variables and Arithmetic Expression
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的更多相关文章
- [UCSD白板题] Maximize the Value of an Arithmetic Expression
Problem Introduction In the problem, your goal is to add parentheses to a given arithmetic expressio ...
- 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 ...
- hihocoder Arithmetic Expression【在线查询】
Arithmetic Expression 时间限制:2000ms 单点时限:200ms 内存限制:256MB 描述 Given N arithmetic expressions, can you ...
- ACM Arithmetic Expression
Description Given N arithmetic expressions, can you tell whose result is closest to 9? Input Line 1: ...
- Arithmetic Expression
时间限制:2000ms 单点时限:200ms 内存限制:256MB 描述 Given N arithmetic expressions, can you tell whose result is cl ...
- 【微软编程一小时】题目1 : Arithmetic Expression
时间限制:2000ms 单点时限:200ms 内存限制:256MB 描写叙述 Given N arithmetic expressions, can you tell whose result is ...
- 【Codeforces 115D】Unambiguous Arithmetic Expression
Codeforces 115 D 题意:给一个没有括号的表达式,问有多少种添加括号的方法使得这是一个合法的表达式?输入可能有正负号.加减乘除.数字. 思路1: 这是不能过的\(naive\)的\(dp ...
- Accessing Scoped Variables
To permit the JSP page to access the data, the servlet needs to use setAttribute to store the data i ...
- [Erlang 0118] Erlang 杂记 V
我在知乎回答问题不多,这个问题: "对你职业生涯帮助最大的习惯是什么?它是如何帮助你的?",我还是主动回答了一下. 做笔记 一开始笔记软件做的不好的时候就发邮件给自己, ...
随机推荐
- 渲染函数render和函数式组件
vnode对象 vnode对象包括(vnode并不是vue实例,而是vue实例中渲染函数render执行后生成的结果) this.tag = tag // 当前节点标签名 this.data = da ...
- tomcat 部署swagger 请求到后端乱码
问题: @ApiOperation(value = "", notes = "查看关键词列表") @ResponseBody @RequestMapping(v ...
- Django之前后端交互使用ajax的方式
1. 在项目中前后端数据相互是一种常态, 前后端交互使用的是ajax请求和form表单的请求两种方式" ajax与form表单的区别在于: form 是整个页面刷新提交的, 但是ajax ...
- python爬虫之解析库Beautiful Soup
为何要用Beautiful Soup Beautiful Soup是一个可以从HTML或XML文件中提取数据的Python库.它能够通过你喜欢的转换器实现惯用的文档导航,查找,修改文档的方式, 是一个 ...
- js实现图片上传预览功能,使用base64编码来实现
实现图片上传的方法有很多,这里我们介绍比较简单的一种,使用base64对图片信息进行编码,然后直接将图片的base64信息存到数据库. 但是对于系统中需要上传的图片较多时并不建议采用这种方式,我们一般 ...
- es6入门set和map
ES6提供了新的数据结构Set.它类似于数组,但是成员的值都是唯一的,没有重复的值. Set函数可以接受一个数组(或类似数组的对象)作为参数,用来初始化. var set = new Set([1, ...
- vi 操作
vi 3种模式:1命令行模式,2插入模式,3末行模式 Vim启动后直接进入的是命令行模式,命令行模式顾名思义就是可以输入各种命令的意思, 1. 命令行下按i键进入“插入模式”,命令行下按:键进入“末行 ...
- 【JAVA】杨辉三角
ソース public Yanghui3jiao() { List<String[]> rowList = new ArrayList<String[]>(); List< ...
- ucore-lab1-练习5report
实验5--实现函数调用堆栈跟踪函数 需要完成kdebug.c中函数print_stackframe的实现,可以通过函数print_stackframe来跟踪函数调用堆栈中记录的返回地址. 一.函数堆栈 ...
- React Context(一):隐式传递数据
一 Context概述 Context provides a way to pass data through the component tree without having to pass pr ...