所有你列的运算都实现了,代码如下
#ifndef _CALCULARTOR_H
#define _CALCULARTOR_H
#include <iostream>
#include <string>
#include <cmath>
#include <ctype.h>
#include <cstdlib>
using namespace std;
struct OPRT{
int size;
int top;
char *elements;
OPRT(int sizeOprt){
size=sizeOprt;
elements=new char[sizeOprt];
top=-1;
}
~OPRT(){
delete [] elements;
}
bool empty(){
if(top==-1)
return true;
else
return false;
}
bool full(){
if(top >= size-1)
return true;
else
return false;
}
bool push(char x){
if(full())
return false;
top++;
elements[top]=x;
return true;
}
bool pop(char &x){
if(empty())
return false;
x=elements[top];
top--;
return true;
}
char gettop(){
char x;
x=elements[top];
return x;
}
};
struct OPRD{
int size;
int top;
double *elements;
OPRD(int size1){
size=size1;
elements=new double[size1];
top=-1;
}
~OPRD(){
delete [] elements;
}
bool empty(){
if(top==-1)
return true;
else
return false;
}
bool full(){
if(top >= size-1)
return true;
else
return false;
}
bool push(double x){
if(full())
return false;
top++;
elements[top]=x;
return true;
}
bool pop(double &x){
if(empty())
return false;
x=elements[top];
top--;
return true;
}
double gettop(){
double x;
x=elements[top];
return x;
}
};
int precede(char cStackTop,char cNew);
double operate(double a,char theta,double b);
#endif
//:This is the main function of calculator
#include "calculator.h"
bool bError = false;
int main(void)
{
const int iSizeOfStk = 20;
OPRD oprd(iSizeOfStk);
OPRT oprt(iSizeOfStk);
string sInExp;
string sOprd; //to get num from sInExp
double dOprd;
cout << "Please enter the expression ended with '=' :" << endl;
getline(cin,sInExp,'=');
sInExp = sInExp + "=";
oprt.push('=');
//calculat
for( int iLoopStart = 0; iLoopStart < sInExp.length(); )
{
if( bError ) //error in exp exit loop
break;
int iLoopEnd = iLoopStart;
if( isdigit(sInExp.at(iLoopStart) ) ) //num
{
bool hasDot = false;
for( ; iLoopEnd < sInExp.length(); iLoopEnd++) //get num
{
if( isdigit(sInExp.at(iLoopEnd)) == 0 ) //find . or end of num
{
if(sInExp.at(iLoopEnd) == '.')
{
hasDot = true;
}
else
{
sOprd.assign( sInExp, iLoopStart, iLoopEnd-iLoopStart );
if( hasDot )
{
dOprd = atof( sOprd.c_str() );
}
else
{
dOprd = atoi( sOprd.c_str() );
}
oprd.push( dOprd );
break;
}
}
}
}
else //oprt
{
int iPre = precede( oprt.gettop(),sInExp.at( iLoopEnd ) );
while( iPre < 0) //运算
{
char cTemp;
double numA,numB;
if( !(oprt.pop(cTemp) && oprd.pop(numB) && oprd.pop(numA) ) )
//未成功出栈
{
bError = true;
}
else
{
double dTemp = operate(numA,cTemp,numB);
oprd.push(dTemp);
}
iPre = precede( oprt.gettop(),sInExp.at( iLoopEnd ) );
}
if( iPre == 1 ) //push into stack oprt
{
oprt.push( sInExp.at( iLoopEnd ) );
}
else if( iPre == 0 ) //() or miss( or miss )
{
char cNow = sInExp.at( iLoopEnd );
if( (oprt.gettop() == cNow) && (oprt.gettop() == '=') && (oprd.top == 0) )
{
break; //finish
}
else if( (cNow == ')') && (oprt.gettop() == '(') )
{
char cTemp;
oprt.pop(cTemp);
}
else
{
bError = true;
}
}
iLoopEnd++;
}
iLoopStart = iLoopEnd;
}
if( !bError )
{
double dTemp;
oprd.pop(dTemp);
cout.setf(ios::fixed | ios::showpoint);
cout.precision(2); //需要几位小数就设为多少
cout << dTemp << endl;
cout.unsetf(ios::fixed); //取消设置输出格式
cout.unsetf(ios::showpoint);
}
else
{
cout << "ERROR IN INFIX NOTATION" << endl;
}
return 0;
}
//:This is the implementation of calculator
#include "calculator.h"
extern bool bError;
//return the precede of two oprd
int precede(char cStackTop,char cNew)
{
int level[8][9]={
//row: stacktop, col:new entered
// +, -, *, /, %, ^, (, ), =
{-1,-1, 1, 1, 1, 1, 1,-1,-1}, //+
{-1,-1, 1, 1, 1, 1, 1,-1,-1}, //-
{-1,-1,-1,-1,-1, 1, 1,-1,-1}, //*
{-1,-1,-1,-1,-1, 1, 1,-1,-1}, ///
{-1,-1, 1, 1,-1, 1, 1,-1,-1}, //%
{-1,-1,-1,-1,-1, 1, 1,-1,-1}, //^
{ 1, 1, 1, 1, 1, 1, 1, 0, 0}, //(
{ 1, 1, 1, 1, 1, 1, 1, 0, 0}}; //=
// -1 pop,1 push,0 check or finished
int dStackTop,dNew;
switch(cStackTop)
{
case '+':
dStackTop = 0;
break;
case '-':
dStackTop = 1;
break;
case '*':
dStackTop = 2;
break;
case '/':
dStackTop = 3;
break;
case '%':
dStackTop = 4;
break;
case '^':
dStackTop = 5;
break;
case '(':
dStackTop = 6;
break;
case '=':
dStackTop = 7;
break;
}
switch(cNew)
{
case '+':
dNew = 0;
break;
case '-':
dNew = 1;
break;
case '*':
dNew = 2;
break;
case '/':
dNew = 3;
break;
case '%':
dNew = 4;
break;
case '^':
dNew = 5;
break;
case '(':
dNew = 6;
break;
case ')':
dNew = 7;
break;
case '=':
dNew = 8;
break;
}
return level[dStackTop][dNew];
}
//end of precede
//calculat the result
double operate(double a,char theta,double b)
{
double temp;
switch(theta)
{
case '+':
return a+b;
case '-':
return a-b;
case '*':
return a*b;
case '/':
return a/b;
case '^':
if( a < 0 && b!=(int)b )
{
bError = true;
return -1;
}
temp=pow(a,b);
return temp;
case '%':
if( b!=(int)b || a!= (int)a)
{
bError = true;
return -1;
}
return (int)a%(int)b;
break;
default:
bError = true;
return -1;
}
}
//end of the operate
追问编译的时候有一个错误Cannot open include file: 'calculator.h': No such file or directory,怎么办呢?
追答这个程序并不只一个文件,一共有三个,你得先建一个项目,然后把三个文件导进去,再编译
追问可以给我讲详细点嘛?
追答如果一个程序既有头文件(这个你应该知道吧?)又有源文件,你就得建一个项目,然后在项目中添加你需要的头文件和源文件,这个很基本,当然可能我这个程序复杂了点,你试着改一下吧
追问可以给我说说操作过程吗?