Click here to Skip to main content
14,271,826 members
Rate this:
Please Sign up or sign in to vote.
See more:
So, I got an idea to create a project in C which I named the Universal Calculator. Basically, it is a code which helps to solve general problems and calculations in Mathematics. It consists of main.cpp and method.c. It works without any errors. Can you please suggest me the way I can improve my project because I want to post it somewhere (f.e. github), or use this code to create a software, or use it as a plugin on a website.

What I have tried:


main.cpp:
//Emil Ahmadov
//Program language C
//Universal Calculator

#include <stdio.h>
#include "method.c"
int main()
{
    double a;
    double b;
    int check;
    int check2;
    int check3;
    double c;
    const double pi=3.14159265359;
    printf("Welcome to the Universal Calculator\n");
    printf("Instructions: \n");
    printf("1. Choose the command\n");
    printf("2. Enter the number or numbers\n");
    printf("3. Here is your answer\n\n");
    printf("Operations with integers, decimals, radicals (press 1)\n"
           "Trigonometric operations  (press 2 )\n"
           "Solve quadratic equation (press 3)\n"
           "Math formulas (press 4)\n");
    printf("\nYour choice: ");
    scanf("%d", &check);
    while(check>4||check<1)
    {
        printf("\nWrong Command!\n");
        printf("Your choice: ");
        scanf("%d", &check);
    }
    if(check==1)
    {
        printf("\nChoose the operation:\n");
        printf("\nAddition (press 1)\nSubtraction (press 2)\nMultiplication (press 3)\nDivision (press 4)\n"
               "Raise into Power (press 5)\nExponent (press 6)\nLogarithm-base 10 (press 7)\nNatural logarithm (press 8)\n"
               "Square root (press 9)\nRound the number (press 10)\n");
        printf("\nYour choice: ");
        scanf("%d", &check3);
        while(check3>10||check3<1)
        {
            printf("\nWrong Command!\n");
            printf("Your choice: ");
            scanf("%d", &check3);
        }
        printf("\nNote: The digits entered should be either integers or decimals\n");
        if(check3==1 || check3==2 || check3==3 || check3==5)
        {
            printf("\nEnter the first number: ");
            scanf("%lf", &a);
            printf("Enter the second number: ");
            scanf("%lf", &b);
            if(check3==1)
            {
                printf("\naddition: %lf\n", add(a,b));
            }
            else if(check3==2)
            {
                printf("\nsubtraction: %lf\n", subtract(a,b));
            }
            else if(check3==3)
            {
                printf("\nmultiplication: %lf\n", multiply(a,b));
            }
            else
            {
                printf("\npower: %lf\n", power(a, b));
            }
        }
        else if(check3==4)
        {
            printf("\nEnter the first number: ");
            scanf("%lf", &a);
            printf("Enter the second number: ");
            scanf("%lf", &b);
            while(b==0)
            {
                printf("\nError! divider shouldn't be equal to zero\n");
                printf("Enter the second number again: ");
                scanf("%lf", &b);
            }
            printf("\ndivision: %lf\n", divide(a,b));
        }
        else if(check3==6 || check3==7 || check3==8 || check3==9 || check3==10)
        {
            if(check3==6)
            {
                printf("\nEnter the number: ");
                scanf("%lf", &a);
                printf("\nexponent (e^x) : %lf\n", exponent(a));
            }
            else if(check3==7)
            {
                printf("\nEnter the base of the logarithm: ");
                scanf("%lf", &a);
                while(a<=0)
                {
                    printf("\nError! Base of the logarithm should be positive\n");
                    printf("Enter the base again: ");
                    scanf("%lf", &a);
                }
                printf("\nlogarithm (base 10) : %lf\n", logarithm(a));
            }
            else if(check3==8)
            {
                printf("\nEnter the base of a natural logarithm: ");
                scanf("%lf", &a);
                while(a<=0)
                {
                    printf("\nError! Base of the logarithm should be positive\n");
                    printf("Enter the base of a natural logarithm again: ");
                    scanf("%lf", &a);
                }
                printf("\nnatural logarithm (base e) : %lf\n", nat_log(a));
            }
            else if(check3==9)
            {
                printf("\nEnter the number: ");
                scanf("%lf", &a);
                while(a<0)
                {
                    printf("\nError! Cannot take the square root from a  negative number\n");
                    printf("Enter the number again: ");
                    scanf("%lf", &a);
                }
                printf("\nsquare root: %lf\n", sq_root(a));
            }
            else
            {
                printf("\nEnter the number: ");
                scanf("%lf", &a);
                printf("\nrounded : %lf\n", round(a));
            }
        }
    }
    else if(check==2)
    {
        printf("\nChoose the operation:\n");
        printf("\nsin: press 1\ncos: press 2\ntan: press 3\nctg: press 4\nsinh: press 5\ncosh: press 6\ntanh: press 7\nctgh: press 8\n");
        printf("\nYour choice: ");
        scanf("%d", &check2);
        while(check2>8||check2<1)
        {
            printf("\nWrong Command!\n");
            printf("Your choice: ");
            scanf("%d", &check2);
        }
        if(check2==1||check2==2||check2==3||check2==4||check2==5||check2==6||check2==7||check2==8)
        {
            printf("\nEnter the angle in degrees: ");
            scanf("%lf", &c);
            double res;
            res = trig(c, check2);
            printf("\nresult is: %lf\n", res);
        }
    }
    else if(check==3)
    {
        float c1, c2, c3;
        float r1, r2;
        printf("\nNote: The digits entered should be either integers or decimals\n");
        printf("\nEnter the first coefficient: ");
        scanf("%f", &c1);
        printf("Enter the second coefficient: ");
        scanf("%f", &c2);
        printf("Enter the third coefficient: ");
        scanf("%f", &c3);
        float disc;
        disc=pow(c2, 2)-4*c1*c3;
        if(disc>0)
        {
            printf("\nRoots are real and different!\n\n");
            r1=(-c2+sqrt(disc))/(2*c1);
            r2=(-c2-sqrt(disc))/(2*c1);
            printf("first root = %f\n", r1);
            printf("second root = %f\n", r2);
        }
        else if(disc==0)
        {
            printf("\nRoots are real and same!\n\n");
            r1= (-c2+sqrt(disc))/(2*c1);
            printf("first root = second root = %f\n", r1);
        }
        else
        {
            printf("\nRoots are complex and different!\n\n");
            float realPart = (-c2)/(2*c1);
            float imagPart = sqrt(-disc)/(2*c1);
            printf("first root = %f + %fi\n",realPart, imagPart);
            printf("second root = %f - %fi\n",realPart, imagPart);
        }
    }

    else if(check==4)
    {
        int check4;
        printf("\nCalculate the average of numbers (press 1)");
        printf("\nCalculate the percent of a number (press 2)");
        printf("\nCalculate the factorial (press 3)");
        printf("\nPythagorean theorem (press 4)");
        printf("\nCalculate the perimeter (press 5)");
        printf("\nCalculate the area (press 6)");
        printf("\nCalculate the volume (press 7)\n");
        printf("\nYour choice: ");
        scanf("%d", &check4);
        while(check4>7||check4<1)
        {
            printf("\nWrong Command!\n");
            printf("Your choice: ");
            scanf("%d", &check4);
        }
        if(check4==1)
        {
            int i, n;
            double sum=0;
            double a[100];
            double avg;
            printf("\nEnter the number of digits for which you want to find the average: ");
            scanf("%d", &n);
            while (n > 100 || n <= 0)
            {
                printf("\nError! number should in range of (1 to 100).\n");
                printf("Enter the number again: ");
                scanf("%d", &n);
            }
            for(i=0; i<n; i++)
            {
                printf("Enter the digit %d: ", i+1);
                scanf("%lf", &a[i]);
                sum=sum+a[i];
            }
            avg=sum/n;
            printf("\nThe average is %lf\n", avg);
        }
        else if(check4==2)
        {
            double a;
            double b;
            double res;
            printf("\nEnter the digit: ");
            scanf("%lf", &a);
            printf("\nEnter the percentage: ");
            scanf("%lf", &b);
            res=a*b/100;
            printf("\nThe result is %lf", res);
        }
        else if(check4==3)
        {
            int k;
            long fac=1;
            printf("\nEnter a number to calculate its factorial: ");
            scanf("%d", &k);
            fac=factorial(k);
            printf("\nFactorial of %d = %ld", k, fac);
        }
        else if(check4==4)
        {
            int check5;
            double cath1;
            double cath2;
            double hyp;
            printf("\nWhich side of a right angle triangle do you want to find?\n");
            printf("hypotenuse (press 1)\n");
            printf("cathetus (press 2)\n");
            printf("\nYour choice: ");
            scanf("%d",&check5);
            while(check5>2||check5<1)
            {
                printf("\nWrong Command!\n");
                printf("\nYour choice: ");
                scanf("%d", &check5);
            }
            if(check5==1)
            {
                printf("\nEnter the first cathetus: ");
                scanf("%lf", &cath1);
                printf("Enter the second cathetus: ");
                scanf("%lf", &cath2);
                hyp=sqrt(pow(cath1,2)+pow(cath2, 2));
                printf("\nThe calculated hypotenuse: %lf", hyp);
            }
            else if(check5==2)
            {
                printf("\nEnter the hypotenuse: ");
                scanf("%lf", &hyp);
                printf("Enter the known cathetus: ");
                scanf("%lf", &cath1);
                cath2=sqrt(pow(hyp, 2)-pow(cath1,2));
                printf("\nThe calculated cathetus: %lf", cath2);
            }
        }
        else if(check4==5)
        {
            int check6;
            double sq;
            double rect1, rect2;
            double tria1, tria2, tria3;
            double rad;
            printf("\nPerimeter of which shape do you want to calculate? ");
            printf("\nsquare (press 1)");
            printf("\nrectangle (press 2)");
            printf("\ntriangle (press 3)");
            printf("\ncircle (press 4)\n");
            printf("\nYour choice: ");
            scanf("%d", &check6);
            while(check6>4||check6<1)
            {
                printf("\nWrong Command!\n");
                printf("\nYour choice: ");
                scanf("%d", &check6);
            }
            if(check6==1)
            {
                printf("\nEnter the side of a square: ");
                scanf("%lf", &sq);
                printf("\nPerimeter = %lf", 4*sq);
            }
            else if(check6==2)
            {
                printf("\nEnter the length of a rectangle: ");
                scanf("%lf", &rect1);
                printf("Enter the width of a rectangle: ");
                scanf("%lf", &rect2);
                printf("\nPerimeter = %lf", 2*(rect1+rect2));
            }
            else if(check6==3)
            {
                printf("\nEnter the first side of a triangle: ");
                scanf("%lf", &tria1);
                printf("Enter the second side of a triangle: ");
                scanf("%lf", &tria2);
                printf("Enter the third side of a triangle: ");
                scanf("%lf", &tria3);
                printf("\nPerimeter = %lf", tria1+tria2+tria3);
            }
            else if(check6==4)
            {
                printf("\nEnter the radius of a circle: ");
                scanf("%lf", &rad);
                printf("\nPerimeter = %lf", 2*pi*rad);
            }
        }
        else if(check4==6)
        {
            int check7;
            double rect1, rect2;
            double tria1, tria2, tria3;
            double sq, rad;
            double s,s1;
            double triang, constant;
            double side, h;
            double r1, r2;
            double b1, b2, h1;
            printf("\nArea of which shape do you want to calculate? ");
            printf("\nsquare (press 1)");
            printf("\nrectangle (press 2)");
            printf("\ntriangle using Heron's formula (press 3)");
            printf("\nequilateral triangle (press 4)");
            printf("\ncircle (press 5)");
            printf("\nparallelogram (press 6)");
            printf("\ntrapezoid  (press 7)");
            printf("\nellipse (press 8)\n");
            printf("\nYour choice: ");
            scanf("%d", &check7);
            while(check7>8||check7<1)
            {
                printf("\nWrong Command!\n");
                printf("\nYour choice: ");
                scanf("%d", &check7);
            }
            if(check7==1)
            {
                printf("\nEnter the side of a square: ");
                scanf("%lf", &sq);
                printf("\nArea = %lf", pow(sq,2));
            }
            if(check7==2)
            {
                printf("\nEnter the length of a rectangle: ");
                scanf("%lf", &rect1);
                printf("Enter the width of a rectangle: ");
                scanf("%lf", &rect2);
                printf("\nArea = %lf", rect1*rect2);
            }
            if(check7==3)
            {
                printf("\nEnter the first side of a triangle: ");
                scanf("%lf", &tria1);
                printf("Enter the second side of a triangle: ");
                scanf("%lf", &tria2);
                printf("Enter the third side of a triangle: ");
                scanf("%lf", &tria3);
                s=(tria1+tria2+tria3)/2;
                s1=s*(s-tria1)*(s-tria2)*(s-tria3);
                printf("\nArea = %lf", sqrt(s1));
            }
            if(check7==4)
            {
                printf("\nEnter the side of an equilateral triangle: ");
                scanf("%lf", &triang);
                constant=sqrt(3)/4;
                printf("\nArea = %lf", constant*pow(triang, 2));
            }
            if(check7==5)
            {
                printf("\nEnter the radius of a circle: ");
                scanf("%lf", &rad);
                printf("\nArea = %lf", pi*pow(rad,2));
            }
            if(check7==6)
            {
                printf("\nEnter the side of a parallelogram: ");
                scanf("%lf", &side);
                printf("Enter the height of a parallelogram: ");
                scanf("%lf", &h);
                printf("\nArea = %lf", side*h);
            }
            if(check7==7)
            {
                printf("\nEnter the first base of a trapezoid: ");
                scanf("%lf", &b1);
                printf("Enter the second base of a trapezoid: ");
                scanf("%lf", &b2);
                printf("Enter the height of a trapezoid: ");
                scanf("%lf", &h1);
                printf("\nArea = %lf", (b1+b2)*h1/2);
            }
            if(check7==8)
            {
                printf("\nEnter the first radius of an ellipse: ");
                scanf("%lf", &r1);
                printf("Enter the second radius of an ellipse: ");
                scanf("%lf", &r2);
                printf("\nArea = %lf", pi*r1*r2);
            }
        }
        else if(check4==7)
        {
            int check8;
            double sq;
            double rect1, rect2, rect3;
            double r,h;
            double b;
            double e1, e2, e3;
            double v;
            printf("\nVolume of which shape do you want to calculate? ");
            printf("\ncube (press 1)");
            printf("\nrectangular prism (press 2)");
            printf("\ncylinder (press 3)");
            printf("\npyramid (press 4)");
            printf("\ncone (press 5)");
            printf("\nsphere (press 6)");
            printf("\nellipsoid  (press 7)\n");
            printf("\nYour choice: ");
            scanf("%d", &check8);
            while(check8>7||check8<1)
            {
                printf("\nWrong Command!\n");
                printf("\nYour choice: ");
                scanf("%d", &check8);
            }
            if(check8==1)
            {
                printf("\nEnter the side of a cube: ");
                scanf("%lf", &sq);
                printf("\nVolume = %lf", pow(sq,3));
            }
            if(check8==2)
            {
                printf("\nEnter the length of a rectangular prism: ");
                scanf("%lf", &rect1);
                printf("Enter the width of a rectangular prism: ");
                scanf("%lf", &rect2);
                printf("Enter the height of a rectangular prism: ");
                scanf("%lf", &rect3);
                printf("\nVolume = %lf", rect1*rect2*rect3);
            }
            if(check8==3)
            {
                printf("\nEnter the radius of a cylinder: ");
                scanf("%lf", &r);
                printf("Enter the height of a cylinder: ");
                scanf("%lf", &h);
                printf("\nVolume = %lf", pi*pow(r,2)*h);
            }
            if(check8==4)
            {
                printf("\nEnter the side of the base of a pyramid: ");
                scanf("%lf", &b);
                printf("Enter the height of a pyramid: ");
                scanf("%lf", &h);
                printf("\nVolume = %lf", b*h/3);
            }
            if(check8==5)
            {
                printf("\nEnter the radius of the base of a cone: ");
                scanf("%lf", &r);
                printf("Enter the height of a cone: ");
                scanf("%lf", &h);
                printf("\nVolume = %lf", pi*pow(r,2)*h/3);
            }
            if(check8==6)
            {
                printf("\nEnter the radius of a cone: ");
                scanf("%lf", &r);
                printf("\nVolume = %lf", 4/3*pi*pow(r,3));
            }
            if(check8==7)
            {
                printf("\nEnter the first radius of an ellipsoid: ");
                scanf("%lf", &e1);
                printf("Enter the second radius of an ellipsoid: ");
                scanf("%lf", &e2);
                printf("Enter the third radius of an ellipsoid: ");
                scanf("%lf", &e3);
                printf("\nVolume = %lf", 4/3*pi*e1*e2*e3);
            }
        }
    }
    printf("\n\nThanks for using the Universal Calculator!\n");
    return 0;
}


