A number expressed in scientific notation is represented by its mantissa (a frac
ID: 3819104 • Letter: A
Question
A number expressed in scientific notation is represented by its mantissa (a fraction) and its exponent. Write a function that reads a character string that represents a number in C++ scientific notation and stores the number in a struct with two members. Write a function that prints the contents of this struct as a real value. Also write functions that compute the sum, product, difference, and quotient of two numbers in scientific notation stored in two struct arguments. (Hint: The string -0.1234E20 represents a number in scientific notation. The fraction -0.1234 is the mantissa and the number 20 is the exponent).
Explanation / Answer
ScientificNumber.h
#include<string>
struct ScientificNumber
{
std::string mantissa;
std::string exponent;
};
main.cpp
#include<iostream>
#include<string>
#include"ScientificNumber.h"
using namespace std;
ScientificNumber ReadStringandConverttoScientificNumber()
{
string str;
cin>>str;
unsigned int locationOfE = str.find('E');
string mantissa = str.substr(0, locationOfE);
string exponent = str.substr(locationOfE + 1, str.size()-locationOfE);
ScientificNumber sn;
sn.mantissa = mantissa.c_str();
sn.exponent = exponent.c_str();
return sn;
}
string ScientificNumberinRealNumber(ScientificNumber number, bool print=true)
{
string realnumber = "";
int locationofdot = number.mantissa.find('.');
int numberofcharactersafterdot = (number.mantissa.substr(locationofdot+1, number.mantissa.size() - locationofdot)).size();
int numberofzeros = atoi(number.exponent.c_str()) - numberofcharactersafterdot;
unsigned int wheretoplacedot = 0;
for (size_t idx = 0; idx < number.mantissa.size(); idx++)
{
if (number.mantissa[idx] != '.')
{
realnumber.push_back(number.mantissa[idx]);
}
}
if (numberofzeros >= 0)
{
for (size_t idx = 0; idx < numberofzeros; idx++)
{
realnumber.push_back('0');
}
}
else
{
wheretoplacedot = realnumber.size() + numberofzeros;
string stringafterdot = realnumber.substr(wheretoplacedot, realnumber.size() - wheretoplacedot);
realnumber.erase(wheretoplacedot);
realnumber.push_back('.');
realnumber.append(stringafterdot);
}
if (print == true)
{
cout << realnumber <<endl;
}
return realnumber;
}
double sum(ScientificNumber a, ScientificNumber b)
{
double number1 = atof(ScientificNumberinRealNumber(a,false).c_str());
double number2 = atof(ScientificNumberinRealNumber(b,false).c_str());
return number1 + number2;
}
double product(ScientificNumber a, ScientificNumber b)
{
double number1 = atof(ScientificNumberinRealNumber(a,false).c_str());
double number2 = atof(ScientificNumberinRealNumber(b, false).c_str());
return number1 * number2;
}
double difference(ScientificNumber a, ScientificNumber b)
{
double number1 = atof(ScientificNumberinRealNumber(a, false).c_str());
double number2 = atof(ScientificNumberinRealNumber(b, false).c_str());
return number1 - number2;
}
double quotient(ScientificNumber a, ScientificNumber b)
{
double number1 = atof(ScientificNumberinRealNumber(a, false).c_str());
double number2 = atof(ScientificNumberinRealNumber(b, false).c_str());
return number1/number2;
}
int main()
{
ScientificNumber a = ReadStringandConverttoScientificNumber();
ScientificNumber b = ReadStringandConverttoScientificNumber();
ScientificNumberinRealNumber(a);
cout<<sum(a, b);
cout << difference(a, b);
cout << product(a, b);
cout << quotient(a, b);
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.