首页 技术 正文
技术 2022年11月9日
0 收藏 887 点赞 2,569 浏览 2127 个字
  1. 打印摄氏度
/* 1.1 使用int类型进行计算 */
#include <stdio.h>/* print Fahrenheit-Celsius table
for fahr = 0, 20, ...., 300 */
main()
{
int fahr, celsius;
int lower, upper, step; lower = 0; /* lower limit of temperature table */
upper = 300; /* upper limit */
step = 20; /* step size */ fahr = lower;
while(fahr <= upper) {
celsius = 5 * (fahr - 32) / 9;
printf("%d\t%d\n", fahr, celsius); // printf(""%3d %6d\n", fahr, celsius); /* right-justified */ fahr = fahr + step;
}
}/* 1.2 使用 float 类型进行计算 */
#include <stdio.h>main()
{
float fahr, celsius;
int lower, upper, step; lower = 0;
upper = 300;
step = 20; fahr = lower;
while(fahr <= upper) {
celsius = (5.0 / 9.0) * (fahr - 32.0);
// fahr 3个字符宽度,无小数部分; celsius 6个字符宽度,额外包括一个小数位
printf("%3.0f %6.1f\n", fahr, celsius);
fahr = fahr + step;
}
}/* 1.3 使用for循环语句 */
#include <stdio.h>main()
{
int fahr;
for(fahr = 0; fahr <= 300; fahr = fahr + 20) {
printf("%3d %6.1f\n", fahr, (5.0 / 9.0)*(fahr - 32));
}
}/* 1.4 符号常量的应用
因为单纯的数字(如300,20),表达的意义非常模糊,不易于阅读 */
#include <stdio.h>// 定义符号常量
#define LOWER 0
#define UPPER 300
#define STEP 20main()
{
int fahr; for(fahr = LOWER; fahr <= UPPER; fahr = fahr + STEP) {
printf("%3d %6.1f\n", fahr, (5.0 / 9.0) * (fahr - 32));
}
}
  1. 数据类型:

    • 基本数据类型:

      • int
      • short
      • long
      • float
      • double
      • char
      • arrays
      • structures
      • unions
      • pointers
    • printf()说明:

      • %d: 十进制整数(decimal)
      • %f: floating point
      • %o: octal(八进制)
      • %x: hexadecimal(十六进制)
      • %c: character
      • %s: string
      • %%: %
  2. 字符串的处理
/* file copying (version 1) */
#include <stdio.h>main()
{
int c; while((c = getchar()) != EOF)
putchar(c);
}/* character counting */
#include <stdio.h>main()
{
long nc; for(nc = 0; getchar() != EOF; ++nc)
;
printf("%.0f\n", nc);
}/* line counting */
#include <stdio.h>main()
{
int c, nl; nl = 0;
while((c = getchar()) != EOF)
if (c == '\n')
++nl;
printf("%d\n", nl);
}/* 1-9:
Write a program to copy its input to its output, replacing each string of one or more
blanks by a single blank */ /* version 1*/
#include <stdio.h> #define NONBLANK '-' int main(void)
{
int c, lastc; lastc = NONBLANK; while((c = getchar()) != EOF) {
if(c == ' ') {
if(lastc != ' ') {
putchar(c);
}
} else {
putchar(c);
}
lastc = c;
}
return 0;
}/* a bare-bones version of the UNIX program wc */
#include <stdio.h>#define IN 1
#define OUT 0main()
{
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);
}

参考资料:

The C Programming Language

相关推荐
python开发_常用的python模块及安装方法
adodb:我们领导推荐的数据库连接组件bsddb3:BerkeleyDB的连接组件Cheetah-1.0:我比较喜欢这个版本的cheeta…
日期:2022-11-24 点赞:878 阅读:9,088
Educational Codeforces Round 11 C. Hard Process 二分
C. Hard Process题目连接:http://www.codeforces.com/contest/660/problem/CDes…
日期:2022-11-24 点赞:807 阅读:5,564
下载Ubuntn 17.04 内核源代码
zengkefu@server1:/usr/src$ uname -aLinux server1 4.10.0-19-generic #21…
日期:2022-11-24 点赞:569 阅读:6,412
可用Active Desktop Calendar V7.86 注册码序列号
可用Active Desktop Calendar V7.86 注册码序列号Name: www.greendown.cn Code: &nb…
日期:2022-11-24 点赞:733 阅读:6,185
Android调用系统相机、自定义相机、处理大图片
Android调用系统相机和自定义相机实例本博文主要是介绍了android上使用相机进行拍照并显示的两种方式,并且由于涉及到要把拍到的照片显…
日期:2022-11-24 点赞:512 阅读:7,822
Struts的使用
一、Struts2的获取  Struts的官方网站为:http://struts.apache.org/  下载完Struts2的jar包,…
日期:2022-11-24 点赞:671 阅读:4,905