题目要求:输入一个数代表多少字节,输出其对应的GBytes/Mbytes/Kbytes/Bytes。
通俗举例:1024000Byte= 1024000/1024=1000Kbytes;余数可以不考虑,取整即可。
题目看起来很简单,下面代码用到了字符数组、循环、右移运算,使其简洁精炼。
- void ByteTransformFun_pithy( long lByteVal )
- {
- std::cout << lByteVal << "Bytes = ";
- static const char* prx[]={ "B", "KB", "MB", "GB" };
- int i=0;
- for( ; lByteVal >= 1024 && i<3; ++i )
- {
- lByteVal = lByteVal >> 10;
- }
- std::cout << lByteVal << prx[i] << std::endl;
- }