Question: Write an algorithm of the C++ project below that describes both the conversion class and the driver program of the project below. By algorithm, this

Write an algorithm of the C++ project below that describes both the conversion class and the driver program of the project below. By algorithm, this should be a step-by-step set of instructions to solve a specific task. Each instruction will be written in English, and numbered in order. Each step should be detailed, concise, and unambiguous. The algorithm should be detailed enough to be translated to code, although it should not be actual code. You are describing steps to be carried out in any formal language, not just C++.

main driver file:

#include

#include

#include

#include "convert.h"

using namespace std;

int main()

{

string str;

cout <<

"Please enter a string representing a positive integer or decimal: ";

cin >> str;

convertToNumeric ctn(str);

long int integerValue = ctn.getIntValue();

double doubleValue = ctn.getDecValue();

double integral;

/* If the string represents an integer value, adding decimal portion to

* the decimal value.

*/

if (std::modf(doubleValue, &integral) == 0)

{

cout << "Integer Conversion: " << integerValue << endl;

cout << "Decimal Conversion: " << std::fixed << std::setprecision(1)

<< integral <

}

/*

* Else, display the values normally.

*/

else

{

cout << "Integer Conversion: " << integerValue << endl;

cout << "Decimal Conversion: " << doubleValue << endl;

}

/*

* Demonstrating that the converted values are useable with

* long int-specific and double-specific operations.

*/

cout << endl;

cout << "Demonstration of long int: " << integerValue << " + 100 = " <<

(integerValue + 100) << endl;

cout << "Demonstration of decimal: " << doubleValue << " + 100.12 = " <<

(doubleValue + 100.12) << endl;

return 0;

}

convert.cpp file:

#include

#include "convert.h"

using namespace std;

convertToNumeric::convertToNumeric()

{

str = "123";

}

/*

* This accessor function is the overloaded constructor that takes a string

* and calls the convert method to convert into respective integer and

* decimal value. There is not return value. The input is s, which is the

* string to be converted.

*/

convertToNumeric::convertToNumeric(string s)

{

str = s;

if(convert(str))

{

cout << "Conversion Successful!" << endl;

}

else

{

cout << "Conversion failed." <

}

}

/*

* This mutator function serves the purpose of converting a string into its

* respective integer and decimal values. The input is s, which is the

* string to be converted. The function returns a boolean value indicating

* whether conversion is successful or not.

*/

bool convertToNumeric::convert(string s)

{

bool success = false;

bool dotFound = false;

/*

* Here, we are looping through each character in the string and storing

* the integer portion in cstk1 and the decimal portion in cstk2.

* Ex: If s = 123.45, cstk1 = 321 and cstk2 = 54

* (since the stack follows LIFO)

*/

int i;

for(i = 0; i < s.size(); i++)

{

if(s[i] == '.') dotFound = true;

else if(dotFound)

{

cstk2.push(s[i]);

}

else

{

cstk1.push(s[i]);

}

}

/*

* Here, we are popping out each value from stack cstk1 and generating

* the integer value.

* Ex: s = 123.45 and cstk1 = 321, num = 0

* Iteration 1: num = 0 + (3 * 1) = 3

* Iteration 2: num = 3 + (2 * 10) = 23

* Iteration 3: num = 23 + (1 * 100) = 123

* Now intValue = 123

*/

long int num = 0; int multiplyValue = 1;

char val = '0';

while(cstk1.pop(val))

{

int valInInt = val - '0';

num += (valInInt * multiplyValue);

multiplyValue *= 10;

}

intValue = num;

/*

* Here, we are popping out each value from stack cstk2 and generating

* the decimal value.

* Ex: s = 123.45 and cstk2 = 54, num = 0, doubleValue = 123

* (equated to integer value)

* Iteration 1: num = 0 + (5 * 1) = 5

* Iteration 2: num = 5 + (4 * 10) = 45

* Iteration 3: num = 45 / 100 = 0.45

* Now doubleValue = 123 + 0.45 = 123.45

*/

doubleValue = double(num);

num = 0; multiplyValue = 1;

while(cstk2.pop(val))

{

int valInInt = val - '0';

num += (valInInt * multiplyValue);

multiplyValue *= 10;

}

doubleValue += (double(num) / multiplyValue);

success = true;

return success;

}

/*

* The accessor function below is returning the integer value of the string

* that has been converted.

*/

long int convertToNumeric::getIntValue()

{

return intValue;

}

/*

* The accessor function below is returning the double value of the string

* that has been converted.

*/

double convertToNumeric::getDecValue()

{

return doubleValue;

}

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!