该楼层疑似违规已被系统折叠 隐藏此楼查看此楼
//弧度转角度
//接楼上
double htoj(double a)
{
return 180*a/P;
}
//产生0~1之间的浮点随机数
double rand0(void)
{
/* 初始化伪随机数生成器 */
srand((unsigned int)time(NULL));
return (double)rand() / RAND_MAX;
}
/*函数计算*/
double math_hs(char *str)
{
char str0[N],*s,*s0;
char*p[N];
double x[N][2],d=0;
int i=0,n=0,j=0;
strcpy(str0,str);
s0 = strtok(str0,d2);
p[i] = strtok(str, d1);
while ((s = strtok(NULL, d1)))
{
i++;
n=i+1;
p[i] = s;
//printf(">>%s\n",p[i]);
}
for (j=0;j<=i;j++)
{
if(strchr(p[j],','))
{
x[j][0]=atof(strtok(p[j],","));
x[j][1]=atof(strtok(NULL,","));
}
else
x[j][0]=atof(p[j]);
}
if (strstr(s0,"sin"))
{
printf("三角正弦:sin(%gº)",x[0][0]);
d=sin(jtoh(x[0][0]));
}
if (strstr(s0,"cos"))
{
printf("三角余弦:cos(%gº)",x[0][0]);
d=cos(jtoh(x[0][0]));
}
if (strstr(s0,"tan"))
{
printf("三角正切:tan(%gº)",x[0][0]);
d=tan(jtoh(x[0][0]));
}
if (strstr(s0,"asin"))
{
printf("反正弦:asin(%g)",x[0][0]);
d=htoj(asin(x[0][0]));
}
if (strstr(s0,"acos"))
{
printf("反余弦:acos(%g)",x[0][0]);
d=htoj(acos(x[0][0]));
}
if (strstr(s0,"atan"))
{
printf("反正弦:atan(%g)",x[0][0]);
d=htoj(atan(x[0][0]));
}
if (strstr(s0,"sinh"))
{
printf("双曲正弦:sinh(%g)",x[0][0]);
d=sinh(x[0][0]);
}
if (strstr(s0,"cosh"))
{
printf("双曲余弦:cosh(%g)",x[0][0]);
d=cosh(x[0][0]);
}
if (strstr(s0,"tanh"))
{
printf("双曲正切:tanh(%g)",x[0][0]);
d=tanh(x[0][0]);
}
if (strstr(s0,"lg"))
{
printf("常用对数:lg(%g)",x[0][0]);
d=log10(x[0][0]);
}
if (strstr(s0,"ln"))
{
printf("自然对数:ln(%g)",x[0][0]);
d=log(x[0][0]);
}
if (strstr(s0,"sqrt"))
{
printf("开方:√(%g)",x[0][0]);
d=sqrt(x[0][0]);
}
if (strstr(s0,"exp"))
{
printf("开方:e^(%g)",x[0][0]);
d=exp(x[0][0]);
}
if (strstr(s0,"^"))
{
printf("%g^%g",x[0][0],x[1][0]);
d=pow(x[0][0],x[1][0]);
}
if (strstr(s0,"tj"))
{
d=n;
tj(x,n);
}
if (strstr(s0,"gx"))
{
d=n;
gx(x,n);
}
if (strstr(s0,"npr"))
d=npr((int)x[0][0],(int)x[0][1]);
if (strstr(s0,"ncr"))
d=ncr((int)x[0][0],(int)x[0][1]);
if (strstr(s0,"pxb"))
d=pxb(x);
if (strstr(s0,"pxh"))
d=pxh(x);
if (strstr(s0,"!"))
d=jc((int)x[0][0]);
if (strstr(s0,"rand"))
d=rand0();
return d;
}
/* 处理乘除并计算 */
double CX(void)
{
double cs;
double acting = FS();
while ((token[m] == '*') || (token[m] == '/'))
switch (token[m])
{
case '*':
token[++m] = str[t++];
acting *= FS();
break;
case '/':
token[++m] = str[t++];
cs = FS();
if (cs == 0)/*除数为0时*/
{
printf("除数不能为零,或者");
}
acting /= cs;
break;
}
return acting;
}
/* 处理加减并计算 */
double JJ(void)
{
double acting = CX();
while ((token[m] == '+') || (token[m] == '-'))
switch (token[m])
{
case '+':
token[++m] = str[t++];
acting += CX();
break;
case '-':
token[++m] = str[t++];
acting -= CX();
break;
}
return acting;
}
/* 处理括号,数字,小数点 */
double FS(void)
{
double acting;
char number[N];
int i = 0;
if (token[m] == '(')
{
token[++m] = str[t++];
acting = JJ();
if (token[m] == ')')
token[++m] = str[t++];
}
else if (isdigit(token[m]) || token[m] == '.')
{
while (isdigit(token[m]) || token[m] == '.')/* 将字符串转换为浮点数 */
{
number[i++] = token[m++];
token[m] = str[t++];
}
number[i] = '\0';
acting = atof(number);
}
return acting;
}
/*主函数*/
int main(void)
{
double jg;
int k=0;
printf(SM);
while (1)
{
scanf("%s",str);
n=strlen(str);
if (strpbrk(str,d2)==NULL)
strcpy(str,"0");
if ( str[0] == '-' || str[0] == '+' || str[0] == '*' || str[0] == '/' )
{
for ( int b = n;b >=1;b-- )
str[b] = str[b-1];
str[0] = '0';
}
c = 1;
m = t = 0;
if (islower(str[0])||strchr(str,'!')||strchr(str,'^'))
{
jg=math_hs(str);
if (islower(str[0])&&!strstr(str,"ncr")&&!strstr(str,"ncr"))
k=1;
}
else
{
token[m] = str[t++];
jg=JJ();
k=0;
}
if ((jg>MAX||jg0&&jg
printf("=%g\n",jg);
else
{
if (PD==0)
printf("=%d\n",(int)jg);
else
printf("=%lf\n",jg);
}
printf("--------------------------------------------\n");
if (c == 1)
{
for (; n >= 0; --n)
str[n] = '\0';
n = c = 0;
}
}
return 0;
}
//转载请注明来源谢谢(*°∀°)=3