📄 lzb.cpp.cpp
字号:
typedef char SElemType;
#include <string.h>
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include <process.h>
#include <iostream.h>
#define TURE 1
#define FALSE 0
#define OK 1
#define ERROR 0
#define INPUTSIZE 50
#define STACK_INIT_SIZE 10 /* 存储空间初始分配量 */
#define STACK_INCREMENT 2 /* 存储空间分配增量 */
typedef struct SqStack
{
double *base; //* 在栈构造之前和销毁之后,base的值为NULL *//
double *top;//* 栈顶指针 *//
int stacksize; //* 当前已分配的存储空间,以元素为单位 *//
}
SqStack; //* 顺序栈 *//
void InitStack(SqStack &S)
{ // *构造一个空栈S *//
if(!(S.base=(double *)malloc(STACK_INIT_SIZE*sizeof(double))))
exit(OVERFLOW); // *存储分配失败*//
S.top=S.base;
S.stacksize=STACK_INIT_SIZE;
}
double ClearStack(SqStack &S)
{//*将栈置为空栈*//
S.top=S.base;
return OK;
}
double GetTop(SqStack S,double &e)
{ //* 若栈不空,则用e返回S的栈顶元素,并返回OK;否则返回ERROR *//
if(S.top>S.base)
{
e=*(S.top-1);
return OK;
}
else
return ERROR;
}
void Push(SqStack &S,double e)
{ // *插入元素e为新的栈顶元素*//
if(S.top-S.base>=S.stacksize) //* 栈满,追加存储空间 *//
{
S.base=(double *)realloc(S.base,(S.stacksize+STACK_INCREMENT)*sizeof(double));
if(!S.base)
exit(OVERFLOW); // *存储分配失败*//
S.top=S.base+S.stacksize;
S.stacksize+=STACK_INCREMENT;
}
*(S.top)++=e;
}
double Pop(SqStack &S,double &e)
{ //*若栈不空,则删除S的栈顶元素,用e返回其值,并返回OK;否则返回ERROR *//
if(S.top==S.base)
return ERROR;
e=*--S.top;
return OK;
}
SElemType Precede(SElemType t1,SElemType t2)
{ //*判断t1,t2两符号的优先关系 *//
char f;
switch(t2)
{
case '+':
case '-':if(t1=='('||t1=='=')
f='<'; // t1<t2
else
f='>'; // t1>t2
break;
case '*':
case '/':if(t1=='*'||t1=='/'||t1==')'||t1=='s'||t1=='l'||t1=='g'||t1=='c'||t1=='h')
f='>'; // t1>t2
else
f='<'; // t1<t2
break;
case '(':if(t1==')')
{
printf("Error!\n");
exit(ERROR);
}
else
f='<'; // t1<t2
break;
case ')':switch(t1)
{
case'(':f='='; // t1=t2
break;
case'=':printf("Error!!\n");
exit(ERROR);
default :f='>'; // t1>t2
}
break;
case'=':switch(t1)
{
case'=' :f='='; // t1=t2
break;
case'(' :printf("Error!!!\n");
exit(ERROR);
default :f='>';
}
break;// t1>t2
case'^': if(t1=='+'||t1=='-'||t1=='*'||t1=='/'||t1=='('||t1=='=')
f='<';
else
f='>';
break;
case'%':if(t1=='='||t1=='-'||t1=='*'||t1=='/'||t1=='('||t1=='=')
f='<';
else
f='>';
break;
case's':
case'c':
case'g':
case'l':
case'h':switch(t1)
{
case')':
case'l':
case'g':
case'c':
case's':
case'h':printf("Error!");exit(ERROR);
case'(':
case'=':
case'+':
case'-':
case'*':
case'/':
case'^':
case'%':
f='<';
}
break;
}
return f;
}
double Operate1( double a,SElemType theta, double b)
{ //*做四则运算和乘方、求余运算*//
double c;
switch(theta)
{
case'+':c=a+b;break;
case'-':c=a-b;break;
case'*':c=a*b;break;
case'/':c=a/b;break;
case'^':c=pow(a,b);break;
case'%':c=fmod(a,b);break;
}
return c;
}
double Operate2(SElemType theta,double a)
{ //*做sin、cos、lg、ln、exp的运算*//
double c;
switch(theta)
{
case's':c=sin(a);break;
case'c':c=cos(a);break;
case'l':c=log10(a)/log10(2);break;
case'g':c=log10(a);break;
case'h':c=exp(a);break;
}
return c;
}
double In(SElemType c)
{ //* 判断c是否为运算符之一 *//
switch(c)
{
case'+':
case'-':
case'*':
case'/':
case'(':
case')':
case'=':
case'^':
case'%':
case's':
case'l':
case'g':
case'c':
case'h':
return 1;
default:return 0;
}
}
void main()
{
printf("\nDefined:s()=sin(),c()=cos(),l()=ln(),g()=lg(),h()=exp()\n");
SqStack OPTR,OPND;
int e=1;
double a,b,x,theta;
double d,f;
char c;
char z[60];
int i;
int m,n;
int l;
char p[100][250];
InitStack(OPTR); //*构造运算符栈OPTR,用以寄存运算符 *//
InitStack(OPND); //*构造运算数栈OPND,用以寄存操作 数或运算结果 *//
while(e) //*循环使用以下程序或者出程序的判断点 *//
{
printf("please input row:\n");
scanf("%d",&n);
for(m=1;m<=n;m++)
scanf ( "%s", p[m] ) ;
printf("\n");
for(m=1;m<=n;m++)
{
l=0;
c=*(p[m]+(l++));
Push(OPTR,'='); // *插入元素=为栈OPTR新的栈顶元素 *//
GetTop(OPTR,theta);// *若栈不空,则用theta返回OPTR的栈顶元素,并返回OK;否则返回ERROR *//
while(c!='='||theta!='=') //*判断c和栈顶元素是否为等号 *//
{
if(In(c))
{
switch(Precede(theta,c))// *判断theta,c两符号的优先关系 *//
{
case '<': //*栈顶元素优先权低 *//
Push(OPTR,c); //* 插入元素c为新的栈顶元素 *//
c=*(p[m]+(l++));
GetTop(OPTR,theta);
break;
case '='://*脱括号并接收下一字符*//
Pop(OPTR,theta); //*若栈不空,则删除OPTR的栈顶元素,用theta返回其值,并返回OK;否则返回ERROR *//
GetTop(OPTR,theta);
if(theta=='s'||theta=='l'||theta=='c'||theta=='g'||theta=='h') //*对特殊运算符号的处理 *//
{
Pop(OPND,f); //*若栈不空,则删除OPND的栈顶元素,用f返回其值,并返回OK;否则返回ERROR *//
Pop(OPTR,theta);
Push(OPND,Operate2(theta,f));
GetTop(OPTR,theta);
c=*(p[m]+(l++));
}
else
{
c=*(p[m]+(l++));
}
case '>': //*退栈并将运算结果入栈 *//
if(theta=='+'||theta=='-'||theta=='*'||theta=='/'||theta=='^'||theta=='%')
{
Pop(OPTR,theta); //*若栈不空,则删除S的栈顶元素,用e返回其值,并返回OK;否则返回ERROR *//
Pop(OPND,b);
Pop(OPND,a);
f=Operate1(a,theta,b);
Push(OPND,f);
GetTop(OPTR,theta);
}
else
break;
} //*switch语句结束 *//
}//* if(In(c) 语句结束*//
else
if(c>='0'&&c<='9'||c=='.') //*对数字的处理 *//
{
i=0;
do
{
z[i]=c;i++;
c=*(p[m]+(l++));
}
while((c<='9'&&c>='0')||c=='.');
d=atof(z);
Push(OPND,d);
}
else
//* c为非法字符 *//
{
printf("error\n");
exit(ERROR);
}
} //* while(c!='='||theta!='=')结束 *//
GetTop(OPND,x);
printf("%.8f\n",x);
ClearStack(OPTR);
ClearStack(OPND);
}//*f0r语句循环至此 *//
printf("\nWant to use it again?Yes:1,No:0");
scanf("%d",&e);
if(e==0)
{
printf("\nThanks for using the it!!!");
printf("\npress any key to continue...");
}
} //* while(e)结束点*//
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -