2012-07-21 65 views
1

我正在嘗試編寫一個代碼讓用戶編寫自己的數字,並決定他是否希望按升序或降序對它們進行排序,並使用冒泡排序對它們進行排序。這是我到目前爲止所能寫的(又名明顯入口);泡泡分類中的用戶輸入

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

int main() 
{ 
    int n, a, number; 
    printf("Enter your numbers. Write -1 to stop. \n"); 
    do { 
    scanf("%d", &a); 
    } while(a != -1); 
    printf("Enter 1 if you want them to be in ascending order. Enter 2 if you want descending order\n"); 
    scanf("%d", &a); 
    if(a = 1) 
    do { 
     system("PAUSE"); 
     return 0; 
    } 

我的問題是,我真的不知道如何合併泡沫排序。在我能找到的所有例子中,都有數組已經被預先設定好了。我想我應該從一個結構開始,但我不知道。

編輯:

我來到這麼遠感謝幫助,那種它「作品」,直到我寫1或2,然後崩潰。有什麼建議麼?

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

int main() 
{ 
int myarray[100],index,a,b,swap,turn; 
index=0; 
printf("Enter your numbers. Write -1 to stop. \n"); 
do{ 
      scanf("%d", &myarray[index]); 
      index++; 
      }while(myarray[index-1] != -1); 
printf("Enter 1 if you want them to be in ascending order. Enter 2 if you want descending order\n"); 
scanf("%d",&b); 
if(b == 1) { 
    for(turn=1; turn <= myarray[100] -1; turn++) 

    for(index = 0; index <= myarray[100]; index++) 
    { 
    if (myarray[index] > myarray[index+1]){ 
    swap = myarray[index]; 
    myarray[index] = myarray[index+1]; 
    myarray[index+1] = swap; } 
    } 
} 
else { 
    for(turn=1; turn <= myarray[100] -1; turn++) 

    for(index = 0; index <= myarray[100]; index++) 
    { 
    if (myarray[index] < myarray[index+1]){ 
    swap = myarray[index]; 
    myarray[index] = myarray[index+1]; 
    myarray[index+1] = swap; } 
    } 
} 
system("PAUSE"); 
return 0; 
} 
+0

您正在使用a = 1而不是== 1。 – 2012-07-21 17:41:47

+0

訪問'myarray [100]'是數組超出範圍。 – BLUEPIXY 2012-07-22 01:08:45

回答

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

typedef enum _order { 
    Ascending=1, Descending 
} order; 

void swap(int *x, int *y){ 
    int wk; 
    wk=*x;*x=*y;*y=wk; 
} 

int needSwap(int x, int y, order dir){ 
    if(dir == Ascending) 
     return x > y; 
    if(dir == Descending) 
     return x < y; 
    return 0; 
} 

void bubbleSort(int *array, int top, int end, order dir){ 
    int i, j, swaped; 
    for(i = top; i < end; ++i){ 
     swaped = 0; 
     for(j = top + 1; j <= end - i; ++j) 
      if(needSwap(array[j-1], array[j], dir)){ 
       swap(&array[j-1], &array[j]); 
       swaped = 1; 
      } 
     if(swaped == 0)break; 
    } 
} 

int main(){ 
    int myarray[100], index, order; 
    index=0; 
    printf("Enter your numbers. Write -1 to stop. \n"); 
    do{ 
     scanf("%d", &myarray[index++]); 
    }while(myarray[index-1] != -1 && index < 100); 
    --index;//Correction to point to the final value 
    printf("Enter 1 if you want them to be in ascending order.\n" 
      "Enter 2 if you want descending order\n"); 
    scanf("%d",&order); 
    bubbleSort(myarray, 0, index-1, order); 
    {//result print 
     int i; 
     for(i=0;i<index;++i) 
      printf("%d ", myarray[i]); 
     printf("\n"); 
    } 
    system("PAUSE"); 
    return 0; 
} 
+0

非常感謝:) – dawsonrose 2012-07-22 09:11:43

+0

@ user315052 - 當然。我解決了。 – BLUEPIXY 2012-07-22 12:36:55

+0

現在是否冒泡排序? – dawsonrose 2012-07-22 12:52:42

2

