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

📄 新建 文本文档.txt

📁 数据结构中
💻 TXT
字号:
//CALCULAT.CPP
#include <iostream.h>
#include <fstream.h>
#include <string.h>
#include <afx.h>
#include <Windows.h>
#include"Rational.h"
#include"Stack.cpp"

template<class Type>
class Calculator
{
    private:
		int Maxsize;
		Stack<Type> data;//操作数栈
		Stack<char> oper;//操作符栈
		Stack<int>  degree;  //操作符优先级栈
		void AddOperand(Type);
		int Get2Operands(Type &,Type &);
		void DoOperator(char op);
    public:
		Calculator(int sz):data(sz),oper(sz),degree(sz),Maxsize(sz){};
		void writeStreamData(ofstream fsm);
		Type Run(CString pc,CString fname);
		void Clear();
};
///////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////
template<class Type> 
void Calculator<Type>::writeStreamData(ofstream fsm)
{
                int c=data.Qutop();
				fsm.write("操作数栈:  ",strlen("操作数栈:  "));
				if(data.IsEmpty()) fsm.write("NULL",4);
				for(int ii=0;ii<=c;ii++)
				{
				    CString tem;
					Type rat_tem;
					rat_tem=data.Quelems(ii);
					int zi=rat_tem.Getnum();
					int mu=rat_tem.Getden();
					
					if(ii==c)
					{
						if(mu==1)
							tem.Format("%d\n",zi);
						else
                            tem.Format("%d/%d\n",zi,mu);

					}
					else
					{
						
						if(mu==1)
						  tem.Format("%d\t",zi);
						else
                          tem.Format("%d/%d\t",zi,mu);

					}
					fsm.write(tem,tem.GetLength());   
				}

}
template<class Type>
void Calculator<Type>::AddOperand(Type value)
{ 
	data.Push(value); 
}
///////////////////////////////////////////////////////////
template<class Type>
int Calculator<Type>::Get2Operands(Type &left,Type &right)
{
    if(data.IsEmpty())
	    return 0;
    right=data.Pop();
    if(data.IsEmpty())
        return 0;
    left=data.Pop();
    return 1;
  }
///////////////////////////////////////////////////////////
template<class Type>
void Calculator<Type>::DoOperator(char op)
{
    Type left,right,ling;
    if(Get2Operands(left,right))
	
		
		
		
	switch(op)
	{
		
       case'+':
			{
			    data.Push(left+right);
				break;
			}
	    case'-': 
			{
			    data.Push(left-right);
				break;
			}
	    case'*': 
			{
				data.Push(left*right);
				break;
			}
	    case'/':
			{
				if(right==ling)
				    data.MakeEmpty();
		        else  
					data.Push(left/right);
		        break;
			}
	}
    else  
		data.MakeEmpty();
}
///////////////////////////////////////////////////////////
template<class Type>
void Calculator<Type>::Clear()
{ 
	data.MakeEmpty();
}
///////////////////////////////////////////////////////////
template<class Type>
Type Calculator<Type>::Run(CString pc,CString fname)
  {
   
	//----------
	
	 ofstream fout;
     fout.open(fname,ios::out,0);
	 
	 
    int num;
    int de;//输入字符的优先级
    char c;//输入的字符
	int i=0;
	int  n;
	n=pc.GetLength();
    int j;//temp数组的下标
//  *pc//存放输入的全部字符
    char op;
    char temp[15];//存放一个临时数字串
    c=pc.GetAt(i++);
	Type ling;
    
	 while(c!=0)
     {
	       //fout<<oper;
		//判断是否是数字
			
		 if(c>='0'&&c<='9')
			{ 
				   j=0;
				  
				   while(c>='0'&&c<='9')		
				  {
				   
					temp[j]=c;
					j++;
					
					if(i<n)
					{c=pc.GetAt(i++);}
					else
					{c=0;}
                    
				  }
				 
				num=atoi(temp);
				ling.Setnumden(num,1);
				AddOperand(ling);
                writeStreamData(fout);
			     for(int k=0;k<j;k++)
				 {
				    temp[k]=0;
				 }
			}
			
	///////////判断运算符的优先级//////////////////////////
			
		//**遇到右括号出栈 直到遇到左括号**//	
			if(c==')')
			{
			  de=1;
			  while(oper.Gettop()!='(')
			  {
			    op=oper.Pop();
				fout<<oper;
				degree.Pop();
				DoOperator(op);
				writeStreamData(fout);
			  
			  }
              oper.Pop();
			  fout<<oper;
			 if(i<n)
			  c=pc.GetAt(i++);
			 else
				 c=0;
			}
			
	 //**********判断运算符为加号或减号***************		
			if(c=='+'||c=='-')
			{
		      de=2;
              if(degree.IsEmpty()||degree.Gettop()==4)
			  { 
				  degree.Push(de);
			      oper.Push(c);
				  fout<<oper;
			      
			  }
			  else if(de<=degree.Gettop())
			  {
			      op=oper.Pop();
				  fout<<oper;
				  degree.Pop();
				  DoOperator(op);
                  writeStreamData(fout);
                  oper.Push(c);
				  fout<<oper;
				  degree.Push(de);
			  
			  }
			  if(i<n)
			    c=pc.GetAt(i++);
			  else
                c=0;
			  
			}
           if(c=='*'||c=='/')        
		   {
             de=3;
			 if(oper.IsEmpty()||de>degree.Gettop()||degree.Gettop()==4)
			 {
			    degree.Push(de);
				oper.Push(c);
				fout<<oper;
			 }
			 else if(de<=degree.Gettop())
			 {
			    op=oper.Pop();
				degree.Pop();
				DoOperator(op);
				writeStreamData(fout);
                oper.Push(c);
				fout<<oper;
			    degree.Push(de);
			 }
		     if(i<n)
			   c=pc.GetAt(i++);
		      else
				  c=0;
		   }
    
	     //////若是运算符是左等号直接进栈//////////
		   if(c=='(')
		   {
		       de=4;
			   oper.Push(c);
			   fout<<oper;
			   degree.Push(de);
			   if(i<n)
			     c=pc.GetAt(i++);
               else
		         c=0;
		   }
	
	}	
	while(!oper.IsEmpty())//处理剩余运算符
    {
		op=oper.Pop();
		fout<<oper;
		degree.Pop();
		DoOperator(op);
		writeStreamData(fout);
    }
	fout.close();
    return data.Gettop();
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -