SlideShare une entreprise Scribd logo
1  sur  92
第3章 算法和控制语句
教学目标 ,[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object]
[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object]
3.1  算法初步 ,[object Object],[object Object],[object Object],[object Object]
算法的概念 ,[object Object],[object Object],[object Object]
算法的表示  ,[object Object],[object Object],[object Object],[object Object],[object Object]
[object Object]
[object Object],#include<stdio.h> int main() { int i,n,s=0; scanf(&quot;%d&quot;,&n); i=1; while(i<=n) { s=s+i; i++; } printf(&quot;s=%d&quot;,s); return 0; }
算法举例  ,[object Object]
[object Object]
[object Object]
[object Object]
[object Object]
[object Object]
[object Object],[object Object],[object Object],[object Object],[object Object],[object Object]
三种基本结构  ,[object Object]
[object Object]
[object Object]
[object Object],[object Object],[object Object],[object Object]
3.2  C 语言的标准输入和输出 ,[object Object],[object Object],[object Object],[object Object]
3.2.1 格式化输入输出 ,[object Object],[object Object],[object Object]
[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object]
[object Object],567.789 float a=567.789;printf(“%g”,a); 按 e 、 f 格式中较短的一种输出 g ABC printf(“%s”,“ABC”); 按字符串输出 s ff int a=255;printf(“%x”,a); 按十六进制输出 X  或 x 101 int a=65;printf(“%o”,a); 按八进制输出 o 5.677890e+02 double a=567.789;printf(“%e”,a); 按指数形式输出 E  或 e 567.789000 float a=567.789;printf(“%f”,a); 按浮点数输出 f 567 int a=567;printf(“%u”,a); 按十进制无符号整数输出 u 567 int a=567;printf ( “%d”,a); 按十进制整数输出 d A char a=65;printf(“%c”,a); 按字符输出 c 结果 举例 含义 格式字符
修饰符: 确定数据输出的宽度、精度、小数位数、对齐方式   在 e,f,g 前,指定输出精度为 double 型 在 d,o,x,u 前,指定输出精度为 long 型 l 在八进制和十六进制数前显示前导 0 , 0x # 输出数值时指定左面不使用的空位置自动填 0 0 指定在有符号数的正数前显示正号 (+) + 输出数据在域内左对齐(缺省右对齐 ) - 对实数 , 指定小数点后位数 ( 四舍五入 ) 对字符串 , 指定实际输出位数 .n 输出数据域宽 , 数据长度 <m, 左补空格 ; 否则按实际输出 M 含义 修饰符
[object Object],#include<stdio.h> int main() { int x=1234 , y=3,z=4; float f=123.456; double m=123.456; char ch='a', a[]=&quot;Hello,world!&quot;; printf(&quot;%d %d&quot;,y,z);  printf(&quot;y=%d , z=%d&quot;,y,z); printf(&quot;%8d,%2d&quot;,x,x);  printf(&quot;%f,%8f,%8.1f,%.2f,%.2e&quot;,f,f,f,f,f); printf(&quot;%lf&quot;,m); printf(&quot;%3c&quot;,ch); printf(&quot;%s%15s%10.5s%2.5s%.3s&quot;,a,a,a,a,a); return 0; }
程序运行结果: 3  4 y=3 , z=4 1234,1234 123.456001,123.456001,  123.5,123.46,1.23e+002 123.456000 a Hello,world! Hello,world! Hello Hello Hel
[object Object],[object Object],[object Object],[object Object]
[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],l 修饰符 功  能 h m * 用于 d,o,x 前,指定输入为 short 型整数 用于 d,o,x 前,指定输入为 long 型整数 用于 e,f 前,指定输入为 double 型实数 指定输入数据宽度,遇空格或不可转换字符则结束 抑制符,指定输入项读入后不赋给变量
[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object]
[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object]
[object Object],例  int x; char ch; scanf(“%d”,&x); scanf(“%c”,&ch); printf(“x=%d,ch=%d”,x,ch); 执行: 123  输出: x=123,ch=10 解决方法: ( 1 )用 getchar() 清除 ( 2 )用函数 fflush(stdin) 清除全部剩余内容 (3)  用格式串中空格或“ %*c” 来“吃掉”
例  int x; char ch; scanf(“%d”,&x); ch=getchar(); printf(“x=%d,ch=%d”,x,ch); 执行: 123  输出: x=123,ch=10 例  int x; char ch; scanf(“%d”,&x); scanf(“  %c”,&ch); 或  scanf(“%*c%c”,&ch);
3.2.2 其它输入输出 ,[object Object],[object Object],[object Object],#include<stdio.h>  int main()  {       char c;                  c='B';                  putchar(c);              putchar('42');         putchar(0x42);       return 0; }   BBB
[object Object],[object Object],#include<stdio.h>  int main()  {  char ch;  ch=getchar();  putchar(ch);  printf(&quot;%d&quot;,ch);  return 0; }
3.2.3 C 语言语句 ,[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object]
3.2.4  顺序结构程序设计 ,[object Object],#include<stdio.h> int main() { int x,y,max; scanf(&quot;%d %d&quot;,&x,&y); max=x>y?x:y; printf(&quot;max=%d&quot;,max); return 0; }
3.3  条件语句 ,[object Object],[object Object],[object Object],[object Object],[object Object]
3.3.1  if 语句 ,[object Object],[object Object],[object Object],[object Object],[object Object]
[object Object],[object Object],[object Object],[object Object]
[object Object]
[object Object],#include<stdio.h> int main() { int a,b,c,t; scanf(&quot;%d %d %d&quot;,&a,&b,&c); if(a>b) { t=a; a=b; b=t; } ,[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object]
3.3.2  if  else  语句  ,[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object]
[object Object],#include<stdio.h> int main() { int a,b,max; scanf(&quot;%d %d&quot;,&a,&b); if(a>b) max=a; else max=b; printf(&quot; 最大值: %d&quot;,max); return 0; }
[object Object],#include<stdio.h> int main() { int n,b; scanf(&quot;%d&quot;,&n); if(n%2==1) b=3*n+1; else b=n/2; printf(&quot; 变换后的数为: %d&quot;,b); return 0; }
3.3.3  if else if  语句 ,[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object]
[object Object],[object Object],[object Object],[object Object]
[object Object],#include <stdio.h> int main() {  char c; printf(&quot; 输入一个字符 :&quot;); c=getchar(); if(c<32)  printf(&quot; 是控制字符 &quot;); else if(c>='0'&&c<='9')  printf(&quot; 是数字字符 &quot;); else if(c>='A'&&c<='Z'||c>='a'&&c<='z')  printf(&quot; 是字母 &quot;); else printf(&quot; 是其它字符 &quot;); return 0; } 输入一个字符: ctrl  回车,是控制字符 输入一个字符: A  回车 ,是字母 输入一个字符: 9  回车 ,是数字字符 输入一个字符: /  回车 ,是其它字符
[object Object],[object Object],[object Object],[object Object],[object Object]
if(weekday==0) printf(&quot;%2d&quot;,date); else if(weekday==1) printf(&quot;%7d&quot;,date); else if(weekday==2) printf(&quot;%12d&quot;,date); else if(weekday==3) printf(&quot;%17d&quot;,date); else if(weekday==4) printf(&quot;%22d&quot;,date); else if(weekday==5) printf(&quot;%27d&quot;,date); else printf(&quot;%32d&quot;,date); return 0; } #include <stdio.h> int main() { int date,weekday,original_date=3; scanf(&quot;%d&quot;,&date); if(date<1||date>31) { printf(&quot; 数据输入错误 !&quot;); return 1; } weekday=(date+original_date-1)%7; printf(&quot;2010 年 12 月日历 &quot;); printf(&quot;----------------------------------&quot;); printf(&quot;Sun Mon Tue Wen Thr Fri Sta &quot;); printf(&quot;----------------------------------&quot;);
[object Object],#include <stdio.h> #include<stdlib.h> int main() { int cj; scanf(&quot;%d&quot;,&cj); if(cj<0||cj>100) { printf(&quot; 数据输入错误 &quot;); exit 0; } if(cj>=90&&cj<=100) printf(&quot; 优 &quot;); else if (cj>=80&&cj<90) printf(&quot; 良 &quot;); else if(cj>=70&&cj<80) printf(&quot; 中 &quot;); else if(cj>=60&&cj<70) printf(&quot; 及格 &quot;); else printf(&quot; 不及格 &quot;); return 0; }
3.3.4  条件语句的嵌套 嵌套具有 else 子句的 if 语句 if ( 表达式 1) if ( 表达式 2)  语句序列 1 else  语句序列 2 嵌套不含 else 子句的 if 语句   if ( 表达式 1) 语句序列 1 else  if ( 表达式 2)  语句序列 2  一般形式 if ( 表达式 1) if ( 表达式 2)  语句序列 1 else  语句序列 2 else if( 表达式 3)  语句序列 3 else  语句序列 4
[object Object],if(……) if(……) if(……) else…... else…... else…...
例:  if (a==b) if(b==c) printf(“a==b==c”); else printf(“a!=b”); 修改:  if (a==b) {  if(b==c) printf(“a==b==c”); } else printf(“a!=b”); 实现 if ~ else  正确配对方法:加 { }
[object Object],#include <stdio.h> int main() {  int x,y; printf(&quot; 输入两个整数  x,y:&quot;); scanf(&quot;%d,%d&quot;,&x,&y); if(x!=y) if(x>y)  printf(&quot;x>y&quot;); else  printf(&quot;x<y&quot;); else printf(&quot;x==y&quot;); return 0; } ,[object Object],[object Object],[object Object],[object Object],[object Object],[object Object]
3.3.5  条件语句的应用 ,[object Object],[object Object]
 
