程序運行的時候出錯,自己弄了好久不知道怎麼辦,百度了也不行#include
#include
using namespace std;
enum error_code{success,underflow,overflow}; //定義枚舉
char opr[]={'+','-','*','/','(',')','#'};
struct node{
char data0;
int data1;
node *next;
};
int comp[7][7]= //優先級矩陣的實現
{
{2,2,1,1,1,2,2},
{2,2,1,1,1,2,2},
{2,2,2,2,1,2,2},
{2,2,2,2,1,2,2},
{1,1,1,1,1,3,0},
{0,0,0,0,0,0,0},
{1,1,1,1,1,0,3}};
class stack0{ //棧的實現
public:
stack0();
bool empty() const; //判空
error_code get_top0(char &x); //運算符取頂
error_code push0(const char x); //運算符壓棧
error_code pop0(char&op); //運算符彈棧
void dest();
private:
int count; //輔助空間
node *top;
};
class stack1{ //棧的實現
public:
stack1();
bool empty() const; //判空
error_code get_top1(double &x); //操作數取頂
error_code push1(const double x);//操作數壓棧
error_code pop1(double&a); //操作數彈棧
void dest();
private:
int count; //輔助空間
node *top;
};
stack0::stack0() {count=0;top=0;} //初始化
stack1::stack1() {count=0;top=0;} //初始化
void stack0::dest()
{
node *u=new node;
while(top)
{
u=top;
top=top->next;
delete u;
}
}
void stack1::dest()
{
node *u=new node;
while(top)
{
u=top;
top=top->next;
delete u;
}
}
bool stack0::empty()const{
return count==0;
}
bool stack1::empty()const{
return count==0;
}
error_code stack0::get_top0(char &x) {
if(empty()) return underflow;
x=top->data0;
return success;
}
error_code stack1::get_top1(double &x) {
if(empty()) return underflow;
x=top->data1;
return success;
}
error_code stack0::push0(const char x){
node *s=new node;
s->data0=x;
s->next=top;
top=s;
count++;
return success;
}
error_code stack1::push1(const double x){
node *s=new node;
s->data1=x;
s->next=top;
top=s;
count++;
return success;
}
error_code stack0::pop0(char&op){
if(empty()) return underflow;
else{
node *u=new node;
u=top;
top->next=u->next;
delete u;
count--;
return success;
}
}
error_code stack1::pop1(double&a){
if(empty()) return underflow;
else{node *u=new node;
u=top;
top->next=u->next;
delete u;
count--;
return success;
}
}
char cp(char ch1,char ch2) //比較兩個運算符的優先級函數
{
int i,m,n;
char prv;
int prio;
for(i=0;i
{
if(ch1==opr[i])
m=i;
if(ch2==opr[i])
n=i;
}
prio=comp[m][n];
switch(prio){
case 2:
prv='>';
break;
case 3:
prv='=';
break;
case 1:
prv='<';
break;
case 0:
prv='@';
cout<<"wrong."<
break;
}
return prv; //返回'>' '>' '='
}
double js(double a,char op,double b){ //將彈棧的數進行運算
double result;
switch(op){
case '+':
result=a+b;
break;
case '-':
result=b-a;
break;
case '*':
result=a*b;
break;
case '/':
result=b/a;
break;
}
return result;
}
bool ifopt(char ch) //判斷是否為字符
{
for(int i=0;i<7;i++)
{
if(ch==opr[i])
return true;
}
return false;
}
double jsq() //計算的主要功能的實現
{
char op,i=0,*str,x='#';
double a,b,temp,num;
stack0 operater;stack1 object; //創建類對象
operater.push0('#'); //提前將#壓棧
cout<<"input and end with the #"<<endl;
str=(char*)malloc(40*sizeof(char)); //習慣用c語言的方式
gets(str); //讀取字符
while(str[i]!='#'||x!='#'){ //判斷條件
if(!ifopt(str[i])){ //將字符型的數轉化成double型並壓棧
num=str[i]-'0';
i++;
while(!ifopt(str[i])){
num=num*10+str[i]-'0';
i++;
}
object.push1(num);
}
else{
switch(cp(x,str[i]))
{
case '<':
operater.push0(str[i]);
i++;
break;
case '=':
operater.pop0(x);
i++;
break;
case '>':
operater.pop0(op);
object.pop1(a);
object.pop1(b);
temp=js(a,op,b);
object.push1(temp);
break;
}
}operater.get_top0(x); //取頂
}
object.get_top1(temp);
return temp; //返回結果
//object.dest();
//operater.dest();
}
int main()//主函數
{
double result=jsq();
cout<<result<<endl;
return 0;
}
頭文件粘貼的時候掉了,#include''iostream''和include''cstdio''