📄 main.cpp
字号:
#include<iostream>
#include<stdlib.h>
#include<math.h>
//-------------------------------
#define OK 1
#define ERROR 0
#define TRUE 1
#define FALSE 0
#define STACK_INIT_SIZE 100
#define STACKINCREMENT 10
//------------------------
typedef struct{
float *base;
float *top;
int size;
}OpndStack;
typedef struct{
char *base;
char *top;
int size;
}OptrStack;
//--------------------------------
typedef int Status;
using namespace std;
//--------------STACK OPERATE-----------------------------------------------------------------
//--------------------InitStack---------------------
Status InitStack(OpndStack &s){
s.base=(float*)malloc(STACK_INIT_SIZE*sizeof(float));
if(s.base==NULL)exit(OVERFLOW);
s.top=s.base;
s.size=STACK_INIT_SIZE;
return(OK);
}
Status InitStack(OptrStack &s){
s.base=(char*)malloc(STACK_INIT_SIZE*sizeof(char));
if(s.base==NULL)exit(OVERFLOW);
s.top=s.base;
s.size=STACK_INIT_SIZE;
return(OK);
}
//------------------Push-------------------------
Status Push(OpndStack &s,float e){
if(s.top-s.base>=s.size){
s.base=(float*)malloc(STACKINCREMENT*sizeof(float));
if(!s.base)exit(OVERFLOW);
s.top=s.base+s.size;
}
*s.top++=e;
return(OK);
}
Status Push(OptrStack &s,char e){
if(s.top-s.base>=s.size){
s.base=(char*)malloc(STACKINCREMENT*sizeof(char));
if(!s.base)exit(OVERFLOW);
s.top=s.base+s.size;
}
*s.top++=e;
return(OK);
}
//----------------------GetTop----------------------
float GetTop(OpndStack &s){
if(s.top==s.base)return 0;
return (*(s.top-1));
}
char GetTop(OptrStack &s){
if(s.top==s.base)return ERROR;
return *(s.top-1);
}
//---------------------Pop----------------
Status Pop(OpndStack &s,float &e){
if(s.top==s.base)return ERROR;
e=*--s.top;
return OK;
}
Status Pop(OptrStack &s,char &e){
if(s.top==s.base)return ERROR;
e=*--s.top;
return OK;
}
//---------------tell wether the charactor is an operator---------------------------------------
Status InOptr(char b){
if(b=='+'||b=='-'||b=='*'||b=='/'||b=='('||b==')'||b=='#')
return(TRUE);
else
return FALSE;
}
//-----------------------TRANSFORM CHARACTORS TO NUMBERS-------------------------------------
float transform(char *c,int &count){
char *q;
int n,k,l;
float integer=0,decimal=0;
for(q=c,n=0;!InOptr(*q)&&*q!='#';n++,q++);
for(k=1;k<n+1&&*(c+k-1)!='.';k++);
if(k>n)//k=n+1
for(l=1;l<k;l++){
integer=integer+(c[l-1]-48)*pow(10.0F,k-l-1.0F);
}
if(k==n){
for(l=1;l<k;l++){
integer=integer+(c[l-1]-48)*pow(10.0F,k-l-1.0F);
}
}
if(k<n&&k>1){
for(l=1;l<k;l++)
integer=integer+(c[l-1]-48)*pow(10.0F,k-l-1.0F);
for(l=1;l<n-k+1;l++)
decimal=decimal+(c[k+l-1]-48)*pow(10.0F,(float)(-l));
}
if(k==1){
for(l=1;l<n;l++)
decimal=decimal+(c[l]-48)*pow(10.0F,(float)(-l));
}
count=n;
return(integer+decimal);
}
//--------------------get the PRI ----------------------------- ------------------------
int f(char e);
char Precede(char a,char b){
char c[7][7]={{62,62,60,60,60,62,62},{62,62,60,60,60,62,62},
{62,62,62,62,60,62,62},{62,62,62,62,60,62,62},
{60,60,60,60,60,61,0},{62,62,62,62,0,62,62},
{60,60,60,60,60,0,61}};
return(c[f(a)][f(b)]);
}
int f(char e){
if(e=='+')return(0);
if(e=='-')return(1);
if(e=='*')return 2;
if(e=='/')return 3;
if(e=='(')return 4;
if(e==')')return 5;
if(e=='#')return 6;
else exit(0);
}
//-----------------get the answer by using the numbers and operator----------
float Operate(float a,char theta,float b)
{
switch(theta)
{
case'+':
return(a+b);
case'-':
return(a-b);
case'*':
return(a*b);
case'/':
return(a/b);
default:
cout<<"the expression have something wrong\n"<<endl;
exit(0);
}
}
//--------use the string to get the answer-------------
float EvaluateExpression(char *c,int n){
int i=0,count;
char theta;
float e,a,b;
OptrStack Optr;
OpndStack Opnd;
InitStack(Optr);
InitStack(Opnd);
Push(Optr,'#');
while(c[i]!='#'||GetTop(Optr)!='#'){
if(i>n){
cout<<"You forgot add the sign'#' after the expression.Please check it and add the sign'#'at the end of the expression..\n";
exit(0);
}
if(!InOptr(c[i])){
e=transform(&c[i],count);
i=i+count;
Push(Opnd,e);
}
else
switch(Precede(GetTop(Optr),c[i])){
case'<':
Push(Optr,c[i]);
i++;
break;
case'=':
Pop(Optr,theta);
i++;
break;
case'>':
Pop(Optr,theta);
Pop(Opnd,a);
Pop(Opnd,b);
Push(Opnd,Operate(b,theta,a));
break;
}//switch
}//while
if(Opnd.top-Opnd.base!=1||Optr.top!=Optr.base+1){
cout<<"sorry,the expression you input have something wrong.Please check it and reactive the program..\n";
exit(0);
}
return(GetTop(Opnd));
}//EvaluateExpression
//------------main----------------
int main(){
int n=0,i=0;
char c[100];
cout<<"please input the expression :\n (ending by the charator '#')"<<endl;
for(cin>>c[0];c[n]!='#';n++,cin>>c[n]);
for(;i<n;i++){
if(!(InOptr(c[i])||(c[i]>47&&c[i]<58)||c[i]=='.')){
cout<<"there are some irregular numbers in your expression.please check it and reinput."<<endl;
exit(0);
}
}
cout<<"the answer is :\n"<<EvaluateExpression(c,n)<<endl;
return(0);
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -