答案:#include <stdio.h>
struct s_node
{ int data;
struct s_node *next;
}; typedef struct s_node s_list;
typedef s_list *link;
link operator=NULL;
link operand=NULL;
link push(link stack,int value)
{ link newnode;
newnode=(link) malloc(sizeof(s_list));
if(!newnode)
{ printf("\nMemory allocation failure!!!");
return NULL;
} newnode->data=value;
newnode->next=stack;
stack=newnode;
return stack; }
link pop(link stack,int *value)
{ link top;
if(stack !=NULL)
{ top=stack;
stack=stack->next;
*value=top->data;
free(top);
return stack; }
else
*value=-1; }
int empty(link stack)
{ if(stack==NULL)
return 1;
else
return 0; }
int is_operator(char operator)
{ switch (operator)
{
case '+': case '-': case '*': case '/': return 1;
default:return 0;
}
}
int priority(char operator)
{
switch(operator)
{
case '+': case '-' : return 1;
case '*': case '/' : return 2;
default: return 0; } }
int two_result(int operator,int operand1,int operand2)
{ switch(operator)
{ case '+':return(operand2+operand1);
case '-':return(operand2-operand1);
case '*':return(operand2*operand1);
case '/':return(operand2/operand1); } }
答案补充void main()
{ char expression[50];
int position=0;
int op=0;
int operand1=0;
int operand2=0;
int evaluate=0;
printf("\nPlease input the inorder expression:");
gets(expression);
while(expression[position]!='\0'&&expression[position]!='\n')
{ if(is_operator(expression[position]))
{ if(!empty(operator))
while(priority(expression[position])<= priority(operator->data)&&
!empty(operator))
{ operand=pop(operand,&operand1);
答案补充#include<iostream> #include<math.h> using namespace std; main () { char k; double s,g; k='-'; cout<<"求绝对值请按A;求平方根请按B;加减乘除清分别按+-*/" <<endl; cin>>g; while (1) { cin>>k; if (k=='=') break; if (k=='A') { g=fabs (g); break; } if (k=='B') { g=sqrt (g); break; } cin>>s; if (k=='+') g+=s; if (k=='-') g-=s; if (k=='*') g*=s; if (k=='/') { if (s==0) cout<<"wrong"; else g/=s; } } cout<<g; }
上一个:有C++基础学JAVA会觉得难吗?
下一个:有一简单的电话号码查询的C++编程问题