用c++写的将数学表达式转换为结果的代码(如输入1+(2*9-5)= 14)

上英语课无聊突然想了一段代码,不足之处还望指教:吐舌头吐舌头

#pragma once
//  [5/24/2012 Administrator]
//  [5/25/2012 Administrator]
#include "stdafx.h"
#include <iostream>
using namespace std;
class computer{
private:
	typedef struct Node{
		float _num;
		char _operator;
		Node* head;
		Node* next;
		Node(float n=0,char p=0,Node* h=NULL,Node* t=NULL):_num(n),_operator(p),head(h),next(t){};
	}*NODE;
	
private:
	NODE _begin;
	NODE _end;
	char* p;
public:
	class itrator{
	private:
		NODE current;
	public:
		itrator():current(){};
		~itrator(){};
		itrator(NODE p):current(p){}
		itrator(itrator& itr):current(itr.current){}
	public:
		itrator& operator++()
		{
			if (this->current!=NULL)
			{
				this->current=this->current->next;
				return (*this);
			} 
			else
			{
				return *this;
			}
		};
		itrator operator++(int)
		{
			itrator old=*this;
			++(*this);
			return old;
		};
		char& operator*()
		{
			return this->current->_operator;

		}

		itrator& operator--()
		{
			if (this->current->head!=NULL)
			{
				this->current=this->current->head;
				return (*this);
			} 
			else
			{
				return *this;
			}
		};
		itrator operator--(int)
		{
			itrator old=*this;
			--(*this);
			return old;
		};

