Chinaunix首页 | 论坛 | 博客
  • 博客访问: 1184720
  • 博文数量: 573
  • 博客积分: 0
  • 博客等级: 民兵
  • 技术积分: 66
  • 用 户 组: 普通用户
  • 注册时间: 2016-06-28 16:21
文章分类

全部博文(573)

文章存档

2018年(3)

2016年(48)

2015年(522)

分类: C/C++

2015-12-02 14:53:54


点击(此处)折叠或打开

  1. #include <string.h>
  2. #include <stdio.h>
  3. #include <stdlib.h>
  4. #include <math.h>
  5. #include <assert.h>

  6. //编译:cc shuzhi_conversion.c -lm

  7. #define STACK_INIT_SIZE 10 /*栈空间的初始分配量*/
  8. #define STACKINCREAMENT 5 /*栈空间的分配增量*/

  9. typedef char eletype;

  10. /*定义顺序栈的结构体:描述了栈的所有要素*/
  11. typedef struct stack
  12. {
  13.     eletype * base; /*栈底指针*/
  14.     eletype * top; /*栈顶指针*/
  15.     int stacksize; /*记录栈的大小*/
  16. }STACK;
  17. typedef STACK * StackPtr;

  18. int InitStack(StackPtr stackptr);
  19. int StackIsEmpty(STACK stack);
  20. int Push(StackPtr stackptr, eletype ele);
  21. int Pop(StackPtr stackptr, eletype * ele);
  22. int GetTop(STACK stack, eletype * ele);
  23. int PrintStack(STACK stack);
  24. int StackLen(STACK stack);
  25. int DestroyStack(StackPtr stackptr);

  26. int conversion(int srcflag, char * src, int destflag, char * dest);
  27. int conversion_n_to_10(int srcflag, char * src, int destflag, char * dest);
  28. int conversion_10_to_n(int srcflag, char * src, int destflag, char * dest);

  29. int main(int argc, char ** argv)
  30. {
  31.     //freopen("shuzhi_conversion.txt", "w", stdout);
  32.     int srcflag;
  33.     int destflag;
  34.     char src[1024];
  35.     char dest[10242];
  36.     
  37.     printf("********************************************************************\n");
  38.     
  39.     srcflag = 10;
  40.     destflag = 16;
  41.     memset(src, 0, sizeof(src));
  42.     strcpy(src, "255");
  43.     memset(dest, 0 , sizeof(dest));
  44.     conversion(srcflag, src, destflag, dest);
  45.     printf("%d进制数=[%s], 转化为,%d进制数=[%s]\n", srcflag, src, destflag, dest);
  46.     
  47.     srcflag = 10;
  48.     destflag = 2;
  49.     memset(src, 0, sizeof(src));
  50.     strcpy(src, "34818");
  51.     memset(dest, 0 , sizeof(dest));
  52.     conversion(srcflag, src, destflag, dest);
  53.     printf("%d进制数=[%s], 转化为,%d进制数=[%s]\n", srcflag, src, destflag, dest);
  54.     
  55.     srcflag = 10;
  56.     destflag = 2;
  57.     memset(src, 0, sizeof(src));
  58.     strcpy(src, "2048");
  59.     memset(dest, 0 , sizeof(dest));
  60.     conversion(srcflag, src, destflag, dest);
  61.     printf("%d进制数=[%s], 转化为,%d进制数 =[%s]\n", srcflag, src, destflag, dest);
  62.     
  63.     srcflag = 10;
  64.     destflag = 2;
  65.     memset(src, 0, sizeof(src));
  66.     strcpy(src, "32770");
  67.     memset(dest, 0 , sizeof(dest));
  68.     conversion(srcflag, src, destflag, dest);
  69.     printf("%d进制数=[%s], 转化为,%d进制数=[%s]\n", srcflag, src, destflag, dest);
  70.     
  71.     printf("********************************************************************\n");
  72.     
  73.     srcflag = 16;
  74.     destflag = 10;
  75.     memset(src, 0, sizeof(src));
  76.     strcpy(src, "FF");
  77.     memset(dest, 0 , sizeof(dest));
  78.     conversion(srcflag, src, destflag, dest);
  79.     printf("%d进制数=[%s], 转化为,%d进制数=[%s]\n", srcflag, src, destflag, dest);
  80.     
  81.     srcflag = 9;
  82.     destflag = 10;
  83.     memset(src, 0, sizeof(src));
  84.     strcpy(src, "13");
  85.     memset(dest, 0 , sizeof(dest));
  86.     conversion(srcflag, src, destflag, dest);
  87.     printf("%d进制数=[%s], 转化为,%d进制数=[%s]\n", srcflag, src, destflag, dest);
  88.     
  89.     srcflag = 8;
  90.     destflag = 10;
  91.     memset(src, 0, sizeof(src));
  92.     strcpy(src, "12");
  93.     memset(dest, 0 , sizeof(dest));
  94.     conversion(srcflag, src, destflag, dest);
  95.     printf("%d进制数=[%s], 转化为,%d进制数=[%s]\n", srcflag, src, destflag, dest);
  96.     
  97.     srcflag = 2;
  98.     destflag = 10;
  99.     memset(src, 0, sizeof(src));
  100.     strcpy(src, "11111111111111111111111111111111");
  101.     memset(dest, 0 , sizeof(dest));
  102.     conversion(srcflag, src, destflag, dest);
  103.     printf("%d进制数=[%s], 转化为,%d进制数=[%s]\n", srcflag, src, destflag, dest);
  104.     
  105.     printf("********************************************************************\n");
  106.     
  107.     srcflag = 16;
  108.     destflag = 2;
  109.     memset(src, 0, sizeof(src));
  110.     strcpy(src, "FF");
  111.     memset(dest, 0 , sizeof(dest));
  112.     conversion(srcflag, src, destflag, dest);
  113.     printf("%d进制数=[%s], 转化为,%d进制数=[%s]\n", srcflag, src, destflag, dest);
  114.     
  115.     srcflag = 8;
  116.     destflag = 2;
  117.     memset(src, 0, sizeof(src));
  118.     strcpy(src, "766");
  119.     memset(dest, 0 , sizeof(dest));
  120.     conversion(srcflag, src, destflag, dest);
  121.     printf("%d进制数=[%s], 转化为,%d进制数=[%s]\n", srcflag, src, destflag, dest);
  122.     
  123.     srcflag = 2;
  124.     destflag = 16;
  125.     memset(src, 0, sizeof(src));
  126.     strcpy(src, "11111110");
  127.     memset(dest, 0 , sizeof(dest));
  128.     conversion(srcflag, src, destflag, dest);
  129.     printf("%d进制数=[%s], 转化为,%d进制数=[%s]\n", srcflag, src, destflag, dest);
  130.     
  131.     srcflag = 9;
  132.     destflag = 7;
  133.     memset(src, 0, sizeof(src));
  134.     strcpy(src, "12");
  135.     memset(dest, 0 , sizeof(dest));
  136.     conversion(srcflag, src, destflag, dest);
  137.     printf("%d进制数=[%s], 转化为,%d进制数=[%s]\n", srcflag, src, destflag, dest);
  138.     
  139.     printf("********************************************************************\n");

  140.     return 0;
  141. }

  142. /*功能:2,将其他任何进制的数,转化为10进制数*/
  143. int conversion_n_to_10(int srcflag, char * src, int destflag, char * dest)
  144. {
  145.     assert((srcflag != 10)&&(destflag == 10));
  146.     
  147.     STACK stack;
  148.     InitStack(&stack);
  149.     
  150.     char stach_push; /*存放入栈元素*/
  151.     char stach_pop; /*存放出栈元素*/
  152.     int i = -1;
  153.     
  154.     i = 0;
  155.     stach_push = *src;
  156.     while(stach_push != '\0')
  157.     {
  158.         Push(&stack, stach_push);
  159.         stach_push = *(src + ++i);
  160.     }
  161.     
  162.     i = 0;
  163.     int wei = -1;
  164.     double tempshu_10 = 0.00;
  165.     while(StackIsEmpty(stack) != 1)
  166.     {
  167.         Pop(&stack, &stach_pop);
  168.         switch(stach_pop)
  169.         {
  170.             case '0': wei = 0; break;
  171.             case '1': wei = 1; break;
  172.             case '2': wei = 2; break;    
  173.             case '3': wei = 3; break;
  174.             case '4': wei = 4; break;
  175.             case '5': wei = 5; break;
  176.             case '6': wei = 6; break;
  177.             case '7': wei = 7; break;
  178.             case '8': wei = 8; break;
  179.             case '9': wei = 9; break;
  180.             case 'A': wei = 10; break;
  181.             case 'B': wei = 11; break;
  182.             case 'C': wei = 12; break;
  183.             case 'D': wei = 13; break;
  184.             case 'E': wei = 14; break;
  185.             case 'F': wei = 15; break;
  186.         }
  187.         tempshu_10 += wei * pow(srcflag, i++);
  188.     }
  189.     sprintf(dest, "%.0f", tempshu_10);
  190.     
  191.     DestroyStack(&stack);
  192.     return 0;
  193. }

  194. /*功能:1,将10进制数,转化为其他任何进制的数*/
  195. int conversion_10_to_n(int srcflag, char * src, int destflag, char * dest)
  196. {
  197.     assert((srcflag == 10)&&(destflag != 10));
  198.     
  199.     STACK stack;
  200.     InitStack(&stack);
  201.     
  202.     char stach_push; /*存放入栈元素*/
  203.     char stach_pop; /*存放出栈元素*/
  204.     int i = -1;
  205.     
  206.     int shu_10 = -1;
  207.     shu_10 = atoi(src);
  208.     int yu = -1;
  209.     
  210.     while(shu_10 != 0)
  211.     {
  212.         yu = shu_10 % destflag;
  213.         switch(yu)
  214.         {
  215.             case 0: stach_push = '0'; break;
  216.             case 1: stach_push = '1'; break;
  217.             case 2: stach_push = '2'; break;    
  218.             case 3: stach_push = '3'; break;
  219.             case 4: stach_push = '4'; break;
  220.             case 5: stach_push = '5'; break;
  221.             case 6: stach_push = '6'; break;
  222.             case 7: stach_push = '7'; break;
  223.             case 8: stach_push = '8'; break;
  224.             case 9: stach_push = '9'; break;
  225.             case 10: stach_push = 'A'; break;
  226.             case 11: stach_push = 'B'; break;
  227.             case 12: stach_push = 'C'; break;
  228.             case 13: stach_push = 'D'; break;
  229.             case 14: stach_push = 'E'; break;
  230.             case 15: stach_push = 'F'; break;
  231.         }
  232.         Push(&stack, stach_push); /*把求余得到的余值入栈*/
  233.         shu_10 = shu_10 / destflag; /*得到的模值会继续求余*/
  234.     }
  235.     
  236.     i = 0;
  237.     while(StackIsEmpty(stack) != 1)
  238.     {
  239.         Pop(&stack, &stach_pop);
  240.         sprintf(dest + i++, "%c", stach_pop);
  241.     }    
  242.     
  243.     DestroyStack(&stack);
  244.     return 0;
  245. }

  246. /*功能:将[srcflag]进制数=字符串[src],转化为[destflag]进制的数=字符串[dest]
  247. 目前
  248. 1,能将10进制数,转化为其他任何进制的数
  249. 2,能将其他任何进制的数,转化为10进制数
  250. */
  251. int conversion(int srcflag, char * src, int destflag, char * dest)
  252. {
  253.     if((srcflag != 10)&&(destflag != 10))
  254.     {
  255.         char temp[1024];
  256.         memset(temp, 0, sizeof(temp));
  257.         conversion_n_to_10(srcflag, src, 10, temp);
  258.         conversion_10_to_n(10, temp, destflag, dest);
  259.         return 0;
  260.     }
  261.     
  262.     if((srcflag != 10)&&(destflag == 10))
  263.     {
  264.         conversion_n_to_10(srcflag, src, destflag, dest);
  265.         return 0;
  266.     }
  267.     
  268.     if((srcflag == 10)&&(destflag != 10))
  269.     {
  270.         conversion_10_to_n(srcflag, src, destflag, dest);
  271.         return 0;
  272.     }    
  273. }

  274. /*功能:栈的初始化*/
  275. int InitStack(StackPtr stackptr)
  276. {
  277.     stackptr->base = (eletype *)malloc(STACK_INIT_SIZE * sizeof(eletype));
  278.     /*stack-> 等价于 (*stack).*/
  279.     /*栈底指针指向开辟的空间的地址低端,开辟的空间的地址类型是:eletype*/
  280.     if(stackptr->base == NULL)
  281.     {
  282.         printf("malloc失败,退出程序\n");
  283.         exit(-1);
  284.     }
  285.     stackptr->top = stackptr->base; /*栈顶指针也指向地址低端,表明是空栈*/
  286.     stackptr->stacksize = STACK_INIT_SIZE; /*记录栈的容量,不是栈中元素个数*/
  287.     
  288.     return 0;
  289. }

  290. /*功能:判断栈是否为空 1:空栈,2:非空栈*/
  291. int StackIsEmpty(STACK stack)
  292. {
  293.     //栈顶和栈底相同,即可判断为空
  294.     if(stack.top == stack.base)
  295.         return 1;
  296.     else
  297.         return 2;
  298. }

  299. /*功能:入栈操作*/
  300. int Push(StackPtr stackptr, eletype ele)
  301. {
  302.     /*要先判断栈是否已满*/
  303.     if((stackptr->top - stackptr->base) >= stackptr->stacksize)
  304.     {
  305.         /*top指针已经指向栈空间的最高地址的外面了。表示栈中元素已满:即“上溢”,则追加存储空间*/
  306.         stackptr->base = (eletype *)realloc(stackptr->base, (stackptr->stacksize + STACKINCREAMENT) * sizeof(eletype));
  307.         if(stackptr->base == NULL)
  308.         {
  309.             printf("realloc失败,退出程序\n");
  310.             exit(-1);
  311.         }
  312.         stackptr->top = stackptr->base + stackptr->stacksize; /*当前栈顶指针=栈底指针+栈的原来的容量*/
  313.         stackptr->stacksize = stackptr->stacksize + STACKINCREAMENT; /*栈的容量已经改变*/
  314.     }
  315.     *(stackptr->top) = ele; //元素e入栈
  316.     stackptr->top = (stackptr->top)++; //保证:栈顶指针始终在栈顶元素的下一个位置
  317.     
  318.     /*
  319.     *((*stackptr).top) = ele;
  320.     (*stackptr).top = (*stackptr).top + 1;
  321.     */
  322.     //printf("入栈一个元素=[%d],栈空间大小=[%d],栈中元素个数=[%d]\n", ele, stackptr->stacksize, StackLen(*stackptr));
  323.     return 0;
  324. }

  325. /*功能:出栈操作*/
  326. int Pop(StackPtr stackptr, eletype * ele)
  327. {
  328.     /*先判断是否是空栈*/
  329.     if(stackptr->top == stackptr->base)
  330.     {
  331.         printf("栈为空,无元素可出栈!\n");
  332.         return -1;
  333.     }
  334.     stackptr->top = stackptr->top -1;
  335.     *ele = *(stackptr->top);
  336.     //printf("出栈一个元素=[%d],栈空间大小=[%d],栈中元素个数=[%d]\n", *ele, stackptr->stacksize, StackLen(*stackptr));
  337.     return 0;
  338. }

  339. /*功能:取栈顶元素*/
  340. int GetTop(STACK stack, eletype * ele)
  341. {
  342.     /*先判断是否是空栈*/
  343.     if(stack.top == stack.base)
  344.     {
  345.         printf("栈为空,无栈顶元素可取!\n");
  346.         return -1;
  347.     }
  348.     stack.top = stack.top -1; /*只是取栈顶元素,不可移动栈顶指针*/
  349.     *ele = *(stack.top);
  350.     printf("当前栈顶元素=[%c]!\n", *ele);
  351.     return 0;
  352. }

  353. /*功能:打印栈中的所有元素*/
  354. int PrintStack(STACK stack)
  355. {
  356.     printf("栈空间大小=[%d],栈中元素个数=[%d]\n", stack.stacksize, StackLen(stack));
  357.     /*先判断是否是空栈*/
  358.     if(stack.top == stack.base)
  359.     {
  360.         printf("栈为空,无需打印栈中元素!\n");
  361.         return -1;
  362.     }
  363.     int num = 0;
  364.     stack.top = stack.top -1;
  365.     printf("从栈顶开始:");
  366.     while(stack.top != stack.base)
  367.     {
  368.         num++;
  369.         printf("(第%02d个元素)=[%c]->", num, *(stack.top));
  370.         (stack.top)--;
  371.     }
  372.     if(stack.top == stack.base) /*说明已经指向栈底了*/
  373.     {
  374.         num++;
  375.         printf("(第%02d个元素)=[%c]\n", num, *(stack.top));
  376.     }
  377.         
  378.     return 0;
  379. }

  380. /*功能:求栈中元素的个数*/
  381. int StackLen(STACK stack)
  382. {
  383.     int num = 0;
  384.     /*先判断是否是空栈*/
  385.     if(stack.top == stack.base)
  386.     {
  387.         /*printf("栈为空!\n");*/
  388.         num = 0;
  389.         return num;
  390.     }
  391.     stack.top = stack.top -1;
  392.     while(stack.top != stack.base)
  393.     {
  394.         num++;
  395.         (stack.top)--;
  396.     }
  397.     if(stack.top == stack.base) /*说明已经指向栈底了*/
  398.     {
  399.         num++;
  400.     }
  401.     
  402.     return num;
  403. }


  404. /*功能:销毁栈*/
  405. int DestroyStack(StackPtr stackptr)
  406. {
  407.     free(stackptr->base);
  408.     stackptr->base = NULL;
  409.     stackptr->top = NULL;
  410.     stackptr->stacksize = 0;
  411.     //printf("顺序栈已销毁!\n");
  412.     
  413.     return 0;
  414. }

阅读(677) | 评论(0) | 转发(0) |
0

上一篇:顺序栈

下一篇:单非循环链表

给主人留下些什么吧!~~