c语言24点程序,C语言24点程序

第一种:

#include

double fun(double a1,double a2,int b)

{switch(b)

{case 0:return (a1+a2);

case 1:return (a1-a2);

case 2:return (a1*a2);

case 3:return (a1/a2);

}

}

void main()

{int i,j,k,l,n,m,r,save[4];

double num[4]={1,1,1,1},tem1,tem2,tem3,abc=1111;

char sign[5]="+-*/";

printf("input 4 numbers:");

for(i=0;i<4;i++)

{scanf("%lf",num+i); save[i]=num[i];}

for(i=0;i<4;i++)

for(j=0;j<4;j++)

if(j!=i)

{for(k=0;k<4;k++)

if(k!=i&&k!=j)

{for(l=0;l<4;l++)

if(l!=i&&l!=j&&l!=k)

{for(n=0;n<4;n++)

for(m=0;m<4;m++)

for(r=0;r<4;r++)

{tem1=fun(num[i],num[j],n);

tem2=fun(tem1,num[k],m);

tem3=fun(tem2,num[l],r);

if(tem3==24.0)printf("{(%d%c%d)%c%d}%c%d=24\n",save[i],sign[n],save[j],sign[m],save[k],sign[r],save[l]);

else

if(tem3==-24.0)printf("{%d%c(%d%c%d)}%c%d=24\n",save[k],sign[m],save[i],sign[n],save[j],sign[r],save[l]);

else

if(tem3==1.0/24.0)printf("%d%c{(%d%c%d)%c%d}=24\n",save[l],sign[r],save[i],sign[n],save[j],sign[m],save[k]);

else

if(tem3==-1.0/24.0)printf("%d%c{%d%c(%d%c%d)}=24\n",save[l],sign[r],save[k],sign[n],save[i],sign[m],save[j]);

else

{tem1=fun(num[i],num[j],n);

tem2=fun(num[k],num[l],r);

tem3=fun(tem1,tem2,m);

if(tem3==24.0)

printf("(%d%c%d)%c(%d%c%d)=24\n",save[i],sign[n],save[j],sign[m],save[k],sign[r],save[l]);

}

}

}

}

}

}

第二种(图形界面):

#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);

int text1(char *s) ;

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))

break;

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;

top=push(top,result);

}

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);

settextstyle(3, 0, 5);

outtextxy(220, 220, s);

getch();

return ;

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值