zoukankan      html  css  js  c++  java
  • C语言第一章基本概念练习。

    教材《C程序设计语言》

    练习1-3,4  编写一个用于打印摄氏与化石温度对照表的程序。(带标题)

     1 #include <stdio.h>
     2 #include <math.h>
     3 
     4 #define P printf
     5 #define PIN P("input:"L);
     6 #define D "%d"
     7 #define F "%f"
     8 #define C "%c"
     9 #define L "\n"
    10 #define B " "
    11 #define T "    "
    12 #define S scanf
    13 
    14 int main()
    15 {
    16     int fahr, celsius;
    17     int lower, upper, step;
    18 
    19     lower = 0;
    20     upper = 300;
    21     step = 20;
    22     fahr = lower;
    23     P("华氏与摄氏转换表"L);
    24     P("华氏" B "摄氏" L );
    25     while(fahr <= upper){
    26         celsius = 5 * (fahr-32) / 9;
    27         P(D T D L, fahr, celsius);
    28         fahr += step;
    29     }
    30     return 0;
    31 }

     练习1-5 修改使其逆序(300度到0度)

    #include <stdio.h>
    #include <math.h>
    
    #define P printf
    #define PIN P("input:"L);
    #define D "%d"
    #define F "%f"
    #define C "%c"
    #define L "\n"
    #define B " "
    #define T "    "
    #define S scanf
    
    int main()
    {
        int fahr, celsius;
        int lower, upper, step;
    
        lower = 0;
        upper = 300;
        step = 20;
    
        P("华氏与摄氏转换表"L);
        P("华氏" B "摄氏" L );
       for(fahr = upper; fahr >= lower; fahr -= step){
            celsius = 5 * (fahr-32) / 9;
            P(D T D L, fahr, celsius);
        }
        return 0;
    }

    练习1-7 编写一个用于打印EOF值的程序

     1 #include <stdio.h>
     2 #include <math.h>
     3 
     4 #define P printf
     5 #define PIN P("input:"L);
     6 #define D "%d"
     7 #define F "%f"
     8 #define C "%c"
     9 #define L "\n"
    10 #define B " "
    11 #define T "    "
    12 #define S scanf
    13 
    14 int main()
    15 {
    16     int c;
    17 
    18     P(D L C L F L , EOF, EOF, EOF);
    19     return 0;
    20 }

    看来EOF是int的-1;

    练习1-8  编写一个用于统计空格、制表符、换行符个数的程序。

     1 #include <stdio.h>
     2 #include <math.h>
     3 
     4 #define P printf
     5 #define PIN P("input:"L);
     6 #define D "%d"
     7 #define F "%f"
     8 #define C "%c"
     9 #define L "\n"
    10 #define B " "
    11 #define T "    "
    12 #define S scanf
    13 
    14 int main()
    15 {
    16     long c,sl,tl,nl;
    17 
    18     sl = tl = nl = 0;
    19     while ((c = getchar())!=EOF){
    20         if (c==' ')
    21             ++sl;
    22         else if (c=='\t')
    23             ++tl;
    24         else if (c=='\n')
    25             ++nl;
    26     }
    27     P(D L, sl, tl, nl);
    28     return 0;
    29 }

    最后应该看lastchar是不是‘n',不是得加一个新行数。。。

  • 相关阅读:
    MongoDB安装
    前端构建工具gulp入门教程
    限制input输入类型(多种方法实现)
    【E20200105-1】Centos 7.x 下vsftpd配置文件常用配置详解
    【E20200102-1】centos 7 下vsftp的安装和配置
    【E20200101-1】Centos 7.x 关闭防火墙(firewall)和SELinux
    Linux下如何修改用户默认目录
    linux给普通用户增加ssh权限
    在IIS7中应用Application Request Routing配置反向代理
    vmware相关服务默认禁用 修改服务弹出服务拒绝访问解决办法
  • 原文地址:https://www.cnblogs.com/pengzheng/p/3046120.html
Copyright © 2011-2022 走看看