Chinaunix首页 | 论坛 | 博客
  • 博客访问: 292464
  • 博文数量: 76
  • 博客积分: 0
  • 博客等级: 民兵
  • 技术积分: 715
  • 用 户 组: 普通用户
  • 注册时间: 2015-05-20 20:38
文章分类
文章存档

2016年(20)

2015年(56)

分类: 嵌入式

2016-02-29 19:53:02


  1. 栈的应用之数制转换
  2. 16年2月29日19:46:56
  3. /*
  4.  *==============================================================================
  5.  *
  6.  * Filename: 3.2.c
  7.  *
  8.  * Description: 数制转换
  9.  *
  10.  * Version: 1.0
  11.  * Created: 2016年02月29日 19时07分33秒
  12.  * Revision: none
  13.  * Compiler: gcc
  14.  *
  15.  * Author: ZER0 (ybx), 471685488@qq.com
  16.  * Organization: TJPU
  17.  *
  18.  *==============================================================================
  19.  */

  20. #include <stdio.h>
  21. #include <stdlib.h>
  22. #include <malloc.h>

  23. #define STACK_INIT_SIZE 10
  24. #define STACK_INCREMENT 2

  25. typedef struct {
  26.     int *top;
  27.     int *base;
  28.     int stacksize;
  29. }Stack;

  30. int * InitStack(Stack *S)
  31. {
  32.     S->base = (int *)malloc(sizeof(int) * STACK_INIT_SIZE);
  33.     if (!S->base)
  34.     {
  35.         printf("Can not malloc memory for Stack.\n");    
  36.         exit(-1);
  37.     }

  38.     S->top = S->base;
  39.     S->stacksize = STACK_INIT_SIZE;

  40.     return S->base;
  41. }

  42. void Push(Stack *S, int val)
  43. {
  44.     if ((S->top - S->base) > S->stacksize)
  45.     {
  46.         S->base = (int *)realloc(S->base, (S->stacksize + STACK_INCREMENT) * sizeof(int));
  47.         if (!S->base)
  48.         {
  49.             printf("Can not realloc memory for Stack.\n");    
  50.             exit(-1);
  51.         }    

  52.         S->top = S->base + S->stacksize;
  53.         S->stacksize += STACK_INCREMENT;
  54.     }

  55.     *(S->top) = val;
  56.     S->top++;
  57. }

  58. int Pop(Stack *S, int *val)
  59. {
  60.     if (S->top == S->base)
  61.     {
  62.         printf("The stack is empty.\n");
  63.         exit(-1);
  64.     }

  65.     *val = *(--S->top);
  66.     return 1;
  67. }

  68. void TraverseStack(Stack S)
  69. {
  70.     while (S.top > S.base)
  71.     {
  72.         printf("%d ", *--S.top);
  73.     }

  74.     printf("\n");
  75. }

  76. int main(int argc, char const *argv[])
  77. {
  78.     Stack S;
  79.     int n, N;
  80.     int val;

  81.     InitStack(&S);

  82.     printf("Please input the number you want to change: \n");
  83.     scanf("%u", &n);

  84.     printf("Please input the decimal you want change to : (2/8/10/16)\n");
  85.     scanf("%u", &N);

  86.     while(n)
  87.     {
  88.         Push(&S, n % N);
  89.         n = n / N;
  90.     }

  91.     printf("The answer is :\n");

  92.     while(!(S.top == S.base))
  93.     {
  94.         Pop(&S, &val);
  95.         if (val <= 9)
  96.         {
  97.             printf("%d", val);    
  98.         }
  99.         else
  100.         {
  101.             printf("%c", val + 55);
  102.         }
  103.     }

  104.     printf("\n");

  105.     return 0;
  106. }

  107. 关于数制转换的问题,最好的解决方法就是通过栈的方式,因为仔细思考数学上面是怎么计算数值之间的进制转换的??比如说想要到十进制的123转换成二进制,怎么求这个二进制呢??用123不断地除以2,直到0为止,然后从后往前输出每一个得数。这样的方式正好符合栈的性质:先进后出。以上就是源程序,对于16进制,我们把大于10的数用字母来表示。

阅读(1495) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~