0
\$\begingroup\$

I want to make my calculator code shorter.

Here is my code:

extern "C"
{
    int printf(const char *format, ...);
    extern int scanf(const char *format, ...);
    double pow(double x, double y);
}

int main()
{
    do
    {
        double num1;
        double a, b = 1;
        char ch1;
        float r;
        scanf("%lf", &num1);
        printf("\ta - Factorial\n\tb - Continue\n");
        scanf(" %c", &ch1);
        switch (ch1)
        {
            case 'a':
                r = b;
                for (a = 1; a <= num1; a++)
                {
                    b = b * a;
                }
                if (b < 500000001 & num1 > 0)
                {
                    printf("%f\n", b);
                }
                else
                {
                    if (b > 500000000)
                    {
                        printf("Number is Big\n");
                    }
                    if (num1 < 1)
                    {
                        printf("Number is Small\n");
                    }
                }
                break;
            case 'b':
                double num2;
                char ch2;
                scanf("%lf", &num2);
                printf("\ta - Add\n\tb - Substract\n\tc - Multiply\n\td - Divide\n\te - Power\n\tf - Radical\n");
                scanf(" %c", &ch2);
                switch (ch2)
                {
                    case 'a':
                        r = num1 + num2;
                        if (num1 + num2 < 500000001 & num1 + num2 > -500000001 & num1 - num2 < 500000001 & num2 - num1 < 500000001)
                        {
                            printf("%lf + %lf = %f\n", num1, num2, r);
                        }
                        else if (num1 + num2 > 500000000)
                        {
                            printf("Number is Big\n");
                        }
                        break;
                    case 'b':
                        r = num1 - num2;
                        if (num1 + num2 < 500000001 & num1 + num2 > -500000001 & num1 - num2 < 500000001 & num2 - num1 < 500000001)
                        {
                            printf("%lf - %lf = %f\n", num1, num2, r);
                        }
                        else if (num1 + num2 > 500000000)
                        {
                            printf("Number is Big\n");
                        }
                        break;
                    case 'c':
                        r = num1 * num2;
                        if (num1 * num2 < 500000001 & num1 * num2 > -500000001)
                        {
                            printf("%lf * %lf = %f\n", num1, num2, r);
                        }
                        else
                        {
                            if (num1 * num2 > 500000000)
                            {
                                printf("Number is Big\n");
                            }
                            if (num1 * num2 < -500000000)
                            {
                                printf("Number is Small\n");
                            }
                        }
                        break;
                    case 'd':
                        r = num1 / num2;
                        if (num1 * num2 < 500000001 & num1 * num2 > -500000001 && num2 > 0 | num1 == 0 & num2 != 0)
                        {
                            printf("%lf / %lf = %f\n", num1, num2, r);
                        }
                        else
                        {
                            if (num1 * num2 > 500000000)
                            {
                                printf("Number is Big\n");
                            }
                            if (num1 * num2 < -500000000)
                            {
                                printf("Number is Small\n");
                            }
                            if (num1 > 0 & num2 == 0 | num1 == 0 & num2 == 0)
                            {
                                printf("Undefined\n");
                            }
                        }
                        break;
                    case 'e':
                        r = pow(num1, num2);
                        if (pow(num1, num2) < 500000001 & pow(num1, num2) > -500000001)
                        {
                            printf("%lf ^ %lf = %f\n", num1, num2, r);
                        }
                        else
                        {
                            if (pow(num1, num2) > 500000000)
                            {
                                printf("Number is Big\n");
                            }
                            if (pow(num1, num2) > -500000000)
                            {
                                printf("Number is Small\n");
                            }
                        }
                    case 'f':
                        r = pow(num1, 1 / num2);
                        if (pow(num1, num2) < 500000001 & pow(num1, num2) > -500000001)
                        {
                            printf("%lf √ %lf = %f\n", num1, num2, r);
                        }
                        else
                        {
                            if (pow(num1, num2) > 500000000)
                            {
                                printf("Number is Big\n");
                            }
                            if (pow(num1, num2) > -500000000)
                            {
                                printf("Number is Small\n");
                            }
                        }
                        break;
                    default:
                        printf("Invalid\n");
                }
                break;
        }
    }
    while (1);
}

