c语言程序运行时提示出现了一个问题,导致程序停止工作,求大神指点
就是用堆栈做计算#include"stdafx.h"#include"stacknum.h"#include"StackOperator.h"intpriority(ch...
就是用堆栈做计算
#include "stdafx.h"
#include "stacknum.h"
#include "StackOperator.h"
int priority(char inner, char outer)
{
if(inner=='-' && outer== '*')
return 1;
else if(inner=='-' && outer=='/')
return 1;
else if (inner=='+' && outer=='*')
return 1;
else if (inner=='+' && outer=='/')
return 1;
else return 0;
}
int main(int argc, char* argv[])
int c;
int a,b;
char exp[100];
int i;
StackNum stackNum;
StackOperator stackOp;
printf("%4d", stackNum.IsEmpty());
printf("Enter an expression ");
scanf("%s", exp);
i = 0;
while(exp[i] != '\0') {
if(exp[i] >= '0' && exp[i] <= '9' )
stackNum.Push(exp[i] - '0');
else if (stackOp.IsEmpty())
stackOp.Push(exp[i]);
else {
char op = stackOp.Pop();
int res = 0;
res = priority(op, exp[i]);
if ( res == 1){
stackOp.Push(op);
stackOp.Push(exp[i]);
}
else {a=stackNum.Pop();
b=stackNum.Pop();
if( op='+')
stackOp.Push(a+b);
else if(op='-')
stackOp.Push(a-b);
else if(op='*')
stackOp.Push(a*b);
else stackOp.Push(a/b);
stackOp.Push(exp[i]);
}
}
i++;
}
char op = stackOp.Pop();
a=stackNum.Pop();
b=stackNum.Pop();
if( op='+')
stackOp.Push(a+b);
else if(op='-')
stackOp.Push(a-b);
else if(op='*')
stackOp.Push(a*b);
else stackOp.Push(a/b);
stackOp.Push(exp[i]);
c=stackNum.Pop();
printf("%d\n",c);
printf("Hello World!\n");
return 0;
}堆栈都是做好了的 展开
#include "stdafx.h"
#include "stacknum.h"
#include "StackOperator.h"
int priority(char inner, char outer)
{
if(inner=='-' && outer== '*')
return 1;
else if(inner=='-' && outer=='/')
return 1;
else if (inner=='+' && outer=='*')
return 1;
else if (inner=='+' && outer=='/')
return 1;
else return 0;
}
int main(int argc, char* argv[])
int c;
int a,b;
char exp[100];
int i;
StackNum stackNum;
StackOperator stackOp;
printf("%4d", stackNum.IsEmpty());
printf("Enter an expression ");
scanf("%s", exp);
i = 0;
while(exp[i] != '\0') {
if(exp[i] >= '0' && exp[i] <= '9' )
stackNum.Push(exp[i] - '0');
else if (stackOp.IsEmpty())
stackOp.Push(exp[i]);
else {
char op = stackOp.Pop();
int res = 0;
res = priority(op, exp[i]);
if ( res == 1){
stackOp.Push(op);
stackOp.Push(exp[i]);
}
else {a=stackNum.Pop();
b=stackNum.Pop();
if( op='+')
stackOp.Push(a+b);
else if(op='-')
stackOp.Push(a-b);
else if(op='*')
stackOp.Push(a*b);
else stackOp.Push(a/b);
stackOp.Push(exp[i]);
}
}
i++;
}
char op = stackOp.Pop();
a=stackNum.Pop();
b=stackNum.Pop();
if( op='+')
stackOp.Push(a+b);
else if(op='-')
stackOp.Push(a-b);
else if(op='*')
stackOp.Push(a*b);
else stackOp.Push(a/b);
stackOp.Push(exp[i]);
c=stackNum.Pop();
printf("%d\n",c);
printf("Hello World!\n");
return 0;
}堆栈都是做好了的 展开
1个回答
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询
广告 您可能关注的内容 |