第七章 编程练习

7-1

 1 #include <stdio.h>
 2 
 3 int main(int argc, char const *argv[])
 4 {
 5     char a;
 6     int n_space = 0;
 7     int n_enter = 0;
 8     int n_char = 0;
 9 
10     printf("Enter something(end with #):");
11 
12     while ((a = getchar()) != '#') {
13         if (a == ' ')
14             n_space++;
15         else if (a == '\n')
16             n_enter++;
17         else
18             n_char++;    
19     }
20     printf("There are %d spaces,%d Enters and %d charactors.\n", n_space, n_enter, n_char );
21  
22     return 0;
23 }
24 
25 /* 
26 Enter something(end with #):hello world!
27 fuck Japanese!Pull nuclear waste water into
28 Pacific!#
29 There are 7 spaces,2 Enters and 57 charactors.
30 
31 */

 7-3

#include <stdio.h>

int main(int argc, char const *argv[])
{
    int a;
    int n_even = 0;
    int sum_even = 0;
    int n_odd = 0;
    int sum_odd = 0;

    printf("Enter a integer(end with 0):");
    scanf("%d", &a);

    while (a != 0) {
        if (a % 2 == 0){
            n_even ++;
            sum_even += a;
        }else{
            n_odd++;
            sum_odd += a;    
        }
        printf("Enter a integer(end with 0):");
        scanf("%d", &a);
    }
    if (n_even == 0)
        printf("The number of evens is %d.\n", n_even );
    else
        printf("The number of evens is %d,and the average of evens is %f.\n", n_even, 1.0*sum_even/n_even );
    if (n_odd == 0)
        printf("The number of odds is %d.\n", n_odd );
    else
        printf("The number of odds is %d,and the average of odds is %f.\n", n_odd, 1.0*sum_odd/n_odd );

    printf("Done!\n");
    
    return 0;
}

/* 
Enter a integer(end with 0):2
Enter a integer(end with 0):3
Enter a integer(end with 0):5
Enter a integer(end with 0):110
Enter a integer(end with 0):0
The number of evens is 2,and the average of evens is 56.000000.
The number of odds is 2,and the average of odds is 4.000000.
Done!

*/

7-4

#include <stdio.h>

int main(int argc, char const *argv[])
{
    char ch;
    int cnt1 = 0;
    int cnt2 = 0;


    printf("Enter something(end with #):");
    
    while ((ch = getchar()) != '#') {
        if (ch == '.'){
            cnt1 ++;
            putchar('!');
        }else if (ch == '!'){
            cnt2 ++;
            putchar('!');
            putchar('!');    
        }else{
            putchar(ch);    //putchar()接收到'\n'会把之前得到的字符全打印出来?
        }
    }
    printf("%d \'.\' was(were) repalced by \'!\'\n", cnt1);
    printf("%d \'!\' was(were) repalced by \'!!\'\n", cnt2);
    printf("\n");
    printf("Done!\n");
    
    return 0;
}

/* 
Enter something(end with #):Hi!John.RUN!
Hi!!John!RUN!!
ok!
ok!!
#
1 '.' was(were) repalced by '!'
3 '!' was(were) repalced by '!!'

Done!

*/

7-5

#include <stdio.h>

int main(int argc, char const *argv[])
{
    char ch;
    int cnt1 = 0;
    int cnt2 = 0;


    printf("Enter something(end with #):");
    
    while ((ch = getchar()) != '#') {
        switch(ch){
        case '.':
            cnt1 ++;
            putchar('!');
            break;
        case '!':
            cnt2 ++;
            putchar('!');
            putchar('!');
            break;    
        default:
            putchar(ch);    //putchar()接收到'\n'会把之前得到的字符全打印出来?
        }
        
    }
    printf("%d \'.\' was(were) repalced by \'!\'\n", cnt1);
    printf("%d \'!\' was(were) repalced by \'!!\'\n", cnt2);
    printf("\n");
    printf("Done!\n");
    
    return 0;
}

/* 
Enter something(end with #):Hi!John.RUN!
Hi!!John!RUN!!
#
1 '.' was(were) repalced by '!'
2 '!' was(were) repalced by '!!'

Done!

*/

7-6

#include <stdio.h>

int main(int argc, char const *argv[])
{
    char ch;
    int cnt = 0;

    printf("Enter something(end with #):");
    
    while ((ch = getchar()) != '#') {
        if (ch == 'e'){
            ch = getchar();
            if (ch == '#') goto here;
            else if (ch == 'i') cnt++;
        }
    }
    here:printf("There is(are) %d \'ei\' in your input.\n", cnt);
    printf("Done!");
    
    return 0;
}

/* 
Enter something(end with #):Receive your eieio award#
There is(are) 3 'ei' in your input.
Done!
*/

7-7-7-11 题目太长,有空再做~

posted @ 2023-08-31 18:32  园友3218619  阅读(275)  评论(0)    收藏  举报