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
我在知乎回答问题不多,这个问题: "对你职业生涯帮助最大的习惯是什么?它是如何帮助你的?",我还是主动回答了一下. 做笔记 一开始笔记软件做的不好的时候就发邮件给自己, ...
随机推荐
- python至winreg模块
_winreg模块在python3中已经改名了 https://blog.csdn.net/zhangxiaoyang0/article/details/72236305?fps=1&loca ...
- BOUNDARIES AND SPACE
BOUNDARIES AND SPACE Review Nice work! You've learned a lot. Let's review the web and CSS concepts c ...
- C# 数值的隐式转换
Debug2.Log(5/8.0f, 5.0f/8, 5/8);//output:0.625, 0.625, 0 隐式数值转换表
- 机器学习中的算法(2)-支持向量机(SVM)基础
版权声明:本文由LeftNotEasy发布于http://leftnoteasy.cnblogs.com, 本文可以被全部的转载或者部分使用,但请注明出处,如果有问题,请联系wheeleast@gma ...
- python学习Day2 python 、pycharm安装及环境变量配置
复习 进制转换:二进制&十六进制转换(从左往右1248机制,每四位二进制对应一位16进制) 二进制&十进制转换 2n-1幂次方相加 十进制到二进制转化 将十进制除以2,把余数记下 ...
- 一个JavaBean和DTO转换的优秀案例
所有的JavaBean和DTO的互相转换接口,JavaBean转化成DTO或者DTO转换JavaBean: public abstract class Converter<A, B> { ...
- 【Django】重定向
view函数中使用重定向方法 return HttpResponseRedirect('redir2.html')的时候不自觉的在前面加了request参数,结果报错: TypeError at /b ...
- jstl标准标签库 常用标签
JSTL(JSP Standard Tag Library)标准标签库: 1, 核心标签(最常用, 最重要的) 表达式控制标签 out 输出常量 value---直接赋值 输出变量 default-- ...
- Eclipse 创建Android 模拟器失败:no cpu/abi system image available for this target
(从网上搜了一个使用Android 4.4 API 20编译的图片) 这是因为SDK中没有模拟器使用的操作系统镜像. 如果项目使用API 19编译,则SDK中的system-images文件夹下,需要 ...
- 【python深入】collections-Counter使用总结
关于collections的使用,首先介绍:Counter的使用 需要执行:from collections import Counter 在很多使用到dict和次数的场景下,Python中用Coun ...