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
我在知乎回答问题不多,这个问题: "对你职业生涯帮助最大的习惯是什么?它是如何帮助你的?",我还是主动回答了一下. 做笔记 一开始笔记软件做的不好的时候就发邮件给自己, ...
随机推荐
- Dubbo注册Zookepper服务的虚拟IP
使用dubbo在zookepper上注册服务,使用dubbo的服务器IP为192.168.70.105 而在zookepper上显示服务提供者为 dubbo://202.102.110.203:808 ...
- 判断是否某App
<!DOCTYPE html><html> <head> <meta charset="UTF-8"> <meta name= ...
- React琐碎
1 dangerouslySetInnerHTML 使用此属性的原因是得到一个安全的数据,生成一个仅包含唯一key——__html的对象,表明这段内容会被当成text/taint使用,它的使用方式是这 ...
- 【395】yield 和 yield from
yield:生成器 yield from:将生成器 yield 的内容相当于逐一在 yield 一般 参考:Python 3: Using "yield from" in Gene ...
- 自然语言处理的CNN模型中几种常见的池化方法
自然语言处理的CNN模型中几种常见的池化方法 本文是在[1]的基础上进行的二次归纳. 0x00 池化(pooling)的作用 首先,回顾一下NLP中基本的CNN模型的卷积和池化的大致原理[2].f ...
- week07 13.2 NewsPipeline之 二 News Fetcher - Xpath
我们使用Xpath来专门做一个scrapter 我们专门弄个文件夹 里面全部是 各个新闻源(CNN BBC等)的scraper来抓取网站的text内容 主要函数(就是传入text内容的那个url)然后 ...
- [Ting's笔记Day8]活用套件carrierwave gem:(3)Deploy图片上传功能到Heroku网站
前情提要: 身为Ruby新手村民,创造稳定且持续的学习步调很重要,我用的方法就是一周在IT邦写三篇笔记,希望藉由把笔记和遇到的bug记录下来的过程,能帮助到未来想用Ruby on Rails架站的新手 ...
- Django+Vue.js框架快速搭建web项目
一.vue环境搭建1.下载安装node.js.2.安装淘宝镜像cnpm,在命令窗口输入: npm install -g cnpm --registry=https://registry.npm.tao ...
- jqGrid的userData的用法!!!
在一次项目中想从后台自定义一些返回值传回jqGrid,所以就想到了jqGrid的这个userData属性,但是真的是坑了我好惨,这里记录一下! 1.首先看说明,这个jsonReader的默认配置,us ...
- FortiGate 5.2/5.4 SSLVPN建立
1.定义源IP池 即用户通过sslvpn拨号成功后获取到的IP地址. 2.定义路由地址 即用户通过sslvpn拨号成功后允许获取到的路由表. 3.建立sslvpn portal 4.定义用户和用户组 ...