1

Here is the problem. I'm trying to write a calculator similar to one that is in all computers. It should take values from one EditBox, make all needed calculations, and then display in another EditBox. For example: 3*6/2; result: 9; I've managed to do this:

double rezultatas = 0;
double temp = 0;
// TODO: Add your control notification handler code here
UpdateData(TRUE);
for (int i = 0; i < seka_d.GetLength(); i++)
{
    if (seka_d[i] == '/' || seka_d[i] == '*')
    {
        if (seka_d[i] == '*')
        {
            temp = (seka_d[i - 1] - '0') * (seka_d[i + 1] - '0');
        }
        if (seka_d[i] == '/')
        {
            temp = (seka_d[i - 1] - '0') / (seka_d[i + 1] - '0');
        }
        //temp = (seka_d[i - 1] - '0') / (seka_d[i + 1] - '0');

    }
    if (seka_d[i] == '+' || seka_d[i] == '-')
    {
        if (seka_d[i] == '-')
        {
            temp = (seka_d[i - 1] - '0') - (seka_d[i + 1] - '0');
        }
        if (seka_d[i] == '+')
        {
            temp = (seka_d[i - 1] - '0') + (seka_d[i + 1] - '0');
        }
        //temp = (seka_d[i - 1] - '0') + (seka_d[i + 1] - '0');

    }
    if (seka_d[i] == '-')
    {
        temp = (seka_d[i - 1] - '0') - (seka_d[i + 1] - '0');
    }

    //rezultatas++;
}
result_d = temp;
UpdateData(FALSE);

It checks string seka_d for any simbols like '*','-','/','+' and the does the operation with two neighbor simbols(ex. 1+2, sums 1 and 2)(i know it doesnt work properly with multiple operations yet), but now I have to also operate with doubles, so I want to know is it possible to convert a part of string to an integer or double(ex. 0.555+1.766). Idea is to take part of string from start to a symbol(from start until '+') and from symbol to an end of string or another symbol(ex. if string is 0.555+1.766-3.445 it would take a part of string from '+' until '-'). Is it possible to do it this way?

1
  • This question is not related to MFC technology, so I removed that tag, and added another more appropriate for the question. Commented Sep 25, 2015 at 10:49

1 Answer 1

1

You can use CString::Tokenize https://msdn.microsoft.com/en-us/library/k4ftfkd2.aspx

or convert to std::string

std::string s = seka_d;

Here is MFC example:

void foo()
{
    CStringA str = "1.2*5+3/4.1-1";
    CStringA token = "/+-*";

    double result = 0;
    char operation = '+'; //this initialization is important
    int pos = 0;
    CStringA part = str.Tokenize(token, pos);
    while (part != "")
    {
        TRACE("%s\n", part); 
        double number = atof(part);

        if (operation == '+') result += number;
        if (operation == '-') result -= number;
        if (operation == '*') result *= number;
        if (operation == '/') result /= number;

        operation = -1;
        if (pos > 0 && pos < str.GetLength())
        {
            operation = str[pos - 1];
            TRACE("[%c]\n", operation);
        }

        part = str.Tokenize(token, pos);
    }

    TRACE("result = %f\n", result);
}

Note, this doesn't handle parenthesis. For example a*b+c*d is ((a*b)+c)*d Window's calculator does the same thing.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.