数据结构 进栈 出栈 C
发布网友
发布时间:2022-05-16 01:59
我来回答
共1个回答
热心网友
时间:2023-10-08 23:05
// Expssion1.cpp : Defines the entry point for the console application.
//
#include "stdafx.h"
#include<iostream.h>
#include "stdio.h" /*C语言头文件*/
#include "string.h"
#include "ctype.h"
#include "math.h"
#include "malloc.h"
#include "process.h"
#define N 100
#define OK 1
#define Error 0
double result;
char ch[N];
typedef enum
{Empty=2,Lbrace=3,Rbrace=4,
Plus=5,Minus=6, Divide=7,
Times=8,End=9} Operator;
typedef struct op_stack{
double *base;
double *top;
double stacklength;
}O_stack;
typedef struct Num_stack{
double *base;
double *top;
double stacklength;
}N_stack;
N_stack L;
O_stack L1;
void initstack1(O_stack &L1)
{ L1.stacklength=0;
L1.base=(double *)calloc(N/2+1,sizeof(double));
if(!L1.base) {printf("no memery!"); exit (0); }
L1.top=L1.base;
cout<<"chu shi hua O"<<endl; return;
}
void initstack(N_stack &L)
{ L.stacklength=0;
L.base=(double *)calloc(N/2,sizeof(double));
if(!L.base) { printf("no memery!"); exit(0); }
L.top=L.base;
cout<<"chu shi hua N"<<endl; return;
}
int push_num(N_stack &L,double num)
{
cout<<"push num"<<endl;
if(L.top-L.base>=L.stacklength)
{
L.base=(double*)calloc(N/2,sizeof(double));
if(!L.base)exit(OVERFLOW);
L.top=L.base+N/2;
L.stacklength+=N/2;
}
*L.top++=num; cout<<num<<"yi jin ya ru"<<endl;
return OK;
}
int pop_num(N_stack &L)
{
int e;
cout<<"pop num"<<endl;
if(L.top==L.base) return Error;
e=*--L.top;
cout<<e<<endl;
return e;
}
int push_opnd(O_stack &L1,int operate)
{
cout<<"push opnd"<<endl;
if(L1.top-L1.base>=L1.stacklength)
{
L1.base=(double*)calloc(N/2,sizeof(double));
if(!L1.base)exit(OVERFLOW);
L1.top=L.base + N/2;
L1.stacklength+=N/2+1;
}
*L1.top++=operate;
cout<<operate<<endl;
return OK;
}
int pop_opnd(O_stack L1)
{ cout<<"pop opnd"<<endl;
int e;
if(L1.top==L1.base) return Error;
e=*--L1.top;
cout<<e<<endl;
return e;
}
double caculate(int cur_opnd)
{ double num1,num2;
num1=pop_num(L);
num2=pop_num(L);cout<<"da"<<num1<<" "<<num2<<endl;
if(L1.base>=L1.top)
return Error;
//cur_opnd=pop_opnd(L1);
cout<<"dasd"<<cur_opnd<<endl;
switch(cur_opnd){
case Plus:
result=num1+num2;//pop_opnd() ;
break;
case Minus:
result=num2-num1;//pop_opnd() ;
break;
case Divide:
result=num2/num1;//pop_opnd() ;
break;
case Times:
result=num1*num2;//pop_opnd() ;
break;
default:
return Error;
} push_num(L,result);//为什么呀不进去呢?
cout<<result<<endl;
return OK;
}
int change_opnd(int op) { /*将字符串转换成对应的数字*/
switch(op) {
case '+': op=Plus; /*处理加号*/
break;
case '-': op=Minus; /*处理减号*/
break;
case '*': op=Times; /*处理乘号*/
break;
case '/': op=Divide; /*处理除号*/
break ;
case '(': /*处理左括号*/
op=Lbrace;
break;
case ')': /*处理右括号*/
op=Rbrace;
break;
case '#': /*处理结束符*/
op=End;
break;
}
return op;
}
int Make_str() { /*语法检查,并计算表达式的值*/
int i=0,j=0,dot=0;
cout<<"jiru"<<endl;
gets(ch);
char str_num[32];
char s;
while(ch[i]!='\0'){ /*若字符串没有结束,则继续循环*/
cout<<"pan zi fu"<<endl;
if(isdigit(ch[i])){ /*若是数字,则进行数字处理*/
while(isdigit(ch[i])){ /*取连续的数字*/
str_num[j]=ch[i];
i++; j++;
cout<<"msdn"<<endl;
if(ch[i]=='.'){ /*数字后是小数点*/
str_num[j]=ch[i]; /*计算小数点个数*/
i++; j++; dot++;
}
if(dot>1) /*若小数点个数大于1,则出错*/
return Error;
}
str_num[j]='\0'; /*在字符串数字后加\0*/
push_num(L,atof(str_num)); /* 将该数字压入数字堆栈*/
cout<<"ya ru zi fu"<<endl;
j=0; /*计数器复零*/
dot=0;
i--;
}
else if(ch[i]=='(' || ch[i]=='+' || ch[i]=='-' || ch[i] == '*'|| ch[i]=='/' || ch[i]==')' || ch[i]=='#'){
s=change_opnd(ch[i]);
push_opnd(L1,s);
} /*若是规定的运算符,则转换成数字,并压入堆栈并判断*/
else /*若是其它的字符,则为非法*/
return Error;
i++; /*继续判断下一个字符*/
}
return OK;
}
main() {
initstack(L);
initstack1( L1);
int s;
printf("Please input a expression:\n");
Make_str();/*判断合法性,并计算结果*/
while(L1.base!=L1.top)
{
s=pop_opnd(L1);
caculate(s);}
cout<<"fan hui";
printf("results of this expression=%f\n",result);
/*正确时输出结果*/
return OK;
}