上英语课无聊突然想了一段代码,不足之处还望指教:
#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);
}
}