简体   繁体   English

基本值交换功能

[英]Basic Value Swap function

I'm trying to design a piece of code that works like this. 我正在尝试设计一段像这样的代码。 The user enters a 3 digit number, let's say they chose 653, they also input which numbers in that integer they wish to swap around. 用户输入一个3位数的数字,假设他们选择了653,他们还希望输入该整数中要交换的数字。 For example: 例如:

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

This then returns the following value: 然后返回以下值:

635 is the new number. 

I am trying to do this in a function I called digit_swap. 我正试图在我称为digit_swap的函数中执行此操作。 Im not really sure how I to approach this as I'm very new to coding and even newer to coding. 我不太确定如何处理此问题,因为我对编码非常陌生,甚至对编码还是比较新的。 I think I have to seperate the integer into the units, tens and hundred components and to do that I did the following: 我认为我必须将整数分成单位,数十个和一百个组成部分,然后执行以下操作:

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

The only thing is, would I use a bunch of if statements to determine the swapping of the numbers or would it be a loop. 唯一的是,我将使用一堆if语句来确定数字的交换还是一个循环。 I really have no idea how to go about this. 我真的不知道该怎么做。 As for my code I have the following. 至于我的代码,我有以下内容。

#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;

}

Even when I test to see if it working by adding a return first in the else segment of the if statement it returns nothing. 即使当我通过在if语句的else段中return first添加return first来测试它是否工作时,它也不返回任何内容。 Any help is appreciated, I'm not asking you to do the code for me either. 感谢您的帮助,我也不要求您为我做代码。

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;
}

I didn't test your code but I think there is an issue with the way you want to procede. 我没有测试您的代码,但我认为您要进行的方式存在问题。

you want to modify "number" by passing it to your function 您想要通过将“数字”传递给函数来对其进行修改

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);

}   

} }

if you want to modify a variable inside a function and the change can be see outside you will need to use pointer. 如果要在函数内部修改变量,并且可以在外部看到更改,则需要使用指针。 If you are new to programming I suggest you to do something like this in your main code. 如果您不熟悉编程,建议您在主代码中执行类似的操作。 The way function works, it will create copy of all your parameter, the change you made on them are not on the originals one. 函数的工作方式将创建所有参数的副本,您对它们所做的更改不在原始参数上。

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;

}

you store in the new result variable the "return of your function" 您在新的结果变量中存储“函数的返回值”

First you either need to pass number by reference otherwise number in digit_swap is just a copy of number in main(). 首先,您要么需要通过引用传递数字,否则digit_swap中的数字只是main()中数字的副本。 Your other option is to just call the function like this: 您的另一个选择是像这样调用该函数:

number = digit_swap(number, option_one, option_two);

or by reference 或参考

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

To help you with swaping i would suggest an int array. 为了帮助您进行交换,我建议您使用一个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;

I hope that helps. 希望对您有所帮助。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM