我想不出该该如何将A~F,作为一个占位去传入其中,只能想到普通的数字传入其中。我也尝试将输入的值变为string 类型 ,但输入时每个数字得需要空格 隔开
问题:
问题连接:https://edu.csdn.net/skill/program/70366?practiceId=37987636
请把输入的数字转换为十进制。 如果数字中只有0和1,那么就认为是二进制。 如果数字中只有0到7,那么就认为是八进制。 如果数字中有不止0到7,那么就认为是十六进制。 如果出现了0到9、A到F(不区分大小写)之外的字母,那么就请输出“NaN”。
这是我的做法:
这是我的源代码:
#include<iostream>
using namespace std;
#define A 10
#define B 11
#define C 12
#define D 13
#define E 14
#define F 15
int judgement(int n)
{
int max = 0;
while (n)
{
int current = 0;
current = n % 10;
n = n / 10;
if (current > max || max == 0)
{
max = current;
}
}
if (max >= 8) return 1; //16进制
if (max > 2 ) return 2; //8进制
if (max <= 2) return 3; //2进制
}
int solve(int num, int n)
{
int sum = 0, index = -1;
if (num == 1)
{
while (n)
{
int e = 1;
int current = 0;
index++;
current = n % 10;
n = n / 10;
if (index == 0)
{
sum = current;
continue;
}
for(int i = 0;i < index;i++)
{
e *= 16;
}
current *= e;
sum += current;
}
}
if (num == 2)
{
while (n)
{
int e = 1;
int current = 0;
index++;
current = n % 10;
n = n / 10;
if (index == 0)
{
sum = current;
continue;
}
for (int i = 0; i < index; i++)
{
e *= 8;
}
current *= e;
sum += current;
}
}
if (num == 3)
{
while (n)
{
int e = 1;
int current = 0;
index++;
current = n % 10;
n = n / 10;
if (index == 0)
{
sum = current;
continue;
}
for (int i = 0; i < index; i++)
{
e *= 2;
}
current *= e;
sum += current;
}
}
return sum;
}
int main()
{
int n;
cin >> n;
int num = judgement(n);
cout <<"属于3种情况中的:" << num << endl;
int result = solve(num, n);
cout << result << endl;
return 0;
}