method.c:

#include <stdio.h>
#define _USE_MATH_DEFINES
#include <math.h>

double add(double a, double b)
{
    return a+b;
}

double subtract(double a, double b)
{
    return a-b;
}

double multiply(double a, double b)
{
    return a*b;
}

double divide(double a, double b)
{
    return a/b;
}

double power(double a, double b)
{
    if(a==0)
    {
        return 0;
    }
    else if(b==0)
    {
        return 1;
    }
    else if(b<0)
    {
        long double c;
        int sign;
        sign= (-1);
        b*=sign;
        c = 1/(pow(a,b));
        return c;
    }
    else
    {
        long double d=pow(a, b);
        return d;
    }
}

double exponent(double a)
{
    return exp(a);
}

double nat_log(double a)
{
       return log(a);
}

double logarithm(double a)
{
        return log10(a);
}

double sq_root(double a)
{
    return sqrt(a);
}

double trig(double c, int check2)
{
    const double pi=3.14159265359;
    c=c*pi/180;
    if(check2==1)
    {
        return sin(c);
    }
    else if(check2==2)
    {
        return cos(c);
    }
    else if(check2==3)
    {
        return tan(c);
    }
    else if(check2==4)
    {
        return (1/tan(c));
    }
    else if(check2==5)
    {
        return sinh(c);
    }
    else if(check2==6)
    {
        return cosh(c);
    }
    else if(check2==7)
    {
        return tanh(c);
    }
    else if(check2==8)
    {
        return (1/tanh(c));
    }
    else
    {
        printf("Wrong Command!");
    }
}

