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

标签 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 没有被正确记录。

最佳答案

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

printf("%c", intval);

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

for(i=0;i<5;i++)
printf("%d ", intval[i]);
<小时/> 当您使用 scanf 读取 intval[i] 时,

i 是未初始化的。

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

初始化为0。

int intval[5], i = 0;

关于c - 尝试使用扑克游戏的冒泡排序方法对 C 中的值进行排序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14510114/

相关文章:

c++ - 开关可能会掉落(不,可能不会)

将均匀分布转换为泊松分布

c - 锁定 OMP 区域

c - 如何通过放置任何工具/库或其他来调试 C 项目

javascript - 如何根据对象数组中的用户条目进行过滤

java - 获取学生姓名和分数并按顺序对它们进行排序的代码

c - 告诉 gcc 在 C99 之前的模式下编译失败

php - MySQL 选择 SQL : how to write order by to short 1. 1.1、1.1.2 等

c++ - 在 GCC 工作正常的情况下使用 Clang 时无限递归模板实例化?

c - gcc 如何处理本地包含的文件?