Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

I am new to c++ and have been making the good old Calculator. I have gone forwar

ID: 655055 • Letter: I

Question

I am new to c++ and have been making the good old Calculator. I have gone forward from a 2 value calculator to a 3 value calculator and wondered.. How is it possible to make a 10 digit calculator (purely using +,-,*,/) that doesn't forever to code.. I have a if statement which returns what happens if the operators are + and + or + and - etc.. and if i was to go to 4 value, i would be writing 64 ifs, and 5 value would be 256 if statements.

Is there a way to change this such that it wouldn't require writing out so many if statements?

Here is my the important parts of the code:

Calculator.cpp:

#include "stdafx.h"
#include <iostream>
#include "headers.h"
using namespace std;

int main()
{
int nInput1 = GetUserInput();
char chOperation = GetMathematicalOperation();
{
    if (chOperation == '=')
    {
        cout << "The answer is: " << nInput1 << endl;
        return 0;
    }
}
int nInput2 = GetUserInput();
char chOperation2 = GetMathematicalOperation();
{
    if (chOperation2 == '=')
    {
        int nResult = CalculateResult(nInput1, chOperation, nInput2);
        cout << "The answer is: " << nResult << endl;
        return 0;
    }
}

int nInput3 = GetUserInput();

int nResult = CalculateResult2(nInput1, chOperation, nInput2, chOperation2, nInput3);

PrintResult(nResult);
}
CalculateResult.cpp:

#include "stdafx.h"
#include <iostream>
#include <string>
using namespace std;


int CalculateResult(int nX, char chOperation, int nY)
{
if (chOperation == '+')
    return nX + nY;
if (chOperation == '-')
    return nX - nY;
if (chOperation == '*')
    return nX * nY;
if (chOperation == '/')
    return nX / nY;

return 0;
}
GetMathematicalOperation.cpp:

#include "stdafx.h"
#include <iostream>
#include <string>
using namespace std;

char GetMathematicalOperation()
{
cout << "Please enter an operator (+,-,*,/ or =): ";

char chOperation;
cin >> chOperation;
return chOperation;
}
CalculateResult2.cpp:

#include "stdafx.h"
#include <iostream>
#include <string>
using namespace std;


int CalculateResult2(int nX, char chOperation, int nY, char chOperation2, int nZ)
{
if (chOperation == '+' && chOperation2 == '+')
    return nX + nY + nZ;
if (chOperation == '+' && chOperation2 == '-')
    return nX + nY - nZ;
if (chOperation == '+' && chOperation2 == '*')
    return nX + nY * nZ;
if (chOperation == '+' && chOperation2 == '/')
    return nX + nY / nZ;

if (chOperation == '-' && chOperation2 == '+')
    return nX - nY + nZ;
if (chOperation == '-' && chOperation2 == '-')
    return nX - nY - nZ;
if (chOperation == '-' && chOperation2 == '*')
    return nX - nY * nZ;
if (chOperation == '-' && chOperation2 == '/')
    return nX - nY / nZ;

if (chOperation == '*' && chOperation2 == '+')
    return nX * nY + nZ;
if (chOperation == '*' && chOperation2 == '-')
    return nX * nY - nZ;
if (chOperation == '*' && chOperation2 == '*')
    return nX * nY * nZ;
if (chOperation == '*' && chOperation2 == '/')
    return nX * nY / nZ;

if (chOperation == '/' && chOperation2 == '+')
    return nX / nY + nZ;
if (chOperation == '/' && chOperation2 == '-')
    return nX / nY - nZ;
if (chOperation == '/' && chOperation2 == '*')
    return nX / nY * nZ;
if (chOperation == '/' && chOperation2 == '/')
    return nX / nY / nZ;
return 0;
}

Explanation / Answer

Programming is all about data structures and algorithms. In your example, there is no apparent data strcture: your functions are working with raw user input and directly provide the answer, without introducing any intermediary structure to represent the logic of the computation.

As a practical consequence of the well-known equation

programs = data structures + algorithms
the absence of adequate data structure forces you to represent the logic of the computation in the algorithms part, that is, in your code. This is why your code looks so complicated, with that large number of ifs. And your instinct is right, whenever your code looks complicated, it shall ring a bell yelling there is something wrong and that something is typically that you are using the wrong data structure.

The abstraction you are missing here is what is called an algebraic expression, which you can think of as an unevaluated expression. So now, I will answer your question on how to write your calculator

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Chat Now And Get Quote