0

I want to use C++ to make a calculator, so that I can enter an expression and calculate the result.

For example,

input

(5.2+4)*ln3.4+sin3

output

11.39985

The problem is that I don't know how to separate the number and the operator from the string. For the length of the operands and numbers are different. Is there any good way?

liushi
  • 83
  • 7
  • 3
    Let me just say that the problem you're trying to solve is not as trivial as you seem to believe. You have to know how to do parsing and lexical analysis, something way too much for a simple answer. – PaulMcKenzie Oct 05 '19 at 23:25
  • 1
    You can clean a number of ideas and helpful hints just by entering `"[c++] calculator"` in the search box at the top of the window. – David C. Rankin Oct 05 '19 at 23:27

3 Answers3

0

Use a library such as exprtk.

I'm going to assume that you're a total noob which leads me to advise you to always Google for a library that solves your problem.

vaid
  • 1,390
  • 12
  • 33
0

That's actually a much harder problem than it seems at first, and I say that from experience.

If you want an example of how to do it completely from scratch, here is a question where I posted an example I was working on. It is certainly not complete, but links to a great Java article (actually, probably the best article) on Pratt parses, which in my opinion is the best way to parse expressions. My question was on my attempt to port the Java code found there to C++. You can see a problem I ran into there.

You will also need to know some theory on lexers, and learn how to create tokens, which I don't ask about there.

The point is, you have a lot of research ahead of you, if you want to start from scratch, or even if you want to just know the theory of what's going on, but I certainly encourage you to try it if you don't have a deadline for it.

0

I programmed the calculator using function ,loops, switch case. it can also be programmed with using if else statement but then it will be a simple one. I had made it like a real calculator it can take unlimited argument, while the other takes only 2 numbers as argument.

#include <iostream>
#include <conio.h>
using namespace std;

int add()
{
    int total, a, sum = 0;
    cout << "how much number you want to calculate :";
    cin >> total;

    for (int i = 1; i <= total; i++)
    {
        cout << "enter number " << i << "for +" << endl;
        cin >> a;
        sum = sum + a;
    }
    cout << "total addition is:" << sum;
    return 0;
}
int subtract()
{
    int sub = 0, a, b[20], total;
    cout << "how much number you want to calculate :";
    cin >> total;
    cout << "enter number 1 for - : \n";
    cin >> a;
    a = -a;
    for (int i = 1; i < total; i++)
    {
        cout << "enter number " << i << "for - :" << endl;
        cin >> b[i];
        sub = sub - a - b[i];
        a = 0;
    }
    cout << "HENCE THE SUBTRACT IS :" << sub;
}
int divide()
{
    float h;
    float a;
    float b;
    cout << "enter number 1 : ";
    cin >> a;

    cout << "enter number 2 : ";
    cin >> b;

    h = a / b;
    cout << "division is :" << h;
}
int multiply()
{
    int a[20];
    int total, multi = 1;
    cout << "how much number you want to multiply\n";
    cin >> total;
    for (int i = 0; i < total; i++)
    {
        cout << "enter number to multiply\n";
        cin >> a[i];
        multi = multi * a[i];
    }

    cout << "multiplicaion is : " << multi;
}
int main()
{
    int num1[20], num2[20];
    char sign;
    cout << "chouse a sign"
            "\npress this for executing the action +"
            "\npress this for executing the action -"
            "\npress this for executing the action X"
            "\npress this for executing the action /\n";
    cin >> sign;

    switch (sign)
    {
    case '+':
        add();
        break;

    case '-':
        subtract();
        break;

    case '*':
        multiply();
        break;

    case '/':
        divide();
        break;

    default:
        cout << "chouse something";
        break;
    }

    return 0;
} 

    
            
  • This code indeed does some calcuator work, but not in the sense of the question. It just avoids all the lexing and parsing stuff whichg the question is all about. – Klaus Gütter Dec 31 '22 at 10:34