3.4  多分支语句 ,[object Object],[object Object],[object Object]
3.4.1  switch 多分支语句 ,[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object]
[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object]
#include <stdio.h> #include <stdlib.h> int main() { int score; scanf(&quot;%d&quot;,&score); if(score<0||score>100 ) { printf(&quot; 输入数据错误 &quot;); exit(0); } switch( score/10 ) { case 10: case 9:printf(&quot; 优秀 &quot;);break; case 8:printf(&quot; 良好 &quot;);break; case 7:printf(&quot; 中等 &quot;);break; case 6:printf(&quot; 及格 &quot;);break; default:printf(&quot; 不及格 &quot;):break } return 0; }
3.4.2  多分支语句的嵌套 ,[object Object],switch(…) { …… if(…) { …… } else { …… } 语句序列 } if 语句嵌套 switch 语句  switch 语句嵌套 switch 语句   switch(…) { …… switch(…) { 语句序列 1 } 语句序列 2 }
3.4.3  多分支语句应用 ,[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object]
#include<stdio.h> int main() { int year; double money,rate,total; printf(&quot; 输入存款和存期 :&quot;); scanf(&quot;%lf %d&quot;,&money,&year); switch(year) { case 1:rate=0.0225;break; case 2:rate=0.0279;break; case 3:rate=0.0333;break; case 5:rate=0.0360;break; case 8:rate=0.0414;break; default:rate=0.0;printf(&quot; 输入的存期错误 !&quot;);break; } total=money+money*rate*year; printf(&quot; 从银行获得的总金额为: %.2lf&quot;,total); return 0; }
[object Object]
3 . 5  循环语句  ,[object Object],[object Object],[object Object],[object Object]
3.5.1  while  循环语句 ,[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object]
[object Object]
[object Object]
3.5.2  do  while  循环语句  ,[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object]
[object Object],#include<stdio.h> int main() { int x,sum=0; do { scanf(&quot;%d&quot;,&x); sum=sum+x; }while(x!=0); printf(&quot;sum=%d&quot;,sum); return 0; }
[object Object]
[object Object],[object Object],[object Object],#include <stdio.h> int main() {  int i,sum=0; scanf(&quot;%d&quot;,&i); while(i<=10) {  sum+=i; i++; } printf(&quot;%d&quot;,sum); return 0; } #include <stdio.h> int main() {  int i,sum=0; scanf(&quot;%d&quot;,&i); do {  sum+=i; i++; }while(i<=10); printf(&quot;%d&quot;,sum); return 0; }
3.5.3  for 循环语句 ,[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object]
#include<stdio.h> #include<stdlib.h> int main() { int i,sum=0,p=1,n; scanf(&quot;%d&quot;,&n); if(n<0) { printf(&quot; 负数没有阶乘! &quot;); exit(0); } for(i=1;i<=n;i++) { p=p*i; sum=sum+p; } printf(&quot; 阶乘的和为: %d&quot;,sum); return 0; } 例 3.30 :计算
[object Object],#include<stdio.h> #include<math.h> #include<stdlib.h> int main() { int i,m,n; scanf(&quot;%d&quot;,&n); if(n<0) { printf(&quot; 输入数据错误! &quot;); exit(0); } m=sqrt(n); for(i=2;i<=m;i++) if(n%i==0)break;  if(i>m) printf(&quot;%d 是素数 &quot;,n); else printf(&quot;%d 不是素数 &quot;,n); return 0; }
[object Object],#include<stdio.h> int main() { int i,m,n,k; for(i=100;i<1000;i++) { m=i/100;  k=i%10;  n=(i-100*m)/10;  if(i==m*m*m+n*n*n+k*k*k)   printf(&quot;%d &quot;,i); } return 0; }
3.5.4  循环语句的嵌套  ,[object Object],while() {  …… while() {  …… } … ... } do {  …… do {  …… }while( ); … ... }while( ); while() {  …… do {  ……  }while( ); …… . } for( ; ;) {  …… do {  …… }while(); …… while() {  …… } … ... } ,[object Object],[object Object],[object Object],[object Object],[object Object]
[object Object],乘法九九表 ------------------------------------------------------- 1  2  3  4  5  6  7  8  9 --------------------------------------------------------- 1  1  2  3  4  5  6  7  8  9 2  2  4  6  8  10  12  14  16  18 3  3  6  9  12  15  18  21  24  27 4  4  8  12  16  20  24  28  32  36 5  5  10  15  20  25  30  35  40  45 6  6  12  18  24  30  36  42  48  54 7  7  14  21  28  35  42  49  56  63 8  8  16  24  32  40  48  56  64  72 9  9  18  27  36  45  54  63  72  81 ---------------------------------------------------------
 
