zoukankan      html  css  js  c++  java
  • C程序设计语言 第1章(1)

    练习1-1 在你自己的系统运行”hello, world“程序。再有意去掉程序中的部分内容,看看会得到什么出错信息。

    1 void main ()
    2 {
    3     printf("hello, world
    ");
    4 }

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

    1 void main ()
    2 {
    3     printf("hello, world
    c
    ");
    4 }

    运行结果:

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

     1 #include <stdio.h>
     2 void main ()
     3 {
     4     float fahr, celsius;
     5     int lower, upper, step;
     6 
     7     lower = 0;
     8     upper = 300;
     9     step = 20;
    10     
    11     printf("The Transformation
    ");
    12     fahr = lower;
    13     while (fahr <= upper)
    14     {
    15         celsius = (5.0/9.0) * (fahr-32.0);
    16         printf("%3.0f %6.1f
    ", fahr, celsius);
    17         fahr = fahr + step;
    18     }
    19 }

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

     1 #include <stdio.h>
     2 void main ()
     3 {
     4     float fahr, celsius;
     5     int lower, upper, step;
     6 
     7     lower = 0;
     8     upper = 300;
     9     step = 20;
    10     
    11     printf("The Transformation
    ");
    12     celsius = lower;
    13     while (fahr <= upper)
    14     {
    15         fahr = celsius / (5.0 / 9.0) + 32.0;
    16         printf("%3.0f %6.1f
    ", celsius, fahr);
    17         celsius = celsius + step;
    18     }
    19 }

    练习1-5 修改温度程序,要求以逆序打印温度转换表。

     1 #include <stdio.h>
     2 void main ()
     3 {
     4     float fahr, celsius;
     5     int lower, upper, step;
     6 
     7     lower = 0;
     8     upper = 300;
     9     step = 20;
    10     
    11     printf("The Transformation
    ");
    12     fahr = upper;
    13     while (fahr >= lower)
    14     {
    15         celsius = (5.0/9.0) * (fahr-32.0);
    16         printf("%3.0f %6.1f
    ", fahr, celsius);
    17         fahr = fahr - step;
    18     }
    19 }

    练习1-6 验证表达式getchar() != EOF的值是0还是1.

    1 #include <stdio.h>
    2 void main ()
    3 {
    4     int c;
    5     while (c = getchar() != EOF)
    6         printf("%d
    ", c);
    7     printf("%d 
    ", c);
    8 }

    这样在按下crtl+c的时候会打印出0,然后退出程序。

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

    1 #include <stdio.h>
    2 void main ()
    3 {
    4     printf("%d
    ", EOF);
    5 }

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

     1 #include <stdio.h>
     2 void main ()
     3 {
     4     int c, nb, nt, nl;
     5     nb = nt = nl = 0;
     6     while ((c = getchar()) != EOF)
     7     {
     8         if (c == ' ')
     9             nb++;
    10         if (c == '	')
    11             nt++;
    12         if (c == '
    ')
    13             nl++;
    14     }
    15     printf("%d %d %d
    ", nb, nt, nl);
    16 }

    输入crtl+z,然后回车才可以跳出循环,得到结果。

    练习1-9 编写一个将输入复制到输出的程序,并将其中连续的多个空格用一个空格代替。

     1 #include <stdio.h>
     2 void main ()
     3 {
     4     int c, nb, prev;
     5     nb = 0;
     6     prev = 0;
     7 
     8     while ((c = getchar()) != EOF)
     9     {
    10         if (c == ' ')
    11         {
    12             prev = 1;
    13             nb++;
    14         }
    15         else
    16         {
    17             if (prev == 1)
    18             {
    19                 putchar(' ');
    20                 prev = 0;
    21             }
    22             else
    23             {
    24                 putchar(c);
    25                 prev = 0;
    26             }
    27         }
    28     }
    29 }
    很难实现的,才叫梦想!
  • 相关阅读:
    hibernate_0100_HelloWorld
    MYSQL子查询的五种形式
    JSF是什么?它与Struts是什么关系?
    nop指令的作用
    htmlparser实现从网页上抓取数据(收集)
    The Struts dispatcher cannot be found. This is usually caused by using Struts tags without the associated filter. Struts tags are only usable when the
    FCKeditor 在JSP上的完全安装
    Java遍历文件夹的2种方法
    充电电池和充电时间说明
    吃知了有什么好处
  • 原文地址:https://www.cnblogs.com/zhangyaoluo/p/3398232.html
Copyright © 2011-2022 走看看