请教一个C的程序,各位大侠帮帮忙,急需回复!

bobsame 2003-05-15 12:54:53
0-13任取四个数,
加减乘除等于24,否则排除就可以。
...全文
28 5 打赏 收藏 转发到动态 举报
写回复
用AI写文章
5 条回复
切换为时间正序
请发表友善的回复…
发表回复
Ruan76 2003-05-15
  • 打赏
  • 举报
回复
晕,这么多
junweishao 2003-05-15
  • 打赏
  • 举报
回复
我这里有一个程序:1到9,加减乘除都用到(不能有括号),和为100,有些地方你该以下就应该能用了:
#include "stdio.h"
#include "stdlib.h"
#include "string.h"
#include "math.h"
#define N 100
extern char nun[21];
char precede(char r,char s)
{ switch(r)
{ case '+': if(s=='*'||s=='/'||s=='(') return '<';
else return '>';
case '-': if(s=='*'||s=='/'||s=='(') return '<';
else return '>';
case '*': if(s=='(') return '<';
else return '>';
case '/': if(s=='(') return '<';
else return '>';
case '(': if(s==')') return '=';
else return '<';
case ')': return '>';
case '=': return '<';
default :return '<';
}
}
double operate(double a,char theta,double b)
{ switch(theta)
{ case '+': return a+b;
case '-': return a-b;
case '*': return a*b;
case '/': return a/b;
}
}
int isop(char ch)
{if(ch=='+'||ch=='-'||ch=='*'||ch=='/'||ch=='('||ch==')'||ch=='=')return 1;
return 0;
}
int num(char *p)
{ int i,k;
int j=0;
k=strlen(p);
for(i=0;i<k;i++)
if(*(p+i)=='+'||*(p+i)=='-'||*(p+i)=='*'||*(p+i)=='/')j++;
return j;
}
clear(char **p,char **r)
{ int i;
int k=1;
while(*p+k<*r)
{ if(precede(*(*p+k),*(*p+k+1))=='=')
{ i=0;
do{ *(*p+k+i)=*(*p+k+i+2);
i++;
}while(*(*p+k+i+1)!='\0');
**r='\0';
*r=*r-2;
}
k++;
}
}
double fun()
{ char *p,*top1,*base1,ch;
double *s,*top2,f;
int i=0,j=0,k=0;
int a,b,m,n,count=0;
double e;
p=(char *)malloc(N*sizeof(char));
s=(double *)malloc(N*sizeof(double));
base1=top1=p;
top2=s;
*top1='=';
ch=nun[count++];
while(ch!='=')
{ if(isop(ch)) {*(++top1)=ch;ch=nun[count++];i=0;j=0;k=0;}
else {a=ch-'0';
*top2++=(double)a;
ch=nun[count++];
i++;
if(a=='.'-'0'){top2--;i=0;j=1;k=0;continue;}
if(j==1){k++;f=*--top2;
*(top2-1)=*(top2-1)+f*pow(10.0,(double)(-k));
i=0;}
if(i==2){f=*--top2;*(top2-1)=*(top2-1)*10.0+f;i=1;}
}
}
*(top1+1)='\0';
top2--;
do{ switch(precede(*(top1-1),*top1))
{ case '<': { *(top2-1)=operate(*(top2-1),*top1,*top2);
*top1='\0';
top1--;top2--;
} break;
case '>': { b=1;
while(precede(*(top1-b-1),*(top1-b))=='>')
b++;
m=num(top1-b);
*(top2-m)=operate(*(top2-m),*(top1-b),*(top2-m+1));
for(n=0;n<=b;n++) *(top1-b+n)=*(top1-b+n+1);
for(n=1;n<=m;n++) *(top2-m+n)=*(top2-m+n+1);
top1--;
top2--;
clear(&base1,&top1);
}
}
}while(*top1!='=');
printf("%s%lf ",nun+1,*top2);
e=*top2;
free(p);
free(s);
return e;
}
#include ".\program\calcul.c"
#include "stdio.h"
#include "string.h"
#include "ctype.h"
static char optr[4]={'+','-','*','/'};
char nun[21]={'0','-','1',' ','2',' ','3',' ','4',' ','5',' ','6',' ','7',' ','8',' ','9','=','\0'};
main()
{ long int i,k;
int j,l;
double e;
int ff[8]={0};
for(i=0;i<65536;i++)
{ k=i;
for(j=0;j<8;j++){l=k%4;k=k/4;ff[7-j]=l;}
for(j=0;j<8;j++)nun[2*j+3]=optr[ff[j]];
e=fun(); printf("%ld\n",i);
if(e-100>-0.1&&e-100<0.1)
if(strchr(nun,'+'))
if(strchr(nun,'-'))
if(strchr(nun,'*'))
if(strchr(nun,'/'))
{printf("\nThe result is:\n");
printf("%s100\n",nun+1);
break;
}
}
if(i==65536)printf("\nThere is no suitable expression!!\n");
}
bm1408 2003-05-15
  • 打赏
  • 举报
回复
自己做,不是难!又来要作业!
suayee 2003-05-15
  • 打赏
  • 举报
回复
想遍个游戏就说话,别藏藏掩掩的,可以用简单的循环,或用四维数组.都可以实现
chinaninee 2003-05-15
  • 打赏
  • 举报
回复
说得明白一点,是选出可以的组合,还是用户输入4个数,然后判断?

69,371

社区成员

发帖
与我相关
我的任务
社区描述
C语言相关问题讨论
社区管理员
  • C语言
  • 花神庙码农
  • 架构师李肯
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