#include <stdio.h> int main() {  int i,j; printf(&quot;---------------------------------------&quot;); for(i=1;i<10;i++) printf(&quot;%4d&quot;,i); printf(&quot;---------------------------------------&quot;); for(i=1;i<10;i++)  /* 外循环 */ { printf(&quot;%d&quot;,i); for(j=1;j<10;j++)  /* 内循环 */ printf(&quot;%4d&quot;,i*j); printf(&quot;&quot;); } printf(&quot;---------------------------------------&quot;); return 0; }
[object Object],设 i 、 j 、 k 分别代表 1 角、 2 角和 5 角的数量 则: i+2j+5k=100 #include<stdio.h> int main() { int i,j,k; for(i=0;i<=100;i++) for(j=0;j<=50;j++) for(k=0;k<=20;k++)   if(i+2*j+5*k==100)   printf(&quot;%d %d %d&quot;,i,j,k); return 0; }
3.6  转移语句 ,[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object]
 
[object Object],#include<stdio.h> int main() { int k; for(k=100;k<=200;k++) { if(k%3==0)   continue; printf(&quot; %d &quot;,k); } return 0; }
[object Object],[object Object],[object Object],[object Object]
[object Object],#include<stdio.h> #define PI 3.1415926 int main() { int r; double area; for(r=1;r<=10;r++) { area=PI*r*r; if(area>100.0)   break; printf(&quot; %.2lf&quot;,area); } return 0; }
3.7  综合应用 ,[object Object]
[object Object]
#include<stdio.h> int main() { int i,f1=1,f2=1; for(i=1;i<=20;i++) { printf(&quot;%12d %12d&quot;,f1,f2); if(i%2==0)  /* 每行输出 4 个数 */ printf(&quot;&quot;); f1=f1+f2; f2=f2+f1; } return 0; }
[object Object]
[object Object]
[object Object]

