控制结构A.doc
文本预览下载声明
顺序结构
--求一个整数n除以另一个整数m的商和余数(nm)
declare @n int,@m int
declare @p int,@q int
set @n=15
set @m=4
set @p=@n/@m
set @q=@n%@m
select @p,@q
--对一个3位正整数x进行分解求和,如789,求s=7+8+9
declare @a int
declare @l int,@m int,@n int
declare @s int
set @a=789
set @l=floor(@a/100)
set @m=floor((@a-@l*100)/10)
set @n=@a-@l*100-@m*10
set @s=@l+@m+@n
print @s
--两个变量的值交换,如a=3,b=5,交换后a=5,b=3
declare @a int,@b int
declare @temp int
select @a=5,@b=3
select @temp=@a,@a=@b,@b=@temp
select @a,@b
--假设某班有50个学生,其学号的尾数为01-50,随机抽取一个学生课堂发言。
declare @t int
set @t=ceiling(rand()*50)
print str(@t,2)+号发言
--求解方程2x2+7x-78=0
declare @a int,@b int,@c int, @x1 float,@x2 float
declare @t float
select @a=2,@b=7,@c=-78
set @t=sqrt(@b^2-4*@a*@c)
set @x1=(-@b+@t)/2
set @x2=(-@b-@t)/2
select x1=@x1,x2=@x2
declare @a int,@b int,@c int, @x1 float,@x2 float
declare @t float
select @a=2,@b=7,@c=-78
set @t=@b^2-4*@a*@c
if @t=0
begin
set @x1=(-@b+sqrt(@t))/2
set @x2=(-@b-sqrt(@t))/2
select x1=@x1,x2=@x2
end
else
print 方程无实数解
选择结构
If结构
计算分段函数: 当x≠0时, y=sin(x)+x*x+1; 当x=0时, y=cox(x)-3*x;假设自变量x的值为5,要求:用Set语句或Select语句给自变量x赋值,请编写程序。
declare @x float,@y float
set @x=5
if @x0
set @y=sin(@x)+@x*@x
else
set @y=cos(@x)+3*@x
print @y
火车站托运行李,按规定当行李重量不超过50公斤时,每公斤运费0.25元,超过50公斤后,超过部分按每公斤0.40元收费。输入行李重量W公斤,计算出应付多少运费。注意:先使用Set语句或Select语句给变量赋初值。
declare @x float,@y float
set @x=5
if @x=50
set @y=0.25*@x
else
set @y=50*0.25+(@x-50)*0.40
print @y
公用电话收费标准如下:通话时间在3分钟之内(包括3分钟),收费0.5元;3分钟以上,则每超过1分钟加收0.15元。编写一段程序,计算某人通话S分钟,应缴多少电话费。
declare @s float,@y float
set @s=5
if @s=3
set @y=0.5
else
set @y=0.5+(@s-3)*0.15
print @y
若基本工资大于800元,涨原工资的20%;若小于800元大于400元,涨原工资的15%;若小于400元,涨原工资的10%。编程实现:使用Set语句或Select语句输入基本工资,计算涨工资后的工资数。
declare @gz float
set @gz=500
if @gz=800
set @gz=@gz*1.2
else if @gz=400
set @gz=@gz*1.15
else
set @gz=@gz*1.1
print @gz
创建一个存储过程,输入一个年份,判断其是否为闰年并使用输出参数将判断结果输出。注:闰年的条件为能被4整除但不能被100整除,或能被400整除。
if exists (select * from dbo.sysobjects where name=yf and type=p)
drop procedure dbo.yf
go
cr
显示全部