2016-12-16 39 views
-3

我的代碼只能添加兩個Hedecimal數字,但我們應該添加兩個以上,這樣的輸入就像這個FFFFFFFFFFFFF + 1 + 2 + 3 + 4可能更多addtion。我們可以使用運算符重載。 這是我的代碼。超過兩個數字十六進制加法

#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

您可以通過使用'文本的數字轉換爲數字INT數= A - 「0」;'。不需要switch語句。你也可以通過有一個字符數組來查找。 –

+0

非常感謝,我重寫了它。 –

回答

0

首先,您需要使用'+'符號來組合和標記字符串。

然後,您需要使用內置處理庫轉換它們 - 嘗試處理這些數據,因爲字符串只會導致您痛苦!

第三,我建議你重新開始,因爲這代碼看起來很糟糕,很抱歉:0

概念解決你的問題在紙上(考慮使用解析十六進制類型的本地處理),那麼解決它在IDE。

祝你好運!

+0

我的老師剛纔說我不應該是程序員。我現在會嘗試。非常感謝。 –

+0

留在它的朋友,你會變得很好我保證:) – Monza

+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; 
} 
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; 
} 
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