double percentage(double a, double p)
{
    return a*p/100;
}

long factorial(int a)
{
    long fact=1;
    int i;
    for(i=1; i<=a; i++)
    {
        fact=fact*i;
    }
    return fact;
}
Posted
Updated 19-Jul-19 11:15am
Comments
Dave Kreskowiak 19-Jul-19 14:28pm
   
I get that you're proud of what you accomplished. But don't post this code anywhere. You're not going to reuse a single line of it in a "real" application, or even a better calculator app.

As you build experience, you're going to look back on this code and laugh that you wrote it, and then throw it in the trash and write something better. Then a few more years are going to go by and you're going to do it again, and again, and again.

We all do it.
BillWoodruff 19-Jul-19 16:20pm
   
Congratulations, and I hope this personal best motivates you to continue study-experiment-observe-analyze-rewrite-test-repeat ... forever.
KarstenK 20-Jul-19 4:10am
   
You can also use char input and so the user can directly use the "+-/*" operators. Use constants and not integers. Check that your NEVER divide with zero!!!
Rate this:
Please Sign up or sign in to vote.

Solution 2

The issue(s) of posting your code has been dealt with by others so I will give you a few small suggestions to help improve it.

First, you included math.h and added the USE_MATH_DEFINES definition before doing so. Considering that, why have you defined pi? That is what USE_MATH_DEFINES does for you. You get a value called M_PI defined so you don't have to in addition to several other useful definitions like e and the square root of two.

