例题 第三章 整。

例题3-1

#include<stdio.h>
int main(void)
{
    int mynumber=55;
    int yournumber;

    printf("Input yournumber:");
    scanf("%d",&yournumber);
    if(yournumber==mynumber)
        printf("Ok!you are right!\n");
    else
        if(yournumber>mynumber)
            printf("Sorry!your number is bigger than my number!\n");
        else
            printf("Sorry!your number is smaller than my number!\n");

        return 0;
}

例题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.\n");
    }
    else{
        printf("The number is odd.\n");
    }

    return 0;
}

例题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 grade#%d:",i);
        scanf("%lf",&grade);
        total=total+grade;
        if(grade<60){
            count++;
        }
    }
    printf("Grade average=%.2f\n",total/n);
    printf("Number of failures=%d\n",count);

    return 0;
}

例题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\n",x,y);

    return 0;
}

例题3-5

#include<stdio.h>
int main(void)
{
    double valuel,value2;
    char op;
    printf("Type in an expression:");
    scanf("%lf%c%lf",&valuel,&op,&value2);

    if(op=='+')
        printf("=%.2f\n",valuel+value2);
    else if(op=='-')
        printf("=%.2f\n",valuel-value2);
    else if(op=='*')
        printf("=%.2f\n",valuel*value2);
    else if(op=='/')
        printf("=%.2f\n",valuel/value2);
    else
        printf("Unknown operator\n");

    return 0;
}

例题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\n",letter,digit,other);

    return 0;
}

例题3-8

#include<stdio.h>
int main(void)
{
    int choice,i;
    double price;

    for(i=1;i<=5;i++){

        printf("[1]Select crisps\n");
        printf("[2]Select popcorn\n");
        printf("[3]Select chocolate\n");
        printf("[4]Select cola\n");
        printf("[0]exit\n");

        printf("Enter choice:");
        scanf("%d",&choice);

        if(choice==0)
            break;

        switch(choice){
            case 1:price=3.0;break;
            case 2:price=2.5;break;
            case 3:price=4.0;break;
            case 4:price=3.5;break;
            default:price=0.0;break;
        }

        printf("price=%0.1f\n",price);
    }
    printf("Thanks\n");

    return 0;
}

例题3-9

#include<stdio.h>
int main(void)
{
    double valuel,value2;
    char op;

    printf("Type in an expression:");
    scanf("%lf%c%lf",&valuel,&op,&value2);
    switch(op){
        case'+':
            printf("=%.2f\n",valuel+value2);
            break;
        case'-':
            printf("=%.2f\n",valuel-value2);
            break;
        case'*':
            printf("=%.2f\n",valuel*value2);
            break;
        case '/':
            printf("=%.2f\n",valuel/value2);
            break;
        default:
            printf("Unknown operator\n");
            break;
    }

    return 0;
}

例题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'\n':
                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\n",blank,digit,other);

    return 0;
}

例题3-11

#include<stdio.h>
int main(void)
{
    double valuel,value2;
    char op;

    printf("Type in an expression:");
    scanf("%lf%c%lf",&valuel,&op,&value2);
    if(op=='+')
        printf("=%.2f\n",valuel+value2);
    else if(op=='-')
        printf("=%.2f\n",valuel-value2);
    else if(op=='*')
        printf("=%.2f\n",valuel*value2);
    else if(op=='/')
        if(value2!=0)
            printf("=%.2f\n",valuel/value2);
        else
            printf("Divisor can not be 0!\n");
        else
            printf("Unknown operator!\n");

    return 0;
}

posted @ 2013-10-06 01:36  。墨墨恢非受  阅读(147)  评论(0编辑  收藏  举报