2016-08-29 95 views
0

我試圖設計一個這樣工作的代碼片段。用戶輸入一個3位數字,假設他們選擇了653,他們還輸入了他們希望交換的整數中的數字。例如:基本值交換功能

Enter a number and values you wish to swap: "653 2 3" 

這則返回以下值:

635 is the new number. 

我試圖做到這一點的功能我叫digit_swap。我不確定我是如何處理這個問題的,因爲我對編碼非常陌生,甚至對編碼更新。我想我必須爲整數單獨進單位,十位和元件百強要做到這一點我做了以下內容:

third = (number % 10); 
second = ((number % 100)/10); 
first = ((number % 1000)/100); 

唯一的一點是,我會用一堆if語句來確定的交換數字還是會成爲一個循環。我真的不知道如何去做這件事。至於我的代碼,我有以下。

#include <iostream> 

using std::cin; 
using std::cout; 
using std::endl; 

int digit_swap(int number, int InputOne, int InputTwo) { 

int first, second, third; 

    if (number < 100) { 
     cout << "Please enter a 3 digit integer\n"; 
     exit(0); 
    } 
    else if (number >= 1000) { 
     cout << "Please enter a 3 digit integer\n"; 
     exit(0); 
    } 
    else { 

     third = (number % 10); 
     second = ((number % 100)/10); 
     first = ((number % 1000)/100); 

    } 
} 

using namespace std; 
int main() { 
    int option_one, option_two; 
    int number; 
    cin >> number; 
    cin >> option_one >> option_two; 
    digit_swap(number, option_one, option_two); 
    cout << "New number = " << number; 

} 

即使當我測試,看它是否在它沒有返回值if語句的其他部分加入return first工作。任何幫助表示讚賞,我不是要求你爲我做代碼。

+4

你是什麼意思「它什麼都不返回」?它返回一個'int',所以它總是返回一些東西。你忘記存儲'digit_swap'的結果了嗎?現在,當'digit_swap'返回時'first','second'和'third'的值就會丟失,並且由於沒有'return'語句,'digit_swap'的返回值是未定義的價值,但不能單獨由代碼確定)。 – szym

+0

不要因爲輸入無效而致電'exit'。返回所有路徑上的內容,處理是否在'main'中打印它。 – LogicStuff

回答

0

我沒有測試你的代碼,但我認爲你想要進行的方式存在問題。

要通過它傳遞給你的函數

int digit_swap(int number, int InputOne, int InputTwo) { 

int first, second, third; 

if (number < 100) { 
    cout << "Please enter a 3 digit integer\n"; 
    exit(0); 
} 
else if (number >= 1000) { 
    cout << "Please enter a 3 digit integer\n"; 
    exit(0); 
} 
else { 

    third = (number % 10); 
    second = ((number % 100)/10); 
    first = ((number % 1000)/100); 

} 

}修改「數字」

如果你想修改函數內部變量和變化可以看到外面則需要

使用指針。如果你是編程新手,我建議你在你的主代碼中做這樣的事情。函數的工作方式,它會創建所有參數的副本,您對它們所做的更改不在原件上。

int main() { 
    int option_one, option_two; 
    int number; 
    cin >> number; 
    cin >> option_one >> option_two; 
    int result = digit_swap(number, option_one, option_two); 
    cout << "New number = " << result; 

} 

您在新的結果變量

1
int digit_swap(int number, int InputOne, int InputTwo) { 

    int first, second, third; 

    if (number < 100) { 
     // DO Something as you are doing 
    } 
    else { 
     third = (number % 10); 
     number /= 10; 
     second = (number % 10); 
     number /= 10; 
     first = (number % 10); 
     number /= 10; 
    } 
    if(InputOne == 1) { 
     if(InputTwo == 2) { 
      number += second*100 + first*10 + third; 
     } 
     else if(InputTwo == 3) { 
      number += third*100 + second*10 + first; 
     } 
     else{;} 
    } 
    else if(InputOne == 2) { 
     if(InputTwo == 3) { 
      number += first*100 + third*10 + second; 
     } 
    } 
    else{;} 
    return number; 
} 
+0

關於你的代碼的一個簡短的問題,我很驚訝它,但我不得不添加一些其他的if語句,因爲當我嘗試與第一個交換第二個數字時,我得到0? – bose

+0

@bose:我的代碼可以用於'InputOne'和'InputTwo'的升序值。 – Shravan40

0

首先你要麼需要通過引用傳遞號碼「你的函數返回」存儲否則在digit_swap數量僅僅是一個主號碼的副本( )。您的其他選擇是僅調用函數是這樣的:

number = digit_swap(number, option_one, option_two); 

或引用

void digit_swap(int & number, int InputOne, int InputTwo); 

爲了幫助您swaping我建議一個int數組。

int arr[3]; 
arr[0] = number/100; 
arr[1] = number/10; 
arr[2] = number % 10; 
int temp = arr[InputOne-1]; 
arr[InputOne-1] = arr[InputTwo-1]; 
arr[InputTwo-1] = temp; 

我希望有所幫助。