C程序设计语言 (K&R)

摘要:
在300处,分别打印华氏温度和摄氏温度的比较表*/intmain(){intfahr,Celsius;lower,upper,step;lower=0;/*温度计下限*/upper=300;/*温度上限*/step=20;/*步长*/fahr=lower;而{Celsius=5*/9;printf;fahr=fahr+step;}return0;}ViewCode#include/*print华氏摄氏度=0,20,…,300;浮点版本*/intmain(){floatfahr,celsius;floatlower,upper,step;lower=0;/*lowerlimitftemperaturescale*/upper=300;/*上限*/step=20;/*步长*/fahr=lower;而{celsius=*;printf;fahr=fahr+step;}return0;}ViewCode#include/*print华氏摄氏度=0,20,…,300;浮点版本*/intmain(){intfahr;forprintf;return0;}ViewCode#include #defineLOWER0/*下限值表*/#defineUPPER300/*上限值*/#defineSTEP20/*步长*/*打印华氏摄氏度表*/intmain(){intfahr;forprintf;return0;}ViewCode 2.文件复制、getchar和putchar函数#include/*copyinputtooutput;1stversion*/intmain(){intc;c=getchar();而(c!=EOF)putchar;return0;}ViewCode 3.字符数。Double还可以自动递增#include<stdio h>/*countcharactersininput;1stversion*/intmain(){longnc;nc=0;while(getchar()!=EOF){++nc;if++nl;ifstate=OUT;elseif{state=IN;++nw;}}printf;return0;}ViewCode 6.统计信息、空白字符和其他字符。使用数组统计数据#include<studio h>/*countdigits,whites*/intmain(){intc,i,nwhite,otherer;intndigit[10];nwhite=otherer=0;forndigit[i]=0;while((c=getchar())!=EOF)if++ndigit[c-'0'];/*countdigits*/elseif++nwhite;elseif++otherer;printf;forprintf;return0;}ViewCode 7.幂函数幂(m,n)调用#include intpower/*testpowerfunction*/intmain(){inti;for(

  The C Programming Language,C程序设计语言 (K&R),为C语言的设计者Dennis M. Ritchie和著名的计算机科学家Brian W.Kernighan合著的 一本介绍C语言的权威经典著作,学习c语言至今,第一次读这本书,这本书适合有一定的c语言基础的深入学习

  为什么说不适合初学者,觉得这本书更像一本字典,比如在函数章节,举例代码中用到了下面章节的数组,用到了数据结构的栈知识,为了学习大师的代码,在读这本书的时候,书中引用的代码,一一记录如下

  第一章 导言

  1、华氏温度与摄氏温度对照表

