//
// Base Converter
//
// Integer number base converter (binary to base 36)
//
// * NOTE: User defined identifiers are shown surrounded by < >.
// DO NOT TYPE THE < > as part of the names.
// * Do not deviate from the algorithm. Follow the run example in the
// accompanying pdf description of the algorithm. Make sure your output
// looks like the example.
//
// PROGRAM STARTS BELOW THIS LINE ------------------------------------------
// FILL IN THE CODE that includes all the necessary headers
// (up to one extra without penalty) and namespaces
#include <iostream>
#include <string>
#include <fstream>
// FILL IN THE CODE with the prototypes of any function used by
// main and defined after main.
void overwriteFileAndReopen(std::ifstream& ifst, std::string fileName, std::string text);
unsigned int stream2unsigned(std::ifstream& ifst, unsigned int& fact, unsigned int base);
std::string unsigned2string(unsigned int numb, unsigned int base);
int main()
{
// FILL IN A STATEMENT that writes to the console the prompt
// "Enter base to change from: ". See pdf instructions for an example.
std::cout << "Enter base to change from:";
// FILL IN A STATEMENT to define an integer variable called <baseFrom>
int baseFrom;
// FILL IN A STATEMENT that reads <baseFrom> from the console
std::cin >> baseFrom;
// FILL IN A STATEMENT that writes to the console the prompt
// "Enter base to change to: ". See pdf instructions for an example.
std::cout << "Enter base to change to:";
// FILL IN A STATEMENT to define an integer variable called <baseTo>
int baseTo;
// FILL IN A STATEMENT that reads <baseTo> from the console
std::cin >> baseTo;
// FILL IN THE CODE that defines a string variable called <fileName> and
// initializes it to "converterText.txt"
std::string fileName = "converterText.txt";
// FILL IN A STATEMENT that defines a string variable called <number>
std::string number;
// FILL IN THE CODE for a loop that repeats as long as <number> is not "0"
// the loop contains the following statements (1-7):
while (number != "0")
{
// 1. FILL IN A STATEMENT that writes to the console the prompt
// "Enter number (0 to exit): ". See pdf instructions for an example.
std::cout << "Enter number (0 to exit): ";
// 2. FILL IN A STATEMENT that reads <number> from the console
std::cin >> number;
// 3. FILL IN A STATEMENT that defines an input file stream object called
// <input>
std::ifstream input;
// 4. FILL IN A STATEMENT that calls the function <overwriteFileAndReopen>
// with arguments <input>, <fileName>, and <number>
overwriteFileAndReopen(input, fileName, number);
// 5. FILL IN A STATEMENT that defines an integer called <n>
unsigned int n;
// 6. FILL IN A STATEMENT that defines an integer called <decimal> and
// initializes it with the return value resulting from calling the
// function <stream2unsigned> with arguments <input>, <n>, and <baseFrom>
unsigned int decimal = stream2unsigned(input, n, baseFrom);
// 7. FILL IN A STATEMENT that calls the function <unsigned2string> with
// arguments <decimal> and <baseTo> and writes the return value to the
// console preceded by "--> ". See pdf instructions for an example.
std::cout << "-->" << unsigned2string(decimal, baseTo) << std::endl;
}
}
// FILL IN THE CODE that defines the header to a void function called
// <overwriteFileAndReopen>. It takes the following three parameters:
// an input file stream object called <ifst> passed by reference,
// a string called <fileName>,
// and a string called <text>
void overwriteFileAndReopen(std::ifstream& ifst, std::string fileName, std::string text)
{
// The body of the function contains the following statements:
// 1. FILL IN A STATEMENT to close <ifst>
ifst.close();
// 2. FILL IN THE CODE to define an output file stream object called
// <oFile> and used it to open the file given in <fileName>
std::ofstream oFile(fileName);
// 3. FILL IN A STATEMENT to write the contents of <text> to the file
oFile << text;
// 4. FILL IN A STATEMENT that closes <oFile>
oFile.close();
// 5. FILL IN A STATEMENT to open the file indicated by <fileName>
// using the <ifst> object
ifst.open(fileName);
// 7. FILL IN THE CODE to check if <ifst> failed to open. If it did,
// it executes the following two statements:
if (!ifst.is_open())
{
// A. FILL IN A STATEMENT that write to the console the message
// "Error reading ", followed by <fileName>
std::cout << "Error reading " << fileName;
// B. FILL IN A STATEMENT to end immediately the program execution
exit(-1);
}
}
// FILL IN THE CODE that defines the header to a function called <head> that
// returns a character. It takes one parameter, an input file stream object
// called <ifst> passed by reference.
char head(std::ifstream& ifst)
{
// The body of the function contains the following statements:
// 1. FILL IN THE CODE that if <ifst> failed to open, it returns '\0'
if (!ifst.is_open())
{
return '\0';
}
// 2. FILL IN A STATEMENT that defines a character called <sc>
char sc = '\0';
// 3. FILL IN A STATEMENT that reads a single character into <sc> from the file
ifst.read(&sc, 1);
// 4. FILL IN A STATEMENT that returns the value of <sc>
return sc;
}
// FILL IN THE CODE that defines the header to a function called <char2digit> that
// returns a positive integer. It takes one parameter of character type named <a>
unsigned int char2digit(char a)
{
// The body of the function contains the following statements:
// 1. FILL IN THE CODE that if <a> is between '0' and '9', inclusive,
// it returns a - '0'
if (a >= '0' && a<= '9')
{
return a - '0';
}
// 2. FILL IN THE CODE that if <a> is between 'a' and 'z', inclusive,
// it returns a - 'a' + 10
if (a >= 'a' && a <= 'z')
{
return a - 'a' + 10;
}
// 3. FILL IN THE CODE that if <a> is between 'A' and 'Z', inclusive,
// it returns a - 'A' + 10
if (a >= 'A' && a <= 'Z')
{
return a - 'A' + 10;
}
// 4. FILL IN THE CODE that returns 0 if none of the above conditions are
// satisfied
return 0;
}
// FILL IN THE CODE that defines the header to a function called <digit2char> that
// returns a character. It takes one parameter of positive integer type called <a>
char digit2char(unsigned int a)
{
// The body of the function contains the following statements:
// 1. FILL IN THE CODE that if <a> is between numbers 0 and 9, inclusive,
// it returns a + '0'
if (a >= 0 && a <= 9)
{
return a + '0';
}
// 2. FILL IN THE CODE that if <a> is between numbers 10 and 36, inclusive,
// it returns a - 10 + 'A'
if (a >= 10 && a <= 36)
{
return a - 10 + 'A';
}
// 3. FILL IN THE CODE that returns 0 if none of the above conditions are
// satisfied
return 0;
}
// FILL IN THE CODE that defines the header to a function called <stream2unsigned>
// that returns a positive integer. It takes the following three parameters:
// an input file stream object called <ifst> passed by reference,
// a positive integer called <fact> passed by reference,
// and a positive integer called <base>
unsigned int stream2unsigned(std::ifstream& ifst, unsigned int& fact, unsigned int base)
{
// The body of the function contains the following statements:
// 1. FILL IN THE CODE that if <ifst> failed to open or <base> is greater
// than 36, it returns 0
if (!ifst.is_open() || base > 36)
{
return 0;
}
// 2. FILL IN A STATEMENT that sets <fact> to 1
fact = 1;
// 3. FILL IN A STATEMENT that defines a character called <sc> and initializes
// it to the return value of calling the function <head> with argument
// <ifst>
char sc = head(ifst);
// 4. FILL IN THE CODE that if <sc> is '\0' or '\n', it returns 0
if (sc == '\0' || sc == '\n')
{
return 0;
}
// 5. FILL IN A STATEMENT that defines a positive integer called <sum> and
// initializes it to the return value of calling the function
// <stream2unsigned> with arguments <ifst>, <fact>, and <base>
unsigned int sum = stream2unsigned(ifst, fact, base);
// 6. FILL IN A STATEMENT that defines a positive integer called <dec>.
// Initialize <dec> to the result of calling <char2digit> with argument <sc>
// times <fact> plus <sum>
// (that is, something like <dec> equal to char2digit * <fact> + <sum>)
unsigned int dec = char2digit(sc) * fact + sum;
// 7. FILL IN A STATEMENT that sets <fact> to itself times <base>
fact *= base;
// 8. FILL IN A STATEMENT that returns <dec>
return dec;
}
// FILL IN THE CODE that defines the header to a function called <unsigned2string>
// that returns a string. It takes two parameters:
// a positive integer called <numb>,
// and a positive integer called <base>
std::string unsigned2string(unsigned int numb, unsigned int base)
{
// The body of the function contains the following statements:
// 1. FILL IN THE CODE that if <numb> is not zero, it returns the sum of the
// return value of calling the function <unsigned2string> with arguments
// <numb> / <base> and <base>, and the function <digit2char> with argument
// <numb> % <base>
if (numb != 0)
{
return unsigned2string(numb / base, base) + digit2char(numb % base);
}
// 2. FILL IN THE CODE that that returns "0"
return "0";
}