C++ C++;使用十六进制的全减法器
我有一些意见,例如: 100000000000-1= 和 AAAA BBBBB CCCCC DDD EEE FFFFF-ABCDEF0123456789ABCDEF= 我需要将十六进制字符串逐位转换为十进制,并在需要借用时跟踪。我不确定在发生借用时如何调整操作数1的值。比如在第一行的时候你要借13次。 目前,我有C++ C++;使用十六进制的全减法器,c++,C++,我有一些意见,例如: 100000000000-1= 和 AAAA BBBBB CCCCC DDD EEE FFFFF-ABCDEF0123456789ABCDEF= 我需要将十六进制字符串逐位转换为十进制,并在需要借用时跟踪。我不确定在发生借用时如何调整操作数1的值。比如在第一行的时候你要借13次。 目前,我有 #include <iostream> #include <fstream> using namespace std; string decimalToHex
#include <iostream>
#include <fstream>
using namespace std;
string decimalToHex(int);
void subtraction(string, string)
int hexadecimalToDecimal(char hexVal);
int fullSubtractor(int tempOp1, int tempOp2)
int main()
{
ifstream myFile;
string l1, l2, l3, l4, l5, l6, l7, l8; //lines
string l1op1, l1op2, l2op1, l2op2, l3op1, l3op2, l4op1, l4op2, l5op1, l5op2, l6op1, l6op2, l7op1, l7op2, l8op1, l8op2; //parsed operators
myFile.open("data.txt");
if (myFile.is_open()) //check if file opened
{
cout << "File opened successfully. " << endl;
}
else
{
cerr << "File failed to open." << endl;
return 1;
}
while (!myFile.eof())
{
myFile >> l6 >> l7; //read in line by line
}
l6op1 = l6.substr(0, l6.find("-"));
l6op2 = l6.substr(l6.find("-") + 1);
l6op2 = l6op2.substr(0, l6op2.length() - 1);
std::string l6op2_zeros = std::string((l6op1.length()) - l6op2.length(), '0') + l6op2;
cout << l6; // << subtraction(l6op1, l6op2_zeros) << endl;
subtraction(l6op1, l6op2_zeros);
cout << endl;
l7op1 = l7.substr(0, l7.find("-"));
l7op2 = l7.substr(l7.find("-") + 1);
l7op2 = l7op2.substr(0, l7op2.length() - 1);
std::string l7op2_zeros = std::string((l7op1.length()) - l7op2.length(), '0') + l7op2; //appends zeros to front of second operand to make it same length as operand 1
cout << l7; // << subtraction(l7op1, l7op2) << endl;
subtraction(l7op1, l7op2_zeros);
cout << endl;
myFile.close();
return 0;
}
int fullSubtractor(int tempOp1, int tempOp2)
{
static int borrow;
int result = 0;
if ((tempOp1 < tempOp2) || ((tempOp1 == 0) && (borrow == 1)))
{
tempOp1 += 16;
result = tempOp1 - borrow - tempOp2;
borrow = 1;
}
else
{
result = tempOp1 - tempOp2;
borrow = 0;
}
return result;
}
void subtraction(string op1, string op2)
{
string result;
int tempDifference = 0, tempHex = 0;
int j = op2.length() - 1;
for (int i = op1.length() - 1; i >= 0; i--)
{
int temp1 = hexadecimalToDecimal(op1[i]);
int temp2 = hexadecimalToDecimal(op2[j]);
tempHex = fullSubtractor(temp1, temp2);
result = decimalToHex(tempHex) + result;
cout << result << " ";
j--;
}
cout << result << endl;
//return result;
}
int hexadecimalToDecimal(char hexVal)
{
int base = 1;
int dec_val = 0;
if (hexVal >= '0' && hexVal <= '9')
{
dec_val += (hexVal - 48) * base;
base *= 16;
}
else if (hexVal >= 'A' && hexVal <= 'F')
{
dec_val += (hexVal - 55) * base;
// incrementing base by power
base *= 16;
}
return dec_val;
}
string decimalToHex(int decNum)
{
stringstream ss;
ss << hex << decNum;
string hexNum(ss.str());
//cout << hexNum << endl;
return hexNum;
}
#包括
#包括
使用名称空间std;
字符串分贝(整数);
空减法(字符串,字符串)
int十六进制麦芽糖醛(char-hexVal);
整数全减法器(整数tempOp1,整数tempOp2)
int main()
{
ifstreammyfile;
字符串l1、l2、l3、l4、l5、l6、l7、l8;//行
字符串l1op1、l1op2、l2op1、l2op2、l3op1、l3op2、l4op1、l4op2、l6op1、l6op2、l7op1、l7op2、l8op1、l8op2;//解析的运算符
myFile.open(“data.txt”);
if(myFile.is_open())//检查文件是否打开
{
cout l7;//逐行读取
}
l6op1=l6.substr(0,l6.find(“-”);
l6op2=l6.substr(l6.find(“-”)+1);
l6op2=l6op2.substr(0,l6op2.length()-1);
std::string l6op2_zeros=std::string((l6op1.length())-l6op2.length(),'0')+l6op2;
我建议你测试一个包含两个十六进制字符串的函数,减去它们,然后返回结果。你展示给我们的main
函数使用-
字符和其他不相关的东西进行操作。我只是想通过包含main来创建一个可复制的示例。该函数可以工作,除非它需要w从一个以上的数字到100-1。减法只是将一个数字一个数字转换为十进制,然后调用fullSubtractor,如果操作数1小于16,则该减法将递增16。然后,减法将答案转换回十六进制并循环,直到操作数结束。尝试将前导0放入较短的字符串中,使其长度等于长字符串呃字符串。然后它变成了100-001
,多个数字的问题就成了一个没有意义的问题。那么你用铅笔和纸怎么做呢?你总是一次减去一个数字,所以我不知道你说的是什么问题。简单一点——如果基数是10而不是16,你会怎么做?那是一样的除了借用16而不是10之外,可以完成一组步骤。好吧,在答案错误的地方,从那里调试(使用调试器),看看代码的方向与预期的方向不同。