尝试使用冒泡游戏的冒泡排序方法对C中的值进行排序

时间:2013-01-24 20:27:16

标签: c sorting gcc poker

所以我创建了一个(主要是工作的)扑克手分计,我目前唯一的问题是我需要对卡的值进行排序,以便正确识别直线或类似的。

目前我的程序返回外键,其中显示排序的整数金额。

我当前的输入:6D 7D 9D 8C 5D

我目前的输出:

  Six of diamonds
  Seven of diamonds 
  Nine of diamonds 
  Eight of clubs 
  Five of diamonds 
  � <--- (This is where I have told it to display the Numbers in order)
  High Card! 1 point.

基本上我知道我的得分计数是正确的,只是排序不起作用。

我的代码

  #include <stdio.h>

int main( void )
{
 char cardval[10] = {'\0'}, count;
 char maincard[2] = {'\0'};
 int intval[5], i;
 int tempval;
 int dupecount = 0;
 int suitcount = 0;
 int a;


 for(count=0;count<10;count++)
 {   
  scanf(" %c", &(cardval[count] ) );
  scanf(" %d", &(intval[i]) );
  i++;
  count++;
  scanf(" %c", &(cardval[count] ) );

  strcpy( maincard, &cardval[count-1] );
  strcat( maincard, &cardval[count] ); 
  switch (maincard[0])
  {
   case '2':
      printf ( "Two of ");
   break;
   case '3':
      printf ( "Three of ");
   break;
   case '4':
      printf ( "Four of ");
   break;
   case '5':
      printf ( "Five of ");
   break;
   case '6':
      printf ( "Six of ");
   break;
   case '7':
      printf ( "Seven of ");
   break;
   case '8':
      printf ( "Eight of ");
   break;
   case '9':
      printf ( "Nine of ");;
   break;  
   case 'J':
      printf( "Jack of ");
   break;
   case 'Q':
      printf( "Queen of ");
   break;
   case 'K':
      printf( "King of ");;
   break;
   case '0':
      printf( "Ten of ");
   break;
   case 'A':
      printf( "Ace of ");
   break;
   default:
      printf( "\n %c is an incorrect input, please enter a number 0, 2-9, A,K,Q,J", maincard[0]);
      return 0;
   break;
  } 
  switch (maincard[1])
  {
   case 'D':
      printf( "diamonds");
   break;
   case 'S':
      printf( "spades");
   break;
   case 'H':
      printf( "hearts");
   break;
   case 'C':
      printf( "clubs");
   break;
   default:
      printf( "\n %c is an incorrect input, please enter D, S, H, or C as a suit", maincard[1]);
      return 0;
   break;
  }
  printf ("\n");
 }
 for ( a=0; a<5; a++ )
 {
  for ( i=0; i<5-1; i++)
   {
     if ( intval[i] > intval[i+1])
      {
       tempval = intval[i];
       intval[i] = intval[i+1];
       intval[i+1] = tempval;
       tempval = intval [i];
      }
  }
}
printf("%c", intval);

我的代码部分有什么问题?似乎没有正确记录intval。

2 个答案:

答案 0 :(得分:1)

intval是一个包含5个整数的数组。所以以下是错误的:

printf("%c", intval);

要打印数组,请使用循环:

for(i=0;i<5;i++)
printf("%d ", intval[i]);

当您使用scanf阅读i时,

intval[i]是未初始化的。

  scanf(" %d", &(intval[i]) );

将其初始化为0.

int intval[5], i = 0;

答案 1 :(得分:0)

试试这个。

if ( intval[i] > intval[i+1])
  {
   tempval = intval[i+1];
   intval[i+1] = intval[i];
   intval[i]=tempval; 
  }