C 语言 【栈的应用 数制转换】

 1 #include <stdio.h>
 2 #include <malloc.h>
 3 #include <process.h>
 4 #define OK 1
 5 #define STACK_INIT_SIZE 5
 6 #define STACKINCREMENT 5
 7 typedef int ElemType;
 8 
 9 typedef struct
10 {
11  
12  ElemType *base;
13  ElemType *top;
14  int stacksize;
15 }SqStack;
16 void InitStack(SqStack *S)
17 {
18   S->base=(ElemType *)malloc(STACK_INIT_SIZE*sizeof(ElemType)); //分配内存
19   if(!S->base)  //如果为空,则退出
20       exit(1);
21   S->top=S->base;
22   S->stacksize=STACK_INIT_SIZE;
23 }
24 int push(SqStack *S,ElemType e)/*顺序入栈*/
25 {
26   if(S->top-S->base>S->stacksize)//栈中的数据长度大于给定分配大小
27   {
28    S->base=(ElemType *)realloc(S->base,(S->stacksize+STACKINCREMENT)*sizeof(ElemType));//增加内存大小
29    if(!S->base)
30        exit(1);
31    S->top=S->base+S->stacksize;//将增加的长度给更新
32    S->stacksize+=STACKINCREMENT;//更新增加后的长度
33   }
34   *S->top=e;
35   S->top++;
36   return 1;
37 
38 }
39 ElemType pop(SqStack *S,ElemType *e)/*顺序出栈*/
40 {
41   if(S->top==S->base)  //出栈判断栈是否是空
42       printf("此时栈为空,不能出栈!\n");
43   *e=*--S->top;
44       return *e;
45 }
46 int StackEmpty(SqStack *S)/*判断顺序栈是否为空*/
47 {
48     if(S->top==S->base)
49         return 1;
50     else
51         return 0;
52   
53 }
54 void DestroyStack(SqStack *S)/*顺序栈销毁*/
55 {
56   free(S->top);
57 }
58 
59 void Conversion()/*数值转换*/
60 {
61  int n;
62  int m;
63  SqStack s;
64  ElemType e;
65  InitStack(&s);
66  printf("请输入带转换的数值:\n");
67  scanf("%d",&n);
68  printf("请输入要转化的数制:\n");
69  scanf("%d",&m);
70  while(n)
71  {
72      push(&s,n%m);
73      n=n/m;
74  }
75  while(!StackEmpty(&s))
76  {
77    pop(&s,&e);
78    printf("%d",e);
79 
80  }
81  printf("\n");
82  DestroyStack(&s);
83 
84 }
85 int main(void) /*程序入口*/
86 {
87  Conversion();
88  return OK;
89 }
 1 #include <stdio.h>
 2 #include <malloc.h>
 3 #include <process.h>
 4 #define OK 1
 5 #define STACK_INIT_SIZE 5
 6 #define STACKINCREMENT 5
 7 typedef int ElemType;
 8 
 9 typedef struct
10 {
11  
12  ElemType *base;
13  ElemType *top;
14  int stacksize;
15 }SqStack;
16 void InitStack(SqStack *S)
17 {
18   S->base=(ElemType *)malloc(STACK_INIT_SIZE*sizeof(ElemType)); //分配内存
19   if(!S->base)  //如果为空,则退出
20       exit(1);
21   S->top=S->base;
22   S->stacksize=STACK_INIT_SIZE;
23 }
24 int push(SqStack *S,ElemType e)/*顺序入栈*/
25 {
26   if(S->top-S->base>S->stacksize)//栈中的数据长度大于给定分配大小
27   {
28    S->base=(ElemType *)realloc(S->base,(S->stacksize+STACKINCREMENT)*sizeof(ElemType));//增加内存大小
29    if(!S->base)
30        exit(1);
31    S->top=S->base+S->stacksize;//将增加的长度给更新
32    S->stacksize+=STACKINCREMENT;//更新增加后的长度
33   }
34   *S->top=e;
35   S->top++;
36   return 1;
37 
38 }
39 ElemType pop(SqStack *S,ElemType *e)/*顺序出栈*/
40 {
41   if(S->top==S->base)  //出栈判断栈是否是空
42       printf("此时栈为空,不能出栈!\n");
43   *e=*--S->top;
44       return *e;
45 }
46 int StackEmpty(SqStack *S)/*判断顺序栈是否为空*/
47 {
48     if(S->top==S->base)
49         return 1;
50     else
51         return 0;
52   
53 }
54 void DestroyStack(SqStack *S)/*顺序栈销毁*/
55 {
56   free(S->top);
57 }
58 
59 void Conversion()/*数值转换*/
60 {
61  int n;
62  int m;
63  SqStack s;
64  ElemType e;
65  InitStack(&s);
66  printf("请输入带转换的数值:\n");
67  scanf("%d",&n);
68  printf("请输入要转化的数制:\n");
69  scanf("%d",&m);
70  while(n)
71  {
72      push(&s,n%m);
73      n=n/m;
74  }
75  while(!StackEmpty(&s))
76  {
77    pop(&s,&e);
78    printf("%d",e);
79 
80  }
81  printf("\n");
82  DestroyStack(&s);
83 
84 }
85 int main(void) /*程序入口*/
86 {
87  Conversion();
88  return OK;
89 }

 

posted @ 2018-09-24 13:45  Justice-V  阅读(527)  评论(0)    收藏  举报