• C 程序设计语言第二版 第一章 习题


    第一题

    #include <stdio.h>
    
    int main(void){
        printf("hello, world");
        
        return 0;
    }

    第二题

    #include <stdio.h>
    
    int main(void){
        printf("hello, worldc12");
        
        return 0;
    }

    会出现警告,并把c打印出来

    第三题

    #include <stdio.h>
    
    int main(void){
        
        float fahr, celsius;
        int lower, upper, step;
        lower = 0;
        upper = 300;
        step = 20;
        
        fahr = lower;
        printf("Fahrenheit Celsius
    ");
        
        while (fahr <= upper) {
            celsius = (5.0 / 9.0) * (fahr - 32.0);
            printf("%6.0f     %6.1f
    ", fahr, celsius);
            fahr = fahr + step;
        }
        
        return 0;
    }

    第四题

    #include <stdio.h>
    
    int main(void){
        
        float fahr, celsius;
        int lower, upper, step;
        lower = 0;
        upper = 300;
        step = 20;
        
        celsius = lower;
        printf("Celsius Fahrenheit
    ");
        
        while (celsius <= upper) {
            fahr = (9.0 / 5.0) * celsius + 32.0;
            printf("%6.0f     %6.1f
    ", celsius, fahr);
            celsius = celsius + step;
        }
        
        return 0;
    }

    第五题

    #include <stdio.h>
    
    int main(void){
        
        float fahr, celsius;
        int lower, upper, step;
        lower = 0;
        upper = 300;
        step = 20;
        
        
        printf("Fahrenheit Celsius
    ");
        
        for (fahr = upper; fahr >=0; fahr = fahr - step) {
            celsius = (5.0 / 9.0) * (fahr - 32.0);
            printf("%6.0f     %6.1f
    ", fahr, celsius);
        }
            
        return 0;
    }
        

    第六题

    未输入结束符为0,输入结束符为1

    第七题

    #include <stdio.h>
    
    int main(void){
        
        int ch;
        
        while (1) {
            if ((ch = getchar()) == EOF){
                printf("c = %d
    ", ch);
                printf("EOF = %d
    ", EOF);
                break;
            }
        }
            
        return 0;
    }
        

    -1

    第八题

    #include <stdio.h>
    
    int main(void){
        
        int space, tab, enter, ch;
        space = tab = enter = 0;
        while ((ch = getchar()) != EOF) {
            if (ch ==  ' ') {
                space++;
            }else if (ch == '	'){
                tab++;
            }else if (ch == '
    '){
                enter++;
            }
        }
        printf("%d %d %d", space, tab, enter);
        
        return 0;
    }
        

    第9题

    #include <stdio.h>
    
    int main(void){
        
        int is_space, ch;
        is_space = 0;
        while ((ch = getchar()) != EOF) {
            if (is_space == 0) {
                if (ch == ' ') {
                    putchar(ch);
                    is_space = 1;
                }else
                    putchar(ch);
            }else{
                if (ch != ' ') {
                    putchar(ch);
                    is_space = 0;
                }
            }
            
        }
        
        return 0;
    }
        

    书中的答案更加精简,定义了前一个字符,判断前一个字符的内容实现

    第十题

    #include <stdio.h>
    
    int main(void){
        
        int ch;
        while ((ch = getchar()) != EOF) {
            if (ch == '	') {
                putchar('\');
                putchar('t');
            }else if (ch == ''){
                putchar('\');
                putchar('b');
            }else if (ch == '\'){
                putchar('\');
                putchar('\');
            }else
                putchar(ch);
            
        }
        
        return 0;
    }
        

    第十一题

    边界测试

    第十二题

    #include <stdio.h>
    
    int main(void){
        
        int ch, last;
        last = '
    ';
        while ((ch = getchar()) != EOF) {
            if ((ch == '	' || ch == ' ' || ch == '
    ') && last != '
    ') {
                putchar('
    ');
                last = '
    ';
            }else if (ch == '	' || ch == ' ' || ch == '
    ');
            else{
                putchar(ch);
                last = ch;
            }
            
        }
        
        return 0;
    }
        
  • 相关阅读:
    岩石圈
    地球及其圈层结构
    如何请教一个技术问题
    中国游戏路在何方?
    5.4删除二叉搜索树的任意元素
    5.3 删除二叉搜索树的最大元素和最小元素
    uni-app开发小程序准备阶段
    5.2二叉搜索树遍历(前序、中序、后序、层次、广度优先遍历)
    5.1二叉搜索树基础
    【loj
  • 原文地址:https://www.cnblogs.com/sidianok/p/15387448.html
Copyright © 2020-2023  润新知