• 第二章


    2.1例2-1

    /* 显示"Hello World!" */
    # include<stdio.h>
    int main(void)
    {
        printf("Hello World!
    ");
    
        return 0;
    }

    2.1例2-2

    /* 显示"Hello World!" */
    # include<stdio.h>
    int main(void)
    {
        printf("Hello World!
    ");
    
        return 0;
    }

    2.2例2-3

    /* 将华氏温度转换为摄氏温度 */
    #include<stdio.h>
    int main(void)
    {
        /* 定义两个整型变量,celsius表示摄氏度,fahr表示华氏度 */
        int celsius,fahr;
    
        fahr = 100;
            celsius=5*(fahr-32)/9;
    
        printf("fahr=%d,celsius=%d
    ",fahr,celsius);
    
        return 0;
    }

    2.3例2-4

    /* 计算二分段函数 */
    #include<stdio.h>
    int main(void)
    {
        double x,y;
    
        printf("Enter x(x>=0):
    ");
    
        scanf("%lf",&x);
    
        if(x<=15){
            y=4*x/3;
        }
        else{
            y=2.5*x-10.5;
        }
        printf("y=f(%f)=%.2f
    ",x,y);
    
        return 0;
    }

    2.3例2-5

    /* 计算存款的利息 */
    #include<stdio.h>
    #include<math.h>
    int main(void)
    {
        int money,year;
        double rate,sum;
    
        printf("Enter money:");
        scanf("%d",&money);
        printf("Enter year:");
        scanf("%d",&year);
        printf("Enter rate:");
        scanf("%lf",&rate);
        sum=money*pow(1+rate,year);
        printf("sum=%.2f
    ",sum);
    
        return 0;
    }

    2.4例2-6

    #include<stdio.h>
    int main(void)
    {
        int fahr,lower,upper;
        double celsius;
    
        printf("Enter lower:");
        scanf("%d",&lower);
        printf("Enter upper:");
        scanf("%d",&upper);
        printf("fahr celsius
    ");
    
        for(fahr=lower;fahr<=upper;fahr++){
            celsius=(5.0/9.0)*(fahr-32);
            printf("%d%6.1f
    ",fahr,celsius);
        }
    
        return 0;
    }

    2.4例2-7

    /* 计算1+2+3+…+n */
    #include<stdio.h>
    int main(void)
    {
        int i,n,sum;
    
        printf("Enter n:");
        scanf("%d",&n);
        sum=0;
        for(i=1;i<=n;i++){
            sum=sum+i;
        }
    
        printf("Sum of numbers from 1 to %d is %d
    ",n,sum);
    
        return 0;
    }

    2.4例2-8

    /* 计算1-1/3+1/5-1/7+…共n项之和 */
    #include<stdio.h>
    int main(void)
    {
        int denominator,flag,i,n;
        double item,sum;
    
        printf("Enter n:");
        scanf("%d",&n);
    
        flag=1;
        denominator=1;
        sum=0;
    
        for(i=1;i<=n;i++){
            item=flag*1.0/denominator;
            sum=sum+item;
            flag=-flag;
            denominator=denominator+2;
        }
        printf("Sum=%f
    ",sum);
    
        return 0;
    }

    2.4例2-9

    /* 输入一个正整数n,求n! */
    #include<stdio.h>
    int main(void)
    {
        int i,n;
        double product;
    
        printf("Enter n:");
        scanf("%d",&n);
        product=1;
        for(i=1;i<=n;i++){
            product=product*i;
        }
        printf("product=%.0f
    ",product);
    
        return 0;
    }

     

    2.5例2-10

    /* 调用幂函数pow()生成乘方表 */
    #include<stdio.h>
    #include<math.h>
    
    int main(void)
    {
        int i,n;
        double power;
    
        printf("Enter n:");
        scanf("%d",&n);
        for(i=0;i<=n;i++){
            power=pow(2,i);
            printf("pow(2,%d)=%.0f
    ",i,power);
        }
    
        return 0;
    }

    2.5例2-11

    /* 定义和调用求阶乘函数生成阶乘表 */
    #include<stdio.h>
    double fact(int n);
    int main(void)
    {
        int i,n;
        double result;
    
        printf("Enter n:");
        scanf("%d",&n);
        for(i=0;i<=n;i++){
            result=fact(i);
            printf("%d!=%.0f
    ",i,result);
        }
    
        return 0;
    }

  • 相关阅读:
    童鞋,[HttpClient发送文件] 的技术实践请查收
    有关[Http持久连接]的一切,卷给你看
    浅谈MemoryCache的原生插值方式
    HTTP1.1 KeepAlive到底算不算长连接?
    C2 hits the assertion assert(base>is_AddP()) failed: should be addp but is Phi
    C2 EA
    OOM Hook
    C2 Loop predicate
    C2 Build IR
    C2 CCP
  • 原文地址:https://www.cnblogs.com/jianghaoyu0129/p/3350589.html
Copyright © 2020-2023  润新知