0

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

目前,我的程序返回要显示已排序整数数量的外键。

我当前的输入: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。

4

2 回答 2

1

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

printf("%c", intval);

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

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

iintval[i]使用 scanf阅读时未初始化。

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

将其初始化为 0。

int intval[5], i = 0;
于 2013-01-24T20:30:26.440 回答
0

尝试这个。

if ( intval[i] > intval[i+1])
  {
   tempval = intval[i+1];
   intval[i+1] = intval[i];
   intval[i]=tempval; 
  }
于 2013-01-24T21:33:49.817 回答