• (数据结构)数制转换——将一个十进制整数转换成一个非十进制数


    1. #include
    2. #include
    3. //类型创建
    4. typedef struct LinkedStackNode
    5. {
    6. int data;
    7. struct LinkedStackNode* next;
    8. }LinkedStackNode, * LinkedStack;
    9. LinkedStack top;
    10. //链栈的初始化
    11. //头结点的初始化
    12. LinkedStack Init_LinkedStack()
    13. {
    14. LinkedStack top = (LinkedStackNode*)malloc(sizeof(LinkedStackNode));
    15. if (top != NULL)
    16. {
    17. top->next = NULL;
    18. }
    19. else
    20. {
    21. printf("头结点申请空间失败\n");
    22. }
    23. return top;
    24. }
    25. //判断栈空
    26. int LinkedStack_Empty(LinkedStack top)
    27. {
    28. if (top->next == NULL)
    29. {
    30. return 1;
    31. }
    32. else
    33. {
    34. return 0;
    35. }
    36. }
    37. //入栈
    38. int Push_LinkedStack(LinkedStack top, int x)
    39. {
    40. LinkedStackNode* node;
    41. node = (LinkedStackNode*)malloc(sizeof(LinkedStackNode));
    42. if (node == NULL)
    43. {
    44. printf("节点申请空间失败\n");
    45. return 0;
    46. }
    47. node->data = x;
    48. node->next = top->next;
    49. top->next = node;
    50. return 1;
    51. }
    52. //出栈
    53. int Pop_LinkedStack(LinkedStack top, int* x)
    54. {
    55. LinkedStackNode* node;
    56. if (top->next == NULL)
    57. {
    58. printf("链栈为空,无法进行出栈操作\n");
    59. return 0;
    60. }
    61. else
    62. {
    63. node = top->next;
    64. *x = node->data;
    65. top->next = node->next; //将头结点后的节点整体向前一位
    66. free(node);
    67. return 1;
    68. }
    69. }
    70. //读取栈顶元素
    71. int Get_LinkedStack(LinkedStack top, int* x)
    72. {
    73. if (top->next == NULL)
    74. {
    75. printf("链栈为空\n");
    76. return 0;
    77. }
    78. else
    79. {
    80. *x = top->next->data;
    81. return 1;
    82. }
    83. }
    84. //对于输入的任意一个非负十进制整数,打印输出与其等值的任意进制数
    85. void Conversion(int N)
    86. {
    87. int x; //声明变量x,输出转换后各个位数上的数
    88. int jinzhi;
    89. printf("请输入需要转换为的进制数\n");
    90. scanf("%d",&jinzhi);
    91. LinkedStack S = Init_LinkedStack(); //构造空链栈S
    92. while (N > 0) //当N>0时,继续进行数值转换
    93. {
    94. Push_LinkedStack(S,N%jinzhi); //对N对进制数的余数的入栈
    95. N /=jinzhi; //将N的商赋值给本身,便于下一轮计算
    96. }
    97. printf("转化后的进制数为:");
    98. while (!LinkedStack_Empty(S)) //栈不为空时,出栈栈中的余数
    99. {
    100. Pop_LinkedStack(S,&x); //进行出栈操作
    101. printf("%d ",x);
    102. }
    103. }
    104. int main()
    105. {
    106. //栈的初始化
    107. //头结点的初始化
    108. LinkedStack top;
    109. top = Init_LinkedStack();
    110. if (top == NULL)
    111. {
    112. printf("申请链栈空间失败\n");
    113. }
    114. //对于输入的任意一个非负十进制整数,打印输出与其等值的任意进制数
    115. int N;
    116. printf("请输入需要转换的非负十进制数\n");
    117. scanf("%d",&N);
    118. Conversion(N);
    119. return 0;
    120. }

  • 相关阅读:
    运行游戏“找不到XINPUTI_3.dll无法继续执行代码,总共有五种解决方案
    Spring——AOP(很全很详细,耐心看完有收获)
    教资-10月21日
    win11安装教程(附tpm2.0开启或跳过教程)
    msys如何编译64位ffmpeg 带x264 x265
    6个ES6中很酷的数组函数
    MMSegmentation训练自己的语义分割数据集
    六零导航页(LyLme Spage)导航网站源码
    走进 Orca 架构及技术世界
    C++移动构造和移动赋值函数
  • 原文地址:https://blog.csdn.net/2301_79580018/article/details/134035401