# A calculator in C++

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.)

• 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. Nov 13, 2020 at 8:57
• This is not C++, it is C. Nov 13, 2020 at 16:26
• It is C++. You can test it. Nov 13, 2020 at 16:53
• @Arian: It compiles like a C program. What about it makes it C++? Nov 13, 2020 at 22:03

## 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)
• 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. Nov 24, 2020 at 20:16

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.
• 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. Nov 24, 2020 at 20:13

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>).

• 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. Nov 24, 2020 at 20:15