SuperTutor

(15)

$15/per page/Negotiable

About SuperTutor

Levels Tought:
Elementary,Middle School,High School,College,University,PHD

Expertise:
Accounting,Business & Finance See all
Accounting,Business & Finance,Economics,Engineering,HR Management,Math Hide all
Teaching Since: Apr 2017
Last Sign in: 327 Weeks Ago, 5 Days Ago
Questions Answered: 12843
Tutorials Posted: 12834

Education

  • MBA, Ph.D in Management
    Harvard university
    Feb-1997 - Aug-2003

Experience

  • Professor
    Strayer University
    Jan-2007 - Present

Category > Programming Posted 10 Jul 2017 My Price 12.00

zero decimal fraction

Below is my code, but I need help on two things:

1)  If both operands have a zero decimal fraction (i.e. the decimal value is all zeroes, such as in 1.0 or 2.000, then treat the operands as integers and perform the operations -- HOW?

2) otherwise output all floating point results to four fixed decimal places. 

 

int main()

{

 

double num1, num2, total, remainder;

int line;

char sign;

ifstream inFile;

ofstream outFile;

 

 

//

//Input

//

inFile.open("inputPartb.txt");     //open input file

if (!inFile)              //check for failure

{

    cout <<"Problem opening input file";

    return 1;           //return with value 1 -> not successful

}

 

outFile.open("resultsPartb.txt");    //open output file

if (outFile.fail())          //check for failure

{                   //.fail() will return non-zero (true) if open failed

  cout << "Problem opening output file";

  exit (1);             //exit with value 1 -> not successful

}

  inFile >> num1 >> num2 >> sign;

  line = 0;

 

  do

  {

    line++;

    switch (sign)

    {

      case '+':

        total = num1 + num2;

        cout << num1 << " " << sign << " " << num2 << " = " << total << endl;

        break;

      case '-':

        total = num1 - num2;

        cout << num1 << " " << sign << " " << num2 << " = " << total << endl;

        break;

      case '*':

        total = num1 * num2;

        cout << num1 << " " << sign << " " << num2 << " = " << total << endl;

        break;

      case '/':

        if (num2 == 0)

          {

            cout << num1 << " " << sign << " " << num2 << " = ERROR " << endl;

            cout << "ERROR encountered on line " << line << endl;

          }

        else

        {

          total = static_cast<int>(num1) / static_cast<int>(num2);

          remainder = static_cast<int>(num1) % static_cast<int>(num2);

          cout << num1 << " " << sign << " " << num2 << " = " << total << "R" << remainder << endl;

        }

        break;

      case '^':

        if (num1 == 0 && num2 == 0)

        {

          cout << num1 << " " << sign << " " << num2 << " = ERROR" << endl;

          cout << "ERROR encountered on line " << line << endl;

        }

        total = pow(num1, num2);

        cout << num1 << " " << sign << " " << num2 << " = " << total << endl;

        break;

      default:

        cout << num1 << " " << sign << " " << num2 << " = ILLEGAL" << endl;

        cout << "ILLEGAL operator encountered on line " << line << endl;

        break;

      }

  inFile >> num1 >> num2 >> sign;

  }

  while (!inFile.eof());

 

  inFile.close();

  outFile.close ();

}

Answers

(15)
Status NEW Posted 10 Jul 2017 06:07 AM My Price 12.00

-----------

Attachments

file 1499669584-Solutions file.docx preview (51 words )
S-----------olu-----------tio-----------ns -----------fil-----------e -----------Hel-----------lo -----------Sir-----------/Ma-----------dam----------- T-----------han-----------k y-----------ou -----------for----------- yo-----------ur -----------int-----------ere-----------st -----------and----------- bu-----------yin-----------g m-----------y p-----------ost-----------ed -----------sol-----------uti-----------on.----------- Pl-----------eas-----------e p-----------ing----------- me----------- on----------- ch-----------at -----------I a-----------m o-----------nli-----------ne -----------or -----------inb-----------ox -----------me -----------a m-----------ess-----------age----------- I -----------wil-----------l b-----------e q-----------uic-----------kly----------- on-----------lin-----------e a-----------nd -----------giv-----------e y-----------ou -----------exa-----------ct -----------fil-----------e a-----------nd -----------the----------- sa-----------me -----------fil-----------e i-----------s a-----------lso----------- se-----------nt -----------to -----------you-----------r e-----------mai-----------l t-----------hat----------- is----------- re-----------gis-----------ter-----------ed -----------onÂ----------- th-----------is -----------web-----------sit-----------e -----------Tha-----------nk -----------you----------- -----------
Not Rated(0)