Home Upgrade Search Memberlist Extras Hacker Tools Award Goals Help Wiki Contact

C 

c++ calculator 2nd version

I used a class this time and added a few more options plus a while loop, check it out and let me know what I did wrong and how to improve it.
MyMath.h
Code
#ifndef MYMATH_H
#define MYMATH_H


class MyMath
{
    public:
        MyMath();
        int add(int x, int y);
        int multiply(int x, int y);
        double divide(double x, double y);
        int subtract(int x, int y);
        double sqrt(double x);
        int exponent(int x, int y);
};

#endif // MYMATH_H
MyMath.cpp
Code
#include "MyMath.h"

MyMath::MyMath()
{

}

int MyMath::add(int x, int y)
{
    int answer = x + y;
    return answer;
}

int MyMath::subtract(int x, int y)
{
    int answer = x - y;
    return answer;
}

double MyMath::divide(double x, double y)
{
    double answer = x / y;
    return answer;
}

int MyMath::multiply(int x, int y)
{
    int answer = x * y;
    return answer;
}

double MyMath::sqrt(double x)
{
    double g;

    g = x * 50;
    for (int i = 0; i <= x * 2; i++)
    {
        if (g == x)
            return g;
        if (g != x)
            g = (g + x / g) / 2;

    }

    return g;
}

int MyMath::exponent(int x, int y)
{
    int a = 1;

    for (int i = 0; i < y; i++)
        a *= x;

    return a;
}
main.cpp
Code
#include <iostream>
#include "MyMath.h"

int main()
{
    MyMath math;

    int x = 0, y = 0;
    int operation = 0;
    bool running = true;

    while (running)
    {
        std::cout << "Enter two numbers" << std::endl;
        std::cin >> x >> y;
        std::cout << "Choose your operation\n1.Addition\n2.Subtraction\n3.Division\n4.Multiplication\n5.Square root\n6.Exponent\n7.Quit" << std::endl;
        std::cin >> operation;

        switch (operation)
        {
            case 1:
                std::cout << math.add(x, y) << std::endl;
                break;
            case 2:
                std::cout << math.subtract(x, y) << std::endl;;
                break;
            case 3:
                std::cout << math.divide(x, y) << std::endl;;
                break;
            case 4:
                std::cout << math.multiply(x, y) << std::endl;;
                break;
            case 5:
                std::cout << math.sqrt(x) << std::endl;;
                break;
            case 6:
                std::cout << math.exponent(x, y) << std::endl;;
                break;
            case 7:
                running = false;
                std::cout << "Quitting...";
                break;
            default:
                std::cout << "Unknown command!" << std::endl;
        }
    }

    return 0;
}

for the sqrt algorithm in MyMath.cpp i searched online how to do it, it took me awhile to find a good guy to explain it right. It looked very difficult at first but as soon as I understood it, I realized that it is really easy. I like that I learned that much so far. Tomorrow will learn more and code more.

Thanks for the help guys
-Jekwa
This is well written, Well done! ✌
I can't think of any important tips to give.

Just to avoid the need to create a new variable you can return the sum directly. (I prefer how it looks to)
Code
int MyMath::add( int x, int y )
{
    return x + y;
}
@HippoSocks got it, thanks for the feedback. I am really out of ideas what to do next. I guess I will have a third version of the calculator implementing more functions, can't think of any at the moment, but will be up soon.
(Jun 5, 2014 06:12 AM)Jekwa Wrote:  @HippoSocks got it, thanks for the feedback. I am really out of ideas what to do next. I guess I will have a third version of the calculator implementing more functions, can't think of any at the moment, but will be up soon.
These may be a bit to advanced for now but some quick ideas.
  • Modulation (mod)
    • dividend mod divisor = remainder



  • Exponentiation
    • baseexponent = power



  • nth root (√)
    • degree √radicand = root



  • Logarithm (log)
    • logbase(antilogarithm) = logarithm




(Jun 5, 2014 06:30 AM)HippoSocks Wrote:  These may be a bit to advanced for now but some quick ideas.
  • Modulation (mod)
    • dividend mod divisor = remainder



  • Exponentiation
    • baseexponent = power



  • nth root (√)
    • degree √radicand = root



  • Logarithm (log)
    • logbase(antilogarithm) = logarithm





That is really good idea, I will have them implemented in the next version and if I can think of more I will have them implemented in the next version as well. The last version of the c++ calculator is going to be the after the third version, I am thinking of turning it to GUI with more functions. Will see what happens between now and then.

Thanks for the ideas

-Jekwa
Instead of posting an additional thread, just edit the first one, if you would, please.
[Image: chain.png]
@Sajuuk Will do next time, but tell me what do you think?
You have the basics down, now try to code a Shunting Yard Algorithm calc so it can take any amount of operators and parentheses on the same line and will calculate it with correct order of operations.
(Jun 5, 2014 03:01 PM)Trisomy 21 Wrote:  You have the basics down, now try to code a Shunting Yard Algorithm calc so it can take any amount of operators and parentheses on the same line and will calculate it with correct order of operations.

That will be one the next version for sure, thanks for the idea zerоday.


Possibly Related Threads...
Thread / Author Replies Views Last Post
[C#] by gigajewOct 20, 2017 09:56 AM 0 252 Oct 20, 2017 09:56 AM
Last Post: gigajew
[C#] by Zilog80Sep 4, 2016 12:49 AM 6 317 Sep 4, 2016 12:49 AM
Last Post: Zyklon A
[Shell] by WOLGAOct 21, 2014 05:02 PM 28 1,100 Oct 21, 2014 05:02 PM
Last Post: White Light