2015-10-09 113 views
-2

我想創建一個128位的大數。將大數轉換爲二進制數

當我將字符串十進制轉換爲二進制並將位設置爲新數據QInt :__int64 a[2]時,它只有一個小數字(約10位數)的true

這是我的代碼:http://codepad.org/HmYqMQme

#include<iostream> 
#include<stdio.h> 
#include<string> 
using namespace std; 
//new data 
class QInt 
{ 
private: 
    __int64 a[2]; 
public: 
    void Get() 
    { 
     cout << a[0] << endl; 
     cout << a[1] << endl; 
    } 
    QInt() 
    { 
     a[0] = 0; 
     a[1] = 0; 
    } 
    //the funtion get the string binary of a[1] 
    char* GetQInt(char *A); 
    //the devide two of string decimal 
    char* Div2(char *Str); 
    //the funtion set bit to a[0] and a[1] 
    void Setbit(int i, int bit); 
    //con vert decimal to binary 
    QInt ConvertDecimalToBinary(char *De, char *Bi); 
}; 
//the funtion get the string binary of a[1] 
char *QInt::GetQInt(char *A) 
{ 

    for (int i = 0; i < 64; i++) 
    { 
     if (((a[1] >> i) & 1) != 0) 
     { 
      A[63 - i] = 49; 

     } 
     else 
     { 
      A[63 - i] = 48; 
     } 
    } 
    return A; 
} 
// the funtion set bit to a[0] and a[1] 
void QInt:: Setbit(int i, int bit) 
{ 

    //if i<64 we set bit to a[1] 
    if (i < 64) 
    { 
     if (bit==1) 
     { 
      a[1]=(1 << i) | a[1]; 

     } 


    }//similar to a[1] 
    else 
    { 
     if (bit == 1) 


      a[0]=(1 << i) | a[0]; 

    } 
} 
//the devide two of string decimal 
char*QInt:: Div2(char *Str) 
{ 

    char Arr[100]; 
    int n = strlen(Str); 
    int a = 0;//lay phan du 
    int i = 0; 
    int j = 0; 
    while (Str[i] == 0) 
    { 
     i++; 
    } 


    for (i; i < n; i++) 
    { 
     int c = Str[i] - 48 + a * 10; 
     a = c % 2; 

     Arr[j] = c/2 + 48; 
     j++; 
    } 
    Arr[i] = '\0'; 

    for (i = 0; i < strlen(Arr); i++) 
    { 
     Str[i] = Arr[i]; 
    } 
    Str[i] = '\0'; 
    return Str; 
} 
//con vert decimal to binary 
QInt QInt::ConvertDecimalToBinary(char *De,char *Bi) 

{ 
    int bit; 
    int i = 127; 
    int lenth = strlen(De); 
    while (1) 
    { 
     //variable h use to count the number 0 of the string decimal,if h=lenth,exit 
     int k = 0; 
     int h = 0; 
     while (De[k]) 
     { 
      if (De[k] == '0') 
       h++; 
       k++; 
     } 
     if (h == lenth) 
      break; 
     else 
     { 

      bit = (De[lenth - 1] - 48) % 2; 
      Bi[i] = bit + 48; 
      Setbit(127 - i, bit); 
      De = Div2(De); 
      i--; 
     } 
    } 
    Bi[128] = NULL; 

    return *this; 
} 


int main() 
{ 
    char s[100]; 
    char b[200]; 
    char c[200]; 
    for (int i = 0; i < 128; i++) 
    { 
     b[i] = 48; 
    } 
    cout << "Please enter a string : "; 
    gets_s(s, 99); 
    QInt a; 

    a.ConvertDecimalToBinary(s, b); 
    a.Get(); 
    a.GetQInt(c); 
    for (int i = 0; i < 64; i++) 
     cout << b[i]; 
    cout << endl; 
    for (int i = 64; i < 128; i++) 
     cout << b[i]; 
    cout << endl; 
    for (int i = 0; i < 64; i++) 
    { 
     cout << c[i]; 
    } 
    cout << endl; 

    system("pause"); 

    return 0; 
} 
+2

請在這裏輸入相關的代碼,而不僅僅是一個鏈接 – user463035818

回答

0

如果i==100,你有什麼感想(1 << i)呢? 應該它呢?爲什麼你忽略a[0]GetQInt

一般來說,你有很多問題。你對std::string感到不舒服,並且在任何地方都會混淆char*。你不清楚表達(甚至對自己)什麼方法應該做的。你正在把課程放在與班級無關的課堂上。更糟糕的是,有些方法重新定義了的名稱a!這很混亂。