您好,欢迎来到意榕旅游网。
搜索
您的当前位置:首页栈的问题

栈的问题

来源:意榕旅游网
#include #include #include

#define MAX 20

typedef struct stackNum {

double m_data[MAX]; int p_now; }stackNum;

typedef struct stackOper {

int m_data[MAX]; int p_now; }stackOper;

void initstack(); /*初始化堆栈*/

int Make_str(); /*语法检查并计算*/ int push_num(double num); /*将操作数压入堆栈*/ int procede(int operate); /*处理操作码*/

int change_opnd(int operate); /*将字符型操作码转换成优先级*/ int push_opnd(int operate); /*将操作码压入堆栈*/ int pop_opnd(); /*将操作码弹出堆栈*/

double caculate(int cur_opnd); /*简单计算+,-,*,/*/ double pop_num(); /*弹出操作数*/

void error(int number); int seekOper();

int isDigit(char ch); int isOper(char oper);

stackNum StackOfNum; stackOper StackOfOper; char buf[MAX*5]; double number; int HaveResult;

int main() {

initstack(); gets(buf); if(Make_str()) {

printf(\"结果为\%lf\\n\ }

return 0; }

void initstack() {

StackOfNum.p_now = 0; StackOfOper.p_now = 0; }

int Make_str() {

int process = 0; int numberLength; double temp;

char NumberTemp[20]; int size = strlen(buf); HaveResult = 1;

while(process < size) {

if(isDigit(buf[process])) {

numberLength = 1;

while(isDigit(buf[process + numberLength])) {

++numberLength; }

strncpy(NumberTemp,buf+process,numberLength); NumberTemp[numberLength] = '\\0'; temp = atof(NumberTemp); push_num(temp);

process += numberLength; }

else if(isOper(buf[process])) {

procede(buf[process++]); } else {

error(1); return 0; } }

while(StackOfOper.p_now != 0 && HaveResult) {

push_num(caculate(pop_opnd())); }

number = pop_num(); return HaveResult; }

void error(int number) {

if(1 == number) {

printf(\"有非法字符\\n\"); }

else if(2 == number) {

printf(\"表达式不合法\"); }

else if(3 == number) {

printf(\"表达式过长\"); }

HaveResult = 0; }

int seekOper() {

if( 0 == StackOfOper.p_now ) {

error(2); return 0; }

return StackOfOper.m_data[StackOfOper.p_now - 1]; }

int isDigit(char ch) {

if('0' <= ch && ch <= '9') {

return 1; }

if('.' == ch) {

return 1; }

return 0; }

int isOper(char oper) {

if('+' == oper || '-' == oper || '*' == oper || '/' == oper) {

return 1; }

return 0; }

int push_num(double num) {

if(StackOfNum.p_now == MAX) {

error(3); return 0; }

StackOfNum.m_data[StackOfNum.p_now++] = num; return 1; }

int push_opnd(int operate) {

if(StackOfOper.p_now == MAX) {

error(3); return 0; }

StackOfOper.m_data[StackOfOper.p_now++] = operate; return 1; }

int pop_opnd() {

if(StackOfOper.p_now == 0) {

error(2); return 0; }

return StackOfOper.m_data[--StackOfOper.p_now]; }

double pop_num()

{

if(StackOfNum.p_now == 0) {

error(2); return 0; }

return StackOfNum.m_data[--StackOfNum.p_now]; }

double caculate(int cur_opnd) {

double left,right; right = pop_num(); left = pop_num(); switch(cur_opnd) {

case '+': {

return left + right; } case '-': {

return left - right; } case '*': {

return left * right; } case '/': {

return left / right; } }

return 0; }

int procede(int operate) {

if(StackOfOper.p_now == 0) {

push_opnd(operate); return 1; } else {

if(change_opnd(seekOper()) > change_opnd(operate)) {

push_num(caculate(pop_opnd())); }

push_opnd(operate); return 1; }

return 0; }

int change_opnd(int operate) {

if('+' == operate || '-' == operate) {

return 1; }

else if('*' == operate || '/' == operate) {

return 2; }

return 0; }

因篇幅问题不能全部显示,请点此查看更多更全内容

Copyright © 2019- yrrf.cn 版权所有

违法及侵权请联系:TEL:199 1889 7713 E-MAIL:2724546146@qq.com

本站由北京市万商天勤律师事务所王兴未律师提供法律服务