[問題] 函式一問

看板C_and_CPP (C/C++)作者 (崩啾)時間15年前 (2011/01/10 19:32), 編輯推噓0(002)
留言2則, 2人參與, 最新討論串1/1
開發平台(Platform): (Ex: VC++, Gcc, Linux, ...) DEV CPP 額外使用到的庫(Library Used) (Ex: OpenGL, ...) 問題(Question): 這是一題課本的問題,輸入base跟exponent,輸出結果 餵入的資料(Input): base,exponent 預期的正確結果(Expected Output): result = base^exponent 錯誤結果(Wrong Output): result只會輸出base 程式碼(Code): (請善用置底文標色功能) #include<stdio.h> #include<stdlib.h> int integerPower(int base,int exponent); int main(void) { int base; int exponent; int result; printf("Please enter a base:\n"); scanf("%d",&base); printf("Please enter a exponent:\n"); scanf("%d",&exponent); printf("The result is %d\n",integerPower( base,exponent)); system("pause"); return 0; } int integerPower(int base,int exponent) { int i=1; int result=1; if (exponent = 0){ return 1; } else if (exponent = 1){ return base; } else for(i=1;i<=exponent;i++) { result = result * base; } return result; } 補充說明(Supplement): 請各位先進幫忙指出小弟什麼地方寫錯了,感謝 -- ※ 發信站: 批踢踢實業坊(ptt.cc) ◆ From: 202.111.1.107

01/10 19:41, , 1F
你的 if 裡面應該是 == 而不是 =
01/10 19:41, 1F

01/10 19:42, , 2F
== 0, == 1
01/10 19:42, 2F
文章代碼(AID): #1DAkvN3V (C_and_CPP)
文章代碼(AID): #1DAkvN3V (C_and_CPP)