Fork me on GitHub

C语言概述

  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     20

main()
{
    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 0

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);
}

参考资料:

-The C Programming Language

posted @ 2019-03-15 08:11  小a的软件思考  阅读(179)  评论(0编辑  收藏  举报