任意进制转换
【摘要】
//群:970353786
#include "stdio.h"
#include<iostream>
using namespace std;
#define StackSize 100
t...
//群:970353786
#include "stdio.h"
#include<iostream>
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("请输入整数:"); //请输入待转换的整数
scanf_s("%d", &d);
printf("请输入要转换为几进制:"); // 请输入待转换的进制
scanf_s("%d", &b);
t = trans(d, b, str); // 调用进制转换函数
cout << "进制转换结果为:";
if (t == 0) printf("Error!");
else printf("%s\n", str); // 输出转换结果字符串
}
}
文章来源: chuanchuan.blog.csdn.net,作者:川川菜鸟,版权归原作者所有,如需转载,请联系作者。
原文链接:chuanchuan.blog.csdn.net/article/details/116906256
【版权声明】本文为华为云社区用户转载文章,如果您发现本社区中有涉嫌抄袭的内容,欢迎发送邮件进行举报,并提供相关证据,一经查实,本社区将立刻删除涉嫌侵权内容,举报邮箱:
cloudbbs@huaweicloud.com
- 点赞
- 收藏
- 关注作者
作者其他文章
评论(0)