Contenu connexe

Tendances

Slide08 807007748
Slide08 807007748Slide08 807007748
Slide08 807007748
Shiyao Ma
 
数据处理算法设计要点
数据处理算法设计要点数据处理算法设计要点
数据处理算法设计要点
thinkinlamp
 
自然语言处理 中文分词程序实验报告%28含源代码%29
自然语言处理 中文分词程序实验报告%28含源代码%29自然语言处理 中文分词程序实验报告%28含源代码%29
自然语言处理 中文分词程序实验报告%28含源代码%29
aemoe
 
C語言 第4章 基本輸出與輸入功能
C語言 第4章 基本輸出與輸入功能C語言 第4章 基本輸出與輸入功能
C語言 第4章 基本輸出與輸入功能
shademoon
 

Tendances (19)

第7章 语法制导翻译和中间代码生成
第7章 语法制导翻译和中间代码生成第7章 语法制导翻译和中间代码生成
第7章 语法制导翻译和中间代码生成
 
系統程式 -- 第 3 章
系統程式 -- 第 3 章系統程式 -- 第 3 章
系統程式 -- 第 3 章
 
Slide08 807007748
Slide08 807007748Slide08 807007748
Slide08 807007748
 
数据处理算法设计要点
数据处理算法设计要点数据处理算法设计要点
数据处理算法设计要点
 
