2016-02-19 65 views
-2

我有一個完整的C程序,提示用戶輸入一個介於0和100000之間的數字。然後打印出數字的每個數字。我的代碼完成了這項任務,但是它並沒有通過值得理想的功能正確傳遞。我需要一些關於如何接受我所擁有的建議,並按功能正確傳遞用戶價值並返回。C程序按值傳遞

#include <stdio.h> 


    void print_1(int,int); 
    void print_10(int,int); 
    void print_100(int,int); 
    void print_1000(int,int); 
    void print_10000(int,int); 

    int main() { 
     int userInput; 
     printf("Please enter a positive number less than 100,000: \n"); 
     scanf("%d", &userInput); 
     if (userInput > 0 && userInput < 100000) 
     { 
      printf("\nYou have entered: %d\n",userInput);; 

     } 
     else 
     { 
      printf("\nThe number you have entered is out of the specified range\n"); 
      exit(0); 
     } 
     int lastDigit; 
     print_1(userInput,lastDigit); 


    } //End main 


// =========================================================================== 
// Function  : Print_1 
// Description : Prints out the 1st digit of user input 
// 
// Input   : 
//     input userInput 
//     output userInput 
// Return value : 
//     return value 
// 
// Notes: This function repeats several printing the lastDigit then slicing it 
// off before passing on to a new function. 
//============================================================================ 
    void print_1(int userInput,int lastDigit) 
      { 
      lastDigit = userInput % 10; 
      printf("\n1\'s:  %d \n", lastDigit); 
      userInput = (userInput - lastDigit)/10;\ 
      print_10(userInput,lastDigit); 
      } // end print_1 

// =========================================================================== 
// Function  : Print_10 
// Description : Prints out the 1st digit of user input 
// 
// Input   : 
//     input userInput 
//     output userInput 
// Return value : 
//     return value 
// 
// Notes: This function repeats several printing the lastDigit then slicing it 
// off before passing on to a new function. 
//============================================================================ 
    void print_10(int userInput,int lastDigit) 
      { 
      lastDigit = userInput % 10; 
      printf("\n10\'s: %d \n", lastDigit); 
      userInput = (userInput - lastDigit)/10; 
      print_100(userInput,lastDigit); 
      } // end print_10 

// =========================================================================== 
// Function  : Print_100 
// Description : Prints out the 1st digit of user input 
// 
// Input   : 
//     input userInput 
//     output userInput 
// Return value : 
//     return value 
// 
// Notes: This function repeats several printing the lastDigit then slicing it 
// off before passing on to a new function. 
//============================================================================ 
    void print_100(int userInput,int lastDigit) 
      { 
      lastDigit = userInput % 10; 
      printf("\n100\'s: %d \n", lastDigit); 
      userInput = (userInput - lastDigit)/10; 
      print_1000(userInput,lastDigit); 
      } //end print_100 

// =========================================================================== 
// Function  : Print_1000 
// Description : Prints out the 1st digit of user input 
// 
// Input   : 
//     input userInput 
//     output userInput 
// Return value : 
//     return value 
// 
// Notes: This function repeats several printing the lastDigit then slicing it 
// off before passing on to a new function. 
//============================================================================ 
    void print_1000(int userInput,int lastDigit) 
      { 
      lastDigit = userInput % 10; 
      printf("\n1000\'s: %d \n", lastDigit); 
      userInput = (userInput - lastDigit)/10; 
      print_10000(userInput,lastDigit); 
      } //end print_1000 

// =========================================================================== 
// Function  : Print_10000 
// Description : Prints out the 1st digit of user input 
// 
// Input   : 
//     input userInput 
//     output userInput 
// Return value : 
//     return value 
// 
// Notes: This function repeats several printing the lastDigit then slicing it 
// off before passing on to a new function. 
//============================================================================ 
    void print_10000(int userInput,int lastDigit) 
      { 
      lastDigit = userInput % 10; 
      printf("\n10000\'s: %d \n", lastDigit); 
      userInput = (userInput - lastDigit)/10; 
      return userInput; 
      }//end print_10000 

      //End Program 
+0

這段代碼做了什麼?爲什麼這樣搞砸了? –

+2

代碼中的所有函數參數都是按值傳遞的。 – Ben

+0

_但它不能正確傳遞通過理想的函數的值 - 該語句似乎並不反映您粘貼的代碼中的任何有效內容。也許你應該描述你所遇到的問題,而不是你認爲的原因。 – mah

回答

2

我不確定你想要實現這個代碼,但是你的每一個參數都是按值傳遞的。在您的代碼中:

int lastDigit; 
print_1(userInput,lastDigit); 

您正在傳遞未初始化的變量lastDigit。你基本上把「亂碼」發送給你的print_函數。雖然它對結果沒有任何影響,因爲你正在初始化它的功能,這是一個不好的做法,可能會讓你後來陷入麻煩。你應該改變他們全部:刪除lastDigit參數,並在本地聲明它,就像這樣:

void print_1(int userInput) 
{ 
    int lastDigit = userInput % 10; 

但是,如果你想lastDigit保留你的函數內設置的值,則需要按地址傳遞。

int lastDigit; 
print_1(userInput, &lastDigit); 

和修改原型,像這樣:

void print_1(int userInput, int *lastDigit) 

,你將不得不修改該變量的每次使用INSIDE你的代碼。這些上限意味着作爲一個警告,以防萬一傾向於改變這個論點。你需要首先閱讀指針。

http://www.cprogramming.com/tutorial/c/lesson6.html http://www.tutorialspoint.com/cprogramming/c_pointers.htm

0

在你的程序,你需要初始化它的lastDigit,編譯器將compain。

int lastDigit = 0; 

此外,您不需要在您的方法中傳遞lastDigit,因爲它沒有在程序中執行任何操作。

取而代之,您可以使用餘數來找出最後的數字。這是相同的代碼。

int main() { 
     int userInput; 
     printf("Please enter a positive number less than 100,000: \n"); 
     scanf("%d", &userInput); 
     if (userInput > 0 && userInput < 100000) 
     { 
      printf("\nYou have entered: %d\n",userInput);; 

     } 
     else 
     { 
      printf("\nThe number you have entered is out of the specified range\n"); 
      exit(0); 
     } 

     int inputValue = userInput; 
     int count = 1; 

     while(inputValue > 0) 
     { 
      int remainder = inputValue % 10; 
      inputValue = inputValue/10; 
      printf("\nThe %d value is %d \n", count, remainder); 
      count++; 
     } 
return 0; 

}