免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
最近访问板块 发新帖
查看: 3813 | 回复: 3
打印 上一主题 下一主题

[C] 这个程序什么问题? [复制链接]

论坛徽章:
0
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2013-09-10 09:43 |只看该作者 |倒序浏览
/* Deals a random hand of cards */
#include <stdbool.h> /* C99 only */
#include <stdio.h>
#include <stdlib.h>
#include <time.h>

#define NUM_SUITS 4
#define NUM_RANKS 13

int main(void)
{
bool in_hand[NUM_SUITS][NUM_RANKS]={false};
int num_cards, rank, suit;
const char rank_code[]={'2','3','4','5','6','7','8','9','t','j','q','k','a'};
const char suit_code[]={'c','d','h','s'};

srand((unsigned) time(NULL));

printf("Enter number of cards in hand: ");
scanf("%d", &num_cards);

printf("Your hand:");
while (num_cards>0){
suit=rand()%NUM_SUITS;
suit=rand()%NUM_RANKS;
if(!in_hand[suit][rank]){
  in_hand[suit][rank]=true;
  num_cards--;
  printf(" %c%c", rank_code[rank], suit_code[suit]);
}
}
printf("\n");

return 0;
}

这是书上的一道例题,不知为什么我一运行就出错?

论坛徽章:
0
2 [报告]
发表于 2013-09-10 10:01 |只看该作者
  1. while (num_cards>0){
  2. suit=rand()%NUM_SUITS;
  3. [color=Red]suit[/color]=rand()%NUM_RANKS;  // 这里应该是rank吧
  4. if(!in_hand[suit][rank]){
  5.   in_hand[suit][rank]=true;
  6.   num_cards--;
  7.   printf(" %c%c", rank_code[rank], suit_code[suit]);
  8. }
复制代码

论坛徽章:
0
3 [报告]
发表于 2013-09-10 12:46 |只看该作者
额 lz可以学一下如何做调试 这样对于一些简单问题 以后可以自己解决问题
linux下调试用gdb
http://www.gnu.org/software/gdb/

论坛徽章:
0
4 [报告]
发表于 2013-09-23 23:55 |只看该作者
支持3楼 自己动手丰衣足食:wink:
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP