📄 mathstring.cpp
字号:
//转换方法:
//1.判定string[i]为数值,直接存入bolan数组中
//2.判定string[i]为'(',将此'('压入栈,
// 若'('前为数字,则在'('前加一对'*'的处理
//3.判定string[i]为')',则将栈stact中左括号'('以后的运算符和函数依次
// 弹出,并入数组bolan中,然后将左括号'('弹出
//4.判定string[i]为'-','+',则将当前栈stack中左括号'('以后(如无'('则
// 将栈stack中所有)的运算符和函数依次弹出,存入数组bolan中,然后将
// '-'或'+'压入栈stack中
// 如果'-'在第一个位置或其前是'(',即'-'是负号,将'~'(取负)压入stack中
//5.判定string[i]为'*'或'/',则将栈stact中顶端连续的'*'或'/'或函数依次
// 弹出,存入数组bolan中,然后将'*'或'/'压入栈stact中
//6.判定string[i]为函数,则将该函数入栈stact中
// 存入栈stact中变量func的是该函数的编号,详细编号见头文件
// 若该函数前为数字,则在该函数入栈stact前加一次对'*'的处理
//7.判定string[i]=',',将栈stact中'('后的运算符和函数全部弹出,压入数
// 组bolan中,
//8.判定string[i]='x',则用类中未知数变量xx代替,压入数组bolan中
//9.转换结束时,若栈stact不为空,则将栈内所有运算符和函数弹出,存入
// 数组bolan中
//10.转换结束,在数组bolan尾加一opera='#'作为结束符
int CMathString::stringToBolan()
{
Bolan stack[MAX]; //存放运算符和函数的栈
int top=0, //存放栈stact的下标
i, //string的下标
j=0, //bolan数组的下标
t;
char digitStr[20]; //存放要转化成数值的字符串
for(i=0;string[i]!='#';i++)
{
//判定为数值,直接存入bolan数组中
if(('0'<=string[i] && string[i]<='9')||string[i]=='.') //数值
{
t=0;
digitStr[t]=string[i]; //数字存入要转化成数值的字符串
while(('0'<=string[i+1] && string[i+1]<='9')
||string[i+1]=='.')
{ //将连续数字入要转化成数值的字符串
digitStr[++t]=string[i+1];
i++;
}
digitStr[++t]='\0';
//将数字字符串转化成数值,并且入bolan数组
bolan[j].flag=0; //标识该节点为数值
bolan[j].value=stringToDigital(digitStr);
j++;
continue;
}
//判定为'(',将此'('压入栈
//若'('前为数字,则在'('前加一对'*'的处理
if(string[i]=='(')
{
if(string[i-1]<='0' || '9'<=string[i-1])//'('前不为数字
{
top++;
stack[top].flag=1; //标识该节点为运算符
stack[top].oper='(';
continue;
}
else //'('前为数字
{ //加一次对'*'的处理
while(stack[top].oper=='*' || stack[top].oper=='/'||
stack[top].flag==2)
{
bolan[j]=stack[top];
j++;
top--;
}
//将'*'压入栈stact中
top++;
stack[top].flag=1;
stack[top].oper='*';
//将'('压入栈stact中
top++;
stack[top].flag=1;
stack[top].oper='(';
continue;
}
}
//判定为')',则将栈stact中左括号'('以后的运算符和函数依次弹出
//寸入数组bolan中,然后将左括号'('弹出
if(string[i]==')')
{
while(stack[top].oper!='(')
{
bolan[j]=stack[top];
j++;
top--;
}
top--; //将左括号'('弹出
continue;
}
//判定为'-','+',则将当前栈stack中左括号'('以后(如无'('则将
//栈stack中所有)的运算符和函数依次弹出,存入数组bolan中,
//然后将'-'或'+'压入栈stack中
//如果'-'在第一个位置或其前是'(',即'-'是负号,将'~'压入stack中
if(string[i]=='+' || string[i]=='-')
{
if(string[i]=='-'&&(i==0 || string[i-1]=='(')) //'-'是负号
{
top++;
stack[top].flag=1;
stack[top].oper='~';
continue;
}
else //'-','+'是加减符号
{
while(stack[top].oper!='(' && top!=0)
{
bolan[j]=stack[top]; //当前栈stack中的运算符和
//函数依次弹出存入数组bolan中
j++;
top--;
}
//将'-'或'+'压入栈stack中
top++;
stack[top].flag=1;
stack[top].oper=string[i];
continue;
}
}
//判定string[i]为'*'或'/',则将栈stact中顶端连续的'*'或'/'或函
//数依次弹出,存入数组bolan中,然后将'*'或'/'压入栈stact中
if(string[i]=='*' || string[i]=='/')
{
while(stack[top].oper=='*' || stack[top].oper=='/'||
stack[top].flag==2)
{
bolan[j]=stack[top];
j++;
top--;
}
//将'*'或'/'压入栈stact中
top++;
stack[top].flag=1;
stack[top].oper=string[i];
continue;
}
//判定string[i]为函数,则将该函数入栈stact中
//存入栈stact中变量func的是该函数的编号,详细编号见头文件
//若该函数前为数字,则在该函数入栈stact前加一次对'*'的处理
if(string[i]=='a')
{
if('0'<=string[i-1] && string[i-1]<='9') //该函数前为数字
{ //在该函数入栈stact前加一次对'*'的处理
while(stack[top].oper=='*' || stack[top].oper=='/'||
stack[top].flag==2)
{
bolan[j]=stack[top];
j++;
top--;
}
//将'*'压入栈stact中
top++;
stack[top].flag=1;
stack[top].oper='*';
}
//为abs(x)函数
if(seekStr(string,i,"abs"))
{ //将该函数入栈stact中
top++;
stack[top].flag=2;
stack[top].func=1;
i--; //因为在seekStr函数中
continue; //i=i+strlen("abs");
//i已指向函数abs的下一个字符
//而在for语句中有i++,所以此处
//i要减1
}
//为acos(x)函数
if(seekStr(string,i,"acos"))
{ //将该函数入栈stact中
top++;
stack[top].flag=2;
stack[top].func=2;
i--;
continue;
}
//为asin(x)函数
if(seekStr(string,i,"asin"))
{ //将该函数入栈stact中
top++;
stack[top].flag=2;
stack[top].func=3;
i--;
continue;
}
//为atan(x)函数
if(seekStr(string,i,"atan"))
{ //将该函数入栈stact中
top++;
stack[top].flag=2;
stack[top].func=4;
i--;
continue;
}
//为acot(x)函数
if(seekStr(string,i,"acot"))
{ //将该函数入栈stact中
top++;
stack[top].flag=2;
stack[top].func=5;
i--;
continue;
}
}//if(string[i]=='a')
//string[i]为以c开头的库函数
if(string[i]=='c')
{
if('0'<=string[i-1] && string[i-1]<='9') //该函数前为数字
{ //在该函数入栈stact前加一次对'*'的处理
while(stack[top].oper=='*' || stack[top].oper=='/'||
stack[top].flag==2)
{
bolan[j]=stack[top];
j++;
top--;
}
//将'*'压入栈stact中
top++;
stack[top].flag=1;
stack[top].oper='*';
}
//为cos(x)函数
if(seekStr(string,i,"cos"))
{ //将该函数入栈stact中
top++;
stack[top].flag=2;
stack[top].func=6;
i--;
continue;
}
//为cosh(x)函数
if(seekStr(string,i,"cosh"))
{ //将该函数入栈stact中
top++;
stack[top].flag=2;
stack[top].func=7;
i--;
continue;
}
//为cot(x)函数
if(seekStr(string,i,"cos"))
{ //将该函数入栈stact中
top++;
stack[top].flag=2;
stack[top].func=8;
i--;
continue;
}
}//if(string[i]=='c')
//string[i]为以e开头的库函数
if(string[i]=='e')
{
if('0'<=string[i-1] && string[i-1]<='9') //该函数前为数字
{ //在该函数入栈stact前加一次对'*'的处理
while(stack[top].oper=='*' || stack[top].oper=='/'||
stack[top].flag==2)
{
bolan[j]=stack[top];
j++;
top--;
}
//将'*'压入栈stact中
top++;
stack[top].flag=1;
stack[top].oper='*';
}
//为exp(x)函数
if(seekStr(string,i,"exp"))
{ //将该函数入栈stact中
top++;
stack[top].flag=2;
stack[top].func=9;
i--;
continue;
}
}//if(string[i]=='e')
//string[i]为以f开头的库函数
if(string[i]=='f')
{
if('0'<=string[i-1] && string[i-1]<='9') //该函数前为数字
{ //在该函数入栈stact前加一次对'*'的处理
while(stack[top].oper=='*' || stack[top].oper=='/'||
stack[top].flag==2)
{
bolan[j]=stack[top];
j++;
top--;
}
//将'*'压入栈stact中
top++;
stack[top].flag=1;
stack[top].oper='*';
}
//为exp(x)函数
if(seekStr(string,i,"floor"))
{ //将该函数入栈stact中
top++;
stack[top].flag=2;
stack[top].func=10;
i--;
continue;
}
}//if(string[i]=='f')
//string[i]为以m开头的库函数
if(string[i]=='m')
{
if('0'<=string[i-1] && string[i-1]<='9') //该函数前为数字
{ //在该函数入栈stact前加一次对'*'的处理
while(stack[top].oper=='*' || stack[top].oper=='/'||
stack[top].flag==2)
{
bolan[j]=stack[top];
j++;
top--;
}
//将'*'压入栈stact中
top++;
stack[top].flag=1;
stack[top].oper='*';
}
//为mod(x)函数
if(seekStr(string,i,"mod"))
{ //将该函数入栈stact中
top++;
stack[top].flag=2;
stack[top].func=11;
i--;
continue;
}
}//if(string[i]=='m')
//string[i]为以l开头的库函数
if(string[i]=='l')
{
if('0'<=string[i-1] && string[i-1]<='9') //该函数前为数字
{ //在该函数入栈stact前加一次对'*'的处理
while(stack[top].oper=='*' || stack[top].oper=='/'||
stack[top].flag==2)
{
bolan[j]=stack[top];
j++;
top--;
}
//将'*'压入栈stact中
top++;
stack[top].flag=1;
stack[top].oper='*';
}
//为ln(x)函数
if(seekStr(string,i,"ln"))
{ //将该函数入栈stact中
top++;
stack[top].flag=2;
stack[top].func=12;
i--;
continue;
}
//为log(x)函数
if(seekStr(string,i,"log"))
{ //将该函数入栈stact中
top++;
stack[top].flag=2;
stack[top].func=13;
i--;
continue;
}
}//if(string[i]=='l')
//string[i]为以p开头的库函数
if(string[i]=='p')
{
if('0'<=string[i-1] && string[i-1]<='9') //该函数前为数字
{ //在该函数入栈stact前加一次对'*'的处理
while(stack[top].oper=='*' || stack[top].oper=='/'||
stack[top].flag==2)
{
bolan[j]=stack[top];
j++;
top--;
}
//将'*'压入栈stact中
top++;
stack[top].flag=1;
stack[top].oper='*';
}
//为pow(x)函数
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -