| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 786 人关注过本帖
标题:洗牌,发牌程序编译没错,为什么结果出不来?
只看楼主 加入收藏
GeneralJing
Rank: 2
等 级:论坛游民
帖 子:30
专家分:15
注 册:2011-9-26
收藏
 问题点数:0 回复次数:5 
洗牌,发牌程序编译没错,为什么结果出不来?
写了个洗牌和发牌的程序,程序编译正确,但运行后却没有任何结果,希望大家给看看问题出现在哪里?
头文件:DeckOfCards.h
class DeckOfCards
{
public:
    DeckOfCards();
    void shuffle();
    void deal();
private:
    int deck[ 4][ 1 ];
};
函数声明 DeckOfCards1.cpp
#include <iostream>
using std::cout;
using std::left;
using std::right;

#include <iomanip>
using std::setw;

#include <cstdlib>
using std::rand;
using std::srand;

#include <ctime>
using std::time;

#include "DeckOfCards.h"

DeckOfCards::DeckOfCards()
{
    for ( int row = 0; row <= 3; row++ )
    {
        for ( int column = 0; column <= 12; column++ )
        {
            deck[ row ][ column ] = 0;
        }
    }

    srand( time( 0 ) );
}

void DeckOfCards::shuffle()
{
    int row;
    int column;

    for ( int card = 1; card <= 52; card++ )
    {
        do
        {
            row = rand() % 4;
            column = rand() % 13;
        } while( deck[ row ][ column ] != 0 );

        deck[ row ][ column ] = card;
    }
}

void DeckOfCards::deal()
{
    static const char *suit[ 4 ] =
    { "Hearts", "Diamonds", "Clubs", "Spades" };

    static const char *face[ 13 ] =
    { "Ace", "Deuce", "Three", "Four", "Five", "Six", "Seven",
    "Eight", "Nine", "Ten", "Jack", "Queen", "King" };

    for ( int card = 1; card <= 52; card++ )
    {
        for (int row = 0; row <= 3; row++ )
         {   
             for ( int column = 0; column <= 12; column++ )
             {
            if ( deck[ row ][ column ] == card )
            {
                cout << setw( 5 ) << right << face[ column ]
                << " of " << setw( 8 ) << left << suit[ row ]
                << ( card % 2 == 0 ? '\n' : '\t' );
            }
             }
        }
    }
}
主函数 DeckOfCards.h
#include "DeckOfCards.h"
int main()
{
    DeckOfCards deckOfCards;
    deckOfCards.shuffle();
    deckOfCards.deal();
    return 0;
}
搜索更多相关主题的帖子: 声明 shuffle private include 
2011-10-13 21:05
mengcan555
Rank: 4
等 级:业余侠客
帖 子:104
专家分:253
注 册:2011-9-17
收藏
得分:0 
class DeckOfCards
{
public:
    DeckOfCards();
    void shuffle();
    void deal();
private:
    int deck[ 4][ 1 ];
} 把1改成13
2011-10-14 11:26
mengcan555
Rank: 4
等 级:业余侠客
帖 子:104
专家分:253
注 册:2011-9-17
收藏
得分:0 
还有就是 别写这么多
using std::rand;
using std::srand;
直接写成 using namespace std ; 就ok了,那样太麻烦
2011-10-14 11:29
mengcan555
Rank: 4
等 级:业余侠客
帖 子:104
专家分:253
注 册:2011-9-17
收藏
得分:0 
#include <iostream>
//using std::cout;
//using std::left;
//using std::right;

#include <iomanip>
//using std::setw;

#include <cstdlib>
//using std::rand;
//using std::srand;

#include <ctime>
using namespace std;
//using std::time;
class DeckOfCards
{
public:
    DeckOfCards();
    void shuffle();
    void deal();
private:
    int deck[ 4][ 13];
};

//#include "DeckOfCards.h"

DeckOfCards::DeckOfCards()
{
    for ( int row = 0; row <= 3; row++ )
    {
        for ( int column = 0; column <= 12; column++ )
        {
            deck[ row ][ column ] = 0;
        }
    }

    srand( time( 0 ) );
}

void DeckOfCards::shuffle()
{
    int row;
    int column;

    for ( int card = 1; card <= 52; card++ )
    {
        do
        {
            row = rand() % 4;
            column = rand() % 13;
        } while( deck[ row ][ column ] != 0 );

        deck[ row ][ column ] = card;
    }
}

void DeckOfCards::deal()
{
    static const char *suit[ 4 ] =
    { "Hearts", "Diamonds", "Clubs", "Spades" };

    static const char *face[ 13 ] =
    { "Ace", "Deuce", "Three", "Four", "Five", "Six", "Seven",
    "Eight", "Nine", "Ten", "Jack", "Queen", "King" };

    for ( int card = 1; card <= 52; card++ )
    {
        for (int row = 0; row <= 3; row++ )
         {   
             for ( int column = 0; column <= 12; column++ )
             {
            if ( deck[ row ][ column ] == card )
            {
                cout << setw( 5 ) << right << face[ column ]
                << " of " << setw( 8 ) << left << suit[ row ]
                << ( card % 2 == 0 ? '\n' : '\t' );
            }
             }
        }
    }
}

//#include "DeckOfCards.h"
int main()
{
    DeckOfCards deckOfCards;
    deckOfCards.shuffle();
    deckOfCards.deal();
    return 0;
}
2011-10-14 11:30
GeneralJing
Rank: 2
等 级:论坛游民
帖 子:30
专家分:15
注 册:2011-9-26
收藏
得分:0 
回复 2楼 mengcan555
多谢大侠,这个错误让我找了好久,我还以为是底下的那个洗牌函数中的时间函数设置有错误呢,多谢多谢!牛人!!!交个朋友啊哈哈
2011-10-14 15:04
GeneralJing
Rank: 2
等 级:论坛游民
帖 子:30
专家分:15
注 册:2011-9-26
收藏
得分:0 
回复 3楼 mengcan555
关于using namespace std;我想问一下,是不是在一个#include下声明,所有的都不用写了,包括cin,cout,endl,srand,fixed,rand,time等等,这样对吗?
2011-10-14 15:09
快速回复:洗牌,发牌程序编译没错,为什么结果出不来?
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.055811 second(s), 7 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved