69,371
社区成员
发帖
与我相关
我的任务
分享
if(C==')'||C==']'||C=='}')j++;
chs=(char *)malloc(3*t+5);
sybpos=(int *)malloc( sizeof(int)*j + sizeof(double)*(w+j) );
db=(double *)(sybpos+j);
if(!ExprChs || !*ExprChs || stlen(ExprChs)>49999)return 0;
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <float.h>
#include <math.h>
#define fcn 11
double ctg(double d)
{return 1/tan(d);}
double actg(double d)
{return(M_PI_2-atan(d));}
int n_CalcErr;
char MathSyb[6]={ '(', ')', '[', ']', '{', '}' },
Calcfunc[36];/* store error mathfunc-name */
double (*mfun[fcn])(double d)={sin,cos,tan,ctg,log10,asin,acos,atan,actg,exp,fabs};
double db_syb(char *pch,double *bk,int *i)
{
char jao=0;
double db;
if(*pch=='@'){ (*i)++; pch++; jao=1; }
switch(*pch){
case -1 : db=M_E; break;
case -2 : db=M_PI; break;
case 127 : (*i)++; db=bk[pch[1]]; break;
default : db=atof(pch);
}
return jao ? db * M_PI/180: db;
}
static char CalcChs[8]="_^*/%+-",
fun[fcn][5]={"sin","cos","tag","ctg","lg","asin","acos","atag","actg","exp","abs"};
static int cchslen[3]={2,3,2}, begin[3]={0,2,5};
double calc(char *ExprChs)
{
double *bk, rc, db1
#ifndef _STDIO_H
, *db
#endif
;
int x, y, t, b, i=-1, *sybpos,
j=1, w=1, v=1, f=0, ErrNo=0;
char *bu, *symbuf, syb, syb2, C
#ifndef _STDIO_H
, *chs
#endif
;
long l1, l2;
if(!ExprChs || !*ExprChs)return 0;
/* because rc=0 is x87, so use return, smaller size */
t=strlen(ExprChs)+1;
#ifdef _STDIO_H
char chs[3*t+5]; // gcc
#else
chs=(char *)malloc(3*t+5); // TC
#endif
bu=chs; symbuf=chs+t+1;
sybpos=(int *)(symbuf+t+1);
while(++i<t) /* 0 must send to chs, so t...+1 */
if((C=ExprChs[i])!=32){/* 压缩 */
syb2=ExprChs[i+1]|32;
if((syb=C|32)=='e'&&(syb2<97||syb2>122))*bu=-1;
else if(syb=='p'&&syb2=='i'){ *bu=-2; i++; }
else { *bu=C;
if(C==')'||C==']'||C=='}')j++;
else if(strchr(CalcChs,C))w++;
}
bu++;
}
#ifdef _STDIO_H
double db[w+j]; // gcc
#else
db=(double *)malloc(8*(w+j)); // TC
#endif
bk=db+w;
for(x=0;x<5;x+=2){
for(y=0;chs[y]/* no change this,it would change len in follow */;y++){
if(chs[y]==MathSyb[x])sybpos[f++]=y;
if(chs[y]==MathSyb[x+1]){
if(f--<1){ ErrNo=5011+x; goto calcEnd; }
w=sybpos[f];
bu=chs+w+1; chs[y]=0;
CalcRun: for(syb=1,i=0;bu[i]=='-'||bu[i]=='+';i++)
if(bu[i]=='-')syb=-syb;
*db=db_syb(bu+i,bk,&i)*syb;
for(t=0;bu[i];i++){
switch(bu[i]){ /* process bu string */
case -1:
case -2:
case 127: syb2=syb; goto skp1;
default : { syb2=1;
if(strchr(CalcChs,bu[i])){
for(symbuf[t++]=bu[i++];bu[i]=='+'||bu[i]=='-';i++)
if(bu[i]=='-')syb2=-syb2;
skp1: db[t]=db_syb(bu+i,bk,&i)*syb2;
}
for(j=0; j<34 && ((C=bu[i])|32)>96&&(C|32)<123; j++,i++)Calcfunc[j]=C;
if(j>0){
Calcfunc[j]=0;
db1=db_syb(bu+i,bk,&i);
for(j=0;j<fcn&&stricmp(Calcfunc,fun[j]);j++);
if(j>=fcn){ ErrNo=5010; goto calcEnd; }
if(db1==0 && j>2 && j<5){ ErrNo=4997+j; goto calcEnd; }
db[t] = (*mfun[j])(db1)*syb2;
}
}
}
}/* finish bu */
for(i=0;i<3;i++){
for(j=0;j<t;j++)if(memchr(CalcChs+begin[i],symbuf[j],cchslen[i])){/* math */
rc=db[j]; db1=db[j+1];
switch(symbuf[j]){
case '_' : if(rc<2||db1<1){ ErrNo=5003; goto calcEnd; }
rc=log10(db1)/log10(rc); break;
case '^' : rc=pow(rc,db1); break;
case '*' : rc*=db1; break;
case '%' : l1=rc; l2=db1;
case '/' : if(db1==0){ ErrNo=5004; goto calcEnd; }
rc=symbuf[j]!='%'? rc/db1:l1%l2;
break;
case '+' : rc+=db1; break;
case '-' : rc-=db1;
}
db[j]=rc;
if((b=t-j-1)>0){memmove(db+j+1,db+j+2,8*b);memmove(symbuf+j,symbuf+j+1,b);}
/* double is x87 code, use x86 possible
if(t>j+1)for(b1=j;b1<t;b1++){db[b1+1]=db[b1+2];symbuf[b1]=symbuf[b1+1];}*/
t--; j--;
} /* end math */
}
if(x>5)goto calcEnd;
bk[v]=*db;
strcpy(chs+w+2,chs+y+1);/*for(j=y+1,i=w+2;(chs[i]=chs[j])!=0;i++,j++);*/
y=w+1;
chs[w]=127; chs[y]=v++;
}/* end ()[]{} if */
}/* end for */
if(f){ ErrNo=5011+x+1; goto calcEnd; }
}/* end 3 loop for )]} */
bu=chs;
goto CalcRun;
calcEnd: rc= ErrNo&&*chs==0? 0:*db;
#ifndef _STDIO_H
free(db); free(chs); // TC
#endif
n_CalcErr=ErrNo;
return rc;
}
#include <stdio.h>
#ifdef _STDIO_H
#define stricmp strcasecmp
#else
// DOS or Windows
// #define stkalloc malloc
#endif
#include "calc.c" // 以上原问题的代码
int main()
{
char buf[200];
double db;
puts("Enter \'q\' to exit");
do {
gets(buf);
db=calc(buf);
if(n_CalcErr==0)printf("%g\n", db);
}while(buf[0]!='q');
}
#include<string.h>
int main()
{
char* str="lgbkdk";
int a[strlen(str)];
return 0;
}