2012-07-21 64 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