高分求:“表达式求值的设计与实现代码”代码要求用VISUAL C++程序编写代码

2024年12月02日 13:07
有2个网友回答
网友(1):

//Utility.h
#include //standard string operations
#include //standard iostream operations
#include //numeric limits
#include //mathematical functions
#include //file input and output
#include //character classification
#include //date and time function
#include //con input and output
enum Error_code{success,fail,underflow,overflow};
//enum bool{false,true};

//Lk_stack.H
template
struct Node {
// data members
Node_entry entry;
Node *next;
// constructors
Node();
Node(Node_entry item, Node *add_on = NULL);
};

template
class Stack {
public:
// Standard Stack methods
Stack();
bool empty() const;
Error_code push(const Stack_entry &item);
Error_code pop();
Error_code top(Stack_entry &item) const;
void clear();
// Safety features for linked structures
~Stack();
Stack(const Stack &original);
void operator =(const Stack &original);
protected:
Node *top_node;
};

template
Node::Node()
{
next = NULL;
}

template
Node::Node(Node_entry item, Node *add_on)
{
entry = item;
next = add_on;
}

template
Stack::Stack()
{
top_node=NULL;
}

template
bool Stack::empty() const
{
if(top_node==NULL)
return true;
else
return false;
}

template
Error_code Stack::push(const Stack_entry &item)
/*
Post: Stack_entry item is added to the top of
the Stack; returns success or returns a code
of overflow if dynamic memory is exhausted.
*/
{
Node *new_top = new Node(item, top_node);
if (new_top == NULL) return overflow;
top_node = new_top;
return success;
}

template
Error_code Stack::pop()
/*
Post: The top of the Stack is removed. If the Stack
is empty the method returns underflow; otherwise it returns success.
*/
{
Node *old_top = top_node;
if (top_node == NULL) return underflow;
top_node = old_top->next;
delete old_top;
return success;
}

template
Error_code Stack::top(Stack_entry &item) const
{
Error_code result;
if(empty())
return underflow;
else{
item=top_node->entry;
return success;
}
}

template
void Stack::clear() // clear elememt
/*
Post: The Stack is cleared.
*/
{
while (!empty())
pop();
}

template
Stack::~Stack() // Destructor
/*
Post: The Stack is cleared.
*/
{
clear();
}

template
Stack::Stack(const Stack &original) // copy constructor
/*
Post: The Stack is initialized as a copy of Stack original.
*/
{
Node *new_copy, *original_node = original.top_node;
if (original_node == NULL) top_node = NULL;
else { // Duplicate the linked nodes.
top_node = new_copy = new Node(original_node->entry);
while (original_node->next != NULL) {
original_node = original_node->next;
new_copy->next = new Node(original_node->entry);
new_copy = new_copy->next;
}
}
}

template
void Stack::operator = (const Stack &original) // Overload assignment
/*
Post: The Stack is reset as a copy of Stack original.
*/
{
Node *new_top, *new_copy, *original_node = original.top_node;
if (original_node == NULL) new_top = NULL;
else { // Duplicate the linked nodes
new_copy = new_top = new Node(original_node->entry);
while (original_node->next != NULL) {
original_node = original_node->next;
new_copy->next = new Node(original_node->entry);
new_copy = new_copy->next;
}
}
while (!empty()) // Clean out old Stack entries
pop();
top_node = new_top; // and replace them with new entries.
}

//Calculator.h
template
class Calculator{
public:
void Run(); //执行表达式
private:
Stack OPND; //操作数栈
Stack OPTR; //操作符栈
int isp(char op); //栈内优先数
int icp(char op); //栈外优先数
bool Get2Operands(double &x,double &y); //从栈中取出两个操作数
bool DoOperator(char op); //形成运算指令,进行运算
void GetChar(char &ch); //从输入流获取一字符ch,并跳过空格及回车
bool IsOperator(char ch); //判断ch是否为操作符
};

