`
ninghuan
  • 浏览: 9083 次
  • 性别: Icon_minigender_1
  • 来自: 上海
最近访客 更多访客>>
社区版块
存档分类
最新评论

<THE C PROGRAMMING LANGUAGE> 练习 第一章 (1-1 -- 1-5)

    博客分类:
  • C
阅读更多
1-1 在你自己的系统中运行 "hello, world" 程序. 再有意去掉程序中的部分内容, 看看会得到什么错误信息.


#include <stdio.h>

int
main()
{
    printf("hello, world\n");

    return 0;
}


输出了 (后面一行是 Code::Blocks 加的, 很不错哈 ):
引用
hello, world
Process returned 0 (0x0)   execution time : 0.055 s


我把
#include <stdio.h>

删除了, 编译通过, 有个警告, 可以运行.



1-2 做个试验, 当 printf 函数的参数字符串中包含 \c (其中 c 是上面的转义字符序列中未曾列出的某一字符)  时, 观察一下会出现什么情况.


编译时出现一个警告, 字符直接输出了, 不包含 "\" .



1-3 修改温度转换程序, 使之能在转换表的顶部打印一个标题.


#include <stdio.h>

int
main()
{
    float fahr, celsius;
    int lower, upper, step;

    lower = 0;
    upper = 300;
    step = 20;

    printf("F:\tC:\n");

    fahr = lower;

    while (fahr <= upper) {
        celsius = (5.0/9.0) * (fahr - 32.0);
        printf("%3.0f\t%6.1f\n", fahr, celsius);

        fahr += step;
    }

    return 0;
}




1-4 编写一个程序打印摄氏度转换为相应华氏温度的转换表.


#include <stdio.h>

int
main()
{
    float celsius, fahr;
    int lower, upper, step;

    lower = 0;
    upper = 30;
    step = 2;

    printf("C:\tF:\n");

    celsius = lower;

    while (celsius <= upper) {
        fahr = ((9.0/5.0) * celsius) + 32.0;
        printf("%6.1f\t%3.0f\n", celsius, fahr);

        celsius += step;
    }

    return 0;
}


输出:
引用

C:      F:
   0.0   32
   2.0   36
   4.0   39
   6.0   43
   8.0   46
  10.0   50
  12.0   54
  14.0   57
  16.0   61
  18.0   64
  20.0   68
  22.0   72
  24.0   75
  26.0   79
  28.0   82
  30.0   86




1-5 修改温度转换程序, 要求以逆序 (即按照从 300 度到 0 度的顺序) 打印温度转换表


#include <stdio.h>

int
main()
{
    float fahr, celsius;
    int lower, upper, step;

    lower = 0;
    upper = 300;
    step = 20;

    printf("F:\tC:\n");

    fahr = upper;

    while (fahr > lower) {
        celsius = (5.0/9.0) * (fahr - 32.0);
        printf("%3.0f\t%6.1f\n", fahr, celsius);

        fahr -= step;
    }

    return 0;
}
分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics