Chapter IV selection of structural programming ##C language programming (4th Edition)

Posted by dave_biscuits on Thu, 03 Mar 2022 19:42:14 +0100

if statement

Example 4.1 input the scores of two students a and b, and output the highest score.

The preparation procedure is as follows:

#include <stdio.h>
int main()
{
    float a,b,max;
    printf("please enter a and b:");
    scanf("%f,%f",&a,&b);
    if(a>=b) max=a;
    else max=b;
    printf("max=%6.2f\n",max);
    return 0;
}

Example 4.2 input 3 grades and output them from high to low

The preparation procedure is as follows:

#include <stdio.h>
int main()
{
    float a,b,c,t;
    printf("please enter a,b,c:");
    scanf("%f,%f,%f",&a,&b,&c);
    if(a<b)
        {t=a,a=b;b=t;}
    if(a<c)
        {t=a,a=c;c=t;}
    if(b<c)
        {t=b,b=c;c=t;}
    printf("%6.2f,%6.2f,%6.2f\n",a,b,c);
    return 0;
}

The operation results are as follows:

 

Exchange a and B (a becomes the larger of a and b)

Exchange a and c (at this time, a becomes the largest of the three)

Exchange b and c (b becomes the greater of b and c and the second of the three)

Example 4.3 give the length of three sides of a triangle and calculate the area of the triangle

The preparation procedure is as follows:

#include <stdio.h>
#include <math.h>
int main()
{
    double a,b,c,s,area;
    printf("please enter a,b,c:");
    scanf("%lf,%lf,%lf",&a,&b,&c);
    if(a+b>c&&b+c>a&&c+a>b)
        {s=0.5*(a+b+c);
        area=sqrt(s*(s-a)*(s-b)*(s-c));
        printf("area=%6.2f\n",area);
        }
    else
        printf("It is not a trilateral.\n");
    return 0;
}

The operation results are as follows:

Example 4.4 for promotion, customers who buy more goods are given a discount: 5% for those who buy more than 50 (including 50), 7.5% for those who buy more than 100 (including 100), 10% for those who buy more than 300 (including 300), and 15% for those who buy more than 500 (including 500). The user is required to write a program, input the purchase quantity and unit price, and the program outputs the payment payable.

Solution: nested use of if

Payment payable = number of pieces * unit price * (1-preferential discount)

The preparation procedure is as follows:

#include <stdio.h>
#include <math.h>
int main()
{
    int number;
    double cost,price,total;
    printf("please enter number and price:");
    scanf("%d,%lf",&number,&price);
    if(number>=500) cost=0.15;
    else
        if(number>=300) cost=0.10;
        else
            if(number>=100) cost=0.075;
            else
                if(number>=50) cost=0.05;
                else cost=0;
    total=number*price*(1-cost);
    printf("Total=%10.2f\n",total);
    return 0;
}

The operation results are as follows:

Note: else is always paired with the nearest unpaired if above it.

Example 4.4 the program can be rewritten as:

    int number;
    double cost,price,total;
    printf("please enter number and price:");
    scanf("%d,%lf",&number,&price);
    if(number>=500) cost=0.15;
    else if(number>=300) cost=0.10;
    else if(number>=100) cost=0.075;
    else if(number>=50) cost=0.05;
    else cost=0;
    total=number*price*(1-cost);
    printf("Total=%10.2f\n",total);
    return 0;

Example 4.5 write a program to judge whether a year is a leap year

The code is as follows:

#include <stdio.h>
int main()
{
    int y,leap;
    printf("please enter a year:");
    scanf("%d",&y);
    if(y%4==0)
    {
        if(y%100==0)
        {
            if(y%400==0)
                leap=1;
            else
                leap=0;
        }
        else
            leap=1;
    }
    else
        leap=0;
    if(leap)
        printf("%d is ",y);
    else
        printf("%d is not ",y);
    printf("a leap year.\n");
    return 0;
}

The operation results are as follows:

The procedure can be rewritten as follows:

    int y,leap;
    printf("please enter a year:");
    scanf("%d",&y);
    if((y%4==0&&y%100!=0)||(y%400==0))
        leap=1;
    else
        leap=0;
    if(leap)
        printf("%d is ",y);
    else
        printf("%d is not ",y);
    printf("a leap year.\n");
    return 0;

Switch statement

Example 4.6 transportation companies calculate freight for users. The farther the transportation distance (expressed in s and expressed in kilometers), the lower the unit freight (expressed in tons and kilometers). The calculation criteria are as follows:

s<250No discount
250<=s<5002% discount
500<=s<10005% discount
1000<=s<20008% discount
2000<=s<300010% discount
3000<=s15% discount

If the freight per ton per kilometer of goods is p, the weight of goods is w, the distance is s and the discount is d, the calculation formula of total freight f is:

f=p*w*s*(1-d) 

The preparation procedure is as follows:

#include <stdio.h>
int main()
{
    int c,s;
    double p,w,d,f;
    printf("Please enter unit price, weight and distance:");
    scanf("%lf,%lf,%d",&p,&w,&s);
    if(s>=3000) c=12;
    else c=s/250;
    switch(c)
    {
        case 0:d=0;break;
        case 1:d=2;break;
        case 2:
        case 3:d=5;break;
        case 4:
        case 5:
        case 6:
        case 7:d=8;break;
        case 8:
        case 9:
        case 10:
        case 11:d=10;break;
        case 12:d=15;break;
    }
    f=p*w*s*(1-d/100.0);
    printf("Freight:%10.2f element\n",f);
    return 0;
}

The operation results are as follows:

Example 4.7 input a character to judge whether it is a capital letter. If so, convert it into a lowercase letter; If not, do not convert. Then output the final character.

The preparation procedure is as follows:

#include <stdio.h>
int main()
{
    char ch;
    scanf("%c",&ch);
    ch=(ch>='A'&&ch<='Z')?(ch+32):ch;
    printf("%c\n",ch);
    return 0;
}

Topics: C