任意进制转换
发布日期:2021-06-29 14:39:27 浏览次数:2 分类:技术文章

本文共 1165 字,大约阅读时间需要 3 分钟。

//群:970353786#include "stdio.h"#include
using namespace std;#define StackSize 100typedef 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); // 输出转换结果字符串 }}

转载地址:https://chuanchuan.blog.csdn.net/article/details/116906256 如侵犯您的版权,请留言回复原文章的地址,我们会给您删除此文章,给您带来不便请您谅解!

上一篇:所有偶数放所有奇数前
下一篇:回文判断

发表评论

最新留言

第一次来,支持一个
[***.219.124.196]2024年04月09日 07时39分30秒