正文

24点扑克牌的游戏编程2005-05-03 13:06:00

【评论】 【打印】 【字体: 】 本文链接:http://blog.pfan.cn/jay0518/842.html

分享到:

经过调试以后,正常通过:
注意你的tc路径和输入的格式:
#include<stdio.h>
#include<graphics.h>
#include<ctype.h>
#include<time.h>
#include<stdlib.h>
#include<string.h>
#define COL 100
#define COM 40
#define N 20
char after[N];
char  p[4][13]={
{'A','2','3','4','5','6','7','8','9','0','J','Q','K'},
{'A','2','3','4','5','6','7','8','9','0','J','Q','K'},
{'A','2','3','4','5','6','7','8','9','0','J','Q','K'},
{'A','2','3','4','5','6','7','8','9','0','J','Q','K'}
};
typedef struct stack                                                                           /*定义栈1存放运算符*/
{
char stack[N];
int top;
}stack;
stack *L;
typedef struct tack                                                                       /*定义栈2计算表达式值*/
{
int tack[N];
int top;
}tack;
tack *S;
void initstack(stack *M)
{
M->top=0;
}
void inittack(tack *K)
{
K->top=0;
}
void play()
{
int j;
for(j=1;j<=4;j++)
{
bar(COL+100*j-35,COM+100-50,COL+100*j+35,COM+100+50);
setcolor(BLUE);
rectangle(COL+100*j-32,COM+100-48,COL+100*j+32,COM+100+48);
rand1(j);
delay(10000);
}
}
int rand1(int a)
{
int kind,num;
char n;
char str[3];
randomize();
while(1)
  {
   kind=random(4);
   num=random(13);
   if(p[kind][num]!=-1)
      {
        n=p[kind][num];
        p[kind][num]=-1;
        break;
      }
  }
switch(kind)
{
case 0:
setcolor(RED);
sprintf(str,"%c",3);
break;
case 1:
setcolor(BLACK);
sprintf(str,"%c",3);
break;
case 2:
setcolor(RED);
sprintf(str,"%c",4);
break;
case 3:
setcolor(BLACK);
sprintf(str,"%c",5);
break;
}
settextstyle(0,0,2);
outtextxy(COL+a*100-30,COM+100-46,str);
outtextxy(COL+a*100+16,COM+100+32,str);
if(n!='0')
{
settextstyle(0,0,3);
sprintf(str,"%c",n);
outtextxy(COL+a*100-5,COM+100-5,str);
}
else
{
  sprintf(str,"%d",10);
   outtextxy(COL+a*100-6,COM+100-5,str);
  }
  getch();
  return;
}
int text1(char *s)                                                                                       /*显示文本*/
{
setbkcolor(BLUE);
cleardevice();
setcolor(12);
settextstyle(1,0,8);
outtextxy(120,120,s);
setusercharsize(2,1,4,1);
setcolor(15);
settextstyle(220,220,*s);
getch();
return;
}
void init()
{int gdriver,gmode;
gdriver=DETECT;
initgraph(&gdriver,&gmode,"d:\\turboc2");
cleardevice();
}
void close()
{
closegraph();
}
void change(char stm[])                                                                                                 /*将表达式的输出类型进行转换*/
{int i=0,j=0;
char ch;
initstack(L);
ch=stm[i];
while(ch!='\0')
{
      while(isdigit(ch)||ch!='.')
              {
               after[j]=ch;
               j++;
              ch=stm[++i];
              }
     switch(ch)
              {
                case '+':
                case '-':
                            while(L->top!=0&&L->stack[L->top]!='(')
                                     {
                                      after[j]=L->stack[L->top];
                                      j++;
                                      L->top--;
                                     }
                                     L->top++;
                                     L->stack[L->top]=ch;
                                     break;
               case '*':
               case '/':
                while(L->stack[L->top]=='*'||L->stack[L->top]=='/')
                                    {
                                     after[j]=L->stack[L->top];
                                      j++;
                                     L->top--;
                                    }
                                     L->top++;
                                     L->stack[L->top]=ch;
                                     break;
                case '(':
                                     L->top++;
                                     L->stack[L->top]=ch;
                                     break;
               case ')':
                                     while(L->stack[L->top]!='('&&L->top!='\0')
                                              {
                                                after[j]=L->stack[L->top];
                                                j++;
                                                L->top--;
                                               }
                                      if(L->top==0)
                                             {
                                              printf("wrong input format!\n");
                                              exit(0);
                                             }
                                     L->top--;
                                     break;
                case ' ':
                                     break;
                default:
                                   printf("the express include illegal character!\n");
                                   exit(0);
                }
                i++;
                ch=stm[i];
}
while(L->top!=0)
{
   after[j]=L->stack[L->top];
   j++;
}
after[j]='\0';
printf("the changed expression is:");
puts(after);
printf("\n");
}
int compute()                                                                                                /*计算表达式的值*/
{
int m=0,n;
char ch;
inittack(S);
ch=after[m];
while(ch!='\0')
  {
                 if(isdigit(ch))
                    {
                     n=0;
                     do
                      {
                     n=10*n+ch-'0';
                     ch=after[++m];
             }while(ch!='.');
                    switch(ch)
                     {
                      case '+':
                                    S->tack[S->top-1]=S->tack[S->top-1]+S->tack[S->top];
                                    S->top--;
                                    break;
                       case '-':
                                   S->tack[S->top-1]=S->tack[S->top-1]-S->tack[S->top];
                                   S->top--;
                                   break;
                       case '*':
                                   S->tack[S->top-1]=S->tack[S->top]*S->tack[S->top-1];
                                   S->top--;
                                   break;
                        case '/':
                                    S->tack[S->top-1]=S->tack[S->top-1]/S->tack[S->top];
                                    S->top--;
                                    break;
             }
             }
                   ch=after[++m];
}
return S->tack[S->top];
}

main()
{
int i,result;
char ch,str[N];
int gdriver,gmode;
clrscr();
init();                                                                                                                                          /*图形初始化*/
while(1)
  {
   setbkcolor(BLACK);
    cleardevice();
   play();
   gotoxy(1,15);
  printf("-----------------------------------note------------------------------------------------\n");                              /*表达式的输入*/
  printf("Please enter express include only four numbers:\n");
  printf("Format as follows:2.*(3.+5.)/2.\n");
  printf("-----------------------------------------------------------------------------------------\n");
  gets(str);
  change(str);
  result=compute();
  if(result==24)
   text1("VERY GOOD!!!");
   else
    text1("WRONG!!!");
    printf("continue?(Y or N)\n");
   scanf("%c",&ch);
   if(ch=='N'||ch=='n')
   break;
}
close();                                                                                                                /*关闭图形*/

}








阅读(4518) | 评论(0)


版权声明:编程爱好者网站为此博客服务提供商,如本文牵涉到版权问题,编程爱好者网站不承担相关责任,如有版权问题请直接与本文作者联系解决。谢谢!

评论

暂无评论
您需要登录后才能评论,请 登录 或者 注册