第02章基本编程语句.ppt

上传人:本田雅阁 文档编号:2250335 上传时间:2019-03-11 格式:PPT 页数:26 大小:108.01KB
返回 下载 相关 举报
第02章基本编程语句.ppt_第1页
第1页 / 共26页
第02章基本编程语句.ppt_第2页
第2页 / 共26页
第02章基本编程语句.ppt_第3页
第3页 / 共26页
亲,该文档总共26页,到这儿已超出免费预览范围,如果喜欢就下载吧!
资源描述

《第02章基本编程语句.ppt》由会员分享,可在线阅读,更多相关《第02章基本编程语句.ppt(26页珍藏版)》请在三一文库上搜索。

1、第2章 基本编程语句,内容,2.1 说明语句(Dlelarative Statements) 2.2 条件语句( Condition Statements ) 2.3 循环语句(Loop Statements) 2.4 循环设计(Loop Designs) 2.5 输入输出语句(I/O Stataments) * 2.6 转移语句(Move Stataments),2.1 说明语句(Dlelarative Statements),2.1.1 变量定义 功能:声明了名称,为变量分配了存储空间 建议: 在变量定义的同时,为变量赋初值,/= / f0201.cpp/ 变量定义 /= #include

2、 using namespace std; /- int main() double radius; / 定义语句 coutradius; double result = radius*radius*3.14*4; / 定义语句 cout“The result is “result“n“; /=,2.1 说明语句(Dlelarative Statements),2.1.2 函数定义与声明(Function Declaration & Definition),/= f0203.cpp/ 拆成两个函数= #include using namespace std; void sphere(); in

3、t main() sphere(); void sphere() double radius; coutradius; if(radius 0) return; cout“The result is “radius*radius*3.14*4“n“; /=,2.2 条件语句,2.2.1 if语句(if Statement) 2.2.2 条件表达式(Conditional Expresssion) 2.2.3 switch语句(switch Statement),2.2 条件语句,2.2.1 if语句(if Statement) 形式1 格式:if (expression) statement

4、执行过程 形式2 格式:if (expression) statement1 else statement2 执行过程,2.2 条件语句,2.2.2 条件表达式(Conditional Expresssion) 语法形式:(条件?表达式1:表达式2) 2.2.3 switch语句(switch Statement) Swith语句注意事项 1. switch括号中的表达式只能是整形、字符型或枚举类型 2. defalut分支 3. case值即标号不能相同 4. 遇break 跳出,2.3 循环语句(Loop Statements),2.3.1 for循环结构(for Loop Structu

5、re) 2.3.2 for 循环(for Loop) 2.3.3 while循环(while Loop) 2.3.4 do-while 循环(do-while Loop),2.3 循环语句(Loop Statements),2.3.1 for循环结构(for Loop Structure) 一般形式 for(表达式1 ;表达式2 ;表达式3) 循环体语句 执行流程 for语句中expr1, expr2 ,expr3 类型任意,都可省略,但分号不可省,2.3 循环语句(Loop Statements),2.3.3 while循环(while Loop) 一般形式: while(表达式) 循环体语

6、句 执行流程:,2.3 循环语句(Loop Statements),2.3.4 do-while 循环(do-while Loop) 一般形式 do 循环体语句; while(表达式) ; 执行流程 特点:先执行循环体,后判断表达式,2.4 循环设计(Loop Designs),2.4.1 字符图形 2.4.2 素数判定(自学),2.5 输入输出语句(I/O Stataments) *,2.5.1 标准I/ O流(Standard I/O Stream) 2.5.2 流状态(Stream States) 2.5.3 文件流(File Streams),2.5 输入输出语句(I/O Statam

7、ents) *,2.5.1 标准I/ O流(Standard I/O Stream) I/O的任务:稳定可靠的在设备和内存之间传送字节 C+标准IO流实现方法:高级语言的抽象特性,将输入输出设备的底层操作透明化,编程人员可以将输入输出想象成为流信息 : cin输入对象, cout输出对象 声明了对与执行格式化IO的服务 声明了对用户控制文件处理操作非常重要的服务,2.5 输入输出语句(I/O Stataments) *,2.5.2 流状态(Stream States) 常用的流状态 cout3)3); / 0 false 逻辑值用true或false表示 coutfixed12345.678;