系統程式 -- 第 7 章 高階語言
系統程式 -- 第 7 章 高階語言系統程式 -- 第 7 章 高階語言
系統程式 -- 第 7 章 高階語言
 
Python程式設計 - 分支作業
Python程式設計 - 分支作業Python程式設計 - 分支作業
Python程式設計 - 分支作業
 
自然语言处理 中文分词程序实验报告%28含源代码%29
自然语言处理 中文分词程序实验报告%28含源代码%29自然语言处理 中文分词程序实验报告%28含源代码%29
自然语言处理 中文分词程序实验报告%28含源代码%29
 
第三章 栈和队列(新)
第三章 栈和队列(新)第三章 栈和队列(新)
第三章 栈和队列(新)
 
系統程式 -- 第 12 章 系統軟體實作
系統程式 -- 第 12 章 系統軟體實作系統程式 -- 第 12 章 系統軟體實作
系統程式 -- 第 12 章 系統軟體實作
 
Swift编程语言入门教程 中文版
Swift编程语言入门教程 中文版Swift编程语言入门教程 中文版
Swift编程语言入门教程 中文版
 
Python基本資料運算
Python基本資料運算Python基本資料運算
Python基本資料運算
 
Python變數與資料運算
Python變數與資料運算Python變數與資料運算
Python變數與資料運算
 
Python 迴圈作業
Python 迴圈作業Python 迴圈作業
Python 迴圈作業
 
Example
ExampleExample
Example
 
Python程式設計 - 迴圈作業
Python程式設計 - 迴圈作業Python程式設計 - 迴圈作業
Python程式設計 - 迴圈作業
 
Python分支作業
Python分支作業Python分支作業
Python分支作業
 
