#include"Stack.h"
#include<stdio.h>
#include"TryStack.h"
#include<stdlib.h>
#define _CRT_SECURE_NO_DEPRECATE
#define _CRT_SECURE_CPP_OVERLOAD_STANDARD_NAMES 1
void convert(Stack &s, int N,int n)
{
InitStack(&s);
do
{
Push(&s, N%n);
N /= n;
} while (N!=0);
int i, e;
while (!IsEmpty(&s))
{
e = Pop(&s);
if (e > 9)//十六进制时输出字母
{
e = e + 55;
printf("%c", e);
}
else
{
printf("%d", e);
}
}
printf("\n");
}
int main()
{
Stack s;
InitStack(&s);
/*for (int i = 1; i < 20; i += 2)
Push(&s, i);
Print(&s);
printf("栈顶的元素为:\n");
int k = Top(&s);
printf("%d\n", k);
Clear(&s);
if (IsEmpty(&s))
printf("栈为空\n");
else
printf("栈不为空\n");
*/
unsigned n, N;//要转换的进制数和要转换的数
printf("输入要转换的十进制数和要转换为的进制数:\n");
scanf("%d,%d", &N, &n);
printf("%d转换为%d进制后为:\n", N, n);
convert(s, N, n);
system("pause");
return 0;
}
用堆栈实现由十进制数向其他进制的转换
最新推荐文章于 2021-05-20 09:48:20 发布