100 likes | 191 Views
北师大版 《 高中数学必修 3》 第二章“算法初步”. 变量与赋值语句. 陈世勇. 变量与赋值语句. 变量. 变量是计算机内存中的一个地址标签。计算机的所有运算数据都要存储在内存中,而内存的地址非常不容易记忆,所以变量就相当于一个门牌号一样,便于程序的控制。 变量名好比是门牌号,而变量的值就是房间存的东西。变量名以字母开头,由字母、汉字、数字或下划线组成。 它具有“只保存最新赋值”的特性。. m=3 n=2 m=m+n n=m+n m,n?. 赋值语句. 格式:. 变量=表达式. 功能:. 可对程序中的变量赋值,计算。. 说明:.
E N D
北师大版《高中数学必修3》第二章“算法初步” 变量与赋值语句 陈世勇
变量与赋值语句 变量 变量是计算机内存中的一个地址标签。计算机的所有运算数据都要存储在内存中,而内存的地址非常不容易记忆,所以变量就相当于一个门牌号一样,便于程序的控制。变量名好比是门牌号,而变量的值就是房间存的东西。变量名以字母开头,由字母、汉字、数字或下划线组成。它具有“只保存最新赋值”的特性。 m=3 n=2 m=m+n n=m+n m,n? 赋值语句 格式: 变量=表达式 功能: 可对程序中的变量赋值,计算。 说明: (1)“=”的右侧必须是表达式,左侧必须是变量; (2)一个语句只能给一个变量赋值; (3)有计算功能。 5, 7
赋值语句的经典案例: 任意输入三个数,按从大到小的顺序输出。 任务一:在ppt中粗略画出算法框图。 任务二:细化框图。 任务三:框图语句化。
开始 开始 输入a,b,c 输入a,b,c ① 是 a<b 交换a,b的值 否 ② 是 a<c 两两比较 否 交换a,c的值 是 ③ b<c 输出a,b,c 否 交换b,c的值 结束 b=c c=b ? 输出a,b,c 结束 赋值语句的经典案例: 任意输入三个数,按从大到小的顺序输出。 处理:把三者中最大的值赋给a,把中间的值赋给b,把最小的值赋给c。
开始 输入a,b,c 是 a<b t=a,a=b,b=t 否 是 a<c 否 t=a,a=c,c=t 是 b<c 否 t=b,b=c,c=t 输出a,b,c 结束 c a b 请输入a,b,c: ① a b c ② 第1段: 第2段: ③ 第3段:
Private Sub CommandButton2_Click() Dim a, b, c, t As Double a = Val(TextBox1.Value) b = Val(TextBox2.Value) c = Val(TextBox3.Value) If a < b Then t = a a = b b = t End If Label1.Caption = a Label2.Caption = b Label3.Caption = c If a < c Then t = a a = c c = t End If Label4.Caption = a Label5.Caption = b Label6.Caption = c If b < c Then t = b b = c c = t End If Label7.Caption = a Label8.Caption = b Label9.Caption = c MsgBox "输出结果是:" + Str(a) + Str(b) + Str(c) TextBox1.Value = "" TextBox2.Value = "" TextBox3.Value = "" Label1.Caption = "" Label2.Caption = "" Label3.Caption = "" Label4.Caption = "" Label5.Caption = "" Label6.Caption = "" Label7.Caption = "" Label8.Caption = "" Label9.Caption = "" End Sub 程序代码
开始 输入a,b,c 是 a<b t=a,a=b,b=t 否 是 a<c 否 t=a,a=c,c=t 是 b<c 否 t=b,b=c,c=t 输出a,b,c 结束 Private Sub CommandButton2_Click() Dim a, b, c, t As Double a = Val(TextBox1.Value) b = Val(TextBox2.Value) c = Val(TextBox3.Value) …… MsgBox "输出结果是:" + Str(a) + Str(b) + Str(c) TextBox1.Value = "" TextBox2.Value = "" TextBox3.Value = "" Label1.Caption = "" Label2.Caption = "" Label3.Caption = "" Label4.Caption = "" Label5.Caption = "" Label6.Caption = "" Label7.Caption = "" Label8.Caption = "" Label9.Caption = "" End Sub ① If b < c Then t = b b = c c = t End If Label7.Caption = a Label8.Caption = b Label9.Caption = c If a < b Then t = a a = b b = t End If Label1.Caption = a Label2.Caption = b Label3.Caption = c If a < c Then t = a a = c c = t End If Label4.Caption = a Label5.Caption = b Label6.Caption = c ② ③
小结: 练习: 读下面一段程序,指出每一个阶段 a,b的值分别为什么? a=1b=2print a,b a=a+bb=a+bprint a,b a=2*a+bb=2*a+bprint a,b a=a^2-bb=a^2-bprint a,b (1)“自顶而下,由粗到精” 逐步细化,解决问题的思想; (2)变量“喜新厌旧”的特性; (3)通过第三方变量调度,实现交换两变量的值。 1, 2 3, 5 11, 27 94, 8809
课后牛刀小试:尝试着编写一个小程序,输出变量a,b的值。效果如下:课后牛刀小试:尝试着编写一个小程序,输出变量a,b的值。效果如下: b a a=?b=? a=a+bb=a+b a=2*a+bb=2*a+b a=a^2-bb=a^2-b 输入初始值: 过程值: 过程值: 结果为: