Posted on 2012-02-28 00:36
hoshelly 閱讀(196)
評論(0) 編輯 收藏 引用 所屬分類:
DS && Algorithm
#include<stdlib.h>
#include<time.h>
#include<stdio.h>
#define MAXSTACK 100//棧的最大容量
int stack[MAXSTACK];//棧的數組聲明
int top =-1; //棧的頂端
//棧的數據存入
int push(int value)
{
if(top>=MAXSTACK)//是否超過容量
{
printf("棧的內容全滿\n");
return -1;
}
top++;
stack[top]=value;//棧指針加1,存入棧
}
//棧數據的取出
int pop()
{
int temp;
if(top<0)
{
printf("棧內容是空的\n");
return -1;
}
temp = stack[top];
top--;
return temp;
}
//檢查棧是否是空的
int empty()
{
if(top == -1)
return 1;
else
return 0;
}
//主程序:運用empty()檢查牌是否發完
//紅心:數組0~12,方塊:數組13~25,梅花:數組26~38,黑桃:數組39~51
void main()
{
int card[52];
int pos;
int i,temp;
for(i=0;i<52;i++)
card[i]=0;
i=0;
while(i!=5)//洗五張牌循環
{
pos=rand()%52;//隨機數取值0~51
if(card[pos] == 0) //是否是未洗牌
{
push(pos);//存此張牌進棧
card[pos]=1;//設置此張牌洗過
i++;//下一張牌
}
}
while(!empty())//發完棧全部的牌
{
temp=pop(); //取出棧數據
printf("[%c%3d]",temp/13+3,temp%13+1);
}
printf("\n");
}