当前位置:首页 > 工大数据结构第三章作业
cout<<\ } }
double solve(TNode *root) { if(root->left==NULL){ istringstream in; in.str(root->id); double value; in>>value; return value; } else{ switch(root->id[0]){ case '+': return solve(root->left)+solve(root->right); case '-': return solve(root->left)-solve(root->right); case '*': return solve(root->left)*solve(root->right); case '/': return solve(root->left)/solve(root->right); } } }
void Check(char *str) //判断为带符号且紧跟括号的情况,酌情在前面添\{ int k=0,i=0; if(str[0]=='+'||str[0]=='-'){ int b=0; while(str[k]=='+'||str[k]=='-'){ if(str[k]=='-') b++; k++; } if(str[k]!='(') return; char *np=new char[strlen(str)+3]; if(b%2){ np[0]='0'; np[1]='-'; memcpy(np+2,str+k,strlen(str)+1-k); memcpy(str,np,strlen(np)+1); } else{ memcpy(np,str+k,strlen(str)+1-k);
memcpy(str,np,strlen(np)+1); } delete[] np; } }
void main() { char buf[100]; while(1){ cin>>buf; Check(buf); printExpr(buf); } }
//stack类
#include
template
public:
SNode(){next=NULL;}
SNode(const T& td,SNode
SNode
template
public:
Stack(){pdata=NULL;length=0;} ~Stack();
bool isEmpty(); bool pop();
bool push(const T& ); T top(); int size(){ return length;} private:
SNode
template
Stack
SNode
np=sp->next; delete sp; sp=np; } }
template
return pdata==NULL; }
template
SNode
if(!sp->next){
delete sp; pdata=NULL; return true; } else{
while((sp->next)->next) sp=sp->next; delete sp->next; sp->next=NULL; return true; } }
template
bool Stack
SNode
pdata=np; return true; } else{
while(sp->next) sp=sp->next; sp->next=np; return true; } }
template
SNode
while(sp->next) sp=sp->next; return sp->data; }
要求:
1、上述作业要求在单独完成;
2、完成后,于规定期限内提交到ftp服务器的相应目录中中,注意,在提交时将所编写的程序统一拷贝到一个Word文件中,文件名格式为“学号+姓名”
共分享92篇相关文档