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',不是得加一个新行数。。。

原文地址:https://www.cnblogs.com/pengzheng/p/3046120.html