Exercise 4 - Select Structured Programming

1. C4-5. Calculation of agency fees

[Problem Description]

A company stipulates that the agent agency fee is calculated according to the following table:

Transaction amount (yuan) Agency fee

(0,3000) 18+1.2% of principal

[3000,6000) 36+0.6% of principal

[6000~10000) 54+0.3% of principal

Program to calculate and print the agency fee that should be withdrawn for any transaction amount below 10,000 yuan. (1. Complete with switch statement; 2. Complete with if statement)

When wrong data is entered, a "data error!" prompt will be given.

[Input form]

Enter the transaction amount.

[Output form]

Output agency fee.

[Sample input]

3500.00

[Sample output]

the cost is 57.00 RMB

[Example description]
[Grading]

#include<stdio.h>
int main()
{
    double a;
    int b;
    scanf("%lf",&a);
    if(a>0  && a<10000)
    {
       b = a/3000;
       switch(b)
       {
         case 0:printf("the cost is %.2f RMB\n",18+a*0.012);break;
         case 1:printf("the cost is %.2f RMB\n",36+a*0.006);break;
         case 2:printf("the cost is %.2f RMB\n",54+a*0.003);break;
         default :printf("data error!\n"); 
       }
     }
    else
    {
       printf("data error!");
    }
        return 0;
}

2. C4-4. Programming with nested if statements

[Problem Description]

Write a program with nested if statements, find the value of y, a (the value of a is positive) and x, and input it through the keyboard.

When x is +a or -a, y is 0;

When -a<x<a, y is sqrt(aa-xx);

When x>a or x<-a, y is x.

[Input form]

Enter a (positive number) and a value for x.

[Output form]

Output the values โ€‹โ€‹of a, x, and y.

[Sample input]

a=5.5,x=12.5

[Sample output]

a=5.50,x=12.50,y=12.50

[Example description]
[Grading]

#include<stdio.h>
#include<math.h>
int main()
{
    double a,x,y;
    scanf("a=%lf,x=%lf ",&a,&x);
    if(x>a || x<-a) 
         y = x;
    else if((x>-a) && (x<a))
         y = sqrt(a*a-x*x);
    else if(x == a || x == -a)
         y = 0;
    printf("a=%.2f,x=%.2f,y=%.2f\n",a,x,y);
     return 0;
}

3. C4-2. Find the building height of the point

[Problem Description]

There are 4 circular towers, the centers of which are (2, 2), (-2, 2), (-2, -2), (2, -2), and the radius of the circle is 1. The height of these four towers is 10m, and there are no buildings outside the towers. Now input the coordinates of any point, find the height of the building at that point (the height outside the tower is zero). See textbook P109, question 12 for the diagram

[Input form]

Coordinates of a point: enter coordinate values โ€‹โ€‹(x, y)

[Output form]

output the height of the point

[Sample input]

//The following are two sets of test data

2.0,2.0

0.0,0.0

[Sample output]

height is 10

height is 0

[Example description]
[Grading]

#include<stdio.h>
#include<math.h>
int main()
{
    double x,y,a,b,c,d;
    int h;
    scanf("%lf,%lf",&x,&y);
    a = sqrt((x-2)*(x-2)+(y-2)*(y-2));
    b = sqrt((x+2)*(x+2)+(y-2)*(y-2));
    c = sqrt((x+2)*(x+2)+(y+2)*(y+2));
    d = sqrt((x-2)*(x-2)+(y+2)*(y+2));
    if(a<1 || b<1 || c<1|| d<1)
       printf("height is 10");
    else
       printf("height is 0");
    return 0;
}

4. C4-1. Conversion of percentile scores into grades

[Problem Description]

Given a percentile grade, request output grades 'A', 'B', 'C', 'D', 'E'. Above 90 points is 'A', 8089 is 'B', 7079 is 'C', 60~69 is 'D', and below 60 is 'E'. In other cases, please output "Input Error".

[Input form]

An integer: Enter a percentile grade

[Output form]

Output the grade corresponding to the grade

[Sample input]

//The following are multiple sets of test data

70

100

85

66

45

102

[Sample output]

//The following are the results corresponding to multiple sets of data

Level C

Level A

Level B

Level D

Level E

Input Error

[Example description]
[Grading]

#include<stdio.h>
int main()
{
    int a,d;
    scanf("%d",&a);
    d = a/10;
    if(a<101 && a>=0)
    switch(d)
    {
      case 10 :
      case  9 :printf("Level A\n");break;
      case  8 :printf("Level B\n");break;
      case  7 :printf("Level C\n");break;
      case  6 :printf("Level D\n");break;
      case  5 :
      case  4 :
      case  3 :
      case  2 :
      case  1 :
      case  0 :printf("Level E\n");break;
    }
    else 
      printf("Input Error\n");
    return 0;
}

This inspirational quote:
The prerequisite for a person to be lucky is that he has the ability to change himself

Tags: C Algorithm C++

Posted by dearmoawiz on Sun, 15 Jan 2023 14:28:07 +0530