Second, including method.c in another module is bad form. It completely defeats the purpose of having multiple code modules. The way this should be done is through a header that defines the prototypes of your functions. If you want to write the "methods" in c then you need to enclose their prototypes in a 'extern "C"' wrapper like this:
extern "C"
{
// function prototypes go here
}
I can't think of a good reason to do this other than making a library with the methods that could be used in other languages. Then you need to do it that way. Otherwise, just write the methods in c++ and use them like you would any other functions with a header that defines their prototypes. Here's an example of function prototypes in a header :
extern double add(double a, double b);
extern double subtract(double a, double b);
One more thing : you might want to revisit your power function because pow can handle negative exponents just fine so you don't have to do anything special for them.
   
v2
Comments
BillWoodruff 19-Jul-19 16:17pm
   
+5
Rick York 19-Jul-19 16:40pm
   
Many thanks.
Rate this:
Please Sign up or sign in to vote.

Solution 1

Oh gawd ... just what the world needs, another console based calculator.
I'm going to be honest here, and that is going to sound brutal - it isn't, but I'm trying to prevent you making a big mistake, one that you will regret for a very, very long time ... remember that nothing on the internet truly disappears.

console based calculator - Google Search[^]
1,000,000,000 hits. That's the largest result set I have ever seen on Google. Not one million hits: one billion hits. That's one webpage for every seven human beings alive on this planet.
:OMG:
Think about that for a moment.