8、 / 12345.678000 定点数据输出 coutscientific123456.678; / 1.234568e+05 可续计数法,2.5 输入输出语句(I/O Stataments) *,2.5.2 流状态(Stream States) 带参的常用流状态 语法: cout.width(5); /设置显示宽度, width(int) cout.fill(S); /设置填充字符, fill(char) cout.precision(int); /设置有效位数 例子: #include Int main() cout.width(5); cout.fill(S); cout2323; /

9、输出: SSS2323 ,2.5 输入输出语句(I/O Stataments) *,2.5.2 流状态(Stream States) 与连用的设置 要求:包含iomainip头文件 语法: setw(int) setfill(char) setprecison(int),/= f0209.cpp 倒三角形流状态设置版=/ #include #include using namespace std; /- int main() for(int n=1; n=10; +n) coutsetfill( )setw(n)“ “ setfill(M)setw(21-2*n)“M“endl; /=,2.5

10、 输入输出语句(I/O Stataments) *,2.5.3 文件流(File Streams) 文件分类: 文本文件,内容与字符码表对应 二进制文件,不硬性规定与字符码表对应的关系,内容始终时0/1串 打开/关闭文件:文件流的输入出操作与标准输入出基本相同,只是需要以一定方式打开和关闭: ifstream fin(filename, opermode=ios:in); ofstream fout(filename, openmode=ios:out);,/= f0211.cpp 拷贝文件=/ #include using namespace std; /- int main() ifstr

11、eam in(“a.in“); ofstream out(“a.out“); for(string str; getline(in, str); ) outstrendl; /=,/= f0212.cpp 用筛法判断素数=/ #include #include #include using namespace std; /- int main() vector prime(10000,1); for(int i=2; ia /=,2.6 转移语句(Move Stataments),2.6.1 break语句 使程序从循环体和switch语句内跳出,继续执行逻辑上的下一条语句。不宜用在别处。 2

12、.6.2 continue语句 结束本次循环,接着判断是否执行下一次循环。 2.6.3 goto语句 goto 语句标号; 其中,语句标号用来表示语句的标识符,放在语句的最前面,2.7 再做循环设计(More Loop Designs),百钱百鸡问题:公鸡7元1只,母鸡5元1只,小鸡1元三只,问花100元买100只鸡,且公鸡、小鸡、母鸡必须有,问母鸡、小鸡、公鸡各多少只? 分析:采用穷举法 设计变量: 公鸡 :cock 母鸡:hen 小鸡:chick 循环次数: 公鸡:最多(100-5-1)/7 =94/7=132 母鸡:最多(100-7-1)/5= 92/5 = 182 小鸡:最多(100-

13、7-5)*3= 88*3=264 但 由于总数100 ,所以小鸡最多98,/= f0214.cpp 百钱买百鸡= #include using namespace std; /- int main() for(int cock=1; cock=13; +cock) for(int hen=1; hen=18; +hen) for(int chick=1; chick=96; +chick) if(7*cock+5*hen+chick/3-100) continue; /判定钱数为100 if(cock+hen+chick-100) continue; /判定只数为100 if(chick%3)

14、 continue;/判定小鸡数是否为3的倍数 cout“Cock:“cock“, Hens:“hen“, Chicks:“100-cock-henendl; /=,2.7 再做循环设计(More Loop Designs),优化循环要点 减少循环嵌套层次 减少循环次数 减少循环体内的语句,/= / f0216.cpp / 百钱买百鸡 /= #include using namespace std; /- int main() for(int cock=1; cock=13; +cock) for(int hen=1; hen=18; +hen) if(100-cock-hen)%3=0 /=,作业,将P14的例1-1实现,

展开阅读全文
相关资源
猜你喜欢
相关搜索

当前位置:首页 > 其他


经营许可证编号:宁ICP备18001539号-1