您存儲輸入到一個單一變量a,它獲取每次讀取更多的輸入時間覆蓋。您應該存儲每個輸入,以便您的程序知道提供的所有輸入,而不僅僅是提供的最後一個輸入。

數組是一組連續排列的相同類型的變量,並且使用單個名稱和索引進行訪問。

int arr[10]; 

在這個例子中arr構成10角連續int秒。您訪問arr[0]陣列中的第一個int,最後一個與arr[9]。要將輸入輸入到數組中,可以將a存儲在arr的正確索引中。您可以通過計算用戶迄今輸入的數字來維護正確的索引。計數將用作輸入數組的索引。不允許用戶超出聲明中定義的數組邊界,或者當您嘗試將數據存儲在與數組關聯的最後位置之外時(如果發生這種情況,稱爲緩衝區溢出),您將調用未定義的行爲。

在將輸入讀入數組後,可以將該數組傳遞給氣泡排序函數。

讓我們假設你有一個輸入程序是這樣的:

#define MAX_ARR 10 
int a; 
int entered = 0; 
int arr[MAX_ARR]; 
while (entered < MAX_ARR) { 
    if (scanf("%d", &a) != 1) break; 
    if (a == -1) break; 
    arr[entered] = a; 
    ++entered; 
} 
if (entered == MAX_ARR) { 
    printf("No more room in the array (max is %d)\n", MAX_ARR); 
} 

我們已經檢查了scanf返回預期的返回值。我們已經根據停止值檢查了輸入,並且確保用戶輸入的數據不能超過數組可以容納的數量。

輸入數組的元素數量爲entered。因此,遍歷數組,循環會是這個樣子:

int i; 
for (i = 0; i < entered; ++i) { 
    printf("arr[%d] = %d\n", i, arr[i]); 
} 

一個非常簡單的冒泡排序僅僅是不斷循環在陣列上的版本,直到你沒有做任何更多的互換。只要兩個連續的元素不符合要求的順序,您就可以進行交換。對於上升的情況下:

int j, swaps, unsorted = entered; 
do { 
    swaps = 0; 
    for (j = 1; j < unsorted; ++j) { 
     /* ... if arr[j-1] and arr[j] need to swap then: 
        swap them, and 
        increment swaps ... */ 
    } 
} while (swaps > 0); 

你知道,在數組的最後一個位置上的元素將在一個完整經歷冒泡循環的結束它的排序位置,所以unsorted的數量可以減少在每次完成之後。

2

你是正確的,你需要將這些數字存儲在某種數據結構中,如數組或矢量。矢量是一個不錯的選擇,因爲你不知道用戶將輸入多少個數字。這裏是你可以應用到你的代碼草圖:

#include <vector> 

int main() 
{ 
    // ... 
    std::vector<int> userInts; 
    // ... get input 
    userInts.push_back(a); // add int to the end of the list 

    bubbleSort(userInts); 
    // ... 
} 

編輯:我沒有意識到這一點被標記爲C,而不是C++。只需用一些代碼替換std::vector調用即可在C中動態分配數組(或者您自己的向量實現)。或者,如果只知道將輸入整數,然後聲明int userInts[N],循環輸入,將其插入到數組中並進行排序。

EDITx2:請參閱下面的@ user315052的答案,如上所述用固定長度的數組來完成此操作。

+3

問題標記爲C,而不是C++。 – jxh 2012-07-21 16:42:48

+0

@ user315052:啊,謝謝 - 我錯過了。當我回答這個問題時,這是一堆無格式和不完整的代碼,我做了一個錯誤的假設。 – jmdeldin 2012-07-21 20:21:00

1

對於第一個版本有固定大小的數組說

int myarray[100]; 
//Accept the integers 

index=0; 
do { 
    scanf("%d", &myarray[index]); 
    index++; 
} while(myarray[index-1]!= -1); 

現在你有數組和 元素總數的計數 - (索引1)

您可以申請您在數組上的排序算法。

+0

好吧,這真的很有用,但我似乎無法「阻止」輸入。有關於此的任何想法? – dawsonrose 2012-07-21 16:55:17

+1

chnage'a'到'myArray [index-1]',因爲這就是你現在輸入的內容。 – chris 2012-07-21 16:56:12

+0

非常感謝! – dawsonrose 2012-07-21 16:58:34