工大数据结构第三章作业

2026/4/23 22:41:46

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文件中,文件名格式为“学号+姓名”


工大数据结构第三章作业.doc 将本文的Word文档下载到电脑
搜索更多关于: 工大数据结构第三章作业 的文档
相关推荐
相关阅读
× 游客快捷下载通道(下载后可以自由复制和排版)

下载本文档需要支付 10

支付方式:

开通VIP包月会员 特价:29元/月

注:下载文档有可能“只有目录或者内容不全”等情况,请下载之前注意辨别,如果您已付费且无法下载或内容有问题,请联系我们协助你处理。
微信:xuecool-com QQ:370150219