1. 程式人生 > >資料結構 棧 數值轉換

資料結構 棧 數值轉換

新手,可能會有問題,歡迎指正

#include <iostream>
#include <cstdio>
#include <malloc.h>
#define STACK_INIT_SIZE 100
#define STACKINCREMENT 10
#define OVERFLOW -2
#define OK 1
#define ERROR 0
#define TRUE 1
#define FALSE 0
using namespace std;
typedef int SELemType;
typedef struct{
   SELemType *
base; SELemType *top; int stacksize; }sqstack; typedef int status; status initstack(sqstack&s){ s.base=(SELemType * )malloc(STACK_INIT_SIZE * sizeof(SELemType)); if(!s.base) exit(OVERFLOW); s.top=s.base; s.stacksize=STACK_INIT_SIZE; return OK; } status push(sqstack&
s,SELemType e) { if(s.top-s.base>=s.stacksize) { s.base=(SELemType *)realloc(s.base,(s.stacksize+STACKINCREMENT)*sizeof(SELemType)); if(!s.base) exit(OVERFLOW); s.top=s.base+s.stacksize; s.stacksize+=STACKINCREMENT; } *(s.top++)=e; return OK; }
status pop(sqstack &s,SELemType &e) { if(s.top==s.base) return ERROR; e=*--s.top; return OK; } status stackempty(sqstack s) { if(s.top==s.base) return true; else return false; } void conversion(){ sqstack s; initstack(s); int a; SELemType e; while(cin>>a) { while(a) { push(s,a%8); a=a/8; } while(!stackempty(s)) { pop(s,e); cout<<e; } cout<<endl; } } int main() { conversion(); return 0; }