找回密码
 注册
搜索
热搜: 回贴
  • 前程无忧官网首页 有什么好的平台可以
  • 最新的销售平台 互联网营销的平台有哪
  • 制作网页的基本流程 网页制作和网页设
  • 【帝国CMS】输出带序号的列表(数字排
  • 网站建设公司 三一,中联,极东泵车的
  • 织梦 建站 织梦网站模版后台怎么更改
  • 云服务官网 哪些网站有免费的简历模板
  • 如何建网站要什么条件 建网站要用什么
  • 吉林市移动公司电话 吉林省退休人员网
  • 设计类毕业论文 网站设计与实现毕业论
查看: 1737|回复: 4

C++中的随机数怎么搞啊??

[复制链接]
发表于 2009-10-31 01:15:51 | 显示全部楼层 |阅读模式 IP:江苏扬州
我现在在编一个程序,大意是 :有52张牌,电脑随机抽13张,显示13张牌!!
但是我不知道,该用什么函数啊??
以前都是自定义输入的,碰到随机的还真不知道该怎么搞!!
发表于 2009-10-31 01:15:52 | 显示全部楼层 IP:江苏扬州
这个问题论坛有问过,具体你看精华贴!
回复

使用道具 举报

发表于 2009-10-31 01:15:52 | 显示全部楼层 IP:江苏扬州
崇拜
回复

使用道具 举报

发表于 2009-10-31 01:15:54 | 显示全部楼层 IP:江苏扬州
#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; }
回复

使用道具 举报

发表于 2009-10-31 01:15:55 | 显示全部楼层 IP:江苏扬州
kai的对象化编程不错啊,我现在想东西仍然用过程化来想。
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 注册

本版积分规则

QQ|小黑屋|最新主题|手机版|微赢网络技术论坛 ( 苏ICP备08020429号 )

GMT+8, 2024-9-29 15:31 , Processed in 0.239259 second(s), 13 queries , Gzip On, MemCache On.

Powered by Discuz! X3.5

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表