数据结构 栈与队列

#include<iostream.h>

#include<stdlib.h>

#include<math.h>

#define MAXSIZE 20

#define ERROR 0

#define OVERFLOW 0

#define OK 1

typedef int SElemType;

typedef int status;

typedef struct{

 SElemType *base;

 SElemType *top;

 int stacksize;

}Sqstack;

status InitStack(Sqstack &S){

 S.base=new SElemType[MAXSIZE];

 if(!S.base)exit(OVERFLOW);

 S.top=S.base;

 S.stacksize=MAXSIZE;

 return OK;

}

status Push(Sqstack &S,SElemType &e){

 if(S.top-S.base==S.stacksize)return ERROR;

 *S.top++=e;

 return OK;

}

status Pop(Sqstack &S,SElemType &e){

 if(S.top==S.base)return ERROR;

 e=*--S.top;

 return e;

}


void main()

{

 Sqstack S;

 InitStack(S);

 SElemType e;

 SElemType E;

 cout<<"20-8软工2-9-115"<<endl;

 cout<<"请输入一个10进制数"<<endl;

 cin>>e;

 while(e){

 E=e%8;

 Push(S,E);

 e=e/8;}

 cout<<"转化成为8进制后为"<<endl;

 while(S.base!=S.top)

 {

 e=Pop(S,e);

 cout<<e;

 }

 cout<<endl;

 cout<<"请输入一个8进制数"<<endl;

 cin>>e;

 int i=0;

 while(e){

 E=(e%10)*pow(8,i);

 Push(S,E);

 e=e/10;

 i++;

 }

 cout<<"转化成为10进制后为"<<endl;

 i=0;

 while(S.base!=S.top)

 {

 e=Pop(S,e);

 i=e+i;

 }

 cout<<i;

 cout<<endl;

}

 

posted @ 2022-07-19 09:06  霖霖的信箱  阅读(23)  评论(0)    收藏  举报