进制转换问题
1 #include2 #include 3 4 #define STACK_SIZE 100 5 #define STCK_INCREMENT 10 6 7 typedef struct 8 { 9 int *base;10 int *top;11 int stacksize;12 }SqStack;13 14 void InitStack(SqStack &S)15 {16 //为栈申请空间17 S.base = (int*)malloc(STACK_SIZE * sizeof(int));18 if (!S.base)19 {20 exit(-2);21 }22 S.top = S.base;23 S.stacksize = STACK_SIZE;24 }25 26 27 void Push(SqStack &S, int e)28 {29 if ((S.top - S.stacksize) >= S.base) //栈满追加空间30 {31 S.base = (int*)realloc(S.base, (S.stacksize + STCK_INCREMENT) * sizeof(int));32 if (!S.base)33 {34 exit(-2);35 }36 S.top = S.base + S.stacksize;37 S.stacksize += STCK_INCREMENT;38 }39 40 *S.top++ = e;41 }42 43 int Pop(SqStack &S, int &e)44 {45 if (S.base == S.top)46 {47 return 0;48 }49 e = *--S.top;50 return 1;51 }52 53 54 int main()55 {56 int n, e;57 scanf("%d", &n);58 SqStack S;59 InitStack(S);60 while (n)61 {62 Push(S, n % 2);63 n = n / 2;64 }65 66 while (1)67 {68 int status = Pop(S, e);69 if (!status)70 {71 break;72 }73 printf("%d", e);74 }75 return 0;76 }
方法二:
1 #include2 #include 3 4 using namespace std; 5 6 int main() 7 { 8 stack mstack; 9 10 int n;11 cin>>n;12 while(n)13 {14 mstack.push(n%2);15 n = n/2;16 }17 18 while(!mstack.empty())19 {20 cout<