关于 后缀表达式的运算问题
我想计算3+6的和 怎么结果是105呢?#include <iostream.h>#define MAX 100
typedefstruct
{
intdata;
inttop;
}SeqStack;
SeqStack *Init_SeqStack()
{
SeqStack*s;
s=new SeqStack;
if (!s)
{
return NULL;
}
else
{
s->top=-1;
return s;
}
}
int Empty(SeqStack *s){
if (s->top==-1)
return 1;
else
return 0;
}
int Push_SeqStack(SeqStack *s,intx){
if (s->top==MAX-1)
return 0;
else
{ s->top++;
s->data=x;
return 1;
}
}
int Pop_SeqStack(SeqStack *s,int *x){
if (Empty(s))
return 0;
else
{ *x=s->data;
s->top--;
return 1;
}
}
int GetTop(SeqStack *s)
{
return(s->data);
}
/*****/
double calcul_exp(char *A)
{
SeqStack *s;
char ch ;
s = Init_SeqStack();
int a = 0,b = 0,c = 0;
while(ch != '#')
{
if(ch != '+' && ch != '-' &&ch != '*' &&ch != '/' )
Push_SeqStack(s,ch);
else
{
Pop_SeqStack(s,&b);
Pop_SeqStack(s,&a);
switch(ch)
{
case '+': c = a + b;break;
case '-': c = a - b;break;
case '*': c = a * b;break;
case '/': c = a / b;break;
}
Push_SeqStack(s,c);
}
ch = *A++;
}
Pop_SeqStack(s,&c);
return c;
}
void main()
{
char a = "36+#";
double mn = calcul_exp(a);
cout << mn;
}
有几处问题,在你的基础上改了一下,仅供参考:
#include <iostream>
using namespace std;
#define MAX 100
typedefstruct
{
intdata;
inttop;
}SeqStack;
SeqStack *Init_SeqStack()
{
SeqStack*s;
s=new SeqStack;
if (!s)
{
return NULL;
}
else
{
s->top=-1;
return s;
}
}
int Empty(SeqStack *s){
if (s->top==-1)
return 1;
else
return 0;
}
int Push_SeqStack(SeqStack *s,intx){
if (s->top==MAX-1)
return 0;
else
{
s->top++;
s->data=x;
return 1;
}
}
int Pop_SeqStack(SeqStack *s,int *x){
if (Empty(s))
return 0;
else
{
*x=s->data;
s->top--;
return 1;
}
}
int GetTop(SeqStack *s)
{
return(s->data);
}
/*****/
double calcul_exp(char *A)
{
SeqStack *s;
char ch ;
s = Init_SeqStack();
int a = 0,b = 0,c = 0;
ch=*A;
while(ch != '#')
{
if(ch != '+' && ch != '-' &&ch != '*' &&ch != '/' )
{
int i = atoi((const char *)&ch);
Push_SeqStack(s,i);
}
else
{
Pop_SeqStack(s,&b);
Pop_SeqStack(s,&a);
switch(ch)
{
case '+': c = a + b;break;
case '-': c = a - b;break;
case '*': c = a * b;break;
case '/': c = a / b;break;
}
Push_SeqStack(s,c);
}
A++;
ch = *A;
}
Pop_SeqStack(s,&c);
return c;
}
void main()
{
char a = "36+#";
double mn = calcul_exp(a);
cout << mn;
getchar();
}
小新110 发表于 2013-4-24 13:36 static/image/common/back.gif
有几处问题,在你的基础上改了一下,仅供参考:
int i = atoi((const char *)&ch); 这句是什么意思呢? 小新110 发表于 2013-4-24 13:36 static/image/common/back.gif
有几处问题,在你的基础上改了一下,仅供参考:
if(ch != '+' && ch != '-' &&ch != '*' &&ch != '/' )哦 我知道了 把 改成if (ch != 字符)就可以了吧。。。。。。。。
页:
[1]