2014-02-14 64 views
0

我正在試圖通過使用指針增加值來製作一個程序,該程序可以定位三個雙數字。使用指針對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 




    } 

我不知道問題出在哪裏,如何,以及如何解決這些問題。

+0

首先,你只需要一個臨時做你swaping。其次,哪些訂單不起作用? –

+0

我認爲他們中的大多數。我會更新這個問題。 –

回答

0

您正在比較if中的指針。將您的比較更改爲使用* x,* y,* z。由於它們最初來自堆棧(按順序聲明x,y,z),所以指針值的順序始終相同(已排序)。

但是,在你最後一種情況下,你交換x和y兩次,所以你最終沒有排序。

0

沒有直接的答案,原來的問題,而是要實現sort3()一個簡單的方法:

void 
sort3 (int *a, int *b, int *c) 
{ 
    int min, mid, max; 

    if (*a <= *b) { 
     if (*a <= *c) { 
      min = *a; 
      if (*b <= *c) { 
       mid = *b; 
       max = *c; 
      } 
      else { 
       mid = *c; 
       max = *b; 
      } 
     } 
    } 
    else 
     sort3(b, a, c); 

    *a = min; 
    *b = mid; 
    *c = max; 
} 

下面是完整的代碼:

#include <stdio.h> 
#include <stdlib.h> 

void 
sort3 (int *a, int *b, int *c) 
{ 
    int min, mid, max; 

    if (*a <= *b) { 
     if (*a <= *c) { 
      min = *a; 
      if (*b <= *c) { 
       mid = *b; 
       max = *c; 
      } 
      else { 
       mid = *c; 
       max = *b; 
      } 
     } 
    } 
    else 
     sort3(b, a, c); 

    *a = min; 
    *b = mid; 
    *c = max; 
} 

void 
sort_print(int a, int b, int c) 
{ 
    printf("Before: %d, %d, %d\n", a, b, c); 
    sort3(&a, &b, &c); 
    printf("After: %d, %d, %d\n", a, b, c); 
} 

int 
main(void) 
{ 
    sort_print(1, 2, 3); 
    sort_print(1, 3, 2); 
    sort_print(2, 1, 3); 
    sort_print(2, 3, 1); 
    sort_print(3, 1, 2); 
    sort_print(3, 2, 1); 
    exit(EXIT_SUCCESS); 
} 

輸出:

$ ./a.out 
Before: 1, 2, 3 
After: 1, 2, 3 
Before: 1, 3, 2 
After: 1, 2, 3 
Before: 2, 1, 3 
After: 1, 2, 3 
Before: 2, 3, 1 
After: 1, 2, 3 
Before: 3, 1, 2 
After: 1, 2, 3 
Before: 3, 2, 1 
After: 1, 2, 3 
0

您是否正在尋找類似的產品

void d_swap(double *a, double *b) 
{ 
    double tmp = *a; 
    *a = *b; 
    *b = tmp; 
} 

void sort3(double *x, double *y, double *z) 
{ 
    if (*x > *y) { 
    if (*x < *z) { 
     if (*y > *z) { 
     d_swap(y, z); 
     } 
    } else { 
     d_swap (x,z); 
     d_swap (y,z); 
    } 
    } else if (*y < *z) { 
    if (*x < *z) { 
     d_swap(x, y); 
    } else { 
     d_swap (x,y); 
     d_swap (y,z); 
    } 
    } else { 
     d_swap (x, z); 
    } 
    printf("The order sequence is: %.1lf %.1lf %.1lf \n", *x, *y, *z); 
} 

不漂亮,但容易理解:-P

0
void swap(double *x, double *y){ 
    double t = *x; 
    *x = *y; 
    *y = t; 
} 

void sort3(double *x, double *y, double *z){ 
    if(*x > *y) 
     swap(x, y); 
    if(*x > *z) 
     swap(x, z); 
    if(*y > *z) 
     swap(y, z); 
    printf("The order sequence is: %.1lf %.1lf %.1lf \n", *x, *y, *z); 
} 
相關問題