Description
Convert decimal number to octal and output.
Input
The input contains several decimal positive integers.
Output
Output the corresponding octal numbers, one line each.
Sample Input
1
2
3
7
8
9
19
10020345
Sample Output
1
2
3
7
10
11
23
46162771
. . . . . . Very simple, paste the code
#include<stdio.h> #include<string.h> #include<malloc.h> typedef struct ElemType{ int x; }ElemType; typedef struct DataNode{ ElemType *top; ElemType *base; }SqStack; int InitStack(SqStack *S); int Push(SqStack *S,ElemType elem); int Pop(SqStack *S,ElemType *elem); int StackEmpty(SqStack S); intmain() { char ch[100]; while(gets(ch)) { if(ch[0]=='-')//If the number is negative, skip the negative sign first { ElementType element; int n; char *q=&ch[1]; sscanf(q,"%d",&n);//Read the string as an integer number SqStack S; InitStack(&S); while(n) { elem.x=n%8; Push(&S,elem);//Get the remaining pressure stack n=n/8; } printf("-");//print negative sign while(!StackEmpty(S))//Pop stack print { Pop(&S,&elem); } printf("\n"); } Else//The case of not being negative is the same as above { int n; ElementType element; sscanf(ch,"%d",&n); SqStack S; InitStack(&S); if(n==0) printf("0\n"); else { int a[100]; while(n) { elem.x=n%8; Push(&S,elem); n=n/8; } while(!StackEmpty(S)) { Pop(&S,&elem); printf("%d",elem.x); } printf("\n"); } } } } int InitStack(SqStack *S) { ElemType *p = (ElemType *)malloc(200 * sizeof(ElemType)); if(p == NULL) { return -1; } S->top = p; S->base = p; return 0; } int Push(SqStack *S,ElemType elem) { *(S->top) = elem; S->top += 1; return 0; } int Pop(SqStack *S,ElemType *elem) { if(S->top -S->base == 0) { return -1; } S->top -= 1; *elem = *(S->top) ; return 0; } int StackEmpty(SqStack S) { if(S.base == NULL) { return 0; } if(S.top - S.base == 0) { return 1; } return 0; }