null第2章 程序控制结构第2章 程序控制结构2.1 选择控制
2.2 循环控制
2.3 判断
表
关于同志近三年现实表现材料材料类招标技术评分表图表与交易pdf视力表打印pdf用图表说话 pdf
达式的使用
2.4 转向语句null 语句是程序的基本语法成分。C++程序设计语言的语句按功能可以分成三类:
声明语句:指示编译器分配内存,或者提供程序连接信息。
例如,常量或变量说明语句、函数原型声明语句等。
操作语句:描述对数据的处理。
例如,输入输出语句、赋值语句、表达式语句等。
控制语句:用于控制程序的执行
流程
快递问题件怎么处理流程河南自建厂房流程下载关于规范招聘需求审批流程制作流程表下载邮件下载流程设计
。
例如,if语句、Switch语句、while语句、do_while语句、for语句等。 null 在C++中,程序一般包含三种控制结构:顺序结构、选择结构和循环结构。
顺序结构程序是由声明语句、输入输出语句和赋值语句等构成。 例如,// count the girth and area of circle
#include
using namespace std;
int main ()
{ float r, girth, area ;
const float pi = 3.1415 ;
cout << "Please input radius:\n" ;
cin >> r ;
girth = 2 * pi * r ;
area = pi * r * r ;
cout << "radius = " << r << endl ;
cout << "girth = " << girth << endl ;
cout << "area = " << area << endl ;
}
执行时是按语句出现的先后顺序一个语句一个语句逐个执行的。null 选择结构是按条件选择不同语句执行的程序结构。
用来编写选择结构程序的语句为if语句和switch语句。
循环结构是按条件重复执行某些语句的程序结构。
用来编写循环结构程序的语句为while语句、do-while语句和for语句。 2.1 选择控制2.1 选择控制 实际编写程序时,有时我们需要对给定的条件进行判断,并根据判断的结果选择不同的操作。
例如,给定三条边,判断能否构成三角形。若能构成三角形,则求其面积;否则,显示“不能构成三角形”的信息。
又如,求一元二次方程ax2+bx+c=0的根,要对b2-4ac作判断。当b2-4ac的值等于0时求重根;大于0时求两个实根;小于0时求共轭复根。
构成选择结构的语句称为条件语句,C++有if语句和switch语句。2.1.1 if 语句2.1.1 if 语句null1.if 语句的形式和执行流程 if( 表达式 )语句 ; 语句形式(1)执行流程null1.if 语句的形式和执行流程 if( 表达式 )语句 ; 语句形式(1)执行流程 表达式 语 句true (非0)null1.if 语句的形式和执行流程 if( 表达式 )语句 ; 语句形式(1)执行流程 表达式 false (0)null例:
┇
max = a ;
if ( b > a)
max = b ;
cout << "max = " << max << endl;
┇null3例:
┇
max = a ;
if ( b > a)
max = b ;
cout << "max = " << max << endl;
┇null3例:
┇
max = a ;
if ( b > a)
max = b ;
cout << "max = " << max << endl;
┇null3例:
┇
max = a ;
if ( b > a)
max = b ;
cout << "max = " << max << endl;
┇null5例:
┇
max = a ;
if ( b > a)
max = b ;
cout << "max = " << max << endl;
┇null5例:
┇
max = a ;
if ( b > a)
max = b ;
cout << "max = " << max << endl;
┇null例:
┇
max = a ;
if ( b > a)
max = b ;
cout << "max = " << max << endl;
┇null7例:
┇
max = a ;
if ( b > a)
max = b ;
cout << "max = " << max << endl;
┇null7例:
┇
max = a ;
if ( b > a)
max = b ;
cout << "max = " << max << endl;
┇null7例:
┇
max = a ;
if ( b > a)
max = b ;
cout << "max = " << max << endl;
┇null语句形式(2)if( 表达式 ) 语句1 ;
else 语句2 ; 执行流程null语句形式(2)if( 表达式 ) 语句1 ;
else 语句2 ; 执行流程true (非0) 表达式 语 句 1null语句形式(2)if( 表达式 ) 语句1 ;
else 语句2 ; 执行流程false (0) 表达式 语 句 2null例:
┇
if ( b > a )
max = b ;
else max = a ;
cout << "max = " << max << endl;
┇null例:
┇
if ( b > a )
max = b ;
else max = a ;
cout << "max = " << max << endl;
┇null例:
┇
if ( b > a )
max = b ;
else max = a ;
cout << "max = " << max << endl;
┇
5null例:
┇
if ( b > a )
max = b ;
else max = a ;
cout << "max = " << max << endl;
┇
5null例:
┇
if ( b > a )
max = b ;
else max = a ;
cout << "max = " << max << endl;
┇
null例:
┇
if ( b > a )
max = b ;
else max = a ;
cout << "max = " << max << endl;
┇
7null例:
┇
if ( b > a )
max = b ;
else max = a ;
cout << "max = " << max << endl;
┇
7null例:
┇
if ( b > a )
max = b ;
else max = a ;
cout << "max = " << max << endl;
┇
7试一试
用条件表达式修改该程序null例:
┇
max= ( a > b ) ? a :b;
cout << "max = " << max << endl;
┇
7null2.if 语句的嵌套 在if 语句中的执行语句如果又是另一个if语句,称为嵌套if语句。 例如:if ( x>0 ) y = x ;
else if ( x<0 ) y = -x ;
else y = 0;又如:
if ( score >= 90 ) cout << “Grade A :”;
else if ( score >= 80 ) cout << “Grade B : “;
else if ( score >=70 ) cout << “Grade C : “;
else if ( score >= 60 ) cout << “Grade D : “;
else cout << “Grade E : “;null 例如:if ( x>0 ) y = x ;
else if ( x<0 ) y = -x ;
else y = 0; 即可以写为:
if ( x>0 ){ y = x ;}
else { if ( x<0 ) y = -x ;
else y = 0;}C++规定,else 总是与它最接近的if 配对。配对配对null又如:
if ( score >= 90 ) cout << “Grade A :”;
else if ( score >= 80 ) cout << “Grade B : “;
else if ( score >=70 ) cout << “Grade C : “;
else if ( score >= 60 ) cout << “Grade D : “;
else cout << “Grade E : “;即可以写成:
if ( score >= 90 ){ cout << “Grade A :”;}
else { if ( score >= 80 ) {cout << “Grade B : “;}
else { if ( score >=70 ) {cout << “Grade C : “;}
else { if ( score >= 60 ){ cout << “Grade D : “;}
{ else {cout << “Grade E : “;}
}
}
}
}配对配对配对配对null使用复合语句,可以改变条件语句的配对关系。 例如:
if ( 表达式0 )
if ( 表达式1 ) 语句1 ;
else if ( 表达式2 ) 语句2 ;
else 语句3 ; 要改变配对关系可以改为:
if ( 表达式0 )
{ if ( 表达式1 ) 语句1 ;
else if ( 表达式2 ) 语句2 ;
}
else 语句3 ;等价于:
if ( 表达式0 )
{ if ( 表达式1 ) 语句1 ;
else { if ( 表达式2 ) 语句2 ;
else 语句3 ;
}
}null3.应用举例 例2-1 从键盘上输入三个数,求这3个数之中的最大值。
若输入的三个数分别存储在变量a、b和c中,设置一个变量max存放最大值。
求最大值的算法:先比较a和b的值并把较大值赋给max,然后将c与max比较,若c>max,则把c赋给max。此时,max的值就是三个数中的最大值。null #include
using namespace std;
int main()
{ float a, b, c, max ;
cout << ”a, b, c = “;
cin >> a >> b >> c ;
if ( a>b ) max = a ;
else max = b ;
if ( c > max ) max = c ;
cout << "max = " << max << endl ;
}null #include
using namespace std;
int main()
{ float a, b, c, max ;
cout << ”a, b, c = “;
cin >> a >> b >> c ;
if ( a>b ) max = a ;
else max = b ;
if ( c > max ) max = c ;
cout << "max = " << max << endl ;
}null #include
using namespace std;
int main()
{ float a, b, c, max ;
cout << ”a, b, c = “;
cin >> a >> b >> c ;
if ( a>b ) max = a ;
else max = b ;
if ( c > max ) max = c ;
cout << "max = " << max << endl ;
}max = a > b ? a :b;
null 例2-2 输入三条边,若这三条边构成三角形就求该三角形的面积;否则,输出“不是三角形”的信息。
假设三角形的三条边为a、b和c,这三条边构成三角形的条件是:任意两条边之和都大于第三边。
如果这三条边能构成三角形,则求三角形的面积公式为: 其中s = ( a + b + c ) / 2null#include
#include
using namespace std;
int main()
{ float a, b, c, s, area ;
cout << "a, b, c = " ;
cin >> a >> b >> c ;
if ( a+b > c && b+c > a && c+a > b )
{ s = ( a + b + c ) / 2.0 ;
area = sqrt( s * ( s-a ) * ( s-b ) * ( s-c ) ) ;
cout << "area = " << area << endl ;
}
else cout << "It is not a trilateral!" << endl ;
}null 例2-3 把输入字符转换为小写字母。对输入字符进行判断,如果是大写字母,则转换为小写字母;否则,不转换。 #include
using namespace std;
int main()
{ char ch;
cout << "ch = ";
cin >> ch ;
if ( ch >= 'A' && ch <= 'Z' ) ch += 32 ;
cout << ch << endl ;
}null 例2-3 把输入字符转换为小写字母。对输入字符进行判断,如果是大写字母,则转换为小写字母;否则,不转换。 #include
using namespace std;
int main()
{ char ch;
cout << "ch = ";
cin >> ch ;
if ( ch >= 'A' && ch <= 'Z' ) ch += 32 ;
cout << ch << endl ;
}判别是否为大写字母null 例2-3 把输入字符转换为小写字母。对输入字符进行判断,如果是大写字母,则转换为小写字母;否则,不转换。 #include
using namespace std;
int main()
{ char ch;
cout << "ch = ";
cin >> ch ;
if ( ch >= 'A' && ch <= 'Z' ) ch += 32 ;
cout << ch << endl ;
}可以改写为
ch = ( ch >= 'A' && ch <= 'Z' ) ? ch + 32 : ch ;null 例2-4 求一元二次方程ax2 + bx + c = 0的根。 ① 当a=0 时,方程不是二次方程null…
#include
int main()
{ float a, b, c, d, x1, x2, rp, ip ;
cout << "a, b, c = " ; cin >> a >> b >> c ;
if ( fabs( a ) <= 1e-8 )
cout << " It is not quadratic." << endl ;
else { d = b * b - 4 * a * c ;
if ( fabs( d ) <= 1e-8 )
cout << "It has two equal real roots: " << -b / ( 2*a ) << endl ;
else if ( d > 1e-8 )
{ x1 = ( -b + sqrt( d ) ) / ( 2*a ) ; x2 = ( -b - sqrt( d ) ) / ( 2*a ) ;
cout <<"It has two distinct real roots: "<
int main()
{ float a, b, c, d, x1, x2, rp, ip ;
cout << "a, b, c = " ; cin >> a >> b >> c ;
if ( fabs( a ) <= 1e-8 )
cout << " It is not quadratic." << endl ;
else { d = b * b - 4 * a * c ;
if ( fabs( d ) <= 1e-8 )
cout << "It has two equal real roots: " << -b / ( 2*a ) << endl ;
else if ( d > 1e-8 )
{ x1 = ( -b + sqrt( d ) ) / ( 2*a ) ; x2 = ( -b - sqrt( d ) ) / ( 2*a ) ;
cout <<"It has two distinct real roots: "<
int main()
{ float a, b, c, d, x1, x2, rp, ip ;
cout << "a, b, c = " ; cin >> a >> b >> c ;
if ( fabs( a ) <= 1e-8 )
cout << " It is not quadratic." << endl ;
else { d = b * b - 4 * a * c ;
if ( fabs( d ) <= 1e-8 )
cout << "It has two equal real roots: " << -b / ( 2*a ) << endl ;
else if ( d > 1e-8 )
{ x1 = ( -b + sqrt( d ) ) / ( 2*a ) ; x2 = ( -b - sqrt( d ) ) / ( 2*a ) ;
cout <<"It has two distinct real roots: "<
int main()
{ float a, b, c, d, x1, x2, rp, ip ;
cout << "a, b, c = " ; cin >> a >> b >> c ;
if ( fabs( a ) <= 1e-8 )
cout << " It is not quadratic." << endl ;
else { d = b * b - 4 * a * c ;
if ( fabs( d ) <= 1e-8 )
cout << "It has two equal real roots: " << -b / ( 2*a ) << endl ;
else if ( d > 1e-8 )
{ x1 = ( -b + sqrt( d ) ) / ( 2*a ) ; x2 = ( -b - sqrt( d ) ) / ( 2*a ) ;
cout <<"It has two distinct real roots: "<
int main()
{ float a, b, c, d, x1, x2, rp, ip ;
cout << "a, b, c = " ; cin >> a >> b >> c ;
if ( fabs( a ) <= 1e-8 )
cout << " It is not quadratic." << endl ;
else { d = b * b - 4 * a * c ;
if ( fabs( d ) <= 1e-8 )
cout << "It has two equal real roots: " << -b / ( 2*a ) << endl ;
else if ( d > 1e-8 )
{ x1 = ( -b + sqrt( d ) ) / ( 2*a ) ; x2 = ( -b - sqrt( d ) ) / ( 2*a ) ;
cout <<"It has two distinct real roots: "<
int main()
{ float a, b, c, d, x1, x2, rp, ip ;
cout << "a, b, c = " ; cin >> a >> b >> c ;
if ( fabs( a ) <= 1e-8 )
cout << " It is not quadratic." << endl ;
else { d = b * b - 4 * a * c ;
if ( fabs( d ) <= 1e-8 )
cout << "It has two equal real roots: " << -b / ( 2*a ) << endl ;
else if ( d > 1e-8 )
{ x1 = ( -b + sqrt( d ) ) / ( 2*a ) ; x2 = ( -b - sqrt( d ) ) / ( 2*a ) ;
cout <<"It has two distinct real roots: "<
int main()
{ float a, b, c, d, x1, x2, rp, ip ;
cout << "a, b, c = " ; cin >> a >> b >> c ;
if ( fabs( a ) <= 1e-8 )
cout << " It is not quadratic." << endl ;
else { d = b * b - 4 * a * c ;
if ( fabs( d ) <= 1e-8 )
cout << "It has two equal real roots: " << -b / ( 2*a ) << endl ;
else if ( d > 1e-8 )
{ x1 = ( -b + sqrt( d ) ) / ( 2*a ) ; x2 = ( -b - sqrt( d ) ) / ( 2*a ) ;
cout <<"It has two distinct real roots: "<
int main()
{ float a, b, c, d, x1, x2, rp, ip ;
cout << "a, b, c = " ; cin >> a >> b >> c ;
if ( fabs( a ) <= 1e-8 )
cout << " It is not quadratic." << endl ;
else { d = b * b - 4 * a * c ;
if ( fabs( d ) <= 1e-8 )
cout << "It has two equal real roots: " << -b / ( 2*a ) << endl ;
else if ( d > 1e-8 )
{ x1 = ( -b + sqrt( d ) ) / ( 2*a ) ; x2 = ( -b - sqrt( d ) ) / ( 2*a ) ;
cout <<"It has two distinct real roots: "<
int main()
{ float a, b, c, d, x1, x2, rp, ip ;
cout << "a, b, c = " ; cin >> a >> b >> c ;
if ( fabs( a ) <= 1e-8 )
cout << " It is not quadratic." << endl ;
else { d = b * b - 4 * a * c ;
if ( fabs( d ) <= 1e-8 )
cout << "It has two equal real roots: " << -b / ( 2*a ) << endl ;
else if ( d > 1e-8 )
{ x1 = ( -b + sqrt( d ) ) / ( 2*a ) ; x2 = ( -b - sqrt( d ) ) / ( 2*a ) ;
cout <<"It has two distinct real roots: "<
int main()
{ float a, b, c, d, x1, x2, rp, ip ;
cout << "a, b, c = " ; cin >> a >> b >> c ;
if ( fabs( a ) <= 1e-8 )
cout << " It is not quadratic." << endl ;
else { d = b * b - 4 * a * c ;
if ( fabs( d ) <= 1e-8 )
cout << "It has two equal real roots: " << -b / ( 2*a ) << endl ;
else if ( d > 1e-8 )
{ x1 = ( -b + sqrt( d ) ) / ( 2*a ) ; x2 = ( -b - sqrt( d ) ) / ( 2*a ) ;
cout <<"It has two distinct real roots: "<
int main()
{ float a, b, c, d, x1, x2, rp, ip ;
cout << "a, b, c = " ; cin >> a >> b >> c ;
if ( fabs( a ) <= 1e-8 )
cout << " It is not quadratic." << endl ;
else { d = b * b - 4 * a * c ;
if ( fabs( d ) <= 1e-8 )
cout << "It has two equal real roots: " << -b / ( 2*a ) << endl ;
else if ( d > 1e-8 )
{ x1 = ( -b + sqrt( d ) ) / ( 2*a ) ; x2 = ( -b - sqrt( d ) ) / ( 2*a ) ;
cout <<"It has two distinct real roots: "<> w ;
cout << " Please input distsnce( kilometre ) : " ;
cin >> s ;
if ( s < 100 ) p = 30 ;
else if ( s < 200 ) p = 27.5 ;
else if ( s < 300 ) p = 25 ;
else if ( s < 400 ) p = 22.5 ;
else p = 20 ;
t = p * w * s ;
cout << " The cost is: " << setprecision(2) << t << ’$’ << endl ;
}想一想:
几个 else 分支语句的次序可以改变吗?null// 计算货物运费
…
int main()
{ double t , p , w , s ;
cout << "Please input weight( ton ) : " ;
cin >> w ;
cout << " Please input distsnce( kilometre ) : " ;
cin >> s ;
if ( s < 100 ) p = 30 ;
else if ( s < 200 ) p = 27.5 ;
else if ( s < 300 ) p = 25 ;
else if ( s < 400 ) p = 22.5 ;
else p = 20 ;
t = p * w * s ;
cout << " The cost is: " << setprecision(2) << t << ’$’ << endl ;
} 例如:
cin >> s ;
if ( s < 300 ) p = 25 ;
else if ( s < 100 ) p = 30 ;
else if ( s < 400 ) p = 22.5 ;
else if ( s < 200 ) p = 27.5 ;
else p = 20 ;
t = p * w * s ;
若输入 s : 150
输出 t 值等于多少?为什么?null解法一 3 个数的 6 种可能排列方式:
a < b < c a < b && b < c
a < c < b a < c && c < b
b < a < c b < a && a < c
b < c < a b < c && c < a
c < a < b c < a && a < b
c < b < a c < b && b < a可以直接
用 6个 if 语句写出程序补充例 2 输入三个整数,按从小到大顺序输出。nullnull注意
if – else 的匹配…
int main()
{ int a, b, c, t ;
cout << ” Please input three integer numbers: ” ;
cin >> a >> b >> c ;
if ( a < b )
if ( b < c ) cout << a << b << c << endl ;
else if ( a< c ) cout << a << c << b << endl ;
else cout << c << a << b << endl;
else if ( a < c ) cout << b << a << c << endl ;
else if ( b < c ) cout << b << c << a << endl ;
else cout << c << b << a << endl ;
}null…
int main()
{ int a, b, c, t ;
cout << ” Please input three integer numbers: ” ;
cin >> a >> b >> c ;
if ( a < b )
if ( b < c ) cout << a << b << c << endl ;
else if ( a< c ) cout << a << c << b << endl ;
else cout << c << a << b << endl;
else if ( a < c ) cout << b << a << c << endl ;
else if ( b < c ) cout << b << c << a << endl ;
else cout << c << b << a << endl ;
}null…
int main()
{ int a, b, c, t ;
cout << ” Please input three integer numbers: ” ;
cin >> a >> b >> c ;
if ( a < b )
if ( b < c ) cout << a << b << c << endl ;
else if ( a< c ) cout << a << c << b << endl ;
else cout << c << a << b << endl;
else if ( a < c ) cout << b << a << c << endl ;
else if ( b < c ) cout << b << c << a << endl ;
else cout << c << b << a << endl ;
}null…
int main()
{ int a, b, c, t ;
cout << ” Please input three integer numbers: ” ;
cin >> a >> b >> c ;
if ( a < b )
if ( b < c ) cout << a << b