65,210
社区成员
发帖
与我相关
我的任务
分享//.........class.h.....类的定义
#ifndef _C_H
#define _C_H
#define STACK_INIT_SIZE 100
#define STACKINCR 10
#include <iostream>
using namespace std;
class C
{
public:
typedef struct
{
char *base;
char *top;
int stacksize;
}sqstackc;//操作符栈
//sqstackc c;//一个顺序栈???????????????????????????
public:
sqstackc c;//一个顺序栈
C();//构造函数,初始化一个空栈
char gettop(sqstackc c);//取栈顶元素
void push(sqstackc &c ,char e);//入栈
char pop(sqstackc &c);//删除栈顶元素
};
class S
{
public:
typedef struct
{
int *base;
int *top;
int stacksize;
}sqstacks;//操作数栈
//sqstacks s;////////////??????????????????????
public:
sqstacks s;
S();//构造函数,初始化一个空栈
int gettop(sqstacks s);//取栈顶元素
//void push(sqstacks &s ,char e);//入栈
void push(sqstacks &s ,int e); //定义中为int 类型
int pop(sqstacks &s);//删除栈顶元素
};
#endif
// TestToday.cpp : Defines the entry point for the console application.
//
//........class.cpp.......类的操作实现
#include "class.h"
#define ERROR -1 //定义ERROR宏
//...........................C类的成员函数的实现机理.......................................
C::C()//在这里是建立一个空栈
{
//c.base=(int *)malloc(STACK_INIT_SIZE*sizeof(int));
//类型不匹配
c.base=(char *)malloc(STACK_INIT_SIZE*sizeof(int));
if(!c.base)
{
cout <<"内存不够";
throw;
}
c.top=c.base;
c.stacksize=STACK_INIT_SIZE;
}
char C::gettop(sqstackc c)
{
if(c.base==c.top)
return 0;
//return *(zf.c.top-1);///////////////////////////////////////////////////////
//zf不能使用在类中
return *(c.top-1);
}
void C::push(sqstackc &c ,char e)//入栈
{
if(c.top-c.base>=c.stacksize)
{//栈满,追加存储空间
c.base=(char *)realloc(c.base,(c.stacksize+STACKINCR)*sizeof(char));
//if(!c.base)exit(OVERFLOW);
cout <<"内存不够";
c.stacksize+=STACKINCR;
//C类中无法直接调用S类的成员,是否是拼写错误
//c.top=s.base+c.stacksize;////////
c.top=c.base+c.stacksize;////////
}
//C类中无法直接调用S类的成员,是否是拼写错误
//*s.top++=e;//先利用s.top再将s.top增加一
*c.top++=e;//先利用s.top再将s.top增加一
}
//没有pop的定义
char C::pop(sqstackc &c)//删除栈顶元素
{
if(c.top==c.base)
return ERROR ;
return *--c.top;
}
//.............S类成员函数的实现机理.......................
S::S()
{
s.base=(int *)malloc(STACK_INIT_SIZE*sizeof(int));
if(!s.base)
//return(OVERFLOW);
throw;
s.top=s.base;
s.stacksize=STACK_INIT_SIZE;
}
int S::gettop(sqstacks s)
{
if(s.base==s.top)
return ERROR;
return *(s.top-1);
}//只用返回栈顶元素
void S::push(sqstacks &s ,int e)//入栈
{
if(s.top-s.base>=s.stacksize)
{//栈满,追加存储空间
s.base=(int *)realloc(s.base,(s.stacksize+STACKINCR)*sizeof(int));
if(!s.base)
//exit(OVERFLOW);
exit(0);
s.stacksize+=STACKINCR;
s.top=s.base+s.stacksize;////////
}
*s.top++=e;//先利用s.top再将s.top增加一
}
int S::pop(sqstacks &s )//删除栈顶元素
{
if(s.top==s.base)
return ERROR ;
return *--s.top;
}
//...........main.cpp..
#include <iostream>
#include "class.h"
using namespace std;
int in(char c)//[判断读入的字符是不是操作符,要是则返回1,否则返回0
{
switch (c)
{
case '>':
case ' <':
case '+':
case '-':
case '*':
case '/':
case ')':
case '(':
case '#':return 1;break;
default :return 0;break;
}
}
char precede (char a,char b)//操作符的比较
{
int la,lb;
switch (a)
{
case '*': la=4;break;
case '/': la=4;break;
case '+': la=2;break;
case '-': la=2;break;
default :break;
}
switch (b)
{
case '*': lb=3; break;
case '/': lb=3;break;
case '+': lb=1;break;
case '-':lb=1;break;
default: break;
}
if(la>lb)return '>';
if(la<lb)return '<';
return 0;
}
int operate(int a,char theta,int b)//
{
if(theta=='+')
return (a+b);
if(theta=='-')
return (a-b);
if(theta=='*')
return (a*b);
if(theta=='/')
return (char)(a/b);
return -1;
}
void evaluateexpression()
{
char theta,x,c,ch;
int a,b;
C zf,q;
S sz;//定义两个类对象
zf.push(zf.c,'#');
//push(opf,'#');
ch=getchar();//从键盘中输入....
printf("第一个输入的字符为%c",ch);//////////////////////////////////////////////////////////////////////////////////
while(ch!='#'||zf.gettop(zf.c)!='#')
// while(ch!='\n')
{
static int j=0;////////////////////
if(ch>=48&&ch <=57)
{
q.push(q.c,ch);//...入栈...到Q中
printf("此时Q中的数据为:%d\n",zf.gettop(zf.c));
ch=getchar();
}
else
{
if(q.c.base=q.c.top)//(Q.base!=Q.top)
{
for(int i=1,c='\0';q.c.base!=q.c.top;i*=10)//将读入的字符变成数字
//c=c+i*((q.pop)%48);
c+=q.pop(q.c)%48;
printf("此时的数据为c为:%d\n",c);
printf("准备入栈的数据为:%d\n",c);/////////////////
//push(ops,c);
sz.push(sz.s,c);
sz.push(sz.s,c);
printf("此时ops中第%d个数据为:%d\n",++j,sz.gettop(sz.s));////
}
else
{
c=ch;
printf("等待进入的字符: %c\n",c);//////////检
switch(precede(sz.gettop(sz.s),c))
{
case ' <':
//push(opf,c);/////"(" <"-"
zf.push(zf.c,c);
printf("此时opf栈顶的为:%c\n",zf.gettop(zf.c));////////???
printf("此时与opf栈顶比较字符为:%c\n",c);///
ch=getchar();
break;
case '=':
x=zf.pop(zf.c);
printf("....................相等的操作符为:%c\n",x);////检查
ch=getchar();
break;
case '>':
//theta=zf.pop(sz.s);
theta=zf.pop(zf.c);
printf("theta=%c\n",theta);//////////////////
b=sz.pop(sz.s);
printf("b==%d\n",b);//
a=sz.pop(sz.s);
printf("a==%d\n",a);//
printf("a*b%48=%d",a*b%48);////////////////?
//ops.base==ops.top
////
//rintf("完了啊!!");
sz.push(sz.s,operate(a,theta,b));
// ch=operate(a,theta,b);/////
printf("刚计算完入栈ops的数据为:%d\n",sz.gettop(sz.s));////
break;
}
}
}
}
printf("%d\n",sz.pop(sz.s));
}
int main()
{
evaluateexpression();
return 1;
}