程式設計例題上課講解用

2022-12-06 12:00:05 字數 3760 閱讀 7970

1.兩個變數值的交換

declare @a int ,@b int ,@c int

set @a=5

set @b=9

select @a as a,@b as b

set @c=@a

set @a=@b

set @b=@c

select @a as a,@b as b

2.已知三角形的三條邊為a、b、c,求其面積。

declare @a int,@b int,@c int,@p decimal, @s decimal (10,5)

set @a=7

set @b=5

set @c=6

set @p=(@a+@b+@c)/ 2

set @s= sqrt (@p*(@p-@a)*(@p-@b)*(@p-@c))

print @ s

3. 判斷乙個學生成績cj屬於哪個等級

declare @cj int,@dj varchar(20)

set[ @cj=round(rand()*100,0)+1

set @dj=

case

when @ cj<60 then '不及格'

when @cj>=60 and @cj<90 then '及格'

when @cj>=90 and @cj<=100 then '優秀'

else

'資料錯誤'

endselect @ dj as '等級'

4.求資料庫gz中的表檔案gzqd中的基本工資的平均值、總計、最大值和最小值

use gz

select count(*),**g (基本工資),sum(基本工資), max (基本工資),min(基本工資) from gzqd

5.判斷某一年(y)是否為閏年?

declare @d int

set @d=year('2012-5-8')

if (@d%4=0 and @d%100!=0) or (@d%400=0)

print str(@d)+'是閏年'

else

print str(@d)+'不是閏年'

6.計算1+2+3+……100

declare @i int,@s int

set @ s=0

set @i=1

while @i<=100

begin

set @s=@ s +@i

set @i=@i+1

endprint @s

7.計算1+3+5+……99

declare @i int,@s int

set @s=0

set @i=1

while @i<=99

begin

set @s=@s+@ i

set @i=@i+2

endprint @s

8.計算2+4+6+……100空

declare @i int,@s int

set @s=0

set @i=2

while @i<=100

begin

set @s=@s+@ i

set @i=@i+2

endprint @s

9.求所有的3位正整數除以99的商和餘數,並輸出。

[, , , , , , , , , , , ]

declare @i int, @a int, @b int

set @i=100

while @i<=999

begin

set @a=@i/99

set @b=@i%99

select @a , @b

set @i=@i+1

end10.求1!+2!+3!+4!+5!。

declare @i int,@k int,@t int,@s bigint

set @s=0

set @k=1

while @k<=5

begin

set @t=1

set @i=1

while @i<=@ k

begin

set @t=@t*@i

set @i=@i+1

endset @s=@s+@ t

set @k=@k+1

endprint @s

11.統計未來100年中閏年的年份個數

[, , , ]

declare @i as int,@h as int

set @h=0

set @i=2013

while @i<=2113

begin

if (@i%4=0 and @i%100!=0) or (@i%400=0)

-- set @h=@h+1

print @i

set @i=@i+1

end--print @h

12.求1至100之間能被7整除的數之和

[, , , , , , ]

declare @i int,@s int

set @s=0

set @i=1

while @i<=100

begin

if @i%7=0

set @s=@s+@ i

set @i=@i+1

endprint @s

13.求和:s=1+1/2+1/4++1/8……+1/1024

[, , , , , ]

declare @i int,@s decimal(8,5)

set @s=1

set @i=1

while @i<=10

begin

set @s=@s+1.0/ power (2,@i)

set @i=@i+1

endprint @s

14.求s=1+2+3+……+n>9999的最小n

[, , , , , , ]。

declare @n int,@s int

set @s=0

set @n=1

b_1:

set @s=@s+@ n

set @n=@n+1

if @ s >=9999

begin

select @ n-1

return

endgoto b_1

15.建立儲存過程njc,求t=n!,並使用輸出引數返回結果

create procedure njc

@i int,@n int, @t bigint output

as set @t=1

set @i=1

while @i<=@n

begin

set @t=@t*@ i

set @i=@i+1

end16.求解方程ax2+bx+c=0 (a≠0) (建立過程:jfc)

create procedure jfc

@a float, @b float , @c float,

@x1 float output, @x2 float output , @xx varchar(30) output

asif @a=0

return

if @b*@b-4*@a*@c>=0

begin

set @x1=(-@b+sqrt(@b*@b-4*@a*@c))/(2*@a)

set @x2=(-@b-sqrt(@b*@b-4*@a*@c))/(2*@a)

select @x1,@ x2

endelse

set @xx= ' 此方程無實根'

典型例題講解

結構如圖1所示,其中有乙個累加暫存器ac,乙個狀態條件暫存器,各部分之間的連線表示資料通路,箭頭表示資訊傳送方向。1 標明圖中四個暫存器的名稱。2 簡述指令從主訪問到控制器的資料通路。3 簡述資料在運算器和主存之間進行存 取訪問的資料通路。圖1解 1 a為資料緩衝暫存器 dr b為指令暫存器 ir ...

面向介面程式設計講解

1.面向介面程式設計和物件導向程式設計是什麼關係 首先,面向介面程式設計和物件導向程式設計並不是平級的,它並不是比物件導向程式設計更先進的一種獨立的程式設計思想,而是附屬於物件導向思想體系,屬於其一部分。或者說,它是物件導向程式設計體系中的思想精髓之一。2.介面的本質 介面,在表面上是由幾個沒有主體...

軸改錯上課用例題

1.齒輪箱內齒輪為油潤滑,軸承為脂潤滑 1 彈性擋圈為多餘零件 2 軸肩過高,不便於拆軸承 3 軸的臺肩應在輪轂內 4 鍵太長 5 套筒外徑太大,不應與外圈接觸,不便軸承拆卸 6 軸徑太長,軸承裝拆不便 7 聯軸器孔應打通 8 聯軸器沒有軸向固定 9 聯軸器無周向固定 10 缺密封 11 箱體裝軸承...