⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 实验三(2).cpp

📁 2007数据结构课程设计报告+源代码
💻 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 + -