永发信息网

速算24的设计思路

答案:1  悬赏:20  手机版
解决时间 2021-04-24 01:16
  • 提问者网友:杀手的诗
  • 2021-04-23 14:24
C语言要交作业,急需!
最佳答案
  • 五星知识达人网友:深街酒徒
  • 2021-04-23 14:59

#define N 20
#define COL 100
#define ROW 40
#include "stdio.h"
#include "time.h"
#include "graphics.h"
#include "alloc.h"
#include "stdlib.h"
#include "string.h"
#include "ctype.h"
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 node
{
int data;
struct node *link;
}STACK1;
typedef struct node2
{
char data;
struct node2 *link;
}STACK2;
void init(void);
void close(void);
void play(void);
void rand1(int j);
void change(char *e,char *a);
int computer(char *s);
STACK1 *initstack1(STACK1 *top);
STACK1 *push(STACK1 *top,int x);
STACK1 *pop(STACK1 *top);
int topx(STACK1 *top);
STACK1 *ptop(STACK1 *top,int *x);
int empty(STACK1 *top);
STACK2 *initstack2(STACK2 *top);
STACK2 *push2(STACK2 *top,char x);
STACK2 *pop2(STACK2 *top);
char topx2(STACK2 *top);
STACK2 *ptop2(STACK2 *top,char *x);
int empty2(STACK2 *top);
main()
{
char s[N],s1[N],ch;
int i,result;
int gdriver, gmode;
clrscr();
init();
while(1)
{
setbkcolor(BLACK);
cleardevice();
play();
gotoxy(1,15);
printf("--------------------Note-------------------\n");
printf(" Please enter express accroding to above four number\n");
printf(" Format as follows:2.*(5.+7.)\n");
printf(" ----------------------------------------------\n");
scanf("%s%c",s1,&ch);
change(s1,s);
result=computer(s);
if(result==24)
text1("very good");
else
text1("wrong!!!");
printf("Continue (y/n)?\n");
scanf("%c",&ch);
if(ch=='n'||ch=='N')
break;
}
close();
return;
}
void rand1(int j)
{
int kind,num;
char str[3],n;
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+j*100-30,ROW+100-46,str);
outtextxy(COL+j*100+16,ROW+100+32,str);
if(n!='0')
{
settextstyle(0,0,3);
sprintf(str,"%c",n);
outtextxy(COL+j*100-5,ROW+100-5,str);
}
else
{
sprintf(str,"%d",10);
outtextxy(COL+j*100-6,ROW+100-5,str);
}
}
void play(void)
{
int j;
for(j=0;j<4;j++)
{
bar(COL+j*100-35,ROW+100-50,COL+j*100+35,ROW+1*100+50);
setcolor(BLUE);
rectangle(COL+j*100-32,ROW+100-48,COL+j*100+32,ROW+100+48);
rand1(j);
delay(10000);
}
}
void init(void)
{
int gd=DETECT,gm;
initgraph(&gd,&gm,"c:\\tc");
cleardevice();
}
void close(void)
{
closegraph();
}
void change(char *e,char *a)
{
STACK2 *top=NULL;
int i,j;char w;
i=0;
j=0;
while(e[i]!='\0')
{
if(isdigit(e[i]))
{
do{
a[j]=e[i];
i++;
j++;
}while(e[i]!='.');
a[j]='.';j++;
}
if(e[i]=='(')
top=push2(top,e[i]);
if(e[i]==')')
{
top=ptop2(top,&w);
while(w!='(')
{
a[j]=w;
j++;
top=ptop2(top,&w) ;
}
}
if(e[i]=='+'||e[i]=='-')
{
if(!empty2(top))
{
w=topx2(top);
while(w!='(')
{
a[j]=w;
j++;
top=pop2(top);
if(empty2(top))
break;
else
w=topx2(top);
}
}
top=push2(top,e[i]);
}
if(e[i]=='*'||e[i]=='/')
{
if(!empty2(top))
{
w=topx2(top);
while(w=='*'||w=='/')
{
a[j]=w;
j++;
top=pop2(top);
if(empty2(top))

else
w=topx2(top);
}
}
top=push2(top,e[i]);
}
i++;
}
while(!empty2(top))
top=ptop2(top,&a[j++]);
a[j]='\0';
}
int computer(char *s)
{
STACK1 *top=NULL;
int i,k,num1,num2,result;
i=0;
while(s[i]!='\0')
{
if(isdigit(s[i]))
{
k=0;
do{
k=10*k+s[i]-'0';
i++;
}while(s[i]!='.');
top=push(top,k);
}
if(s[i]=='+')
{
top=ptop(top,&num2);
top=ptop(top,&num1);
result=num2+num1;
top=push(top,result);
}
if(s[i]=='-')
{
top=ptop(top,&num2);
top=ptop(top,&num1);
result=num1-num2;
top=push(top,result);
}
if(s[i]=='*')
{
top=ptop(top,&num2);
top=ptop(top,&num1);
result=num1*num2;
top=push(top,result);
}
if(s[i]=='/')
{
top=ptop(top,&num2);
top=ptop(top,&num1);
result=num1/num2;
}
i++;
}
top=ptop(top,&result);
return result;
}
STACK1 *initstack1(STACK1 *top)
{
top=NULL;
return top;
}
STACK1 *push(STACK1 *top,int x)
{
STACK1 *p;
p=(STACK1 *)malloc(sizeof(STACK1));
if(p==NULL)
{
printf("memory is overflow\n!!");
exit(0);
}
p->data=x;
p->link=top;
top=p;
return top;
}
STACK1 *pop(STACK1 *top)
{
STACK1 *q;
q=top;
top=top->link;
free(q);
return top;
}
int topx(STACK1 *top)
{
if(top==NULL)
{
printf("Stack is null\n");
return 0;
}
return top->data;
}
STACK1 *ptop(STACK1 *top,int *x)
{
*x=topx(top);
top=pop(top);
return top;
}
int empty(STACK1 *top)
{
if(top==NULL)
return 1;
else
return 0;
}
STACK2 *initstack2(STACK2 *top)
{
top=NULL;
return top;
}
STACK2 *push2(STACK2 *top,char x)
{
STACK2 *p;
p=(STACK2 *)malloc(sizeof(STACK2));
if(p==NULL)
{
printf("memory is overflow\n!!");
exit(0);
}
p->data=x;
p->link=top;
top=p;
return top;
}
STACK2 *pop2(STACK2 *top)
{
STACK2 *q;
q=top;
top=top->link;
free(q);
return top;
}
char topx2(STACK2 *top)
{
if(top==NULL)
{
printf("Stack is null\n");
return '';
}
return top->data;
}
STACK2 *ptop2(STACK2 *top,char *x)
{
*x=topx2(top);
top=pop2(top);
return top;
}
int empty2(STACK2 *top)
{
if(top==NULL)
return 1;
else
return 0;
}

int text1(char *s)
{
setbkcolor(BLUE);
cleardevice();
setcolor(12);
settextstyle(1, 0, 8);
outtextxy(120, 120, s);
setusercharsize(2, 1, 4, 1);
setcolor(15);
outtextxy(220, 220, s);
getch();
return ;
}




用C 设计的速算24
我要举报
如以上回答内容为低俗、色情、不良、暴力、侵权、涉及违法等信息,可以点下面链接进行举报!
点此我要举报以上问答信息
大家都在看
推荐资讯