C語言 第4章 基本輸出與輸入功能
C語言 第4章 基本輸出與輸入功能C語言 第4章 基本輸出與輸入功能
C語言 第4章 基本輸出與輸入功能
 
Python程式設計 - 串列資料應用
Python程式設計 - 串列資料應用 Python程式設計 - 串列資料應用
Python程式設計 - 串列資料應用
 
系統程式 -- 第 4 章 組譯器
系統程式 -- 第 4 章 組譯器系統程式 -- 第 4 章 組譯器
系統程式 -- 第 4 章 組譯器
 

En vedette

第8章结构体与共用体
第8章结构体与共用体第8章结构体与共用体
第8章结构体与共用体
summerfeng
 
第2章数据类型、运算符和表达式
第2章数据类型、运算符和表达式第2章数据类型、运算符和表达式
第2章数据类型、运算符和表达式
summerfeng
 
Perancangan dan analisa sistem df
Perancangan dan analisa sistem dfPerancangan dan analisa sistem df
Perancangan dan analisa sistem df
Dva Kosongtoejoh
 

En vedette (10)

To Make A Long Story Short
To Make A Long Story ShortTo Make A Long Story Short
To Make A Long Story Short
 
第1章概述
第1章概述第1章概述
第1章概述
 
第9章文件
第9章文件第9章文件
第9章文件
 
第8章结构体与共用体
第8章结构体与共用体第8章结构体与共用体
第8章结构体与共用体
 
第2章数据类型、运算符和表达式
第2章数据类型、运算符和表达式第2章数据类型、运算符和表达式
第2章数据类型、运算符和表达式
 
第5章数组
第5章数组第5章数组
第5章数组
 
Perancangan dan analisa sistem df
Perancangan dan analisa sistem dfPerancangan dan analisa sistem df
Perancangan dan analisa sistem df
 
Assignment
AssignmentAssignment
Assignment
 
Facebook marketing in Nederland
Facebook marketing in NederlandFacebook marketing in Nederland
Facebook marketing in Nederland
 
香港六合彩
香港六合彩香港六合彩
香港六合彩
 

Similaire à 第3章算法与控制语句

Printf和scanf的用法
Printf和scanf的用法Printf和scanf的用法
Printf和scanf的用法
also24
 
C语言学习100例实例程序
C语言学习100例实例程序C语言学习100例实例程序
C语言学习100例实例程序
yiditushe
 
C 02 c语言的基本数据类型与表达式
C 02 c语言的基本数据类型与表达式C 02 c语言的基本数据类型与表达式
C 02 c语言的基本数据类型与表达式
1138177709
 

Similaire à 第3章算法与控制语句 (20)

Printf和scanf的用法
Printf和scanf的用法Printf和scanf的用法
Printf和scanf的用法
 
来自 Google 的 r 语言编码风格指南
来自 Google 的 r 语言编码风格指南来自 Google 的 r 语言编码风格指南
来自 Google 的 r 语言编码风格指南
 
Ch04
Ch04Ch04
Ch04
 
Python学习笔记
Python学习笔记Python学习笔记
Python学习笔记
 
C語言標準輸出入函式
C語言標準輸出入函式C語言標準輸出入函式
C語言標準輸出入函式
 
竞赛中C++语言拾遗
竞赛中C++语言拾遗竞赛中C++语言拾遗
竞赛中C++语言拾遗
 
認識 C++11 新標準及使用 AMP 函式庫作平行運算
認識 C++11 新標準及使用 AMP 函式庫作平行運算認識 C++11 新標準及使用 AMP 函式庫作平行運算
認識 C++11 新標準及使用 AMP 函式庫作平行運算
 
Arduino程式快速入門
Arduino程式快速入門Arduino程式快速入門
Arduino程式快速入門
 
Ch03
Ch03Ch03
Ch03
 
第三章
第三章第三章
第三章
 
C语言学习100例实例程序
C语言学习100例实例程序C语言学习100例实例程序
C语言学习100例实例程序
 
