0
\$\begingroup\$

A week ago I did a three function calculator, I took the input from you guys and made it better. Overall it works fine, I didn't find any problems. But as a beginning programer I'd like some input on this calculator.

// Three function Calculator.cpp : main project file.
#include "stdafx.h"
#include<iostream>
using std::cout;
using std::cin;

long double action(long double num1, long double num2, char act) //External function to do all the calculations
{
    long double total;
    switch (act)
    {
    case '+':
        total = num1 + num2;
        break;
    case '-':
        total = num1 - num2;
        break;
    case '*':
        total = num1*num2;
        break;
    case '/':
        total = num1 / num2;
        break;
    default:
        cout << "Input not recognized\n";
    }
    return total;    //Giving the main function a number to work with.
}

int main()
{
    cout << "Enter action as # to exit program\n";
    cout << "Possible actions:+,-,*,/\n\n";
    long double num1, num2, num3, total;
    char act1, act2;
    while (1)   //inf loop
    {
        beginning:    //label, to reset the program in case of division by zero
        cin >> num1 >> act1 >> num2 >> act2 >> num3;
        if (act1 == '#' || act2 == '#')         //user wants to exit program
        {
            system("pause");
            return 0;
        }
        else if (act1 == '/' && num2 == 0 || act2 == '/' && num3 == 0)      //division by zero happens
        {
            cout << "You can't divide by zero\n";
            cout << "\n\n";
            goto beginning;              //restarting the program from the lable, to the beginning of the 'while' loop
        }
        else if ((act2 == '*' || act2 == '/') && (act1 == '-' || act1 == '+'))     //action2 happens before action1 because of order of operation
        {
            total = action(num2, num3, act2);             //sending the function reciving the total
            total = action(num1, total, act1);            //sending the total of previos function to get overall total
        }
        else      //action1 happens before action2 because of order of operation
        {
            total = action(num1, num2, act1);      //sending the function reciving the total
            total = action(total, num3, act2);     //sending the total of previos function to get overall total
        }
        cout << num1 << act1 << num2 << act2 << num3 << "=" << total;   //printing result of overall function
        cout << "\n\n";    //new line before restarting program
    }
}

Old Calculator


Example:

Input:-6*2+4
Output:-6*2+4=-8

\$\endgroup\$
2
  • \$\begingroup\$ Read about infix to postfix \$\endgroup\$
    – r-sniper
    Dec 11, 2016 at 19:24
  • \$\begingroup\$ Your link to the old calculator is a link to edit the question. Can you please fix that? \$\endgroup\$
    – User319
    Dec 11, 2016 at 19:26

1 Answer 1

1
\$\begingroup\$

A couple quick things to make maintenance easier:

It's easier to work out where you're going if you use continue instead of the goto. If you do keep the goto, I'd at least edit the comment to refer to the beginning of the while loop instead of a specific line number. That will change if you have to change something, and you don't want to have to remember to update the comment.

In terms of readability, when checking for order of operations, you could do something like

if (precedence(act2) > precedence(act1))

Then it would be easier if you wanted to add an exponent operator.

\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.