精華區beta Visual_Basic 關於我們 聯絡資訊
這是老師出的作業 我已經想了四天了 一點頭緒都沒有 哪位強者能給我點提示嗎...? 囧" 次方的我有想到用 Public x As Integer, y As Integer Private Sub Command1_Click() x = Val(Text1.Text) y = Val(Text2.Text) Call Power(x, y) Label4.Caption = x End Sub Public Function Power(Base As Integer, Exponent As Integer) If Exponent = 0 Then Base = 1 Else Base = Base * Power(Base, Exponent - 1) End If End Function 我輸入(3,3)的話 power最後所傳回x的值為0 不是應該base = 3 * 3 * 3 * 1 = 27 嗎.......????? 最大公約數的話 老師有提示用MOD 可是我一點頭緒都沒有... 今天下午就要交了 拜託給我一點提示吧..m(_ _)m -- http://www.wretch.cc/blog/walume 黑特日記 髒話不忌 -- ※ 發信站: 批踢踢實業坊(ptt.cc) ◆ From: 140.128.194.155 ※ 編輯: walume 來自: 140.128.194.155 (05/18 02:26)
Phill:Base 初始值設定為 1 218.168.176.33 05/18
walume:初始值不是我設的val(text1.text)嗎..?不太懂@@"140.128.194.155 05/18
※ 編輯: walume 來自: 140.128.194.155 (05/18 09:56)
walume:是在函數設定初始值為1嗎..?還是原本設定為1?@@"140.128.194.155 05/18
sean25hong:我有寫過耶, 是用for...next檢查x,y除以他都 140.116.84.94 05/18
sean25hong:整除的最大值 140.116.84.94 05/18
> -------------------------------------------------------------------------- < 作者: fumizuki (小獅) 看板: Visual_Basic 標題: Re: [請益]用遞迴寫x^n和求x y的最大公約數 時間: Wed May 18 11:03:43 2005 Function Power(Base As Integer, Exponent As Integer) If Exponent = 0 Then Power = 1 Else Power = Base * Power(Base, Exponent - 1) End If End Function 這是你的,你沒有傳回值,結果當然是零啦。 至於公因數嘛,實在沒辦法... 我學 vb 的時候鮮少見到遞迴的範例,所以不太會寫遞迴/.\ 想了半天想不出個流程來:~~ -- VB 程式設計 倉木麻衣 PTT 星爺板 行列輸入法 ====================================================== Visual_Basic MaiKuraki Stephen Array -- ※ 發信站: 批踢踢實業坊(ptt.cc) ◆ From: 61.222.155.162
walume:沒有傳回值..那要怎麼傳..?@@"140.128.194.155 05/18
> -------------------------------------------------------------------------- < 作者: neplayer (neplayer) 看板: Visual_Basic 標題: Re: [請益]用遞迴寫x^n和求x y的最大公約數 時間: Wed May 18 11:32:26 2005 ※ 引述《fumizuki (小獅)》之銘言: : Function Power(Base As Integer, Exponent As Integer) : If Exponent = 0 Then : Power = 1 : Else : Power = Base * Power(Base, Exponent - 1) : End If : End Function : 這是你的,你沒有傳回值,結果當然是零啦。 : 至於公因數嘛,實在沒辦法... : 我學 vb 的時候鮮少見到遞迴的範例,所以不太會寫遞迴/.\ : 想了半天想不出個流程來:~~ 回原po gcd的程式應該google一下就一整票吧 想一下輾轉相除法 function gcd(a as integer, b as integer) if b=0 gcd=a else gcd=gcd(b,a mod b) end -- mod應該是求餘數的沒錯吧? 第一次用vb寫遞迴XD 應該可以跑....吧 -- ※ 發信站: 批踢踢實業坊(ptt.cc) ◆ From: 211.73.191.98 ※ 編輯: neplayer 來自: 211.73.191.98 (05/18 11:33)
walume:GCD的我昨天已經寫出來囉@@" 感謝幫忙^^140.128.194.155 05/18
fumizuki:不能跑,因為沒有end if 沒有end function:P 61.222.155.162 05/18
fumizuki:試過,沒問題,這程式ok...cc 61.222.155.162 05/18
neplayer:副板主vb這麼熟怎麼不去小補一下遞迴XDD 211.73.191.98 05/18
fumizuki:我一直都是靠範例惡補的XD 61.222.155.162 05/18
> -------------------------------------------------------------------------- < 作者: walume (哇!阿魯米) 看板: Visual_Basic 標題: Re: [請益]用遞迴寫x^n和求x y的最大公約數 時間: Wed May 18 11:59:13 2005 ※ 引述《neplayer (neplayer)》之銘言: : ※ 引述《fumizuki (小獅)》之銘言: : : Function Power(Base As Integer, Exponent As Integer) : : If Exponent = 0 Then : : Power = 1 : : Else : : Power = Base * Power(Base, Exponent - 1) : : End If : : End Function : : 這是你的,你沒有傳回值,結果當然是零啦。 : : 至於公因數嘛,實在沒辦法... : : 我學 vb 的時候鮮少見到遞迴的範例,所以不太會寫遞迴/.\ : : 想了半天想不出個流程來:~~ : 回原po : gcd的程式應該google一下就一整票吧 : 想一下輾轉相除法 : function gcd(a as integer, b as integer) : if b=0 : gcd=a : else : gcd=gcd(b,a mod b) : end Public a As Double, b As Double, r As Double ____________________________________________________________ Private Sub Command1_Click() b = Val(Text1.Text) Mod Val(Text2.Text) r = Val(Text2.Text) Call GCD(b, r) Label4.Caption = r End Sub ____________________________________________________________ Public Function GCD(Number1 As Integer, Number2 As Integer) If Number2 = 0 Then GCD = Number1 Else GCD = GCD(Number2, (Number1 Mod Number2)) End If End Function ____________________________________________________________ 我已經寫出來了 PO出來給大家看看 之前PO的那個老師說不能= =" 好像說要改成這樣子 下面是我剛剛弄好的次方遞迴運算 Public x As Integer, y As Integer ____________________________________________________________ Private Sub Command1_Click() x = Val(Text1.Text) y = Val(Text2.Text) Label4.Caption = Power(x, y) End Sub ____________________________________________________________ Public Function Power(Base As Integer, Exponent As Integer) If Exponent = 0 Then Power = 1 Else Power = Base * Power(Base, Exponent - 1) End If End Function ____________________________________________________________ 終於弄完了... 謝謝各位幫忙喔^^ -- FUCKFUCKFUCK FUCK FUCK FUCKFUCK FUCK FUCK FUCK FUCK FUCK FUCK FUCK FUCK FUCK FUCK FUCK FUCK FUCK FUCKFUCK FUCKFUCK FUCK FUCK FUCK FUCK FUCK FUCK FUCK FUCK FUCK FUCK FUCK FUCK FUCK FUCKFUCK FUCKFUCK FUCK FUCK -- ※ 發信站: 批踢踢實業坊(ptt.cc) ◆ From: 140.128.194.155
walume:忘記說..要怎麼傳回次方那個funtion的值呢@@"140.128.194.155 05/18
fumizuki:函數名稱 = 傳回值 61.222.155.162 05/18
謝謝囉~我在試試看~^^" ※ 編輯: walume 來自: 140.128.194.155 (05/18 12:26)