工大数据结构第三章作业 联系客服

发布时间 : 星期三 文章工大数据结构第三章作业更新完毕开始阅读

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 #include using namespace std; //class stack

template class SNode {

public:

SNode(){next=NULL;}

SNode(const T& td,SNode* p=NULL){data=td; next=p;} T data;

SNode *next; };

template class Stack {

public:

Stack(){pdata=NULL;length=0;} ~Stack();

bool isEmpty(); bool pop();

bool push(const T& ); T top(); int size(){ return length;} private:

SNode *pdata; int length; };

template

Stack::~Stack() {

SNode *sp=pdata,*np; while(sp) {

np=sp->next; delete sp; sp=np; } }

template bool Stack::isEmpty() {

return pdata==NULL; }

template bool Stack::pop() {

SNode *sp=pdata; if(!sp) return false; length--;

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::push(const T& st) {

SNode *np=new SNode(st),*sp=pdata; if(!np) return false; length++; if(!sp){

pdata=np; return true; } else{

while(sp->next) sp=sp->next; sp->next=np; return true; } }

template T Stack::top() {

SNode *sp=pdata; if(!sp) return T();

while(sp->next) sp=sp->next; return sp->data; }

要求:

1、上述作业要求在单独完成;

2、完成后,于规定期限内提交到ftp服务器的相应目录中中,注意,在提交时将所编写的程序统一拷贝到一个Word文件中,文件名格式为“学号+姓名”