This is standard student homework fare: every teacher gives it to every student; every student is really proud of what he produced. Even if the source looks like student code that he's going to be horribly embarrassed about in six months time when he looks back on it with the eye of experience.

Well done on writing it, I hope you get a good mark on your course.
But don't post it for everybody, it really isn't of any use to anyone else in the real world.

You are probably going to hate me for this and I can understand that. But ... you can come back in six months and thank me then.
   
v2
Comments
Em1l1ko 20-Jul-19 5:26am
   
I got you, thx for your feedback, but rn I'm interested in creating a useful project using my coding knowledge (Java and C). What can you suggest me?
OriginalGriff 20-Jul-19 5:51am
   
Get a lot more experience first.
Not only will that help you produce better code, but it'll make the scale of the project you tackle easier to work with - because you will understand the strategies you can use to make coding larger projects both easier and more reliable / maintainable (remember: if you release a useful tool, people will expect you to maintain and enhance it; unlike homework where it's "write and forget" you good be working with the same codebase for five years or more!)

And you must be aware that almost nobody uses console based apps anymore, certainly not for user-interfaced tools. Instead, they are all very very much Graphical User Interface (or GUI for short) based, and that means you need to learn how to code them proficiently, and even the simplest GUI C# app will need to understand a huge wealth of controls, with their associates properties, methods, events, delegates, and interfaces. Add to that the "behind the scenes" classes that you will need - File I/O, databases, collections, ... the list goes on! There is a lot to learn if you want to release useful software (if only because you are coming "late to the game" so there is loads of existing software your app will be compared with).
And C is a bad choice for GUI based apps - you can do it, but it's a serious amount of work, and the learning curve is of the "brick wall" variety! A more modern language like C# or even VB makes it much easier, leaving you to concentrate on the app, instead of the system.

