// 复杂表达式.h
#include<stdio.h>
#include<iostream.h>
#include<math.h>
#define maxsize 100
double zhen(double x) //定义正号函数
{return(x);}
double fu(double x) //定义负号函数
{return(0-x);}
double abs1(double x) //定义绝对值函数
{
if(x<=0)
return(-x);
else
return(x);
}
double power(double x) //定义乘方函数
{return(x*x);}
double jiecheng(int x) //定义阶乘函数
{ double b=1;//printf("%d"n",x);
if(x>=0)
{
for(int i=1;i<=x;i++)
b=b*i;
//x=b;//printf("%d"n",x);
return(b);
}
else
{
printf("阶乘参数小于0,无法计算"n");
return 0;
}
}
void change(char *poststr,char *str) //定义函数将中缀表达式转换为后缀表达式
{
char data[maxsize];
int top=-1;
int i=0;
while(*str!='"0')
{
switch(*str)
{
case'(':if(*(str+1)=='-') //处理负数问题
{
top++;
data[top]=*str;
str++;
str++;
while(*str!=')')
{ *poststr++=*str;
str++;
}
*poststr++='#';
top++;
data[top]='f';break;
}
else
if(*(str+1)=='+') //处理带正号的实数
{
top++;
data[top]=*str;
str++;
str++;
while(*str!=')')
{ *poststr++=*str;
str++;
}
*poststr++='#';
top++;
data[top]='z';break;
}
else
{
top++;data[top]=*str;str++;break;
}
case')':while(data[top]!='(')
{
*poststr++=data[top];
top--;
}
top--;
str++;
break;
case'+':
case'-':while(top!=-1&&data[top]!='(')
{
*poststr++=data[top];
top--;
}
top++;
data[top]=*str;
str++;
break;
case'*':
case'/':
case'%':
while(data[top]=='*'||data[top]=='/'||data[top]=='%'||data[top]=='j'||data[top]=='a'||data[top]=='p'||data[top]=='q'||data[top]=='e'||data[top]=='n'||data[top]=='g'||data[top]=='i'||data[top]=='c'||data[top]=='t'||data[top]=='z'||data[top]=='f')
{
*poststr++=data[top];
top--;
}
top++;
data[top]=*str;
str++;
break;
case'!':top++;data[top]='j';str++;break;
case'a':if(*(str+1)=='b'&&*(str+2)=='s') //用字符a保存绝对值函数abs(x)的信息
{
top++;
data[top]=*str;
for(int j=0;j<3;j++)
{
str++;
}
}
else
{ str++;
printf("表达式包含非法字符,无法计算"n");
}
break;
case's':if(*(str+1)=='q' && *(str+2)=='r'&&*(str+3)=='t') //用字符q保存平方根函数sqrt(x)的信息
{
top++;
data[top]=*(str+1);
for(int j=0;j<4;j++)
{
str++;
}
}
else
if(*(str+1)=='i' && *(str+2)=='n') //用字符i保存正弦函数sin(x)的信息
{
top++;
data[top]=*(str+1);
for(int j=0;j<3;j++)
{
str++;
}
}
else
{ str++;
printf("表达式包含非法字符,无法计算"n");
}
break;
case'e':if(*(str+1)=='x'&&*(str+2)=='p') //用字符e保存以e为底的指数函数exp(x)的信息
{
top++;
data[top]=*str;
for(int j=0;j<3;j++)
{
str++;
}
}
else
{ str++;
printf("表达式包含非法字符,无法计算"n");
}
break;
case'c':if(*(str+1)=='o'&&*(str+2)=='s') //用字符c保存余弦函数cos(x)的信息
{
top++;
data[top]=*str;
for(int j=0;j<3;j++)
{
str++;
}
}
else
{ str++;
printf("表达式包含非法字符,无法计算"n");
}
break;
case't':if(*(str+1)=='a'&&*(str+2)=='n') //用字符t保存正切函数tan(x)的信息
{
top++;
data[top]=*str;
for(int j=0;j<3;j++)
{
str++;
}
}
else
{ str++;
printf("表达式包含非法字符,无法计算"n");
}
break;
case'l':if(*(str+1)=='o'&&*(str+2)=='g'&&*(str+3)=='1'&&*(str+4)=='0') //用字符g保存以10为底的对数函数log10(x)的信息
{
top++;
data[top]='g';
for(int j=0;j<5;j++)
{
str++;
}
}
else
if(*(str+1)=='o'&&*(str+2)=='g') //用字符n保存自然对数函数log(x)的信息
{
top++;
data[top]='n';
for(int j=0;j<3;j++)
{
str++;
}
}
else
{ str++;
printf("表达式包含非法字符,无法计算"n");
}
break;
case'p':if(*(str+1)=='o'&&*(str+2)=='w'&&*(str+3)=='e'&&*(str+4)=='r') //用字符p保存乘方函数power(x)的信息
{
top++;
data[top]=*str;
for(int j=0;j<5;j++)
{
str++;
}
}
else
{ str++;
printf("表达式包含非法字符,无法计算"n");
}
break;
default:
while(*str>=46 && *str<=57&&*str!='/') //将数字及小数点保存到poststr数组
{
*poststr++=*str;
str++;
}
*poststr++='#'; //用#将数字分隔开
break;
}
}
while(top!=-1) //将栈中剩下的字符保存到poststr数组
{
*poststr++=data[top];
top--;
}
*poststr='"0'; //添加数组结束符
}
double outprice(char *poststr) //定义函数将后缀表达式的值算出并返回最后结果
{
double a,b,c,d,e=0;
int k=0,l=0;
int g;
double sdata[maxsize];
int top=-1;
while(*poststr!='"0')
{
switch(*poststr)
{
case'+':a=sdata[top]; //处理加法运算
top--;
b=sdata[top];
c=a+b;
sdata[top]=c;
break;
case'-':a=sdata[top]; //处理减法运算
top--;
b=sdata[top];
c=b-a;
sdata[top]=c;
break;
case'*':a=sdata[top]; //处理乘法运算
top--;
b=sdata[top];
c=a*b;
sdata[top]=c;
break;
case'/':a=sdata[top]; //处理除法运算
top--;
b=sdata[top];
if(a!=0)
{
c=b/a;
sdata[top]=c;
}
else
{
printf("除数不能为零"n");
}
break;
case'%':a=sdata[top]; //处理取模运算
top--;
b=sdata[top];
if(a!=0)
{
if(a-int(a)==0&&b-int(b)==0) //要求取模运算符两侧都为整数
{
g=int(b)%int(a);
sdata[top]=g;
}
else
{
printf("取模运算符两边不为整形,无法计算"n");
return 0;
}
}
else
{
printf("模数不能为零"n");
return 0;
}
break;
case'i': //处理正弦运算
a=sdata[top]*3.1415926/180; //将度转换为弧度
c=sin(a);
sdata[top]=c;
break;
case'j':if(sdata[top]-int(sdata[top])==0)
{
c=jiecheng(int(sdata[top]));
sdata[top]=c;
}
else
{ printf("阶乘参数不为整形,无法计算"n");
return 0;
}
break;
case'q':a=sdata[top]; //处理平方根运算
if(a<=0)
{
printf("平方根函数的参数小于零,无法计算"n");
return 0;
}
else
{
c=sqrt(a);
sdata[top]=c;
}
break;
case'a':a=sdata[top]; //处理绝对值运算
c=abs1(a);
sdata[top]=c;
break;
case'p':a=sdata[top]; //处理乘方运算
c=power(a);
sdata[top]=c;
break;
case'e':a=sdata[top]; //处理指数运算
c=exp(a);
sdata[top]=c;
break;
case'c': //处理余弦运算
a=sdata[top]*3.1415926/180; //将度转换为弧度
c=cos(a);
sdata[top]=c;
break;
case't': //处理正切运算
a=sdata[top]*3.1415926/180; //将度转换为弧度
if(sdata[top]==90)
{
printf("正切的参数不能为90"n");
return 0;
}
else
{
c=tan(a);
sdata[top]=c;
}
break;
case'g':a=sdata[top]; //处理以10为底的对数函数运算
c=log10(a);
sdata[top]=c;
break;
case'n':a=sdata[top]; //处理自然对数函数运算
c=log(a);
sdata[top]=c;
break;
case'z':a=sdata[top]; //处理带正号的实数问题
c=zhen(a);
sdata[top]=c;
break;
case'f':a=sdata[top]; //处理负数问题
c=fu(a);
sdata[top]=c;
break;
default:
for(int i=1;*(poststr+i)!='#'&&(poststr+i)!='"0';i++) //判断数字为整数还是小数
{
if(*(poststr+i)=='.')
k=1;
}
if(k==0) //数字为整数的处理方法
{
d=0;
e=0;
while(*poststr>=48 && *poststr<=57)
{
d=10*d+*poststr-'0';
poststr++;
}
}
if(k==1) //数字为小数的处理方法
{
d=0;
while(*poststr>=48 && *poststr<=57) //处理小数的整数部分
{
if(*poststr!='.')
{
d=10*d+*poststr-'0';
poststr++;
}
if(*poststr=='.')
break;
}
if(*poststr=='.') //处理小数的小数部分
{
for(;*poststr!='#'&&*poststr!='"0';poststr++){};
poststr--;
e=0;
l=0;
while(*poststr>=48 && *poststr<=57)
{l++;
e=0.1*e+(*poststr-'0')*0.1;
poststr--;
k=0;
}
}poststr=poststr+l+1;
}
top++;
sdata[top]=d+e; //将数值进栈
break;
}
poststr++;
}
return(sdata[top]); //返回最后结果
}
//复杂表达式.cpp
#include<stdio.h>
#include<iostream.h>
#include<math.h>
#include"复杂表达式.h"
#define maxsize 100
void main()
{
char str[maxsize];
char poststr[maxsize];
printf("---------------------------------程序说明--------------------------------"n");
printf("本程序能进行:加+ 减- 乘* 除/ 模 % 平方power(x) 等运算"n");
printf("本程序支持:整数 实数 正数 负数"n");
printf("本程序支持函数:"n");
printf("绝对值函数abs(x) 平方根函数sqrt(x) 指数函数exp(x) 自然对数函数log(x)"n");
printf("以10为底的对数函数log10(x) 正弦函数sin(x) 余弦函数cos(x) 正切函数tan(x),阶乘函数x!"n");
printf("负数和带正号的实数请带括号"n");
printf("本程序是以回车键为表达式结束符"n");
printf("本程序由-----ymss-----完成"n");
printf("-------------------------------------------------------------------------"n");
printf("请输入表达式:"n");
cin>>str;
change(poststr,str); //将输入的表达式转换成后缀表达式并保存到poststr数组
// printf("%s"n",poststr);
printf("计算结果为:"n");
cout<<outprice(poststr)<<endl; //输出最后结果
}
运行结果:
请输入表达式:
5-(abs(sin(234))+sqrt(8)*exp(-2)/(log(9)-1))+9%2
计算结果为:
4.87126
Press any key to continue
浙公网安备 33010602011771号