进制转换的模板(采用栈来实现)

FisherKK edited 9 月,3 周前

const char* Num = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ";
typedef int Int;
void print(Int num, int base) { // 十进制输出为base进制
        if (num < 0) cout << '-';
        num = abs(num)
    if (num / base == 0) { cout << Num[num]; return;}
    print(num / base, base);
    cout << Num[num % base];
}

void ProcessDig(Int num, int base) // 十进制的中Stack实现,可以对每一位进行操作
{
        bool flag = true; // 标记符号位
        if (num < 0) {
                flag = false;
                num = -num;
        }
    stack<char> Sc;
    while (num) {
        Sc.push(Num[num % base]);
        num /= base;
    }
    int sum = 0;
    while (!Sc.empty()) {
        // 开始你的操作
        Sc.pop();
    }
}

Comments