• 第三章


    3.1例3-1

    /* 简单的猜数游戏 */
    #include<stdio.h>
    int main(void)
    {
        int mynumber=38;
        int yournumber;
    
        printf("Input your number:");
        scanf("%d",&yournumber);
        if(yournumber=mynumber)
            printf("Ok!you are right!
    ");
        else
            if(yournumber>mynumber)
                printf("Sorry!your number is bigger than my number!
    ");
            else
                printf("Sorry!your number is smaller than my number!
    ");
    
            return 0;
    }

    3.1例3-2

    /* 判断整数的奇偶性 */
    #include<stdio.h>
    int main(void)
    {
        int number;
    
        printf("Enter a number:");
        scanf("%d",&number);
        if(number%2==0){
            printf("The number is even.
    ");
        }
        else{
            printf("The number is odd.
    ");
        }
    
        return 0;
    }

    3.1例3-3

    /* 输入一批学生的成绩,计算平均分,并统计不及格成绩的学生人数 */
    #include<stdio.h>
    int main(void)
    {
        int count,i,n;
        double grade,total;
    
        printf("Enter n:");
        scanf("%d",&n);
        total=0;
        count=0;
        for(i=1;i<=n;i++){
            printf("Enter garde#%d:",i);
            scanf("%lf",&grade);
            total=total+grade;
            if(grade<60){
                count++;
            }
        }
        printf("Grade average=%.2f
    ",total/n);
        printf("Number of failures=%d
    ",count);
    
        return 0;
    }

    3.1-例3-4

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

    3.2例3-5

    /* 求解简单的四则运算表达式 */
    #include<stdio.h>
    int main(void)
    {
        double value1,value2;
        char op;
        printf("Type in an expression:");
        scanf("%lf%c%lf",&value1,&op,&value2);
    
        if(op=='+')
            printf("=%.2f
    ",value1+value2);
        else if(op=='-')
            printf("=%.2f
    ",value1-value2);
        else if(op=='*')
            printf("=%.2f
    ",value1*value2);
        else if(op=='/')
            printf("=%.2f
    ",value1/value2);
        else
            printf("Unknown operator
    ");
    
        return 0;
    }

    3.2例3-7

    /* 统计字符,包括英文字母、数字字符和其他字符 */
    #include<stdio.h>
    int main(void)
    {
        int digit,letter,other;
        char ch;
        int i;
        digit=letter=other=0;
        printf("Enter 10 characters:");
        for(i=1;i<=10;i++){
            ch=getchar();
            if((ch>='a'&&ch<='z')||(ch>='A'&&ch<='Z'))
                letter++;
            else if(ch>='0'&&ch<='9')
                digit++;
            else
                other++;
        }
        printf("letter=%d,digit=%d,other=%d
    ",letter,digit,other);
    
        return 0;
    
    }

    3.2例3-8

    /* 查询自动售货机中商品的价格 */
    #include<stdio.h>
    int main(void)
    {
        int choice,i;
        double price;
    
        for(i=1;i<=5;i++){
            printf("[1]Select crisps
    ");
            printf("[2]Select popcorn
    ");
            printf("[3]Select chocolate
    ");
            printf("[4]Select cola
    ");
            printf("[0]Select exit
    ");
    
            printf("Enter choice:");
            scanf_s("%d",&choice);
            
            if(choice==0)
                break;
            switch(choice){
                case 1:price=3.0;break;
                case 2:price=4.0;break;
                case 3:price=3.5;break;
                case 4:price=0.0;break;
                default:price=0.0;break;
            }
    
            printf("price=%0.1f
    ",price);
        }
            printf("Thanks
    ");
    
            return 0;
        }

    3.3例3-9

    /* 求解简单的四则运算表达式 */
    #include<stdio.h>
    int main(void)
    {
        double value1,value2;
        char op;
    
        printf("Type in an expression:");
        scanf_s("%lf%clf",&value1,&op,&value2);
        switch(op){
        case'+':
            printf("=%.2f
    ",value1+value2);
            break;
        case'-':
            printf("=%.2f
    ",value1-value2);
            break;
        case'*':
            printf("=%.2f
    ",value1*value2);
            break;
        case'/':
            printf("=%.2f
    ",value1/value2);
            break;
        default:
            printf("Unknown operator
    ");
            break;
    
        }
    
    
        return 0;
    }

    3.3例3-10

    /* 统计字符,包括空格或回车、数字字符和其他字符 */
    #include<stdio.h>
    int main(void)
    {
        int blank,digit,other;
        char ch;
        int i;
        blank=digit=other=0;
        printf("Enter 10 characters:");
        for(i=1;i<+10;i++){
            ch=getchar();
    
            switch(ch){
            case'':
            
            case'
    ':
                blank++;
                break;
            case'0':case'1':case'2':case'3':case'4':
            case'5':case'6':case'7':case'8':case'9':
                digit++;
                break;
            default:
                other++;
                break;
            }
        }
        printf("blank=%d,digit=%d,other=%d
    ",blank,digit,other);
    
        return 0;
    }

     

    3.3例3-11

    /* 求解简单的四则运算表达式 */
    #include<stdio.h>
    int main(void)
    {
        double value1,value2;
        char op;
    
        printf("Type in an expression:");
            scanf("%lf%c%lf",&value1,&op,&value2);
        if(op=='+')
            printf("=%.2f
    ",value1+value2);
        else if(op=='-')
            printf("=%.2f
    ",value1-value2);
        else if(op=='*')
            printf("=%.2f
    ",value1*value2);
        else if(op=='/')
            if(value2!=0)
                printf("=%.2f
    ",value1/value2);
            else
                printf("Divisor can not be 0!
    ");
            else
                printf("Unknown operator!
    ");
    
            return 0;
    }

  • 相关阅读:
    Android Studio同时监听多个Button实现activity跳转
    Android Studio--Activity实现跳转功能
    Gym 101170F Free Weights(二分)题解
    Gym 101170I Iron and Coal(BFS + 思维)题解
    Gym 101128F Landscaping(网络流)题解
    Gym 101128A Promotions(思维 + dfs)题解
    Gym 101480I Ice Igloos(思维乱搞)题解
    Gym 101480F Frightful Formula(待定系数)题解
    Gym 101128J Saint John Festival(凸包 + 二分判点和凸包关系)题解
    CodeForces 348D Turtles(LGV定理)题解
  • 原文地址:https://www.cnblogs.com/jianghaoyu0129/p/3350706.html
Copyright © 2020-2023  润新知