gpt4 book ai didi

c++ - 读取 12 位输入 C++

转载 作者:行者123 更新时间:2023-11-28 05:43:30 25 4
gpt4 key购买 nike

我正在尝试编写一个可以读写 12 位的小程序。输入应该没有任何问题,但我会把它包括在内,以便您更好地理解问题。输入应由下面包含的 OFStream12Bits.cpp/main.cpp 创建为 sample.lzw,输出应从写入函数中读取 sample.lzw。我在阅读代码时遇到输出问题,并且在主体中出现代码不匹配。我认为问题出在 operator>> 上,而 readBit 函数并不确定。非常感谢您的帮助,我已经坚持了一段时间!

readbit的指令如下...

//basic readBit
//read12Bits(): 12Bit =
//declare Result : 12Bit = 0;
//for i = 1 to 12
//do
//declare lBit : Bit = get bit from input
//if(lBit == 1)
//then Result = (1 << (i-1)) + Result; //set bit at index i
//od
//return result

我不明白的部分是我需要返回 *this 但没有 + 运算符所以我不能使用结果来设置索引 i 处的位。目前我有这样的代码。

IFStream12Bits& IFStream12Bits::operator>>(int& a12BitValue)
{
//int Result = a12BitValue;
//a12BitValue = ((a12BitValue & 0x0fff) << 1);
a12BitValue = a12BitValue & 0x0fff;


for (int i = 0; i < 12; i++)
{
int bit = readBit();
if (bit == 1)
{
a12BitValue = (1 << (i - 1)) + a12BitValue; //set bit at index i
}
}
return *this;
}

还有readBit的指令如下...

//implements mapping process. returns 0 or 1 depending on value of fBuffer[fByteIndex] & (1 << (fBitIndex - 1))
//see how it works with experiments
//at start check if (fByteCount == 0){reload();} then use reload() called as buffer does not contain any data before calling reload
//next fetch the bit store and then advance fByteIndex and fBitIndex
//if fBitIndex(highest to lowest) reaches 0 you need to switch to the next byte in the buffer. and also decrment fByteCount
//then finally return result

代码是

int IFStream12Bits::readBit()
{
if (fByteCount == 0){ reload(); }

//int bit = fBuffer[fByteIndex] & (1 << (fBitIndex - 1));

int bit = fBuffer[fByteIndex] & (1 << (fBitIndex - 1));
int result = 0;

cout << "bit: " << bit << endl;

//added this just cause
if (bit == 0)
{
result = 0;
}
else
{
result = 1;
}

//additional logic required?
fByteIndex++;
fBitIndex--;


//switch to next byte in the buffer
if (fBitIndex == 0)
{
fByteCount--;
fBitIndex = 8;
fByteIndex = 0;
}

return result;
}

如果您需要了解正在发生的事情,这里有完整的 .cpp 文件...IFStream12Bits.cpp

#include "IFStream12Bits.h"

#include <iostream>
using namespace std;


//default constructor
IFStream12Bits::IFStream12Bits()
{
init();
}

//takes aFIleName
IFStream12Bits::IFStream12Bits(const char* aFileName)
{
init();
open(aFileName);
}

//deconstructor
IFStream12Bits::~IFStream12Bits()
{
close();
}

//initialize the integer member variables with sensible values
//:fBuffer(), fByteCount(0), fByteIndex(0), fBitIndex(8)
//fBitIndex(highToLow)
void IFStream12Bits::init()
{
for (int i = 0; i < 32; i++)
{
fBuffer[i] = 0;
}

fByteCount = 0;
fByteIndex = 0;
fBitIndex = 8;
}

//fills input buffer fBuffer with the next 32 bytes and sets fByteCount to number of bytes read
void IFStream12Bits::reload()
{
//fills fBuffer with 32 bytes
fIStream.read((char*)fBuffer, 32);
//fIStream.read((char*)fBuffer, fByteIndex + (fBitIndex % 8 ? 1 : 0));
//sets fByteCount to number of bytes read
fByteCount = fIStream.gcount();
}

//implements mapping process. returns 0 or 1 depending on value of fBuffer[fByteIndex] & (1 << (fBitIndex - 1))
//see how it works with experiments
//at start check if (fByteCount == 0){reload();} then use reload() called as buffer does not contain any data before calling reload
//next fetch the bit store and then advance fByteIndex and fBitIndex
//if fBitIndex(highest to lowest) reaches 0 you need to switch to the next byte in the buffer. and also decrment fByteCount
//then finally return result
int IFStream12Bits::readBit()
{
if (fByteCount == 0){ reload(); }

//int bit = fBuffer[fByteIndex] & (1 << (fBitIndex - 1));

int bit = fBuffer[fByteIndex] & (1 << (fBitIndex - 1));
int result = 0;

cout << "bit: " << bit << endl;

if (bit == 0)
{
result = 0;
}
else
{
result = 1;
}

//additional logic required?
fByteIndex++;
fBitIndex--;


//switch to next byte in the buffer
if (fBitIndex == 0)
{
fByteCount--;
fBitIndex = 8;
fByteIndex = 0;
}

return result;
}

void IFStream12Bits::open(const char* aFileName)
{
fIStream.open(aFileName, std::fstream::binary);
}

void IFStream12Bits::close()
{
fIStream.close();
}

bool IFStream12Bits::fail()
{
return fIStream.fail();
}

//true if no bytes left in input stream (fByteCount == 0)(should be zero if never read anythign from fIStream)
bool IFStream12Bits::eof()
{
return fByteCount == 0;
}

//read 12Bit codes from the bit input stream implements the read12Bits algorithm as shown in the tutorial
//basic readBit
//read12Bits(): 12Bit =
//declare Result : 12Bit = 0;
//for i = 1 to 12
//do
//declare lBit : Bit = get bit from input
//if(lBit == 1)
//then Result = (1 << (i-1)) + Result; //set bit at index i
//od
//return result
// multiply values by 2 to shift left???????????
IFStream12Bits& IFStream12Bits::operator>>(int& a12BitValue)
{
//int Result = a12BitValue;
//a12BitValue = ((a12BitValue & 0x0fff) << 1);
a12BitValue = a12BitValue & 0x0fff;


for (int i = 0; i < 12; i++)
{
int bit = readBit();
if (bit == 1)
{
a12BitValue = (1 << (i - 1)) + a12BitValue; //set bit at index i
}
}
return *this;
}

OFStream12Bits.cpp

#include "OFStream12Bits.h"

OFStream12Bits::OFStream12Bits()
{
init();
}

OFStream12Bits::OFStream12Bits(const char* aFileName)
{
init();
open(aFileName);
}

OFStream12Bits::~OFStream12Bits()
{
close();
}

void OFStream12Bits::init()
{
for (int i = 0; i < 32; i++)
{
fBuffer[i] = 0;
}

fByteIndex = 0;
fBitIndex = 8;
}

void OFStream12Bits::writeBit0()
{
fBitIndex--;
finishWriteBit();
}

void OFStream12Bits::writeBit1()
{
fBuffer[fByteIndex] += 1 << (fBitIndex - 1);
fBitIndex--;
finishWriteBit();
}

void OFStream12Bits::finishWriteBit()
{
if (fBitIndex == 0)
{
if (fByteIndex == 31)
{
fByteIndex++;
//write full buffer to stream
flush();
}
else
{
fByteIndex++;
fBitIndex = 8;
}
}
}

void OFStream12Bits::open(const char* aFileName)
{
fOStream.open(aFileName, std::ofstream::binary);
}

bool OFStream12Bits::fail()
{
return fOStream.fail();
}

void OFStream12Bits::close()
{
flush();
fOStream.close();
}

void OFStream12Bits::flush()
{
// do we need to add last byte?
fOStream.write((char*)fBuffer, fByteIndex + (fBitIndex % 8 ? 1 : 0));
init();
}

OFStream12Bits& OFStream12Bits::operator<<(int a12BitValue)
{
a12BitValue = a12BitValue & 0x0fff; // mask 12 lower bits

for (int i = 0; i < 12; i++) //write 12 bits
{
if (a12BitValue & 0x01) // the current lowest bit is set
{
writeBit1();
}
else
{
writeBit0();
}
a12BitValue >>= 1; // code = code / 2 --shifting value accross
}

return *this;
}

主要.cpp

#include "OFStream12Bits.h"
#include "IFStream12Bits.h"
#include <iostream>

using namespace std;

void write4096()
{
cout << "Write 4096 codes" << endl;

OFStream12Bits lWriter("sample.lzw");

if (lWriter.fail())
{
cerr << "Error: unable to open output file" << endl;
exit(1);
}

for (int i = 4096; i >= 0; i--)
{
lWriter << i;
}
}

void read4096()
{
cout << "Read 4096 codes" << endl;

IFStream12Bits lInput("sample.lzw");

if (lInput.fail())
{
cerr << "Error: unable to open input file!" << endl;
exit(2);
}

for (int i = 4095; i >= 0; i--)
{
int l12BitValue;

lInput >> l12BitValue;

if (l12BitValue != i)
{
cerr << "Error: Code mismatch: " << l12BitValue << " != " << i << endl;
exit(3);
}
}

if (!lInput.eof())
{
cerr << "Error: Input stream not exhausted" << endl;
}
}

int main()
{
write4096();
read4096();
cout << "SUCCESS" << endl;

return 0;
}

最佳答案

您的输入代码以先前的值开始。您应该从 0 开始,因为您不会清除未设置的位。

IFStream12Bits& IFStream12Bits::operator>>(int& a12BitValue)
{
a12BitValue = 0;

for (int i = 0; i < 12; i++)
{
int bit = readBit();
if (bit == 1)
{
a12BitValue = (1 << (i - 1)) + a12BitValue; //set bit at index i
}
}
return *this;
}

此外,+将在这里工作,但在处理位时使用位运算更清楚。另外,我认为你的类次已经结束了。我会这样写设置位线:

a12BitValue |= 1 << i;

如果你想一想,当i为 0,则要设置第一位(即 11 << 0 。)当 i 时是 1,你想要下一位,依此类推。所以你不需要减一。

我不确定这是唯一的问题,但您可以尝试使用单元测试独立测试每个类。例如,从原始字节缓冲区开始,如 {0x89, 0xAB, 0xCD, 0xEF, 0x01} ,然后读取三组 12 位关闭。验证它们是否正确。然后创建一个空缓冲区,并向其中写入特定位,并检查字节是否正确。

通过独立测试算法,并使用非常严格的输入/输出,您会发现更容易确定缺陷。

关于c++ - 读取 12 位输入 C++,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36671708/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com