2010-12-06 78 views
0

alt text比較位(一次一個位置)

最初我有用戶輸入十進制數(0 - 15),我會把它轉換成二進制數。 說這些數字寫入文本文件,如圖所示。這些數字按1的數字排列。短劃線 - 用於分隔不同的組1.我要讀取此文件,並將一個組的字符串與下面的組中的所有字符串進行比較,即,組1中的所有字符串與組2中的所有字符串進行比較,以及第2組 - 第3組。

該交易是隻允許一列0/1的差異,並且該列由字母t代替。如果遇到不止一列的差異,則不填寫。 所以說組2,0001與組3,0011,只有第二列是不同的。然而,0010和0101是兩列不同。

結果將被寫入到另一個文件.....

此刻,當我讀這些字符串,我使用矢量。我遇到了困難。重要的是我必須一次訪問一個字符,這意味着我已經將矢量字符串分解爲矢量char。但似乎可能有更簡單的方法來做到這一點。

我甚至想過一個哈希表 - 鏈表。將組1分配給H [0]。 H [current_group + 1]與H [current-group]進行比較。但除了第一個比較(比較1和0)之外,超出此範圍的比較在這種散列鏈接方式下不起作用。所以我放棄了這一點。

#include <iostream> 
#include <fstream> 
#include <vector> 
#include <string> 
#include <algorithm> 
#include <iterator> 
using namespace std; 

int main() { 
    ifstream inFile("a.txt"); 
    vector<string> svec; 
    copy(istream_iterator<string>(inFile), istream_iterator<string>(), back_inserter(svec)); 
    copy(svec.begin(), svec.end(), ostream_iterator<string>(cout,"\n")); 
    for(int i = 0; i < svec.size(); i++) 
    { 
    cout << svec[i] << " "; 
    } 
    inFile.close(); 

    return 0; 
} 

這是它寫入文件的示例代碼....但就像我說的,載體的整個交易似乎在我的情況是不切實際....

任何幫助表示讚賞。感謝

+1

看起來非常像'家庭作業'請標記這樣...提示:按位操作,特別是異或會有所幫助。 – mjv 2010-12-06 01:19:51

回答

1

我不明白你的代碼片段 - 它看起來像它所做的一樣,是在輸入文件中讀入一個字符串向量,然後將每個空白分隔的單詞包含在一個單獨的字符串中,然後將其寫回以兩種不同的方式出現(一次用\n分開的單詞,一次用空格分隔)。

看來你所面臨的主要問題是閱讀和解釋文件本身,而不是做必要的計算 - 對嗎?這就是我希望這個答案能幫助你。

我認爲文件的行結構很重要 - 對嗎?在這種情況下,使用global getline() function in the <string> header會更好,它會將整行(而不是空格分隔的單詞)讀入一個字符串中。 (無可否認,這個函數相當隱蔽!)另外,你實際上並不需要將所有行讀入一個向量,然後處理它們 - 它更有效率,並且更容易隨時隨地將它們提取爲數字或位集:

vector<unsigned> last, curr; // An unsigned can comfortably hold 0-15 
ifstream inf("a.txt"); 

while (true) { 
    string line; 
    getline(inf, line); // This is the group header: ignore it 
    while (getline(inf, line)) { 
     if (line == "-") { 
      break; 
     } 

     // This line contains a binary string: turn it into a number 
     // We ignore all characters that are not binary digits 
     unsigned val = 0; 
     for (int i = 0; i < line.size(); ++i) { 
      if (line[i] == '0' || line[i] == '1') { 
       val = (val << 1) + line[i] - '0'; 
      } 
     } 

     curr.push_back(val); 
    } 

    // Either we reached EOF, or we saw a "-". Either way, compare 
    // the last 2 groups. 
    compare_them_somehow(curr, last); // Not doing everything for you ;) 
    last = curr; // Using swap() would be more efficient, but who cares 
    curr.clear(); 
    if (inf) { 
     break; // Either the disk exploded, or we reached EOF, so we're done. 
    } 
} 
0

也許我誤解你的目標,但字符串是經得起數組成員比較:

string first = "001111"; 
string next = "110111"; 
int sizeFromTesting = 5; 
int columnsOfDifference = 0; 

for (int UU = sizeFromTesting; UU >=0; UU--) 
{ 
    if (first[ UU ] != next[ UU ]) 
     columnsOfDifference++; 
} 
cout << columnsOfDifference; 
cin.ignore(99, '\n'); 
return 0; 

替代文件流和限制的保護在適當情況下。

不適用,但適用於字面上逐位比較變量,&都使用每個數字掩碼(第二個數字000010)。 如果或= 0,則它們匹配:兩者都爲0.如果它們或= 1且& = 1,那麼對於兩者都是1。否則他們不同。重複所有的位和組中的所有數字。

0

在vb.net

'group_0 with group_1 
      If (group_0_count > 0 AndAlso group_1_count > 0) Then 
       Dim result = "" 
       Dim index As Integer = 0 
      Dim g As Integer = 0 
      Dim h As Integer = 0 
      Dim i As Integer = 0 

      For g = 0 To group_0_count - 1 
       For h = 0 To group_1_count - 1 
        result = "" 
        index = 0 
        For i = 0 To 3 
         If group_1_0.Items(g).ToString.Chars(i) <> group_1_1.Items(h).ToString.Chars(i) Then 
          result &= "-" 
          index = index + 1 
         Else 
          result &= group_1_0.Items(g).ToString.Chars(i) 
         End If 
        Next 
       Next 
      Next 
     End If 
0

在閱讀它作爲一個整數,那麼你應該需要與bitshifts和位掩碼比較。