gpt4 book ai didi

c - 指针和与 C 的交换

转载 作者:太空宇宙 更新时间:2023-11-04 07:02:16 25 4
gpt4 key购买 nike

我对如何执行此操作有一个大致的了解,您可以从我在下面执行的代码中看到这一点。我遇到的唯一问题是完成互换部分。基本上,我要做的是将最低变量的值移动到第一个变量,将第二个中间值移动到第二个变量,将最大的值移动到第三个变量。

我知道我通过交换和使用 temp 以某种方式执行此操作,但我将如何完成它,因为使用三个值 temp 会以某种方式被覆盖。我错过了什么?所以基本上 a = 4.0, b = 7.0, c = 1.0, c (1.0) 需要进入 a, a ( 4.0)需要进入bb(7.0)需要进入c。谢谢!

#include <stdio.h>
void interchange(double * x, double * y, double * z);

int main(void)
{
double a = 4.0, b = 7.0, c = 1.0;

printf_s("Originally a = %d, b = %d, and c = %d.\n", a, b, c);
interchange(&a, &b, &c);
printf_s("Now, a = %d, b = %d, and c = %d.\n", a, b, c);

return 0;

}

void interchange(double * x, double * y, double * z)
{
double temp;

temp = *z;
*y = *z;
* = temp

// what am I missing here? I cant get my head around this above ^^^

}

感谢指导!

最佳答案

类似于:

void swap(double* first, double* last){
double temp = *first;
*first = *last;
*last = temp;
}

void interchange(double * x, double * y, double * z){
if(*x > *y) swap(x, y);
if(*y > *z) swap(y, z);
if(*x > *y) swap(x, y);
}

关于c - 指针和与 C 的交换,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36604462/

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