我正在試圖通過使用指針增加值來製作一個程序,該程序可以定位三個雙數字。使用指針對3個double值進行排序
我可以打印double值;但是,對於大多數訂單而言,它們的訂單並不正確。
這是我有:
#include <stdio.h>
void sort3(double *x, double *y, double *z);
int main()
{
double x,y,z;
printf("Enter three numbers: ");
scanf("%lf %lf %lf", &x,&y,&z);
sort3(&x,&y,&z);
return 0;
}
void sort3(double *x, double *y, double *z)
{
double temp, temp2, temp3, temp4, temp5, temp6, temp7;
if (y<x && y<z && z>x) // MSL
{
temp = *x;
*x = *y;
*y = temp;
printf("The order sequence is: %.1lf %.1lf %.1lf \n", *x, *y, *z);
}
else if (z<x && (x>y) && (y>z)){ // LMS
temp2 = *z;
*z = *x;
*x = temp2;
printf("The order sequence is: %.1lf %.1lf %.1lf \n", *x, *y, *z);
}
else if(z>y && y<x && x>z) { // LSM
temp3 = *z;
*z = *x;
*x = temp3;
temp4 = *x;
*x = *y;
*y = temp4;
printf("The order sequence is: %.1lf %.1lf %.1lf \n", *x, *y, *z);
}
else if(z>x && y>z && y>x) { // SLM
temp5 = *z;
*z = *y;
*y = temp5;
printf("The order sequence is: %.1lf %.1lf %.1lf \n", *x, *y, *z);
}
else if(x>z && y>z && y>x){ // MLS
temp6 = *x;
*x = *y;
*y = temp6;
temp7 = *y;
*y = *x;
*x = temp7;
printf("The order sequence is: %.1lf %.1lf %.1lf \n", *x, *y, *z);
}
else{
printf("The order sequence is: %.1lf %.1lf %.1lf \n", *x, *y, *z);
} //SML
}
我不知道問題出在哪裏,如何,以及如何解決這些問題。
首先,你只需要一個臨時做你swaping。其次,哪些訂單不起作用? –
我認爲他們中的大多數。我會更新這個問題。 –