[英]Return Value Function in C
您好我正在開發一個生成主代碼的快速程序,用戶必須猜測代碼。 當檢查用戶的猜測時,我使用2個函數用於精確匹配,而另一個用於緊密匹配(其中他們得到的數字在主代碼中但不在正確的位置)
防爆。 主碼1 2 3 4
用戶2 3 2 4輸出應顯示用戶有2個近似匹配和一個完全匹配。 我無法理解如何正確返回int。
當我嘗試在Main中打印時,我的輸出只顯示了exactMatch和closeMatch的默認值。 任何見解將不勝感激。 謝謝。
#include<stdio.h>
#include<math.h>
#include<stdlib.h>
#include<time.h>
#define CODELENGTH 4
#define NUMSYMBOLS 6
int MasterCode[4];
int guess[ 4 ];
int exactMatch;
int closeMatch=0;
void genCode (int MasterCode[])
{
int i=0;
int k;
while (i < CODELENGTH){
MasterCode[i] =rand() %NUMSYMBOLS +1;
i++;
}//end while loop.
for ( k = 0 ; k < 4; k++ ) {
printf( "%d ", MasterCode[ k ] );
}
printf( "\n" );
}
void getGuess (int guess[])
{
int number = 0;
printf( "Please enter your list of 4 numbers between 1 and 6: " );
int j;
int k;
for ( j = 0 ; j < 4; j++ ) {
scanf( "%d", &number );
guess[ j ] = number;
}
printf( "Your array has these values: " );
for ( k = 0 ; k < 4; k++ ) {
printf( "%d ", guess[ k ] );
}
printf( "\n" );
}
int main (int argc, char **argv)
{
srand ( time(NULL) );
genCode(MasterCode);
getGuess(guess);
checkExactMatches(MasterCode, guess, exactMatch);
checkCloseMatches(MasterCode, guess, closeMatch);
printf("%d = Ending exactMatches \n", exactMatch);
printf("%d = Ending closeMatches \n", closeMatch);
}
int checkExactMatches (int MasterCode[], int guess[], int exactMatch )
{
int woot;
for(woot=0; woot<4; woot++){
if (MasterCode[woot] == guess[woot]){
printf("Exact Match found \n");
exactMatch ++;
printf( "%d = Guess \n" , guess[ woot ]);
printf( "%d = MasterCode \n", MasterCode[ woot ]);
printf("%d = exactMatch \n", exactMatch);
}// end if
if (MasterCode[woot] != guess[woot])
printf("No EXACT match \n");
}//end for loop
return exactMatch;
} // end checkExactMatches
int checkCloseMatches (int MasterCode[], int guess[], int closeMatch )
{
int k;
int j;
for(k=0; k<4; k++){
for (j=0; j<4; j++) {
if (MasterCode[k] == guess[j]){
printf("CLOSE Match found \n");
closeMatch ++;
printf( "%d = Guess \n" , guess[ j ]);
printf( "%d = MasterCode \n \n", MasterCode[ k ]);
printf("%d = closeMatch \n \n", closeMatch);
}// end if
if (MasterCode[k] != guess[j])
printf("No CLOSE match \n");
}//end nested for loop
}//end for loop
return closeMatch;
} // end checkCloseMatches
獲取函數返回的值實際上非常簡單,它與為變量賦值基本相同。 在您的情況下,語法將是這樣的:
int result = checkExactMatches(MasterCode, guess, exactMatch);
result現在將保存函數返回的值。
你需要做的是
所以
checkExactMatches(MasterCode, guess, exactMatch);
變
exactMatch = checkExactMatches(MasterCode, guess);
您需要對函數頭進行適當的更改,並避免使用全局變量。
請注意,雖然數組是通過指針傳遞的(意味着函數可以修改指向指針),但是int會按值傳遞。 因此,函數中int的更改僅在函數持續時間內持續存在。
你想用
exactMatch = checkExactMatches(MasterCode, guess);
在這種情況下,您將不再需要傳遞exactMatch
。
C通過值而不是引用傳遞參數,因此將int exactmatch
傳遞給函數會在exactmatch
創建數據的exactmatch
,因此在方法內部執行的操作不會執行任何操作。
你想要做的是從參數列表中刪除exactmatch,而是將其指定為零( int exactmatch=0;
在方法的開頭,從全局變量中刪除exactmatch,並使用類似int resultExact = checkExactMatches(MasterCode, guess);
作為你的函數調用。你已經正確地回答了答案,你只是不在任何地方找到答案。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.