题目:编写一个算法,将非负的十进制整数转换为其他进制的数输出,10及其以上的数字从‘A’开始的字母表示。
算法trans的思路:先用“除基取余”法求得从低位到高位的值,同时采用顺序栈暂时存放每次得到的数,当商为0时,再从栈顶到栈底输出从高位到低位的数字。
在这里插入代码片
```#include "stdio.h"
#include<iostream>//我用的VS2019
using namespace std;
#define StackSize 100
typedef char ElemType;
typedef struct
{
ElemType data[StackSize];
int top;
}SqStack;
int trans(int d, int b, char string[]) //string用于存放转换后的字符串
{
SqStack st;
char ch;
int r, i = 0;
st.top = -1; // 栈初始化
if (b <= 1 || b > 36 || b == 10) // 2≤b≤36且不为10
{
printf_s(" b is Error\n"); return 0;
}
while (d != 0)
{
r = d % b; //求余数
ch = r + (r < 10 ? '0' : 'A' - 10); // 将余数转换为相应的字符
st.top++; st.data[st.top] = ch; // 进栈
d /= b;
}
while (st.top != -1)
{
string[i++] = st.data[st.top]; //将出栈的字符放入字符数组string
st.top--;
}
string[i] = '\0'; //加入字符串结束标志
return 1;
}
void main()
{
while (1)
{
char str[10];
int d, b, t;
printf_s("input integer d (d≥0)please:"); //请输入待转换的整数
scanf_s("%d", &d);//注意VS2019中要加_s
printf("input system b (2≤b≤36) please:"); // 请输入待转换的进制
scanf_s("%d", &b);
t = trans(d, b, str); // 调用进制转换函数
cout << "进制转换结果为:";
if (t == 0) printf("Error!");
else printf("%s\n", str); // 输出转换结果字符串
}
}