Can I make this code shorter? Is it possible to do? If it is, how?

(This code may be updated in my GitHub account.)

\$\endgroup\$
10
  • 7
    \$\begingroup\$ We can only review the code that's here, so your requirement (2) is off-topic for Code Review. What do you have against #include, anyway? That's the standard means of accessing library functions, and much safer than declaring standard functions as you have done here. \$\endgroup\$ Nov 13, 2020 at 8:57
  • 1
    \$\begingroup\$ This is not C++, it is C. \$\endgroup\$
    – Casey
    Nov 13, 2020 at 16:26
  • 1
    \$\begingroup\$ It is C++. You can test it. \$\endgroup\$
    – Arian
    Nov 13, 2020 at 16:53
  • 2
    \$\begingroup\$ While it appears that the edits don't really incorporate advice from the answer, please be aware: after receiving an answer you should not make changes to your code anymore. This is to ensure that answers do not get invalidated. If you have changed your code you can either post it as an answer (if it would constitute a code review) or ask a new question with your changed code (linking back to this one as reference). See the section What should I not do? on What should I do when someone answers my question? for more information \$\endgroup\$ Nov 13, 2020 at 19:15
  • 1
    \$\begingroup\$ @Arian: It compiles like a C program. What about it makes it C++? \$\endgroup\$ Nov 13, 2020 at 22:03

3 Answers 3

7
\$\begingroup\$

Questions:

Can I make this code shorter?

Yes

Is it possible to do?

Yes

If it is, how?

By using functions. You can move common code to a named function.

Notes

This is C code not C++.

Sure it compiles with a C++ compiler (so does a lot of C). C++ is a style of programming, you are not using the C++ style you are using the C style of programming.

Additionally, you are using a lot of C library functions that have C++ equivalents.

Review

  1. Split your code into functions
  2. Use C++ libraries (not C)
  3. Improve your user interface (ask the question then expect answers).
\$\endgroup\$
1
  • \$\begingroup\$ It is bad C++ code, avoiding all the good points of writing C++, in a bad C-like style. But it is most certainly C++ despite that. And it is indubitably not C. \$\endgroup\$ Nov 24, 2020 at 20:16
7
\$\begingroup\$

There is no better way to say this, but your code is shockingly bad. I will just provide a short summary of the things that can be improved:

  • #include headers like everyone else does, don't forward declare standard library functions.
  • Since you tagged the post C++, use C++'s iostream functionality; it gives you type safety.
  • Split your program into several functions, and just have a small loop in main() that reads input and calls another function to do the processing.
  • Use either double or float, don't mix them. Why is r a float?
  • Don't use arbitrary numbers to check if something is "big". Just don't check the result at all, print it and let the user decide whether they think it is useful or not.
  • Use && instead of &, the latter is a bitwise AND which does not do what you want.
\$\endgroup\$
1
  • \$\begingroup\$ Pre-C++20 iostream has some often critical shortcomings compared to stdio. Still, <format> mostly solved that, and they are not really important here, imho. \$\endgroup\$ Nov 24, 2020 at 20:13
4
\$\begingroup\$

This is not a full review, but try using functions actually used in modern C++. There is no point in using printf() and scanf(), as they are just in the language for C-portability. std::cout and std::cin would be the C++ equivalents (you have to #include <iostream>).

\$\endgroup\$
1
  • \$\begingroup\$ Just for C portability? Not that that isn't important enough by itself, but iostream has some distinct advantages (and not only disadvantages) over pre-C++20 iostream. \$\endgroup\$ Nov 24, 2020 at 20:15

Your Answer

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

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