zoukankan      html  css  js  c++  java
  • 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	%d
    ", fahr, celsius);
    
            // printf(""%3d %6d
    ", 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
    ", 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
    ", 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
    ", 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
    ", nc);
    }
    
    
    /* line counting */
    #include <stdio.h>
    
    main()
    {
       int c, nl;
    
       nl = 0;
       while((c = getchar()) != EOF)
           if (c == '
    ')
               ++nl;
       printf("%d
    ", 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 == '
    ') {
                ++nl;
            }
            if (c == ' ' || c == '
    ' || c == '	') {
                state = OUT;
            } else if (state == OUT) {
                state = IN;
                ++nw;
            }
        }
        printf("%d %d %d
    ", nl, nw, nc);
    }
    

    参考资料:

    -The C Programming Language

  • 相关阅读:
    更换惠普G32笔记本的风扇和硬盘,内存条, 谨记 要做好CPU和显卡的 导热硅脂工作!
    怎么更新 WIN10里的SMBv1协议
    ubuntu-12.04.5-desktop-amd64 安装vmwaretools
    如何解决“ VMware Workstation 不可恢复错误: (vcpu-0) vcpu-0:VERIFY vmcore/vmm/main/cpuid.c:386 bugNr=1036521”
    联想移动硬盘无法访问 解决方法1
    阮一峰 ---开发者手册
    Earth Wind 一个查看全球风向的网站
    Linux帮助用法
    Linux历史命令管理以及用法
    Linux操作练习
  • 原文地址:https://www.cnblogs.com/linkworld/p/10534773.html
Copyright © 2011-2022 走看看