Javascript Training
Javascript TrainingJavascript Training
Javascript Training
 
Ch9
Ch9Ch9
Ch9
 
Standford 2015 iOS讀書會 week2: 1. Applying MVC 2. More Swift and Foundation Fra...
Standford 2015 iOS讀書會 week2: 1. Applying MVC 2. More Swift and Foundation Fra...Standford 2015 iOS讀書會 week2: 1. Applying MVC 2. More Swift and Foundation Fra...
Standford 2015 iOS讀書會 week2: 1. Applying MVC 2. More Swift and Foundation Fra...
 
Ch9 教學
Ch9 教學Ch9 教學
Ch9 教學
 
Vim get start_1.0
Vim get start_1.0Vim get start_1.0
Vim get start_1.0
 
C 02 c语言的基本数据类型与表达式
C 02 c语言的基本数据类型与表达式C 02 c语言的基本数据类型与表达式
C 02 c语言的基本数据类型与表达式
 
Ch6 函式
Ch6 函式Ch6 函式
Ch6 函式
 
Ch2 教學
Ch2 教學Ch2 教學
Ch2 教學
 
ncuma_邏輯與迴圈.pptx
ncuma_邏輯與迴圈.pptxncuma_邏輯與迴圈.pptx
ncuma_邏輯與迴圈.pptx
 

