我的代码只能添加两个Hedecimal数字,但我们应该添加两个以上,输入就像这个FFFFFFFFFFFFF + 1 + 2 + 3 + 4可能更多的添加。我们可以使用运算符重载。 这是我的代码。
#include <iostream>
#include <string>
#include<cstddef>
#include<cstring>
using namespace std;
int changeint(char a)
{
switch(a)
{
case '0': return 0;
case '1': return 1;
case '2': return 2;
case '3': return 3;
case '4': return 4;
case '5': return 5;
case '6': return 6;
case '7': return 7;
case '8': return 8;
case '9': return 9;
case 'A': return 10;
case 'B': return 11;
case 'C': return 12;
case 'D': return 13;
case 'E': return 14;
case 'F': return 15;
}
}
char changechar(int a)
{
switch(a)
{
case 0: return '0';
case 1: return '1';
case 2: return '2';
case 3: return '3';
case 4: return '4';
case 5: return '5';
case 6: return '6';
case 7: return '7';
case 8: return '8';
case 9: return '9';
case 10:return 'A';
case 11:return 'B';
case 12:return 'C';
case 13:return 'D';
case 14:return 'E';
case 15:return 'F';
}
}
int main()
{
string input,input_one,input_two;
int count_one,count_two;
cin>>input;
count_one=input.find('+',0);
count_two=input.length();
input_one=input.substr(0,count_one);
input_two=input.substr(count_one+1,count_two-count_one-1);
char a[1000];
char b[1000];
char c[1001];
a[1000]=0;
strcpy(a,input_one.c_str());
b[1000]=0;
strcpy(b,input_two.c_str());
int A[1000],B[1000],C[1001],i,lena=0,lenb=0,lenc=1000;
for(i=0;i<1001;i++)
C[i]=0;
for(i=0;i<1000;i++)
{
A[i]=0;
B[i]=0;
}
while(a[lena]!=0) lena++;
while(b[lenb]!=0) lenb++;
for(i=0;i<lena;i++)
A[lena-i-1]=changeint(a[i]);
for(i=0;i<lenb;i++)
B[lenb-i-1]=changeint(b[i]);
for(i=0;i<1000;i++)
{
C[i]=C[i]+A[i]+B[i];
if(C[i]>15)
{
C[i]=C[i]-16;
C[i+1]++;
}
}
for(i=999;i>=0;i--)
{
if(C[i]==0) lenc--;
else break;
}
if(lenc==0)
lenc=1;
for(i=0;i<lenc;i++)
c[i]=changechar(C[lenc-i-1]);
for(i=0;i<lenc;i++)
cout<<c[i];
return 0;
}
感谢您的帮助。
答案 0 :(得分:3)
这是一个非常复杂的程序,可以解决一个非常微不足道的问题。提示:十进制和十六进制是表示数字的一种方式,算术运算仍然是相同的。由于您将此问题标记为C ++,因此可以使用标准库。这是一个简单的例子,说明如何实现五个数字的加法:
#include <iostream>
#include <vector>
#include <numeric>
int main() {
std::vector<int> vNumbers;
for(int i=0; i<5; ++i)
{
int nNum = 0;
std::cin >>std::hex >> nNum;
vNumbers.push_back(nNum);
}
int nSum = std::accumulate(vNumbers.begin(), vNumbers.end(), 0);
std::cout<<std::hex<<nSum<<std::endl;
return 0;
}
答案 1 :(得分:0)
首先,您需要使用&#39; +&#39;来组合和标记字符串。符号
然后你需要使用内置的处理库来转换它们 - 尝试处理这些数据,因为字符串只会让你痛苦!
第三,我建议你重新开始,因为这段代码看起来很糟糕,抱歉:0
在纸上概念性地解决您的问题(考虑使用解析的十六进制类型的本机处理),然后在IDE中解决它。
祝你好运!
答案 2 :(得分:0)
#include <iostream>
#include <string>
#include <cstdlib>
using namespace std;
class Hedadd
{
private:
string input_string;
public:
void set_the_neibu(string input_the_string);
friend Hedadd operator+(const Hedadd& string_one,const Hedadd& string_two);
void output();
};
void input(string input_string,char zheng,Hedadd first_string[100],Hedadd add);
int changeint(string a)
{
for (int i=0;i<100;i++)
{
if(a[i]=='0')
a[i]='0';
if(a[i]=='1')
a[i]='1';
if(a[i]=='2')
a[i]='2';
if(a[i]=='3')
a[i]='3';
if(a[i]=='4')
a[i]='4';
if(a[i]=='5')
a[i]='5';
if(a[i]=='6')
a[i]='6';
if(a[i]=='7')
a[i]='7';
if(a[i]=='8')
a[i]='8';
if(a[i]=='9')
a[i]='9';
if(a[i]=='A')
a[i]='10';
if(a[i]=='B')
a[i]='11';
if(a[i]=='C')
a[i]='12';
if(a[i]=='D')
a[i]='13';
if(a[i]=='E')
a[i]='14';
if(a[i]=='F')
a[i]='15';
return a[i];
}
}
string changestring(int a[100])
{
for(int i=0;i<100;i++)
{
if(a[i]==0)
a[i]=0;
if(a[i]==1)
a[i]=1;
if(a[i]==2)
a[i]=2;
if(a[i]==3)
a[i]=3;
if(a[i]==4)
a[i]=4;
if(a[i]==5)
a[i]=5;
if(a[i]==6)
a[i]=6;
if(a[i]==7)
a[i]=7;
if(a[i]==8)
a[i]=8;
if(a[i]==9)
a[i]=9;
if(a[i]==10)
a[i]='A';
if(a[i]==11)
a[i]='B';
if(a[i]==12)
a[i]='C';
if(a[i]==13)
a[i]='D';
if(a[i]==14)
a[i]='E';
if(a[i]==15)
a[i]='F';
}
}
string xiangjia(string one,string two)
{
int one_length,two_length,longer,cha,smaller,sum[1000];
string adding_result;
one_length=one.length();
two_length=two.length();
if(one_length<=two_length)
{
longer=two_length;
smaller=one_length;
cha=two_length-one_length;
for(int i=0;i<smaller;i++)
{
one[i+cha]=one[i];
}
for(int m=0;m<cha;m++)
{
one[m]='0';
}
}
else
{
longer=one_length;
smaller=two_length;
cha=one_length-two_length;
for(int i=0;i<smaller;i++)
{
two[i+cha]=two[i];
}
for(int m=0;m<cha;m++)
{
two[m]='0';
}
}
changeint(one);
changeint(two);
for(int i=longer;i>0;i--)
{
sum[i]=one[i]+two[i];
if(sum[i]>='16')
sum[i]=sum[i]-16;
sum[i-1]=sum[i-1]+1;
}
for(int i=longer;i>0;i--)
{
adding_result=changestring(sum);
}
return (adding_result);
}
int main()
{
string input_string;
char zheng;
Hedadd first_string[100],add;
zheng='+';
input(input_string,zheng,first_string,add);
add.output();
return 0;
}
void Hedadd ::set_the_neibu(string input_the_string)
{
input_string=input_the_string;
}
void input(string input_string,char zheng,Hedadd first_string[100],Hedadd add)
{
for(int i=0;i<100;i++)
{
if(i==0)
{
cin>>input_string;
first_string[i].set_the_neibu(input_string);
add=first_string[i];
}
if(i!=0)
{
cin>>zheng>>input_string;
first_string[i].set_the_neibu(input_string);
add=add+first_string[i];
}
}
}
Hedadd operator+(const Hedadd& string_one,const Hedadd& string_two)
{
Hedadd string_together;
string_together.input_string=xiangjia(string_one.input_string,string_two.input_string);
return(string_together);
}
void Hedadd::output()
{
cout<<input_string;
}
答案 3 :(得分:0)
#include <iostream>
#include <string>
#include <cstdlib>
using namespace std;
class Hedadd
{
private:
string input_string;
public:
void set_the_neibu(string input_the_string);
friend Hedadd operator+(const Hedadd& string_one, const Hedadd& string_two);
void output();
};
Hedadd input(string input_string, char zheng, Hedadd first_string, Hedadd add);
void changeint(char a[100],int b[100])
{
for (int i = 0; i<100; i++)
{
if (a[i] <= '9'&&a[i]>='0')
b[i] = a[i] - '0';
else
b[i] = a[i] - 'A' + 10;
}
}
string changestring(int a[100])
{
char b[101];
for (int i = 0; i<100; i++)
{
if (a[i] <= 9&&a[i] >= 0)
b[i] = a[i] + '0';
else
b[i] = a[i] + 'A' - 10;
}
b[100] = '\0';
return string(b);
}
string xiangjia(string one, string two)
{
int one_length, two_length, longer, cha, smaller, sum[100];
char c_one[100];
char c_two[100];
int int_one[100];
int int_two[100];
string adding_result;
one_length = one.length();
two_length = two.length();
longer = one_length > two_length ? one_length : two_length;
for (int i = 0; i < 100; i++)
{
c_one[i] = '0';
c_two[i] = '0';
sum[i] = 0;
}
for (int i = 1; i <= one_length; i++)
{
c_one[100 - i] = one.c_str()[one_length - i];
}
for (int i = 1; i <= two_length; i++)
{
c_two[100 - i] = two.c_str()[two_length - i];
}
changeint(c_one,int_one);
changeint(c_two,int_two);
for (int i = 1; i<=longer; i++)
{
sum[100 - i] += int_one[100 - i] +int_two[100 - i];
if (sum[100 - i] >= 16)
{
sum[100 - i] = sum[100 - i] - 16;
sum[100 - i - 1] = sum[100 - i - 1] + 1;
}
}
adding_result = changestring(sum);
int count = 0;
for (count = 0; count < 100; count++)
{
if (sum[count]!=0)
{
break;
}
}
return (adding_result.substr(count,adding_result.length() - count));
}
int main()
{
string input_string;
char zheng;
Hedadd first_string, add;
zheng = '+';
add = input(input_string, zheng, first_string, add);
add.output();
return 0;
}
void Hedadd::set_the_neibu(string input_the_string)
{
input_string = input_the_string;
}
Hedadd input(string input_string, char zheng, Hedadd first_string, Hedadd add)
{
string temp = "";
cin >> temp;
add.set_the_neibu("0");
int offset = temp.find_first_of('+');
while (offset != std::string::npos)
{
first_string.set_the_neibu(temp.substr(0,offset));
add = add + first_string;
temp = temp.substr(offset+1, temp.length() - offset-1);
offset = temp.find_first_of('+');
}
first_string.set_the_neibu(temp.substr(0, offset));
add = add + first_string;
return add;
}
Hedadd operator+(const Hedadd& string_one, const Hedadd& string_two)
{
Hedadd string_together;
string_together.input_string = xiangjia(string_one.input_string, string_two.input_string);
return(string_together);
}
void Hedadd::output()
{
cout << input_string;
}
enter code here