Question
1. You may NOT use string streams for this lab. 2. You MUST use the given template. 3. You may ONLY write the four prototyped
1. You may NOT use string streams for this lab.
2. You MUST use the given template.
3. You may ONLY write the four prototyped functions: CharToInt, IntToChar, StringToInt, IntToString.
4. You may NOT call any C++ library function from within your four functions. However, you may call one of your other four functions.
#include#include using namespace std; //Prototypes for the functions that you will write int CharToInt(char v); char IntToChar(int v); int StringToInt(string val); string IntToString(int val); int main(int argc, char *argv[]) { string sresult; int left; int right; char op; if (4 != argc) { printf("Usage: %s ", argv[0]); return -1; } //Notice that this calls your StringToInt. So, make sure you debug //StringToInt() to make sure that left and right get a proper //value. left = StringToInt(argv[1]); right = StringToInt(argv[3]); op = argv[2][0]; //Calculate based on the op. Notice that this calls IntToString, //so debug your IntToString() function to make sure that sresult //is given the proper result. This assumes your StringToInt already //works. switch (op) { case 'x': sresult = IntToString(left * right); break; case '/': sresult = IntToString(left / right); break; case '+': sresult = IntToString(left + right); break; case '-': sresult = IntToString(left - right); break; case '%': sresult = IntToString(left % right); break; default: sresult = IntToString(left); break; } //Remember, printf is the only output function you may use for this lab! //The format string is %d %c %d = %s. This means that the first argument //is %d (decimal / integer), %c (character), %d (decimal /integer), //%s (string). Notice that because printf() is a C-style function, you //must pass strings as character arrays. We can convert a C++ string //to a character array (C-style string) by using the c_str() member function. printf("%d %c %d = %s ", left, op, right, sresult.c_str()); return 0; }
CharToInt
This function will take a single, positive character between the values 0 - 9 and return the single digit integer equivalent. For example, '0' will return 0 and '8' will return 8 (as an 8-bit integer).
IntToChar
This function will take a single digit, positive integer and return the character equivalent. This is essentially the reverse of CharToInt. For example, 0 will return '0', 8 will return '8'.
IntToString
This function will take any sized integer, including negative numbers, and convert it into a string. For example, -12345678 will return "-12345678".
StringToInt
This function will take a string and convert it to the equivalent number, including negative numbers. For example, "-123456" will return -123456.
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