Question
RESPONSE DETAILS:
• Please provide a single file, formatted as a Word document, plain text file, or PDF.
**IMPORTANT** Other formats will not be accepted and cannot be evaluated!
• Provide one solution written in Java or C++ (C# acceptable.)
o Code should be production quality – clearly written, runnable, and documented.
• Provide several examples that call your function and demonstrate that it works.
• You may use any functions or classes from the JDK, STL, or .Net Framework. Do not include extraneous code that is not relevant to your solution.
• Provide the average runtime and space complexity (memory usage), and worst-case runtime and space complexity for your solution, and a short explanation as to why.
• State any assumptions you make for your solution.
QUESTION:
Write a function to convert an Integer representing a number of bytes (less than or equal to 1 Gigabyte) into an easy to read format, defined as follows:
• Maximum of 3 digits (not counting a decimal point), and a single letter to signify the unit of measure.
• No leading zeroes, or trailing zeroes after a decimal point.
• Be as accurate as possible.
IMPORTANT DETAILS:
• Maximum of 3 digits (not counting a decimal point), and a single letter to signify the unit of measure. Examples:
o 341B = 341B
o 34200B = 34.2K
o 5910000B = 5.91M
o 1000000000B = 1G
• No leading zeroes, or trailing zeroes after a decimal point. Examples:
o 34K, not 034K
o 7.2M, not 7.20M
• Be as accurate as possible. Example:
o 54123B = 54.1K, not 54K
• Note: For this problem, 1000 bytes =
Solution
/*
============================================================================
Author : James Chen
Email : a.james.chen@gmail.com
Description : Pretty Number Formatting.
Created Date : 18-01-2013
Last Modified :
============================================================================
*/
#include <iostream>
#include <iomanip>
#include <string>
#include <cstdio>
#include <regex>
#include <iterator>
using namespace std;
#pragma warning(disable : 4996)
string FormatNum(int n)
{
char digits[11]{0};
char units[]{'B', 'K', 'M'};
char format[100]{0};
char unit;
if (n < 0 || n > 1000000000){
throw invalid_argument("Invalid input (number should less or equal 1G)");
}
int n1;
int l;
if (n < 1000){
unit = 'B';
sprintf_s(digits, "%d", n);
}
else if (n < 999500){
unit = 'K';
n1 = n / 1000;
l = to_string(n1).size();
sprintf_s(format, "%%3.%df", 3 - l);
sprintf_s(digits, format, n / 1000.0f); }
else if (n < 1000000){
unit = 'M';
sprintf_s(digits, "1");
}
else if (n < 999500000){
unit = 'M';
n1 = n / 1000000;
l = to_string(n1).size();
sprintf_s(format, "%%3.%df", 3 - l);
sprintf_s(digits, format, n / 1000000.0f);
}
else{
unit = 'G';
sprintf_s(digits, "1");
}
regex rx("\\.[0]{1,2}\\b");
regex rx1("\\..[0]\\b");
string sdigits = digits;
if (regex_search(sdigits, rx)){
*strchr(digits, '.') = '\0';
}
else if (regex_search(sdigits, rx1)){
*strrchr(digits, '0') = '\0';
}
char *last = digits + strlen(digits);
*last = unit;
*(last + 1) = '\0';
return digits;
}
void DoTest(int n)
{
cout << "Input " << setw(10) << n << " --- " << FormatNum(n) << endl;
}
int main()
{
int testCases[]
{
341,
34200,
5910000,
1000000000,
34000,
999900,
54123,
1000,
1001,
1019,
1200,
0,
999500,
999400,
999500000,
999400000,
10,
100
};
for (auto i : testCases){
DoTest(i);
}
return 0;
}
Output
Input 341 --- 341B
Input 34200 --- 34.2K
Input 5910000 --- 5.91M
Input 1000000000 --- 1G
Input 34000 --- 34K
Input 999900 --- 1M
Input 54123 --- 54.1K
Input 1000 --- 1K
Input 1001 --- 1K
Input 1019 --- 1.02K
Input 1200 --- 1.2K
Input 0 --- 0B
Input 999500 --- 1M
Input 999400 --- 999K
Input 999500000 --- 1G
Input 999400000 --- 999M
Input 10 --- 10B
Input 100 --- 100B
Press any key to continue . . .