		bool operator==(const itrator &rhs)
		{
			return this->current==rhs.current;
		};
		bool operator!=(const itrator &rhs)
		{
			return !(*this==rhs);
		};
		friend class computer;
	};
	friend class itrator;
public:
	computer()
	{
		p=NULL;
	
		_begin=NULL;
		_end=NULL;
		
	}
	~computer()
	{
		clear();
		
	}
	computer(char* temp,int _n=50)
	{
		p=new char[_n];
		char* n=p;
		char* m=temp;
		while (*m)
		{
			*n=*m;
			n++;
			m++;
		}
		*n='\0';

	}
	void create(char* temp,int _n=50)
	{
		p=new char[_n];
		char* n=p;
		char* m=temp;
		while (*m)
		{
			*n=*m;
			n++;
			m++;
		}
		*n='\0';

	}
	void replace(char* temp)
	{
		clear(); 
		char* n=p;
		char* m=temp;
		while (*m)
		{
			*n=*m;
			n++;
			m++;
		}
		*n='\0';
	}
	bool clear()
	{
		if (_begin==NULL)
		{
			return false;
		}
		NODE p_Node=_begin;
		while (p_Node!=_end)
		{
			p_Node=p_Node->next;
			delete p_Node->head;
		}
		delete _end;
		if (p)
		{
			delete p;
			p=NULL;
		}
		
		return true;

	}
private:
	void Init();
	bool FirFun(NODE _p);
	bool SecFun(NODE _p);
	void include(itrator itr);
	itrator push_back(float a);
	itrator push_back(char b);
	itrator remove(itrator itr);
	
public:
	float getvalue()
	{
		Init();
		itrator itr(_begin);
		while (itr.current!=_end)
		{
			cout<<"mark\n";
			if ((*itr)=='(')
			{
				cout<<"find (\n";
				include(itr);
				break;
			}
			++itr;
		}
		while(FirFun(_begin));
		while(SecFun(_begin));
		return _begin->next->_num;

	}

};
void computer::Init()
{
	cout<<"Init() start:\n";
	if (p==NULL)
	{
		return;
	}
	char* temp=p;
	_begin=new Node(0,0,NULL,NULL);
	_end=new Node(0,0,_begin,NULL);
	_begin->next=_end;
	bool _sel=false;
	float sum=0,_small=1,_sav=0;
	while (*temp)
	{
		if (*temp<='9'&&*temp>='0')
		{
			sum*=10;sum+=int(*temp-'0');
			if (_sav)
			{
				_small*=10;
			}
		}else if (*temp=='*'||*temp=='-'||*temp=='+'||*temp=='/')
		{
			if (_sel)
			{
				push_back(float(sum/_small));
				//cout<<sum;
				push_back(')');
				sum=0;
				_sav=0;
				_small=1;
				push_back(*temp);
				_sel=false;
			} 
			else
			{
				push_back(float(sum/_small));
				//cout<<sum;
				sum=0;
				_sav=0;
				_small=1;
				push_back(*temp);
			}
			
			//cout<<*temp;
		} else if(*temp=='=')
		{
			if (_sel)
			{
				push_back(float(sum/_small));
				//cout<<sum;
				push_back(')');
				sum=0;
				_sav=0;
				_small=1;
				push_back(*temp);
				_sel=false;
			} 
			else
			{
				push_back(float(sum/_small));
				//cout<<sum;
				sum=0;
				_sav=0;
				_small=1;
				push_back(*temp);
			}
			break;
		} else if (*temp=='.')
		{
			_sav=1;
		} else if (*temp=='(')
		{
			push_back(*temp);
		} 
		else if(*temp==')')
		{
			_sel=true;
		}
		if (*temp!='\0')
		{
			temp++;
		}
	}
}
computer::itrator computer::push_back(float a)
{
	NODE temp=new Node(a,0,_end->head,_end);
	temp->head->next=temp;
	_end->head=temp;
	return itrator(temp);



}
computer::itrator computer::push_back(char b)
{
	NODE temp=new Node(0,b,_end->head,_end);
	temp->head->next=temp;
	_end->head=temp;
	return itrator(temp);
}
computer::itrator computer::remove(itrator itr)
{
	/*cout<<"remove() start"<<endl;*/
	NODE _head,_next;
	_head=itr.current->head;
	_next=itr.current->next;
	_head->next=_next;
	_next->head=_head;
	delete itr.current;
	return itrator(_head);
}
bool computer::FirFun(NODE _p)
{
	itrator _temp(_p->next);
	while (_temp.current->next->next!=NULL&&_temp.current->_operator!=')')
	{
	    if (*_temp=='*'||*_temp=='/')
	    {
			if (*_temp=='*')
			{
				_temp.current->head->_num=((_temp.current->head->_num)*(_temp.current->next->_num));
				itrator p_temp(_temp.current->next);
				remove(_temp);
				remove(p_temp);
				return true;
			} 
			else
			{
				_temp.current->head->_num=((_temp.current->head->_num)/(_temp.current->next->_num));
				itrator p_temp(_temp.current->next);
				remove(_temp);
				remove(p_temp);
				return true;
			}
	     }
		++_temp;
	}
	return false;
}
bool computer::SecFun(NODE _p)
{
	itrator _temp(_p->next);
	while (_temp.current->next->next!=NULL&&_temp.current->_operator!=')')
	{
		/*cout<<"SecFUN()="<<*_temp<<endl;*/
		if (*_temp=='+'||*_temp=='-')
		{
			if (*_temp=='+')
			{
				_temp.current->head->_num=((_temp.current->head->_num)+(_temp.current->next->_num));
				itrator p_temp(_temp.current->next);
				remove(_temp);
				remove(p_temp);
				return true;
			} 
			else
			{
				_temp.current->head->_num=((_temp.current->head->_num)-(_temp.current->next->_num));
				itrator p_temp(_temp.current->next);
				remove(_temp);
				remove(p_temp);
				return true;
			}
		}
		++_temp;
	}
	return false;

}
void computer::include(itrator itr)
{
	cout<<*itr<<endl;
	cout<<"include() begin:\n";
	itrator tem(itr);
	++itr;

	while ((*itr)!='('&&(*itr)!=')')
	{
		cout<<"symbol=\t"<<*itr<<endl;
		++itr;
	}
	if((*itr)=='(')
	{
		include(itr);
		while(FirFun(tem.current));
		while(SecFun(tem.current));
		itrator _itr(tem.current->next->next);
		remove(_itr);
		remove(tem);

	}else if ((*itr)==')')
	{
		while(FirFun(tem.current));
		while(SecFun(tem.current));
		remove(itr);
		remove(tem);
	}
}



评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值