C程序设计语言 (K&R)第1张C程序设计语言 (K&R)第2张
#include <stdio.h>
/* 当 fahr=0,20,… ,300 时,分别
打印华氏温度与摄氏温度对照表 */
int main()
{
    int fahr, celsius;
    int lower, upper, step;
    lower = 0; /* 温度表的下限 */
    upper = 300; /* 温度表的上限 */
    step = 20; /* 步长 */
    fahr = lower;
    while (fahr <= upper) {
        celsius = 5 * (fahr-32) / 9;
        printf("%d	%d
", fahr, celsius);
        fahr = fahr + step;
    }
    return 0;
}
View Code
C程序设计语言 (K&amp;R)第1张C程序设计语言 (K&amp;R)第2张
#include <stdio.h>
/* print Fahrenheit-Celsius table
for fahr = 0, 20, ..., 300; floating-point version */
int main()
{
    float fahr, celsius;
    float lower, upper, step;

    lower = 0; /* lower limit of temperatuire scale */
    upper = 300; /* upper limit */
    step = 20; /* step size */

    fahr = lower;

    while (fahr <= upper) {
        celsius = (5.0/9.0) * (fahr-32.0);
        printf("%3.0f %6.1f
", fahr, celsius);
        fahr = fahr + step;
    }
    return 0;
}
View Code
C程序设计语言 (K&amp;R)第1张C程序设计语言 (K&amp;R)第2张
#include <stdio.h>
/* print Fahrenheit-Celsius table
for fahr = 0, 20, ..., 300; floating-point version */
int main()
{
    int fahr;
    for (fahr = 0; fahr <= 300; fahr = fahr + 20)
        printf("%3d %6.1f
", fahr, (5.0/9.0)*(fahr-32));
    return 0;
}
View Code
C程序设计语言 (K&amp;R)第1张C程序设计语言 (K&amp;R)第2张
#include <stdio.h>
#define LOWER 0 /* lower limit of table */
#define UPPER 300 /* upper limit */
#define STEP 20 /* step size */
/* print Fahrenheit-Celsius table */
int main()
{
    int fahr;
    for (fahr = LOWER; fahr <= UPPER; fahr = fahr + STEP)
        printf("%3d %6.1f
", fahr, (5.0/9.0)*(fahr-32));
    return 0;
}
View Code

  2、文件复制,getchar putchar 函数

C程序设计语言 (K&amp;R)第1张C程序设计语言 (K&amp;R)第2张
#include <stdio.h>
/* copy input to output; 1st version */
int main()
{
    int c;
    c = getchar();
    while (c != EOF) {
        putchar(c);
        c = getchar();
    }
    return 0;
}
View Code
C程序设计语言 (K&amp;R)第1张C程序设计语言 (K&amp;R)第2张
#include <stdio.h>
/* copy input to output; 1st version */
int main()
{
    int c;
    while ((c = getchar()) != EOF)
        putchar(c);
    return 0;
}
View Code

  3、字符计数,double也可以自增

C程序设计语言 (K&amp;R)第1张C程序设计语言 (K&amp;R)第2张
#include <stdio.h>
/* count characters in input; 1st version */
int main()
{
    long nc;
    nc = 0;
    while (getchar() != EOF)
        ++nc;
    printf("%ld
", nc);
    return 0;
}
View Code
C程序设计语言 (K&amp;R)第1张C程序设计语言 (K&amp;R)第2张
#include <stdio.h>
/* count characters in input; 2nd version */
int main()
{
    double nc;
    for (nc = 0; getchar() != EOF; ++nc)
        ;
    printf("%.0f
", nc);
    return 0;
}
View Code

  4、行计数

C程序设计语言 (K&amp;R)第1张C程序设计语言 (K&amp;R)第2张
#include <stdio.h>
/* count lines in input */
int main()
{
    int c, nl;
    nl = 0;
    while ((c = getchar()) != EOF)
        if (c == '
')
            ++nl;
    printf("%d
", nl);
    return 0;
}
View Code

  5、单词计数,符号常量

C程序设计语言 (K&amp;R)第1张C程序设计语言 (K&amp;R)第2张
#include <stdio.h>

#define IN 1 /* inside a word */
#define OUT 0 /* outside a word */

/* count lines, words, and characters in input */
int main()
{
    int c, nl, nw, nc, state;
    state = OUT;
    nl = nw = nc = 0;
    while ((c = getchar()) != EOF) {
        ++nc;
        if (c == '
')
            ++nl;
        if (c == ' ' || c == '
' || c == '	')
            state = OUT;
        else if (state == OUT) {
            state = IN;
            ++nw;
        }
    }
    printf("%d %d %d
", nl, nw, nc);
    return 0;
}
View Code

  6、统计数字、空白符、其他字符,利用数组统计数字

C程序设计语言 (K&amp;R)第1张C程序设计语言 (K&amp;R)第2张
#include <stdio.h>
/* count digits, white space, others */
int 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'];  /*count digits*/
        else if (c == ' ' || c == '
' || c == '	')
            ++nwhite;
        else
            ++nother;
        printf("digits =");
        for (i = 0; i < 10; ++i)
            printf(" %d", ndigit[i]);
        printf(", white space = %d, other = %d
",
            nwhite, nother);
    return 0;
}
View Code

  7、求幂的函数 power(m, n) ,传值调用

C程序设计语言 (K&amp;R)第1张C程序设计语言 (K&amp;R)第2张
#include <stdio.h>

int power(int m, int n);

/* test power function */
int main()
{
    int i;
    for (i = 0; i < 10; ++i)
        printf("%d %d %d
", i, power(2,i), power(-3,i));
    return 0;
}

/* power: raise base to n-th power; n >= 0 */
int power(int base, int n)
{
    int i, p;
    p = 1;
    for (i = 1; i <= n; ++i)
        p = p * base;
    return p;
}
View Code
C程序设计语言 (K&amp;R)第1张C程序设计语言 (K&amp;R)第2张
#include <stdio.h>

int power(int m, int n);

/* test power function */
int main()
{
    int i;
    for (i = 0; i < 10; ++i)
        printf("%d %d %d
", i, power(2,i), power(-3,i));
    return 0;
}

/* power: raise base to n-th power; n >= 0; version 2 */
int power(int base, int n) /*传值*/
{
    int p;
    for (p = 1; n > 0; --n)
        p = p * base;
    return p;
}
View Code

  8、输出最长字符串,字符数组,getline,copy,外部变量

C程序设计语言 (K&amp;R)第1张C程序设计语言 (K&amp;R)第2张
#include <stdio.h>

#define MAXLINE 1000 /* maximum input line length */
int getline(char line[], int maxline);
void copy(char to[], char from[]);

/* print the longest input line */
int main()
{
    int len; /* current line length */
    int max; /* maximum length seen so far */
    char line[MAXLINE]; /* current input line */
    char longest[MAXLINE]; /* longest line saved here */
    max = 0;
    while ((len = getline(line, MAXLINE)) > 0)
        if (len > max) {
            max = len;
            copy(longest, line);
        }
    if (max > 0) /* there was a line */
        printf("%s", longest);
    return 0;
}

/* getline: read a line into s, return length */
int getline(char s[],int lim)
{
    int c, i;
    for (i=0; i < lim-1 && (c=getchar())!=EOF && c!='
'; ++i)
        s[i] = c;
    if (c == '
') { /*长度包含回车符*/
        s[i] = c;
        ++i;
    }
    s[i] = '

免责声明:内容来源于网络,仅用于学习参考。如对内容有疑问,请及时联系本站处理。

上篇【Python之路】特别篇--ECMA对象、DOM对象、BOM对象maven之BOM及BOM和provided的一个小坑下篇

宿迁高防,2C2G15M,22元/月;香港BGP,2C5G5M,25元/月 雨云优惠码:MjYwNzM=

相关文章

Linux C语言中gotoxy函数

在Linux中很多函数都不能使用,gotoxy就是其中的一个 实现方法: void gotoxy(int x,int y) { printf("%c[%d;%df",0x1B,y,x); }  解析: ANSI转义序列,很多类unix的终端,包括linux控制台都解释ANSI转义序列,转义符就是ESC,ASCII码是0x1b,比如...

《12个有趣的C语言问答》(4)

C语言面试问答——《12个有趣的C语言问答》评析(4) 前文链接:http://www.cnblogs.com/pmer/p/3324063.html 8,Making changes in Code segment Q:以下代码运行时一定会崩溃,你能说出原因吗?  ? #include<stdio.h>   int main(void...

vscode利用dev配置c语言,VSCode搭建C++/C调试编译环境(使用DevC++)

关于VSCode使用Dev C++的MinGW64来调试C++/C网上的教程试了很多,大部分都已经过时了或者说是不适配了,最后就选择使用Dev原有的东西来实现,不建议自己下载MinGW64,里面安装的时候有些选项不知道选什么的话很容易出现问题。 配置Dev下MinGW64的路径 假设Dev已经安装好了,然后现在要做的就是将Dev目录下的MinGW添加到环境...

C语言探索之旅 | 第一部分第三课:你的第一个程序

作者 谢恩铭,公众号「程序员联盟」。 转载请注明出处。 原文:https://www.jianshu.com/p/c73fecacd006 《C语言探索之旅》全系列 内容简介 前言 控制台程序还是窗口程序 最基础的代码 特殊字符 注释,很有用 总结 第一部分第四课预告 1. 前言 在上一课 C语言探索之旅 | 第一部分第二课:工欲善其事,必先利其...

值得推荐的C/C++框架和库 (真的很强大)

  值得学习的C语言开源项目 - 1. Webbench Webbench是一个在linux下使用的非常简单的网站压测工具。它使用fork()模拟多个客户端同时访问我们设定的URL,测试网站在压力下工作的性能,最多可以模拟3万个并发连接去测试网站的负载能力。Webbench使用C语言编写, 代码实在太简洁,源码加起来不到600行。 下载链接:http...

C语言 goto语句

/* goto语句 */ #include <stdio.h> #include <stdlib.h> #include <string.h> /* goto语句也称为无条件转移语句,其一般格式如下: goto 语句标号; 其中语句标号是按标识符规定书写的符号, 放在某一语句行的前面,标号后加冒号(:...