gpt4 book ai didi

C++ SWAP 函数什么都不做?

转载 作者:行者123 更新时间:2023-11-28 01:41:15 34 4
gpt4 key购买 nike

<分区>

出于某种原因,这会返回原始值,有人可以告诉我为什么吗?谢谢你!我设置了 temp,不确定这是否正确,因为 num1 和 num2 需要设置为相等,如果我不设置,它们都将保持 1 值。

代码:

#include <iostream>
using namespace std;

void swap1(double& num1, double& num2);
void swap2(double& num1, double& num2);

double num1, num2;

int main(){
cout << "Give me two numbers to swap: ";
cin >> num1 >> num2;

cout << "Original: " << num1 << ", " << num2 << endl;
void swap1(double& num1, double& num2);
cout << "First swap: " << num1 << ", " << num2 << endl;
void swap2(double& num1, double& num2);
cout << "Second swap: " << num1 << ", " << num2 << endl;


}

void swap1(double& num1, double& num2){
double temp1;
temp1 = num1;
num1 = num2;
num2 = temp1;
}

void swap2 (double& num1, double& num2){
double temp2;
temp2 = num1;
num1 = num2;
num2 = temp2;
}

输出:抱歉格式错误!

Running /home/ubuntu/workspace/ASSIGN#31.cpp
Give me two numbers to swap: 10 20
Original: 10, 20
First swap: 10, 20
Second swap: 10, 20

34 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com