📄 mathoptr.h
字号:
//MathOptr.h是calculator的头文件
#include<iostream.h>
#include<math.h>
#ifndef MATHOPTR_H
#define MATHOPTR_H
class MathOptr
{
private:
char Op;
int InputPrecedence; //读入优先级
int StackPrecedence; //栈优先级
public:
MathOptr(char ch=' '); //给读入的运算附和左右括号配备优先级
int operator>=(MathOptr a)const; //操作符>=重载
void Evaluate(Stack<double>& Opnd); //执行从运算符栈弹出的运算符optr所要求的运算
int AWriteWrongToFile(char *pFileName);//数据类型不符时出错
char GetOp(void) //返回运算符
{
return Op;
}
};
#endif
///////////////////////////////////////////////////////////////////////////////
//类的成员函数的定义
MathOptr::MathOptr(char ch)
{
Op=ch;
switch(Op)
{
case '+':
case '-':InputPrecedence=1;
StackPrecedence=1;
break;
case '*':
case '%':
case '/':InputPrecedence=2;
StackPrecedence=2;
break;
case '^':InputPrecedence=4;
StackPrecedence=3;
break;
case '(':InputPrecedence=5;
StackPrecedence=-1;
break;
case ')':InputPrecedence=0;
StackPrecedence=0;
break;
}
}
int MathOptr::operator >=(MathOptr a)const
{
return(StackPrecedence>=a.InputPrecedence);
}
void MathOptr::Evaluate(Stack<double>& Opnd)
{
double Opnd1=Opnd.Pop();
double Opnd2=Opnd.Pop();
switch(Op)
{
case '+':Opnd.Push(Opnd1+Opnd2);
break;
case '-':Opnd.Push(Opnd2-Opnd1);
break;
case '*':Opnd.Push(Opnd1*Opnd2);
break;
case '/':
if(!Opnd1)
{
cout<<"ERROR IN INFIX NOTATION !\n";
AWriteWrongToFile("output.txt");
}
else Opnd.Push(Opnd2/Opnd1);
break;
case '%':
if(Opnd1!=(int)Opnd1||Opnd2!=(int)Opnd2)
{
cout<<"ERROR IN INFIX NOTATION !\n";
AWriteWrongToFile("output.txt");
}
else Opnd.Push((int)Opnd2%(int)Opnd1);
break;
case '^':
if(Opnd1!=(int)Opnd1)
{
cout<<"ERROR IN INFIX NOTATION !\n";
AWriteWrongToFile("output.txt");
}
else Opnd.Push(pow(Opnd2,Opnd1));
break;
}
}
int MathOptr:: AWriteWrongToFile(char *pFileName)
{
FILE *fp;
if( (fp = fopen( pFileName, "a+" )) == NULL )
{
printf(" can not open file ! \n");
return 0;
}
fprintf( fp, "ERROR IN INFIX NOTATION !\n");
fclose( fp );
return 1;
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -