(进制转换)数制转换,把一个十进制的数转换为R进制

雨点打透心脏的1/2处 2022-09-14 14:27 321阅读 0赞

前情提要

cpp文件

对16进制转换,本文提供2种

本代码可支持,10进制转换为任意进制的数(但不支持数字转变为字母)

可以加入一些负责转换字母的函数,例如下个代码当中的mode16函数,就是负责将10、11、12、13、14、15转换为a、b、c、d、e、f的。

  1. #include<stdio.h>
  2. #include<stdlib.h>
  3. #include<math.h>
  4. #define maxsize 100
  5. typedef int status;//使后面可以使用status类型,用来表示成功或失败状态
  6. #define ERROR -1
  7. #define OK 1
  8. typedef struct
  9. {
  10. int *base;
  11. int *top;
  12. int stacksize;
  13. }sqstack;
  14. status init(sqstack *S)
  15. {//顺序栈的初始化
  16. S->base=(int *)malloc(sizeof(int));
  17. if(!S->base) exit(OVERFLOW);
  18. S->top=S->base;
  19. S->stacksize=maxsize;
  20. return OK;
  21. }
  22. status push(sqstack *S,int e)//顺序栈入栈
  23. {//插入元素e为新的栈顶元素
  24. if(S->top-S->base==S->stacksize) return ERROR;
  25. *S->top++=e; return OK;
  26. }
  27. char pop(sqstack *S)//顺序栈出栈
  28. {//删除S的栈顶元素,用e返回其值
  29. int e;
  30. if(S->top==S->base) return ERROR;
  31. e=*(--S->top);
  32. return e;
  33. }
  34. char gettop(sqstack *S)//取栈顶元素
  35. {//返回S的栈顶元素,不修改栈顶指针
  36. if(S->top!=S->base)
  37. return *(S->top-1);
  38. }
  39. status StackTraverse(sqstack *S)
  40. {
  41. // 从栈顶到栈底依次输出栈中的每个元素
  42. if(S->top==S->base)printf("The Stack is Empty!");//判断栈是否为空
  43. else
  44. {
  45. printf("The Stack is: ");
  46. S->top--;
  47. while(S->top>=S->base)
  48. {
  49. printf("%d ", *S->top);
  50. S->top--;
  51. }
  52. }
  53. printf("\n");
  54. return OK;
  55. }
  56. bool empty(sqstack *s)//判断栈是否为空
  57. {
  58. return (s->top==s->base);//为空,返回1
  59. }
  60. void conversion(sqstack *s,int i)
  61. {
  62. int e,n;
  63. printf("\n请输入你要转化的数据:");
  64. scanf("%d",&n);
  65. while(n)
  66. {
  67. push(s,n%i);
  68. n=n/i;
  69. }
  70. while(!empty(s))
  71. {
  72. e=pop(s);
  73. printf("%d",e);
  74. }
  75. }
  76. int main()
  77. {
  78. sqstack Sta;int n;
  79. sqstack *S=&Sta;
  80. init(S);
  81. printf("这里是进制转化处理程序\n");
  82. printf("请输入你想将10进制转换为几进制?\n");
  83. scanf("%d",&n);
  84. printf("10进制转化为%d",n);
  85. conversion(S,n);
  86. return 0;
  87. }

输出

watermark_type_ZHJvaWRzYW5zZmFsbGJhY2s_shadow_50_text_Q1NETiBA546L6ZmI6ZSL_size_13_color_FFFFFF_t_70_g_se_x_16

输入

  1. 16
  2. 15362

这个代码额外加了个供16进制进制转换的函数

  1. #include<stdio.h>
  2. #include<stdlib.h>
  3. #include<math.h>
  4. #define maxsize 100
  5. typedef int status;//使后面可以使用status类型,用来表示成功或失败状态
  6. #define ERROR -1
  7. #define OK 1
  8. typedef struct
  9. {
  10. int *base;
  11. int *top;
  12. int stacksize;
  13. }sqstack;
  14. status init(sqstack *S)
  15. {//顺序栈的初始化
  16. S->base=(int *)malloc(sizeof(int));
  17. if(!S->base) exit(OVERFLOW);
  18. S->top=S->base;
  19. S->stacksize=maxsize;
  20. return OK;
  21. }
  22. status push(sqstack *S,int e)//顺序栈入栈
  23. {//插入元素e为新的栈顶元素
  24. if(S->top-S->base==S->stacksize) return ERROR;
  25. *S->top++=e; return OK;
  26. }
  27. char pop(sqstack *S)//顺序栈出栈
  28. {//删除S的栈顶元素,用e返回其值
  29. int e;
  30. if(S->top==S->base) return ERROR;
  31. e=*(--S->top);
  32. return e;
  33. }
  34. char gettop(sqstack *S)//取栈顶元素
  35. {//返回S的栈顶元素,不修改栈顶指针
  36. if(S->top!=S->base)
  37. return *(S->top-1);
  38. }
  39. status StackTraverse(sqstack *S)
  40. {
  41. // 从栈顶到栈底依次输出栈中的每个元素
  42. if(S->top==S->base)printf("The Stack is Empty!");//判断栈是否为空
  43. else
  44. {
  45. printf("The Stack is: ");
  46. S->top--;
  47. while(S->top>=S->base)
  48. {
  49. printf("%d ", *S->top);
  50. S->top--;
  51. }
  52. }
  53. printf("\n");
  54. return OK;
  55. }
  56. bool empty(sqstack *s)//判断栈是否为空
  57. {
  58. return (s->top==s->base);//为空,返回1
  59. }
  60. char made16(int e)
  61. {
  62. char ch='a';
  63. if(e==10)return ch;
  64. else if(e==11) return ch+1;
  65. else if(e==12) return ch+2;
  66. else if(e==13) return ch+3;
  67. else if(e==14) return ch+4;
  68. else if(e==15) return ch+5;
  69. }
  70. void conversion(sqstack *s,int i)
  71. {
  72. int e,n;
  73. printf("\n请输入你要转化的数据:");
  74. scanf("%d",&n);
  75. while(n)
  76. {
  77. push(s,n%i);
  78. n=n/i;
  79. }
  80. while(!empty(s))
  81. {
  82. e=pop(s);
  83. if(e<10)
  84. printf("%d",e);
  85. else printf("%c",made16(e));
  86. }
  87. }
  88. int main()
  89. {
  90. sqstack Sta;int n;
  91. sqstack *S=&Sta;
  92. init(S);
  93. printf("这里是进制转化处理程序\n");
  94. printf("支持2-16进制的转换\n");
  95. printf("请输入你想将10进制转换为几进制?\n");
  96. scanf("%d",&n);
  97. printf("10进制转化为%d",n);
  98. conversion(S,n);putchar('\n');
  99. return 0;
  100. }

输出

watermark_type_ZHJvaWRzYW5zZmFsbGJhY2s_shadow_50_text_Q1NETiBA546L6ZmI6ZSL_size_13_color_FFFFFF_t_70_g_se_x_16 1

mode16函数,转换为16进制的函数,进行了另外一种写法

  1. char made16(int e)
  2. {
  3. char ch;
  4. ch=(0<=e&&e<=9)?(char)(e+'0'):(char)(e-10+'A');
  5. return ch;
  6. }
  7. #include<stdio.h>
  8. #include<stdlib.h>
  9. #include<math.h>
  10. #define maxsize 100
  11. typedef int status;//使后面可以使用status类型,用来表示成功或失败状态
  12. #define ERROR -1
  13. #define OK 1
  14. typedef struct
  15. {
  16. int *base;
  17. int *top;
  18. int stacksize;
  19. }sqstack;
  20. status init(sqstack *S)
  21. {//顺序栈的初始化
  22. S->base=(int *)malloc(sizeof(int));
  23. if(!S->base) exit(OVERFLOW);
  24. S->top=S->base;
  25. S->stacksize=maxsize;
  26. return OK;
  27. }
  28. status push(sqstack *S,int e)//顺序栈入栈
  29. {//插入元素e为新的栈顶元素
  30. if(S->top-S->base==S->stacksize) return ERROR;
  31. *S->top++=e; return OK;
  32. }
  33. char pop(sqstack *S)//顺序栈出栈
  34. {//删除S的栈顶元素,用e返回其值
  35. int e;
  36. if(S->top==S->base) return ERROR;
  37. e=*(--S->top);
  38. return e;
  39. }
  40. char gettop(sqstack *S)//取栈顶元素
  41. {//返回S的栈顶元素,不修改栈顶指针
  42. if(S->top!=S->base)
  43. return *(S->top-1);
  44. }
  45. status StackTraverse(sqstack *S)
  46. {
  47. // 从栈顶到栈底依次输出栈中的每个元素
  48. if(S->top==S->base)printf("The Stack is Empty!");//判断栈是否为空
  49. else
  50. {
  51. printf("The Stack is: ");
  52. S->top--;
  53. while(S->top>=S->base)
  54. {
  55. printf("%d ", *S->top);
  56. S->top--;
  57. }
  58. }
  59. printf("\n");
  60. return OK;
  61. }
  62. bool empty(sqstack *s)//判断栈是否为空
  63. {
  64. return (s->top==s->base);//为空,返回1
  65. }
  66. char made16(int e)
  67. {
  68. char ch;
  69. ch=(0<=e&&e<=9)?(char)(e+'0'):(char)(e-10+'A');
  70. return ch;
  71. }
  72. void conversion(sqstack *s,int i)
  73. {
  74. int e,n;
  75. printf("\n请输入你要转化的数据:");
  76. scanf("%d",&n);
  77. while(n)
  78. {
  79. push(s,n%i);
  80. n=n/i;
  81. }
  82. while(!empty(s))
  83. {
  84. e=pop(s);
  85. if(e<10)
  86. printf("%d",e);
  87. else printf("%c",made16(e));
  88. }
  89. }
  90. int main()
  91. {
  92. sqstack Sta;int n;
  93. sqstack *S=&Sta;
  94. init(S);
  95. printf("这里是进制转化处理程序\n");
  96. printf("支持2-16进制的转换\n");
  97. printf("请输入你想将10进制转换为几进制?\n");
  98. scanf("%d",&n);
  99. printf("10进制转化为%d",n);
  100. conversion(S,n);putchar('\n');
  101. return 0;
  102. }

发表评论

表情:
评论列表 (有 0 条评论,321人围观)

还没有评论,来说两句吧...

相关阅读

    相关 转换

    写出一个程序,接受一个十六进制的数值字符串,输出该数值的十进制字符串。(多组同时输入 ) 输入描述: 输入一个十六进制的数值字符串。 输出描述: 输出该数值的十