C++ to write a program that selects structure
Below, we will use two examples to illustrate how to write more complex C++ programs.
[Example] Write a program to determine whether a certain year is a leap year.
#include <iostream> using namespace std; int main( ) { int year; bool leap; cout<<"please enter year:";//Output prompt cin>>year; //Enter year if (year%4==0) //The year can be divided by 4 { if(year%100==0)//The year can be divided by 4 and 100 can be divided by 100 { if (year%400==0)//The year can be divided by 4 and 400 can be divided by 400 leap=true;//Leap year, let leap=true(true) else leap=false; } //Not leap year, let leap=false(false) else //The year can be divided by 4 but cannot be divided by 100, it is definitely a leap year leap=true; } //It is a leap year, let leap=true else //The year cannot be divided by 4, is definitely not a leap year leap=false; //If it is a non-leap year, let leap=false if (leap) cout<<year<<" is "; //If leap is true, the year and "yes" are output else cout<<year<<" is not ";//If leap is true, the year and "not" are output cout<<" a leap year."<<endl; //Output "leap year" return 0; }
The operation is as follows:
① 2005↙ 2005 is not a leap year. ② 1900↙ 1900 is npt a leap year.
You can also rewrite lines 8 to 16 in the program into 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 to contain all leap year conditions, and replace the above if statement with the following if statement:
if((year%4 == 0 && year%100 !=0) || (year%400 == 0)) leap=true;
else leap=false;
[Example] The transportation company calculates freight for users. The further the distance (s), the lower the freight per kilometer. The standards are as follows:
s<250km No discount 250≤s<500 2%Discount 500≤s<1000 5%Discount 1000≤s<2000 8%Discount 2000≤s<3000 10%Discount 3000≤s 15%Discount
Suppose the basic freight per ton of cargo per kilometer is p (the abbreviation of price), the weight of the cargo is w (the abbreviation of wright), the distance is s, and the discount is d (the abbreviation of discount), then the calculation formula for total freight f (the abbreviation of freight) is
f = p * w * s * (1 - d)
According to this, the program is 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-select branch structure)
The switch statement is a multi-branch selection statement, which is used to implement a multi-branch selection structure. Its general form is as follows:
switch(expression) { case 常量expression1:Statement1 case 常量expression2:Statement2 ... case 常量expressionn:Statementn default:Statementn+1 }
For example, if you require you to print out the percentage score segment according to the test score level, you can use the switch statement to implement it:
switch(grade) { case 'A': cout<<"85~100\n"; case 'B': cout<<"70~84\n"; case 'C': cout<<"60~69\n"; case 'D': cout<<"<60\n"; default: cout<<"error\n"; }
illustrate:
1) The "expression" in brackets 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 certain case clause, the embedded statement in this case clause is executed. If the values 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, otherwise contradictory phenomena will occur (there are two or more implementation plans for the same value of the expression).
4) The order of occurrence of each case and default does not affect the execution result. For example, you can first appear "default:...", then "case 'D':...", and then "case 'A':...".
5) After executing a case clause, the process control is transferred to the next case clause to continue execution. "case constant expression" only serves as a statement number, not a conditional judgment is made there. When executing a switch statement, find a matching case clause based on the value of the switch expression, and then start executing the case clause from this time and no longer makes judgments. For example, in the above example, if the value of grade is equal to 'A', the output will be continuously:
85~100 70~84 60~69 <60 error
Therefore, after executing a case clause, the process should be made out of the switch structure, that is, the execution of the switch statement should be terminated. This can be achieved with a break statement. Rewrite the switch structure above as follows:
switch(grade) { case 'A': cout<<"85~100\n";break; case 'B': cout<<"70~84\n";break; case 'C': cout<<"60~69\n";break; case 'D': cout<<"<60\n";break; default: cout<<"error\n";break; }
The last clause (default) can be used without break statements. If the value of grade is 'B', only "70~84" is output.
Although the case clause contains more than one execution statement, it is not necessary to enclose it in curly braces, and all execution statements in this case clause will be automatically executed in sequence.
6) Multiple cases can share a set of execution statements, such as
case 'A': case 'B': case 'C': cout<<">60\n";break; ...
The same set of statements are executed when the value of grade is 'A'?'B' or 'C'.