Start by continuing your course, and learning the next stages well - it really will benefit you in the long term not to rush into this!

When you have more experience, consider writing an article or tip

https://www.codeproject.com/script/Articles/Submit.aspx

on something you have learned well and see how that is received. It'll take less time (though still days, not minutes) but it'll get you more realistic feedback as to how your code is going. Do check though for both accuracy and existing articles!
Here's one of mine:
https://www.codeproject.com/Articles/728836/Using-struct-and-class-whats-that-all-about-2
And here's a tip (which is a much shorter, more code focussed article):
https://www.codeproject.com/Tips/1042055/Creating-a-Simple-scratch-card-Control-in-WinForms

Compare that kind of code with yours, and you'll start to see why you will wince in six months time! :laugh:

Good luck - let us know how you are getting on!
Rate this:
Please Sign up or sign in to vote.

Solution 3

Quote:
How do I improve my project which I called the universal calculator?

As a student, this project is very good for you and your training/learning as you can expand it and add functionalities.
Naming it 'universal' means it have to compare with top real calculators, and may be graphical models. Lets face it, your code is very very far from a match with what exist.
They support interesting things like:
- free format formulas, this imply compiler stuff and parenthesis matching.
- named variables.
- prime/not prime. Integer Factorization.
- infinite integers, infinite floats, complex numbers..
- CAS to handle symbolic stuff. If fed with A²-B², it can expand to (A+B)*(A-B), and this is just a simple example.

For information, developing code for a real calculator like HP Prime or TI Nspire takes years for a team of fully seasoned programmers.

I think the main interest of this project is to improve your skills and training.
Sharing your code offers only little interest.
   
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)




CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100