【C】The C programming language

 1 #include <stdio.h>
 2 #define UPPER 300
 3 #define LOWER 0
 4 #define STEP 20
 5 
 6 main()
 7 {
 8     float farh, celsius;
 9 
10     farh = LOWER;
11     celsius = LOWER;
12     printf("---C to F---\n");
13     while (celsius <= UPPER)
14     {
15         farh = (9.0*celsius) / 5.0 + 32.0;
16         printf("%3.2f %6.2f\n", celsius, farh);
17         celsius += STEP;
18     }
19     printf("------------\n");
20     for (celsius = UPPER; celsius >= LOWER; celsius -= STEP)
21     {
22         printf("%3.2f %6.2f\n", celsius, (9.0*celsius) / 5.0 + 32.0);
23     }
24 
25     farh = LOWER;
26     celsius = LOWER;
27     printf("---F to C---\n");
28     while (farh <= UPPER)
29     {
30         celsius = 5.0 * (farh-32.0) / 9.0;
31         printf("%3.2f %6.2f\n", farh, celsius);
32         farh += STEP;
33     }
34     printf("------------\n");
35     for (farh = UPPER; farh >= LOWER; farh -= STEP)
36     {
37         printf("%3.2f %6.2f\n", farh, 5.0 * (farh-32.0) / 9.0);
38     }
39 }
1-3、1-4、1-5
 1 #include <stdio.h>
 2 
 3 main()
 4 {
 5     float c;
 6 
 7     while (c = getchar() != EOF)
 8     {
 9         printf("getchar != EOF --- %3.1f\n", c);
10         printf("EOF is %d\n", EOF);
11     }
12 }
1-6、1-7
1-8、1-9、1-10
 1 #include <stdio.h>
 2 
 3 #define OUT 0
 4 #define IN 1
 5 
 6 main()
 7 {
 8     int state = OUT;
 9     int c;
10     int number = 0;
11 
12     while ((c = getchar()) != EOF)
13     {
14         if (((c >= 'A' && c <= 'Z') || (c >= 'a' && c <= 'z')))
15         {
16             putchar(c);
17 
18             if (state == OUT)
19             {
20                 state = IN;
21                 number++;
22             }
23         }
24         else
25         {
26             putchar('\n');
27             state = OUT;
28         }
29     }
30     printf("The number of words is: %d\n", number);
31 }
1-11、1-12
 1 #include <stdio.h>
 2 
 3 #define MAXSIZE 10
 4 #define Height 10    // 垂直直方图高度
 5 #define IN 1
 6 #define OUT 0
 7 
 8 main()
 9 {
10     int wordLength[MAXSIZE], charNumber[128];
11     int c, i, j;
12     int state = OUT;
13     int number;
14 
15     // 下面两个for是初始化
16     for (i = 0; i < MAXSIZE; i++)
17     {
18         wordLength[i] = 0;
19     }
20     for (i = 0; i < 128; i++)
21     {
22         charNumber[i] = 0;
23     }
24 
25     // 下面的while是对输入进行计数
26     while ((c = getchar()) != EOF)
27     {
28         charNumber[c]++;    // 存储字符个数
29 
30         if (((c >= 'A' && c <= 'Z') || (c >= 'a' && c <= 'z')))
31         {
32             if (state == OUT)
33             {
34                 state = IN;
35                 number = 1;
36             }
37             else if (state == IN)
38             {
39                 number++;
40             }
41         }
42         else
43         {
44             if (state == IN)
45             {
46                 state = OUT;
47                 wordLength[number]++;
48             }
49 
50         }
51     }
52 
53     // 下面一个for是输出水平直方图
54     for (i = 0; i < MAXSIZE; i++)
55     {
56         printf("Length of %d:\t", i);
57         for (j = 0; j < wordLength[i]; j++)
58         {
59             printf("***");
60         }
61         printf("\n");
62     }
63     // 下面两个for是输出垂直直方图
64     for (i = 0; i < Height; i++)
65     {
66         for (j = 0; j < MAXSIZE; j++)
67         {
68             if (wordLength[j] >= (MAXSIZE-i))
69             {
70                 printf("*\t");
71             }
72             else
73             {
74                 printf(" \t");
75             }
76         }
77     }
78     for (i = 0; i < MAXSIZE; i++)
79     {
80         printf("%d\t", i);
81     }
82 
83     // 输出各字符个数
84     for (i = 0; i < 128; i++)
85     {
86         printf("Number of ASCII---%d: %d\n", i, charNumber[i]);
87     }
88 }
1-13、1-14
 1 #include <stdio.h>
 2 #define UPPER 300
 3 #define LOWER 0
 4 #define STEP 20
 5 
 6 float CtoF(float celsius);
 7 float FtoC(float farh);
 8 
 9 main()
