Parsing the use of choice structures and switch statements in C++ programming

  • 2020-04-02 03:20:11
  • OfStack

C++ to write the program to choose the structure
Next, two examples illustrate how to write a more complex C++ program.

Program [di 'spknt] n. To determine whether a particular year is a leap year.


#include <iostream>
using namespace std;
int main( )
{
  int year;
  bool leap;
  cout<<"please enter year:";//Output prompts
  cin>>year; //Enter the year
  if (year%4==0) //The year is divisible by 4
  {
   if(year%100==0)//The year is divisible by 4 And can be 100 aliquot 
   {
     if (year%400==0)//The year is divisible by 4 And can be 400 aliquot 
      leap=true;//Make leap=true in a leap year
     else
      leap=false;
   } //Non-leap year, make leap=false
   else //The year is divisible by 4 But cannot be 100 Divisible is definitely a leap year 
     leap=true;
  } //Is a leap year. Make leap=true
  else //A year that is not divisible by 4 is certainly not a leap year
   leap=false; //If it is a non-leap year, make leap=false
  if (leap)
   cout<<year<<" is "; //If leap is true, export the year and "yes"
  else
   cout<<year<<" is not ";//If leap is true, export the year and "no"
  cout<<" a leap year."<<endl; //Output "leap year"
  return 0;
}

The operation is as follows:


 1.  2005 � 
2005 is not a leap year.
 2.  1900 � 
1900 is npt a leap year.

You can also rewrite lines 8 to 16 as the following if statement:


if(year%4!=0)
  leap=false;
else if(year%100!=0)
  leap=true;
else if(year%400!=0)
  leap=false;
else
  leap=true;

You can also use a logical expression that contains all the leap year conditions, replacing the above if statement with the following if statement:
If ((year%4 == 0 && year%100! =0) || (year%400 == 0)) leap=true;
The else leap = false;

Transportation companies calculate freight charges to users. The further the distance (s), the lower the freight per kilometer. The criteria are as follows:


  s<250km  There is no discount 
  250 Or less s<500  2% discount 
  500 Or less s<1000  5% discount 
  1000 Or less s<2000  8% discount 
  2000 Or less s<3000  10% discount 
  3000 Or less s  15% discount 

Suppose that the basic freight per ton per kilometer is p(abbreviation of price), the cargo weight is w(abbreviation of Wright), the distance is s, and the discount is d(abbreviation of discount), then the calculation formula of total freight f(abbreviation of freight) is


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

Write the program as follows:


#include <iostream>
using namespace std;
int main( )
{
  int c,s;
  float p,w,d,f;
  cout<<"please enter p,w,s:";
  cin>>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);
  cout<<"freight="<<f<<endl;
  return 0;
}

The operation is as follows:


please enter p,w,s:100 20 300 � 
freight=588000

C++ switch statement (multiple branch structure)
Switch statement is a multi - branch selection statement, used to implement multi - branch selection structure. Its general form is as follows:


switch( expression )
{
  case  Constant expression 1: statements 1
  case  Constant expression 2: statements 2
  ...
  case  Constant expression n: statements n
  default: statements n+1
 }

For example, if you want to print out the percentile score segment according to the grade of the test score, you can use the switch statement:


switch(grade)
{
  case 'A': cout<<"85~100n";
  case 'B': cout<<"70~84n";
  case 'C': cout<<"60~69n";
  case 'D': cout<<"<60n";
  default: cout<<"errorn";
}

Description:
1) the "expression" in parentheses after the switch is allowed to be of any type.

2) when the value of the switch expression matches the value of the constant expression in a case clause, the embedded statement in this case clause is executed. If the value of the constant expression in all case clauses cannot match the value of the switch expression, the embedded statement of the default clause is executed.

3) the values of each case expression must be different from each other, or there will be contradictory phenomena (there are two or more execution schemes for the same value of the expression).

4) the occurrence order of each case and default does not affect the execution result. For example, "default:..." might appear first. , "case 'D':..." "And then" case 'A':..." .

5) after the execution of one case clause, the process control is transferred to the next case clause to continue the execution. A "case constant expression" is a statement label, not a conditional judgment. When the switch statement is executed, the matching case clause is found according to the value of the switch expression, and the execution starts from this case clause without judgment. For example, in the above example, if the value of grade is equal to 'A', the output will be continuous:


  85~100
  70~84
  60~69
  <60
  error

Therefore, after a case clause is executed, the process should jump out of the switch structure, or terminate the execution of the switch statement. You can do this with a break statement. Rewrite the above switch structure as follows:


switch(grade)

{
  case 'A': cout<<"85~100n";break;
  case 'B': cout<<"70~84n";break;
  case 'C': cout<<"60~69n";break;
  case 'D': cout<<"<60n";break;
  default: cout<<"errorn";break;
}


The last clause (default) can be used without a break statement. If the grade value is 'B', only '70-84' is printed.
 

Although the case clause contains more than one execution statement, it can automatically execute all the execution statements in this case clause in sequence without having to enclose them in curly braces.

6) multiple cases can share a set of execution statements, such as


  case 'A':
  case 'B':
  case 'C': cout<<">60n";break;
  ...

When grade is 'A'? The same set of statements is executed for 'B' or 'C'.


Related articles: