Question
IN C++ Inspired by the Linux bc command, your program will permit conversion between any two bases in the range 2 to 10. (Going beyond
IN C++
Inspired by the Linux bc command, your program will permit conversion between any two bases in the range 2 to 10. (Going beyond base 10 would require the introduction of additional digits such as abcdef used in hexadecimal.) The input base is called ibase and the output base is called obase. For example, ibase can be 2 and obase can be 10 for conversion from base 2 to base 10, or vice versa. The ibase and obase can be the same (not very useful but good for testing), or one could be 5 (the valid digits are 0 to 4) and the other could be 8 (the valid digits are 0 to 7).
Part 1
Pseudocode describes the basic steps of an algorithm without the need to follow the precise syntax of a particular programming language. Like flowcharts, pseudocode describes all control flow such as if's and loops but without the need to draw diagrams.
int string_to_int(string input, int ibase) { if (ibase is invalid) { return -1 } int result = 0 for (i from 0 to (input.length() - 1)) { if (digit is not a valid ibase digit) { return -1 } convert the input[i] printable character to an integer named digit_int result = (result * ibase) + digit_int } return result }
string int_to_string(int value, int obase) { if (value is negative or obase is invalid) { return empty string } declare a string named output do { int remainder = value % obase convert the integer remainder to a printable character named digit_char prepend digit_char to output value = value / obase } while (value != 0) return output }
Part 2
Implement the convert() function that uses string_to_int() and int_to_string():
void convert(istream in, ostream out, int ibase, int obase) { if (ibase or obase are invalid) { return } declare a string named ibase_str read ibase_str from the in stream while (ibase_str != "x") { convert the ibase_str to an integer using string_to_int() convert the integer to obase_str using int_to_string() if (obase_str is not empty) { write obase_str and a newline to the out stream } read ibase_str from the in stream } }
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started