2010-12-06 81 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和位掩码比较。