计算导论与c语言基础week 9.docxVIP

  • 1
  • 0
  • 约8.75千字
  • 约 35页
  • 2021-05-16 发布于北京
  • 举报
《计算概论A》课程 程序设计部分C 语言的基本成分 — 《计算概论A》课程 程序设计部分 C 语言的基本成分 — 控制成分 李 戈 信息科学技术学院 lige@sei.pku. 研究所 .cn 计算机程序的基本结构什么样的结构才能支持程序运行的逻辑? 计算机程序的基本结构 什么样的结构才能支持程序运行的逻辑? 1966年,G. Jacopini 和 C. Bohm在“Communications of the ACM”上发表论论文“Flow Diagrams, Turing Machines and Languages with Only Two Formation Rules” 。 从理论上证明了:任何具有单入口单出口的程序都可以用三 种基本结构表达: 顺序结构 分支结构 循环结构 C. Bohm G. Jacopini, Flow Diagrams, Turing Machines and Languages with Only Two Formation Rules, Communications of the ACM, vol9(5) May 1966, pp 366-371. 分支语句 分支语句 #includeiostream #includeiostream using namespace std; int main() { float weight = 0, height = 0, healthRate = 0; cin weightheight; healthRate = weight / (height*height); if ((18 = healthRate)(healthRate = 25)) cout 体重适中! endl; else if ((25 healthRate)(healthRate = 30)) cout 超重!注意控制! endl; else if (( 30 healthRate)(healthRate = 35)) cout 肥胖!减肥吧! endl; else if ((35 healthRate)(healthRate = 40)) cout 重度肥胖!别吃了! endl; else cout 请直接拨打120! endl; return 0; } 分支语句 分支语句 #include iostream #include iostream using namespace std; int main() { int year = 0; cin year; if (year % 4 == 0) { if (year % 100 == 0) { if (year % 400 == 0) cout Y; else cout N; } else cout Y; } else cout N; return 0; } if 语句使用须知在执行 if 语句前先对表达式求解if ( )内可以是任意的数值类型(包括整型、实型、字符型、指针型数据)if if 语句使用须知 在执行 if 语句前先对表达式求解 if ( )内可以是任意的数值类型 (包括整型、实型、字符型、指针型数据) if (‘a’) cout‘a’endl; cout“OK”endl; if (3) 若表达式的值为 0,按“假”处理 若表达式的值为 非0,按“真”处理 多分支语句Switch语句的一般形式如下:当 多分支语句 Switch语句的一般形式如下: 当表达式的值与某一个case后面的常量表达式的值相等时,就执行此case后面的语句,若所有的case中的常量表达式的值都没有与表达式的值匹配的,就执行 ault后面的语句。 switch(表达式) { case 常量表达式1: 语句1; case 常量表达式2: 语句2; … ; case 常量表达式n: 语句n; ault: 语句n+1; } #includeiostream #includeiostream using namespace std; int main() { char grade = a; cingrade; switch (grade) { case a: cout 85~100 endl; case b: cout 70~84 endl; case c: cout 60~69 endl; case d: cout <60 endl; ault: cout error endl; } return 0; } #includeiostream #includeiostream using namespace std; int main() { char grade = a; cin

文档评论(0)

1亿VIP精品文档

相关文档