10 {
11     float farh, celsius;
12 
13     farh = LOWER;
14     celsius = LOWER;
15     printf("---C to F---\n");
16     while (celsius <= UPPER)
17     {
18 
19         printf("%3.2f %6.2f\n", celsius, CtoF(celsius));
20         celsius += STEP;
21     }
22     printf("------------\n");
23     for (celsius = UPPER; celsius >= LOWER; celsius -= STEP)
24     {
25         printf("%3.2f %6.2f\n", celsius, CtoF(celsius));
26     }
27 
28     farh = LOWER;
29     celsius = LOWER;
30     printf("---F to C---\n");
31     while (farh <= UPPER)
32     {
33         printf("%3.2f %6.2f\n", farh, FtoC(farh));
34         farh += STEP;
35     }
36     printf("------------\n");
37     for (farh = UPPER; farh >= LOWER; farh -= STEP)
38     {
39         printf("%3.2f %6.2f\n", farh, FtoC(farh));
40     }
41 }
42 
43 float CtoF(float celsius)
44 {
45     return ((9.0*celsius) / 5.0 + 32.0);
46 }
47 
48 float FtoC(float farh)
49 {
50     return (5.0 * (farh-32.0) / 9.0);
51 }
1-15
 1 #include <stdio.h>
 2 #define MAXLINE 1000
 3 
 4 int getline(char line[], int maxline);
 5 void copy(char to[], char from[]);
 6 void reverse(char line[], int len);
 7 
 8 int main()
 9 {
10     int ml, len;
11     char line[MAXLINE];
12     char longest[MAXLINE];
13 
14     ml = 0;
15     while ((len = getline(line, MAXLINE)) > 0)
16     {
17         reverse(line, len);
18         if (len > 80)
19         {
20             printf("%s", line);        //1-17
21         }
22         if (len > ml)
23         {
24             ml = len;
25             copy(longest, line);
26         }
27     }
28     if (ml > 0)
29     {
30         printf("%s", longest);
31     }
32     return 0;
33 }
34 
35 int getline(char line[], int maxline)
36 {
37     int c, i, k;
38     int j = 0;
39     int flag = 0;
40 
41     for (i = 0; (c = getchar()) != EOF && c != '\n'; ++i)
42     {
43         if (c == ' ' || c == '\t')
44         {
45             if (flag == 0)      // flag判断在空格或\t后是否还有其他字符
46             {
47                 flag = 1;
48                 k = j;
49             }
50         }
51         else
52         {
53             flag = 0;
54         }
55         if (i < maxline-2)
56         {
57             line[j++] = c;
58         }
59     }
60     if (c == '\n')
61     {
62         if (flag == 1)
63         {
64             j = k;
65             i = k;
66         }
67         line[j++] = c;
68         ++i;
69     }
70     line[j] = '\0';
71 
72     return i;
73 }
74 
75 void copy(char to[], char from[])
76 {
77     int i = 0;
78 
79     while ((to[i] = from[i]) != '\0')
80     {
81         ++i;
82     }
83 }
84 
85 void reverse(char line[], int len)
86 {
87     int i = 0;
88     int j = len-1;
89     int swap;
90 
91     for (i, j; i < j; i++, j--)
92     {
93         swap = line[i];
94         line[i] = line[j];
95         line[j] = swap;
96     }
97 }
1-16、1-17、1-18、1-19
 1 #include <stdio.h>
 2 
 3 #define TABING 8
 4 
 5 main()
 6 {
 7     int nb = 0;
 8     int i, c;
 9 
10     while ((c = getchar()) != EOF)
11     {
12         if (c == ' ')
13         {
14             nb++;
15             if (nb == TABING)
16             {
17                 nb = 0;
18                 printf("\t");
19             }
20         }
21         else if (c == '\t')
22         {
23             nb = 0;
24             printf("\t");
25         }
26         else
27         {
28             for (i = 0; i < nb; i++)
29             {
30                 printf(" ");
31             }
32             nb = 0;
33             printf("%c", c);
34         }
35     }
36 }
1-20、1-21
 1 #include <stdio.h>
 2 #include <limits.h>
 3 
 4 void headFile(void);
 5 void countDirectly(void);
 6 
 7 main()
 8 {
 9     headFile();
10     countDirectly();
11 }
12 
13 void headFile(void)
14 {
15     // signed types
16     printf("signed char min = %d\n", SCHAR_MIN);
17     printf("signed char max = %d\n", SCHAR_MAX);
18     printf("signed short min = %d\n", SHRT_MIN);
19     printf("signed short max = %d\n", SHRT_MAX);
20     printf("signed int min = %d\n", INT_MIN);
21     printf("signed int max = %d\n", INT_MAX);
22     printf("signed long min = %ld\n", LONG_MIN);
23     printf("signed long max = %ld\n", LONG_MAX);
24     // unsigned types
25     printf("unsigned char max = %u\n", UCHAR_MAX);      // %u is output in unsigned int
26     printf("unsigned short max = %u\n", USHRT_MAX);
27     printf("unsigned int max = %u\n", UINT_MAX);
28     printf("unsigned long max = %lu\n", ULONG_MAX);     // %lu is output in unsigned long
29 }
30 
31 void countDirectly(void)
32 {
33     // signed types
34     printf("signed char min = %d\n", -(char)((unsigned char) ~0 >> 1));
35     printf("signed char max = %d\n", (char)((unsigned char) ~0 >> 1));
36     printf("signed short min = %d\n", -(short)((unsigned short) ~0 >> 1));
37     printf("signed short max = %d\n", (short)((unsigned short) ~0 >> 1));
38     printf("signed int min = %d\n", -(int)((unsigned int) ~0 >> 1));
39     printf("signed int max = %d\n", (int)((unsigned int) ~0 >> 1));
40     printf("signed long min = %ld\n", -(long)((unsigned long) ~0 >> 1));
41     printf("signed long max = %ld\n", (long)((unsigned long) ~0 >> 1));
42     // unsigned types
43     printf("unsigned char max = %u\n", (unsigned char) ~0);
44     printf("unsigned short max = %u\n", (unsigned short) ~0);
45     printf("unsigned int max = %u\n", (unsigned int) ~0);
46     printf("unsigned long max = %lu\n", (unsigned long) ~0);
47 }
2-1

 

 

 

 

posted @ 2013-12-20 12:08  SpongeHAO  阅读(181)  评论(0编辑  收藏  举报