?? 實驗三(2).cpp
字號:
#include"iostream.h"
#define maxlen 50
struct node
{
float data;node *next;
};
enum error_cord { overflow, underflow, success};
char cal[50];
char fuu='9';
class stackshu
{
public:
stackshu();
bool kong()const;
bool mande() const;
error_cord Getop(float &x)const ;
error_cord Push(const float x);
error_cord Pop();
private:
int count,n,s;
node *p,*next,*top;
}shu;
stackshu::stackshu()
{
count=0;
top=NULL;
}
bool stackshu::kong() const//判斷是否為空
{
if(count==0)
return true;
else
return false;
}
bool stackshu::mande() const//判斷是否為滿
{
return count==maxlen;
}
error_cord stackshu::Getop(float &x)const//取頂元素
{
if (kong()) return underflow;
x=top->data;
return success;
}
error_cord stackshu::Push(const float x)//進棧
{
if(mande())
return overflow;
node *s;
s=new node;
s->data=x;
s->next=top;
top=s;
count++;
return success;
}
error_cord stackshu::Pop()//刪除元素
{
if (kong()) return underflow;
node *u;
u=top;
top=top->next;
delete u;
count--;
return success;
}
struct dnode{ char data;dnode *next;};
class stackfu
{
public:
stackfu();
bool kong()const;
bool mande() const;
error_cord Getop(char &x)const ;
error_cord Push(const char x);
error_cord Pop();
private:
int count,n,s;
dnode *p,*next,*top;
}fu;
stackfu::stackfu()
{
count=0;
top=NULL;
}
bool stackfu::kong() const//判斷是否為空
{
if(count==0)
return true;
else
return false;
}
bool stackfu::mande() const//判斷是否為滿
{
return count==maxlen;
}
error_cord stackfu::Getop(char &x)const//取頂元素
{
if (kong()) return underflow;
x=top->data;
return success;
}
error_cord stackfu::Push(const char x)//進棧
{
if(mande())
return overflow;
dnode *s;
s=new dnode;
s->data=x;
s->next=top;
top=s;
count++;
return success;
}
error_cord stackfu::Pop()//刪除元素
{
if (kong()) return underflow;
dnode *u;
u=top;
top=top->next;
delete u;
count--;
return success;
}
void suan() //計算
{
char f;
float a,b;
fu.Getop(f);
switch(f)
{
case '+': shu.Getop(a);shu.Pop();shu.Getop(b);shu.Pop();fu.Pop();
shu.Push(a+b);fu.Getop(fuu);
break;
case '-': shu.Getop(a);shu.Pop();shu.Getop(b);shu.Pop();fu.Pop();
shu.Push(b-a);fu.Getop(fuu);
break;
case '*': shu.Getop(a);shu.Pop();shu.Getop(b);shu.Pop();fu.Pop();
shu.Push(a*b);fu.Getop(fuu);
break;
case '/': shu.Getop(a);shu.Pop();shu.Getop(b);shu.Pop();fu.Pop();
shu.Push(b/a);fu.Getop(fuu);
break;
default:break;
}
}
bool youxian(char x) //判斷優先級
{
char q;
if(!fu.kong())
{
fu.Getop(q);
}
switch(x)
{
case'(':return true;break;
case')':return false;break;
case'+': switch(q)
{
case'#':return true;break;
case'(':return true;break;
case')':return true;break;
default:return false;break;
}
break;
case'-': switch(q)
{
case'#':return true;break;
case'(':return true;break;
case')':return true;break;
default:return false;break;
}
break;
case'*': switch(q)
{
case'#':return true;break;
case'+':return true;break;
case'-':return true;break;
case'(':return true;break;
default:return false;break;
}
break;
case'/': switch(q)
{
case'#':return true;break;
case'+':return true;break;
case'-':return true;break;
case'(':return true;break;
default:return false;break;
}
break;
case'#':
if(fu.kong())
return true;
else {
fu.Getop(q);
if(q=='#')
return true;
else
return false;
}
break;
default:return true ;break;
}
}
void calculator() //計算器
{
for(int a=0;a<50;a++)
{
if(cal[a]=='\0')
break;
switch(cal[a])
{
case'(':fuu='(';
while(!youxian(fuu))
suan();
fu.Push(cal[a]);
break;
case')':fuu=')';
while(fuu!='(')
{
suan();
}
fu.Pop();
break;
case'*':fuu='*';
while(!youxian(fuu))
suan();
fu.Push(cal[a]);
break;
case'/':fuu='/';
while(!youxian(fuu))
suan();
fu.Push(cal[a]);
break;
case'+':fuu='+';
while(!youxian(fuu))
suan();
fu.Push(cal[a]);
break;
case'-':fuu='-';while(!youxian(fuu))
suan();
fu.Push(cal[a]);
break;
case'#':fuu='#';while(!youxian(fuu))
suan();
fu.Push(cal [a]);
break;
default:
float quan=0;
while(cal[a]>='0'&& cal[a]<='9')
{
quan=quan*10+cal[a]-'0';
a++;
};
a--;
shu.Push(quan);
}
}
}
void main()
{
float yes;
cout<<"輸入以#開始的計算式并以#結束"<<endl;
cin>>cal;
calculator();
shu.Getop(yes);
cout<<yes<<endl;
}
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -