#include #include #include #include //----------------------------------------------------------------------------- //----------------------------------------------------------------------------- class TCEmployee { public: TCEmployee(); double mTemperature; std::string mTemperatureUnits; std::string mTemperatureName; unsigned mTemperatureSigFigs; int mAge; std::string mAgeUnits; std::string mAgeName; // Imagine dozens of additional parameters void Print(); void Decode(const std::string& Bytes); std::string mBytes; }; //----------------------------------------------------------------------------- //----------------------------------------------------------------------------- TCEmployee::TCEmployee() : mTemperature(), mTemperatureUnits("deg F"), mTemperatureName("Temperature"), mTemperatureSigFigs(1), mAge(), mAgeUnits("years"), mAgeName("Age") { } //----------------------------------------------------------------------------- //----------------------------------------------------------------------------- std::string DoubleToString(double Value, int Digits) { std::ostringstream Stream; Stream << std::fixed << std::setprecision(Digits) << Value; return Stream.str(); } //----------------------------------------------------------------------------- //----------------------------------------------------------------------------- void TCEmployee::Print() { std::cout << mTemperatureName << ": " << DoubleToString(mTemperature, mTemperatureSigFigs) << " (" << mTemperatureUnits << ")\n" << mAgeName << ": " << mAge << " (" << mAgeUnits << ")\n"; } //----------------------------------------------------------------------------- //----------------------------------------------------------------------------- void TCEmployee::Decode(const std::string& Bytes) { // First two bytes are a temperature encoded as an uint16. // 0=>25 DegF // 2^16=>125 DegF // // Next four bytes Age is encoded as an int32 // // Byte Value // 0 Temperature LSB // 1 Temperature MSB // 2 Age LSB // 3 Age ... // 4 Age ... // 5 Age MSB // // Total bytes: 6 static const unsigned ByteCount = 6; mBytes.append(Bytes); if (mBytes.size() < ByteCount) { return; } std::istringstream Stream(Bytes.substr(0, ByteCount)); unsigned short Temperature; Stream.read(reinterpret_cast(&Temperature), 2); mTemperature = Temperature / 65536.0 * 100.0 + 25.0; Stream.read(reinterpret_cast(&mAge), 4); mBytes.erase(0, ByteCount); } //----------------------------------------------------------------------------- //----------------------------------------------------------------------------- class TCTimer { public: void Process(TCEmployee& Employee) { Employee.mAge = Employee.mAge + 1; }; }; //----------------------------------------------------------------------------- //----------------------------------------------------------------------------- int main() { TCEmployee Employee; static const unsigned Capacity = 16; char Buffer[Capacity]; while (std::cin.read(Buffer, Capacity)) { Employee.Decode(std::string(Buffer, Capacity)); } TCTimer Timer; Timer.Process(Employee); Employee.Print(); return 0; }