| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 1092 人关注过本帖
标题:C++中的随机数怎么搞啊??
只看楼主 加入收藏
corrupt
Rank: 2
等 级:新手上路
威 望:3
帖 子:535
专家分:0
注 册:2004-9-29
收藏
 问题点数:0 回复次数:4 
C++中的随机数怎么搞啊??

我现在在编一个程序,大意是 :有52张牌,电脑随机抽13张,显示13张牌!!

但是我不知道,该用什么函数啊??

以前都是自定义输入的,碰到随机的还真不知道该怎么搞!!

搜索更多相关主题的帖子: 随机数 
2004-10-16 23:39
live41
Rank: 10Rank: 10Rank: 10
等 级:贵宾
威 望:67
帖 子:12442
专家分:0
注 册:2004-7-22
收藏
得分:0 
这个问题论坛有问过,具体你看精华贴!
2004-10-17 01:23
poppylx
Rank: 1
等 级:新手上路
帖 子:367
专家分:0
注 册:2004-9-27
收藏
得分:0 

崇拜


动于心而静如水
2004-10-17 04:51
kai
Rank: 16Rank: 16Rank: 16Rank: 16
等 级:版主
威 望:52
帖 子:3450
专家分:59
注 册:2004-4-25
收藏
得分:0 

#include <iostream> #include <string> #include <cstdlib> #include <ctime> #include <windows.h> using namespace std;

#define set_color(i, c) c = (i==0)?"hei":((i==1)?"hong":((i==2)?"cao":((i==3)?"fang":""))) struct Card { char value; string color; };

class Cards { private: Card * myCards; int total; public: Cards(); Cards(int n); void get_13_cards(Cards * my_13_cards); void display(); ~Cards() { if(myCards && total == 1) { total = 0; delete myCards; } else { total = 0; delete []myCards; } } };

Cards::Cards() { myCards = NULL; total = 0; } Cards::Cards(int n) { int i = 0; int j = 0; if(n == 52) { myCards = new Card[52]; total = 52; for(i = 0; i<13; i++) { for(j = 0; j<4; j++) { myCards[4*i+j].value = i+1; set_color(j, myCards[4*i+j].color); } } } else { myCards = new Card[n]; total = n; for(i = 0; i<n; i++) { myCards[i].value = 0; myCards[i].color = " "; } } } void Cards::get_13_cards(Cards * my_13_cards) { int count = 0; int pos = 0; srand( (unsigned)GetTickCount() );

do { pos = rand()%52; if(myCards[pos].value) { ((my_13_cards->myCards)+count)->value = myCards[pos].value; ((my_13_cards->myCards)+count)->color = myCards[pos].color; myCards[pos].value = 0; myCards[pos].color = " "; count++; } }while(count<13);

} void Cards::display() { for(int i = 0; i<total; i++) { if((myCards+i)->value==11) cout<<i+1<<".Card: "<<'J'<<" "<<(myCards+i)->color<<endl; else if((myCards+i)->value==12) cout<<i+1<<".Card: "<<'Q'<<" "<<(myCards+i)->color<<endl; else if((myCards+i)->value==13) cout<<i+1<<".Card: "<<'K'<<" "<<(myCards+i)->color<<endl; else cout<<i+1<<".Card: "<<(int)((myCards+i)->value)<<" "<<(myCards+i)->color<<endl; } }

int main() { Cards myCards(52); Cards my_13_Cards(13); myCards.get_13_cards(&my_13_Cards); my_13_Cards.display();

system("pause"); return 0; }


自由,民主,平等,博爱,进步.
中华民国,我的祖国,中华民国万岁!中华民国加油!
本人自愿加入中国国民党,为人的自由性,独立性和平等性而奋斗!
2004-10-17 05:09
live41
Rank: 10Rank: 10Rank: 10
等 级:贵宾
威 望:67
帖 子:12442
专家分:0
注 册:2004-7-22
收藏
得分:0 

kai的对象化编程不错啊,我现在想东西仍然用过程化来想。

2004-10-17 09:11
快速回复:C++中的随机数怎么搞啊??
数据加载中...
 
   



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

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