第3章算法与控制语句

  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.
  • 10.
  • 11.
  • 12.
  • 13.
  • 14.
  • 15.
  • 16.
  • 17.
  • 18.
  • 19.
  • 20.
  • 21.
  • 22.
  • 23.
  • 24. 修饰符: 确定数据输出的宽度、精度、小数位数、对齐方式 在 e,f,g 前,指定输出精度为 double 型 在 d,o,x,u 前,指定输出精度为 long 型 l 在八进制和十六进制数前显示前导 0 , 0x # 输出数值时指定左面不使用的空位置自动填 0 0 指定在有符号数的正数前显示正号 (+) + 输出数据在域内左对齐(缺省右对齐 ) - 对实数 , 指定小数点后位数 ( 四舍五入 ) 对字符串 , 指定实际输出位数 .n 输出数据域宽 , 数据长度 <m, 左补空格 ; 否则按实际输出 M 含义 修饰符
  • 25.
  • 26. 程序运行结果: 3 4 y=3 , z=4 1234,1234 123.456001,123.456001, 123.5,123.46,1.23e+002 123.456000 a Hello,world! Hello,world! Hello Hello Hel
  • 27.
  • 28.
  • 29.
  • 30.
  • 31.
  • 32. 例 int x; char ch; scanf(“%d”,&x); ch=getchar(); printf(“x=%d,ch=%d”,x,ch); 执行: 123  输出: x=123,ch=10 例 int x; char ch; scanf(“%d”,&x); scanf(“ %c”,&ch); 或 scanf(“%*c%c”,&ch);
  • 33.
  • 34.
  • 35.
  • 36.
  • 37.
  • 38.
  • 39.
  • 40.
  • 41.
  • 42.
  • 43.
  • 44.
  • 45.
  • 46.
  • 47.
  • 48.
  • 49. if(weekday==0) printf(&quot;%2d&quot;,date); else if(weekday==1) printf(&quot;%7d&quot;,date); else if(weekday==2) printf(&quot;%12d&quot;,date); else if(weekday==3) printf(&quot;%17d&quot;,date); else if(weekday==4) printf(&quot;%22d&quot;,date); else if(weekday==5) printf(&quot;%27d&quot;,date); else printf(&quot;%32d&quot;,date); return 0; } #include <stdio.h> int main() { int date,weekday,original_date=3; scanf(&quot;%d&quot;,&date); if(date<1||date>31) { printf(&quot; 数据输入错误 !&quot;); return 1; } weekday=(date+original_date-1)%7; printf(&quot;2010 年 12 月日历 &quot;); printf(&quot;----------------------------------&quot;); printf(&quot;Sun Mon Tue Wen Thr Fri Sta &quot;); printf(&quot;----------------------------------&quot;);
  • 50.
  • 51. 3.3.4 条件语句的嵌套 嵌套具有 else 子句的 if 语句 if ( 表达式 1) if ( 表达式 2) 语句序列 1 else 语句序列 2 嵌套不含 else 子句的 if 语句 if ( 表达式 1) 语句序列 1 else if ( 表达式 2) 语句序列 2 一般形式 if ( 表达式 1) if ( 表达式 2) 语句序列 1 else 语句序列 2 else if( 表达式 3) 语句序列 3 else 语句序列 4
  • 52.
  • 53. 例: if (a==b) if(b==c) printf(“a==b==c”); else printf(“a!=b”); 修改: if (a==b) { if(b==c) printf(“a==b==c”); } else printf(“a!=b”); 实现 if ~ else 正确配对方法:加 { }
  • 54.
  • 55.
  • 56.  
  • 57.
  • 58.
  • 59.
  • 60. #include <stdio.h> #include <stdlib.h> int main() { int score; scanf(&quot;%d&quot;,&score); if(score<0||score>100 ) { printf(&quot; 输入数据错误 &quot;); exit(0); } switch( score/10 ) { case 10: case 9:printf(&quot; 优秀 &quot;);break; case 8:printf(&quot; 良好 &quot;);break; case 7:printf(&quot; 中等 &quot;);break; case 6:printf(&quot; 及格 &quot;);break; default:printf(&quot; 不及格 &quot;):break } return 0; }
  • 61.
  • 62.
  • 63. #include<stdio.h> int main() { int year; double money,rate,total; printf(&quot; 输入存款和存期 :&quot;); scanf(&quot;%lf %d&quot;,&money,&year); switch(year) { case 1:rate=0.0225;break; case 2:rate=0.0279;break; case 3:rate=0.0333;break; case 5:rate=0.0360;break; case 8:rate=0.0414;break; default:rate=0.0;printf(&quot; 输入的存期错误 !&quot;);break; } total=money+money*rate*year; printf(&quot; 从银行获得的总金额为: %.2lf&quot;,total); return 0; }
  • 64.
  • 65.
  • 66.
  • 67.
  • 68.
  • 69.
  • 70.
  • 71.
  • 72.
  • 73.
  • 74. #include<stdio.h> #include<stdlib.h> int main() { int i,sum=0,p=1,n; scanf(&quot;%d&quot;,&n); if(n<0) { printf(&quot; 负数没有阶乘! &quot;); exit(0); } for(i=1;i<=n;i++) { p=p*i; sum=sum+p; } printf(&quot; 阶乘的和为: %d&quot;,sum); return 0; } 例 3.30 :计算
  • 75.
  • 76.
  • 77.
  • 78.
  • 79.  
  • 80. #include <stdio.h> int main() { int i,j; printf(&quot;---------------------------------------&quot;); for(i=1;i<10;i++) printf(&quot;%4d&quot;,i); printf(&quot;---------------------------------------&quot;); for(i=1;i<10;i++) /* 外循环 */ { printf(&quot;%d&quot;,i); for(j=1;j<10;j++) /* 内循环 */ printf(&quot;%4d&quot;,i*j); printf(&quot;&quot;); } printf(&quot;---------------------------------------&quot;); return 0; }
  • 81.
  • 82.
  • 83.  
  • 84.
  • 85.
  • 86.
  • 87.
  • 88.
  • 89. #include<stdio.h> int main() { int i,f1=1,f2=1; for(i=1;i<=20;i++) { printf(&quot;%12d %12d&quot;,f1,f2); if(i%2==0) /* 每行输出 4 个数 */ printf(&quot;&quot;); f1=f1+f2; f2=f2+f1; } return 0; }
  • 90.
  • 91.
  • 92.