我的程序出现问题,该程序用于计算十六进制的加法和减法。我程序的算法是:
但是,我遇到了一个问题,我的计算给了我错误的答案。 (例如,对于FFFFFF + FFFFFFFFFF打印“ FFFFFFE”而不是“ 10000FFFFFE”)
我该怎么解决这个问题?
我为此程序创建了自己的Power函数,因为我需要一个数字,十六进制字符串最多可以达到16 Fs。
幂函数:
unsigned long long int result = 1;
int i;
for (i = 0; i < y; i++)
{
result *= x;
}
return result;
代码:
int i;
int power = FirstHexaNumber.length() - 1;
int power2 = SeconHexaNumber.length() - 1;
int checkLength = FirstHexaNumber.length();
int checkLength2 = SeconHexaNumber.length();
unsigned long long int decimalNumber = 0;
unsigned long long int decimalNumber2 = 0;
unsigned long long int totalDecimal;
int temporary;
string result;
if (Operator == '+') //check if operator is add or minus
{
//Convert Hex to Decimal for first number
for (i = 0; i < checkLength; i++)
{
if (int(FirstHexaNumber[i]) >= 48 && int(FirstHexaNumber[i]) <= 57) { // check if FirstHexaNumber 0 to 9
decimalNumber += ((int(FirstHexaNumber[i])) - 48) * powerFunc(16, power); //formula to convert hexadecimal into decimal, int(FirstHexaNumber[i]) is used to convert hexa into a number
}
else if (int(FirstHexaNumber[i]) >= 65 && int(FirstHexaNumber[i]) <= 70) // check if FirstHexaNumber is A to F
{
decimalNumber += ((int(FirstHexaNumber[i])) - 55)*powerFunc(16, power);
}
else if (int(FirstHexaNumber[i]) >= 97 && int(FirstHexaNumber[i]) <= 102) // check if FirstHexaNumber is a to f
{
int x = powerFunc(16, power);
decimalNumber += ((int(FirstHexaNumber[i])) - 87)* x;
}
power--; //power-- since it starts from "HexaNumber.length - 1". Power should decrease as assignment of power goes down
}
//Convert Hex to Decimal for second number
for (i = 0; i < checkLength2; i++)
{
if (int(SeconHexaNumber[i]) >= 48 && int(SeconHexaNumber[i]) <= 57) {
decimalNumber2 += ((int(SeconHexaNumber[i])) - 48) * powerFunc(16, power2); //formula to convert Hexadecimal to Decimal
}
else if (int(SeconHexaNumber[i]) >= 65 && int(SeconHexaNumber[i]) <= 70)
{
decimalNumber2 += ((int(SeconHexaNumber[i])) - 55)*powerFunc(16, power2); //formula to convert Hexadecimal to Decimal
}
else if (int(SeconHexaNumber[i]) >= 97 && int(SeconHexaNumber[i]) <= 102)
{
unsigned long long int x = powerFunc(16, power2);
decimalNumber2 += ((int(SeconHexaNumber[i])) - 87)*x; //formula to convert Hexadecimal to Decimal
}
power2--;
}
totalDecimal = decimalNumber + decimalNumber2; //Adds the total decimal to convert into hexadecimal
if (totalDecimal == 0)
{
return "0";
}
//Converts Decimal to Hexadecimal
for (i = 0; totalDecimal != 0; i++) //as long as totalDecimal does not hit 0 from being divided by 16, run the loop
{
temporary = totalDecimal % 16; //use temporary as a variable to temporarily hold onto the number remainder of mod 16
if (temporary >= 10) //if temporary >= 10, that means it needs to be converted to alphabet
{
result.insert(0, 1, temporary + 55); //result.insert inserts a string of text into a spot, and pushes everything else backwards.
} //in this case, result.insert assigns "temporary+55" into the spot of characters 0 to 1.
else //else, it means that the decimal will be a number, add 48 to convert to ascii
{
result.insert(0, 1, temporary + 48);
}
totalDecimal = totalDecimal / 16; //divide by 16 to move on to finding the next digit/alphabet
}
return result;
}
else if (Operator == '-') //check if operator is add or minus
{
//Convert Hex to Decimal for first number
for (i = 0; i < checkLength; i++) //as long as the loop does not exceed the length of the hexadecimal, run it
{
if (int(FirstHexaNumber[i]) >= 48 && int(FirstHexaNumber[i]) <= 57) {
decimalNumber += ((int(FirstHexaNumber[i])) - 48) * powerFunc(16, power);
}
else if (int(FirstHexaNumber[i]) >= 65 && int(FirstHexaNumber[i]) <= 70)
{
decimalNumber += ((int(FirstHexaNumber[i])) - 55)*powerFunc(16, power);
}
else if (int(FirstHexaNumber[i]) >= 97 && int(FirstHexaNumber[i]) <= 102)
{
decimalNumber += ((int(FirstHexaNumber[i])) - 87)*powerFunc(16, power);
}
power--;
}
//Convert Hex to Decimal for second number
for (i = 0; i < checkLength2; i++)
{
if (int(SeconHexaNumber[i]) >= 48 && int(SeconHexaNumber[i]) <= 57) {
decimalNumber2 += ((int(SeconHexaNumber[i])) - 48) * powerFunc(16, power2);
}
else if (int(SeconHexaNumber[i]) >= 65 && int(SeconHexaNumber[i]) <= 70)
{
decimalNumber2 += ((int(SeconHexaNumber[i])) - 55)*powerFunc(16, power2);
}
else if (int(SeconHexaNumber[i]) >= 97 && int(SeconHexaNumber[i]) <= 102)
{
decimalNumber2 += ((int(SeconHexaNumber[i])) - 87)*powerFunc(16, power2);
}
power2--;
}
if (decimalNumber >= decimalNumber2)
{
totalDecimal = decimalNumber - decimalNumber2; //subtract bigger number by smaller number.
if (totalDecimal == 0)
{
return "0";
}
for (i = 0; totalDecimal != 0; i++)
{
temporary = totalDecimal % 16;
if (temporary >= 10)
{
result.insert(0, 1, temporary + 55);
}
else
{
result.insert(0, 1, temporary + 48);
}
totalDecimal = totalDecimal / 16;
}
return result;
}
else
{
totalDecimal = decimalNumber2 - decimalNumber; //subtract bigger number by smaller number.
if (totalDecimal == 0)
{
return "0";
}
for (i = 0; totalDecimal != 0; i++)
{
temporary = totalDecimal % 16;
if (temporary >= 10)
{
result.insert(0, 1, temporary + 55);
}
else
{
result.insert(0, 1, temporary + 48);
}
totalDecimal = totalDecimal / 16;
}
return "-" + result;
}
}
return 0;
答案 0 :(得分:3)
您可以尝试以下代码段:
int a,b;
cout << "\nEnter A in hex: ";
cin >> hex >> a;
cout << "\nEnter B in hex: ";
cin >> hex >> b;
cout << "\n Addition of " << hex << a <<" and "<< hex << b << " is " << hex << a+b;
cout << "\n Substraction of " << hex << a << " and " << hex << b << " is " << hex << a - b;
答案 1 :(得分:2)
int x = powerFunc(16, power);
应该是long long x = powerFunc(16, power);
不知道函数pow
的完整来源,返回类型也应该很长。
十六进制计算器可以更简单。
#include <sstream>
std::stringstream ss1(s1),ss2(s2);
ss1 >> std::hex >> i1;
ss2 >> std::hex >> s2;
std::cout << std::hex << std::uppercase << i1 + s2 << std::endl;
std::stringstream res;
res << std::hex << std::uppercase << i1 + i2;
return res.str();
答案 2 :(得分:-1)
根据像我这样的 magical 向导(嗯,程序员),这 magical 函数将实现您的愿望:-
注意:未从任何来源复制源代码。
std::tuple<std::vector<std::string>, std::vector<char>> DistributeHexStringToStrings(std::string Target)
{
std::string::size_type n1 = 0;
while ((n1 = Target.find(std::string(" "), n1)) != std::string::npos)
{
Target.replace(n1, std::string(" ").size(), "");
n1 += std::string("").size();
}
if (Target.find("--") != std::string::npos || Target.find("++") != std::string::npos ||
Target.find("-+") != std::string::npos || Target.find("+-") != std::string::npos)
throw std::invalid_argument("You should not use double signs!");
std::string::size_type n2 = 0;
while ((n2 = Target.find(std::string("-"), n2)) != std::string::npos)
{
Target.replace(n2, std::string("-").size(), "+-");
n2 += std::string("+-").size();
}
std::vector<char> TermOperator;
if (Target[0] != '+' && Target[1] != '-')
TermOperator.emplace_back('+');
for (auto i = 0; i < signed(Target.size()); i++)
if (Target[i] == '+' && Target[i + 1] != '-')
TermOperator.emplace_back('+');
else if (Target[i] == '+' && Target[i + 1] == '-')
TermOperator.emplace_back('-');
else if (!(Target[i] >= '0' && Target[i] <= '9' || Target[i] >= 'A' && Target[i] <= 'F' ||
Target[i] >= 'a' && Target[i] <= 'f' || Target[i] == '+' || Target[i] == '-'))
throw std::invalid_argument("The specified hexadecimal expression is not valid!");
const size_t Terms = std::count(Target.begin(), Target.end(), '+') + 1;
std::stringstream HexStream(Target);
std::vector<std::string> HexStrings;
for (auto i = 0; i < signed(Terms); i++)
{
std::string Temporary;
std::getline(HexStream, Temporary, '+');
if (Temporary.empty() || Temporary == "-" || Temporary == "+")
continue;
HexStrings.emplace_back(Temporary);
}
return std::make_tuple(HexStrings, TermOperator);
}
此函数将以最干净的方式分发字符串。但是工作仍未完成,我们需要正确地使用这个函数以获得我们想要的结果,而我在这里是另一个函数可以进行十六进制的转换实际十六进制数字的字符串:-
std::vector<long long> ConvertHexaDecimalToDecimal(std::vector<std::string> HexStrings)
{
std::vector<long long> HexValues;
for (auto &elem : HexStrings)
{
if (elem[0] == '-')
elem.erase(elem.begin());
int Temporary;
std::stringstream HexConverter(elem);
HexConverter >> std::hex >> Temporary;
if (Temporary == 0)
continue;
HexValues.emplace_back(Temporary);
}
return HexValues;
}
此函数会将所有十六进制字符串值转换为十六进制数字的实际十进制等效项,现在我们需要这样计算:-
long long CalculateHexadecimalExpression(std::vector<long long> &HexValues, std::vector<char> &Operators)
{
if (Operators.size() != HexValues.size())
throw std::invalid_argument("Operators and Number of Hex Values do not match!");
long long Result = 0;
for (auto i = 0; i < signed(Operators.size() & HexValues.size()); i++)
{
switch (Operators[i])
{
case '+':
Result += HexValues[i];
break;
case '-':
Result -= HexValues[i];
break;
default: break;
}
}
return Result;
}
此功能将为您完成所有结果计算。
现在,您可以将这三个功能一起使用以产生所需的期望结果。
示例:-
int main()
{
const std::string Example = "3 + 2";
std::vector<std::string> HStrings;
std::vector<char> HOperators;
std::tie(HStrings, HOperators) = DistributeHexStringToStrings(Example);
auto HNumbers = ConvertHexaDecimalToDecimal(HStrings);
std::cout << ConvertDecimalToHexaDecimalValue(CalculateHexadecimalExpression(HNumbers, HOperators)) << std::endl;
}
输出:-
3855
注意:如果需要,您可以更进一步,并将结果数字也转换为十六进制(使用此功能):-
std::string ConvertDecimalToHexaDecimalValue(const long long Hex, bool UpperCase = true)
{
std::stringstream HexResultStream;
HexResultStream << (Hex < 0 ? "-" : "") << (UpperCase ? std::uppercase : std::nouppercase) << std::hex << abs(Hex);
return HexResultStream.str();
}
ConvertDecimalToHexaDecimalValue(CalculateHexadecimalExpression(HNumbers, HOperators))
这样使用它。
将为您提供 F0F ,为第二个参数分配一个值为false的值,以小写形式生成十六进制字符串。 (例如 ConvertDecimalToHexaDecimalValue(CalculateHexadecimalExpression(HNumbers, HOperators,
false
));
别忘了将这些内容添加到顶部:-
#include <iostream>
#include <vector>
#include <sstream>
#include <tuple>
关于研究员程序员的
Ruks