template
void Calculator::Run()
{
OPND.clear();OPTR.clear();
char ch,OPTR_top,prior_char,op;double operand;
OPTR.push('=');
prior_char='='; //prior_char表示输入的前一个字符,如为数,则令其值为'0'
GetChar(ch);
OPTR.top(OPTR_top);
if(OPTR.top(OPTR_top)==underflow)
{
cout<<"表达式有错!"< return;
};
while(OPTR_top!='='||ch!='=')
{
if(isdigit(ch)||ch=='.')
{
cin.putback(ch);
cin>>operand;
OPND.push(operand);
prior_char='0';
GetChar(ch);
}
else if(!IsOperator(ch))
{
cout<<"表达式中出现非法字符!"< while(cin>>ch,ch!='=');
return;
}
else
{
if((prior_char=='='||prior_char=='(')&&(ch=='+'||ch=='-'))OPND.push(0);
if(isp(OPTR_top) {
OPTR.push(ch);
prior_char=ch;
GetChar(ch);
}
else if(isp(OPTR_top)>icp(ch))
{
OPTR.top(op);OPTR.pop();
if(!DoOperator(op))return;
}
else if(isp(OPTR_top)==icp(ch)&&ch==')')
{
OPTR.pop();
prior_char=')';
GetChar(ch);
};
};
if(OPTR.top(OPTR_top)==underflow)
{
cout<<"表达式有错!"< return;
};
}
if(OPND.top(operand)==underflow)
{
cout<<"表达式有错!"< return;
}
else
{
cout< return;
}

};

template
int Calculator::isp(char op)
{
int result;
switch(op)
{
case '=':
result=0;
break;
case '(':
result=1;
break;
case '^':
result=7;
break;
case '*':
case '/':
case '%':
result=5;
break;
case '+':
case '-':
result=3;
break;
case ')':
result=8;
}
return result;
};

template
int Calculator::icp(char op)
{
int result;
switch(op)
{
case '=':
result=0;
break;
case '(':
result=8;
break;
case '^':
result=6;
break;
case '*':
case '/':
case '%':
result=4;
break;
case '+':
case '-':
result=2;
break;
case ')':
result=1;
}
return result;
};

template
bool Calculator::Get2Operands(double &x,double &y)
{
if(OPND.empty())
{
cout<<"表达式有错!"< return false;
}
OPND.top(y);OPND.pop();

if(OPND.empty())
{
cout<<"表达式有错!"< return false;
}
OPND.top(x);OPND.pop();

return true;
};

template
bool Calculator::DoOperator(char op)
{
Data_element x,y;
bool result=Get2Operands(x,y);
if(result==true)
{
switch(op)
{
case '+':
OPND.push(x+y);
break;
case '-':
OPND.push(x-y);
break;
case '*':
OPND.push(x*y);
break;
case '/':
if(y==0)
{
cout<<"除数为零!"< return false;
}
OPND.push(x/y);
break;
case '%':
if((long)y==0)
{
cout<<"除数为零!"< return false;
}
OPND.push((long)x % (long)y);
break;
case '^':
OPND.push(pow(x,y));
}
return true;
}
else return false;
};

template
void Calculator::GetChar(char &ch)
{
cin>>ch;
while(ch==' '||ch=='\n')
cin>>ch;
};

template
bool Calculator::IsOperator(char ch)
{
if(ch=='='||ch=='('||ch=='^'||ch=='*'||
ch=='/'||ch=='%'||ch=='+'||ch=='-'||ch==')')
return true;
else
return false;
};

//Calculator.cpp
#include"Utility.h"
#include"Lk_stack.h"
#include"Calculator.h"
main()
{
Calculator s;
char iscontinue='Y';
int i;
while(iscontinue=='Y')
{
cout<<"输入表达式(以等号“=”结束):"< s.Run();
cout<<"是否继续(Y/N)?";
cin>>iscontinue;
iscontinue=toupper(iscontinue);
}
}

网友(2):

//m_str里存中序表达式
int compare(char a,char b)
{
if ((a=='*'||a=='/')&&(b=='*'||b=='/'))
return 1;
else if ((a=='+'||a=='-')&&(b=='+'||b=='-'))
return 1;
else if (a!='#'&&b=='#')
return 1;
else if (a=='#'&&b=='#')
return 3;
else if (a=='#'&&b!='#')
return -1;
else if ((a=='*'||a=='/')&&(b=='+'||b=='-'))
return 1;
else if ((a=='*'||a=='/'||a=='+'||a=='-'||a=='(')&&(b=='('))
return -1;
else if ((a=='*'||a=='/'||a=='+'||a=='-'||a==')')&&(b==')'))
return 1;
else if ((a=='(')&&(b==')'))
return 0;

else return -1;

}

double op(double num1,double num2,char opr)
{
double ans;
switch (opr)
{
case '+':ans=num1+num2;
break;
case '-':ans=num1-num2;
break;
case '*':ans=num1*num2;
break;
case '/':ans=num1/num2;
break;
default:break;
}

return ans;
}
void CjisuanDlg::fun()
{
LinkedStack s;
LinkedStack out;
int x,wrong=0,temp=1,spot=0; //x记录符号比较结果wrong记录等式是否发生错误temp记录在负号前面是否是符号,用来辨别负号和减号
char t; //记录删除的字符
double t1,t2;
s.Add('#');
double tt=0;
while(1)
{
if ((m_str[0]<='9'&&m_str[0]>='0')||(temp==1&&m_str[0]=='-'))
{
spot=0;
double tt=_tstof(m_str);

m_str.Delete(0,1);

while ((m_str[0]<='9'&&m_str[0]>='0')||m_str[0]=='.')
{
if (m_str[0]=='.')
spot++;
m_str.Delete(0,1);
}

out.Add(tt);
temp=0;
if (m_str[0]=='(')
{
MessageBox(_T("wrong equation!"));
wrong=1;
break;
}
else if (spot>1)
{
MessageBox(_T("too much spot!"));
wrong=1;
break;
}

}
else
{
temp=1;
x=compare(s.Top(),m_str[0]);

if (x==-1)
{
s.Add(m_str[0]);
m_str.Delete(0,1);
}

else if (x==1)
{
s.Delete(t);

if (out.IsEmpty())
{
MessageBox(_T("wrong equation!"));
wrong=1;
break;
}

out.Delete(t2);

if (out.IsEmpty())
{
MessageBox(_T("wrong equation!"));
wrong=1;
break;
}

out.Delete(t1);

if (t=='/'&&t2==0) //防止除0
{MessageBox(_T("wrong: \"x/0\" !"));
wrong=1;
break;
}

out.Add(op(t1,t2,t));
temp=0;
continue;

}
else if (x==0)
{
s.Delete(t);
m_str.Delete(0,1);
}

else if (x==3)
break;
}

}
if (wrong==0)
{
if (!out.IsEmpty())
{
m_str.Format(_T("%f"),out.Top());
}
else m_str.Delete(0,1);
}
else m_str="";
UpdateData(false);

}