不反对小数,不反对正数。

 

#include <iostream>#include <stdio.h>#include <stdlib.h>#include <stack>#include <math.h>using namespace std;/**********************************************************////将十进制转化为其余进制void D_to(int m, long long x){    if (x == 0)    {        cout << x;        return;    }    stack<int> s;    //将数据边解决边压栈    while (x != 0)    {        int p = x % m;        s.push(p);        x /= m;    }    //边判断边出栈    while (!s.empty())    {        if (s.top() >= 10)            cout << (char)(s.top() + 55);        else            cout << s.top();        s.pop();    }    cout << endl;}/**********************************************************//**********************************************************////将其余进制转化为十进制long long to_D(int n, string y)//须要让原数据在确定是其余进制转10进制后再输出,因为在这种状况下,原数据不肯定是整型数,所以,为了实用于所有状况,要设原数据为char[]{    char up[26] = {'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z'};    char low[26] = {'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z'};    // cout<<"截止到"<<up[n-10-1]<<endl;     //因为超过10的进制,容许应用的字母个数不雷同。不能只判断到“F”,那只是16进制的状况    stack<int> s;    //先把数据压进栈    for (int i = 0; i < y.length(); i++)    {        ///这里千万当心:要分成n<10和n>=10的状况来做。如果不这样做,则谬误例子为“n=4,m=5,y=666”和“n=18,m=7,y=GA980H17CB”.        ///对于第一个例子,能够用“&& y[i]<(n+'0')”解决,因为n=4,是一个字符。但这种办法解决不了第二个反例,因为n=18,是两个字符,加上'0'没有实际意义        if (n < 10)        {            if (y[i] < (n + '0'))                s.push(y[i] - '0');            else            {                cout << "输出谬误" << endl; //遇到非法或超出范围的字符                exit(0);            }        }        else        {            if (y[i] >= '0' && y[i] <= '9')                s.push(y[i] - '0'); //将数字字符转化为整型                                    //要应用“&& y[i]<=low[n-10-1]”,而非简略的“&& y[i]<=‘F’”            else if (y[i] >= 'a' && y[i] <= low[n - 10 - 1])                s.push(y[i] - 87); //将大写字母转化为整型            else if (y[i] >= 'A' && y[i] <= up[n - 10 - 1])                s.push(y[i] - 55); //将小写字母转化为整型            else            {                cout << "输出谬误" << endl; //遇到非法或超出范围的字符                exit(0);            }        }    }    //边计算,边出栈    int i = 0;    long long sum = 0;    while (!s.empty())    {        sum += s.top() * pow(n, i);        i++;        s.pop();    }    // cout<<sum<<endl;    return sum;}/**********************************************************/int main(){    int n;       // n为原始数据的进制    int m;       // m为指标数据的进制    long long x; // x为原始数据(十进制整数)    string y;    // y为原始数据(不确定进制的字符串示意的数)    string z;    // z为原始数据(不确定进制的字符串示意的数)    while (1)    {        cout << "请输出原始数据的进制:";        cin >> n;        cout << "请输出指标数据的进制:";        cin >> m;        if (n == m)        {            cout << "【不必算了,还是自身】" << endl;        }        else if (n == 0 || m == 0)        {            cout << "【无奈计算】" << endl;        }        else if (n == 10 && m != 10)        {            cout << "请输出原始数据:";            cin >> x;            D_to(m, x);        }        else if (m == 10 && n != 10)        {            cout << "请输出原始数据:";            cin >> y;            cout << to_D(n, y) << endl;        }        else if (n != 10 && m != 10)        {            cout << "请输出原始数据:";            cin >> z;            ///先把原始数据转成十进制,再把十进制转化为指标进制数            long long temp = to_D(n, z);            D_to(m, temp);        }        cout << endl;    }    return 0;}