Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 5967397
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 22, 20262026-05-22T19:56:18+00:00 2026-05-22T19:56:18+00:00

float Calculate(const string &query) { std::cout << Query: << query << \n; unsigned int

  • 0
float Calculate(const string &query)
{
        std::cout << "Query: " << query << "\n";
        unsigned int size = query.length();
        char stack[70];
        float res;
        int m = 0;

        for (int i = 0; i < size; i++)
        {
                if (query[i] >= '0' && query[i] <= '9')
                {
                        stack[m] = query[i] - '0';
                        m++;
                        continue;
                }

                switch (query[i])
                {
                        case '+':
                        {
                                res = stack[m - 2] + stack[m - 1];
                                break;
                        }
                        case '-':
                        {
                                res = stack[m - 2] - stack[m - 1];
                                break;
                        }
                        case '*':
                        {
                                res = stack[m - 2] * stack[m - 1];
                                break;
                        }
                        case '/':
                        {
                                res = stack[m - 2] / stack[m - 1];
                                break;
                        }
                }

                    stack[m - 2] = res;
                m--;
                cout << "RES: " << res << "\n";
        }

        return res;
}

It calculates reverse polish notation.

When I call something like: Calculate("11+") it returns right result: 2.

But, when I pass a variable after getting of RPN string:

string inputStr;
string outputStr;

cout << "Put exercise\n";
getline(std::cin, inputStr);

outputStr = GetRPN(inputStr);
cout << "Output str :" << outputStr << ":\n";

float res = Calculate(outputStr);
std::cout << res << "\n";

So, when I input string: 1+1, function GetRPN returns 11+ and I see that in second cout. But result is 0!

What could it be?


string GetRPN(string input)
{
    vector <char> operation;
    string outputStr;      //output string, keep RPN
    int stack_count = 0;

    for(int i = 0; i < input.length(); i++)
    {
        if(input[i] >= '0' && input[i] <= '9')
        {
            outputStr += input[i];
        }
        else
        {
            if(operation.empty())
            {
                operation.push_back(input[i]);
                stack_count++;
            }
            else if(operation[stack_count - 1] == '+' || operation[stack_count - 1] == '-')
            {
                operation.push_back(input[i]);
                stack_count++;
            }
            else if ((operation[stack_count - 1] == '*' || operation[stack_count - 1] == '/') && (input[i] == '*' || input[i] == '/'))
            {
                outputStr += operation[stack_count - 1]; // move mark of operation to output str
                operation.pop_back(); // delet last element from vector
                operation.push_back(input[i]);// plus new operation mark to vector
                stack_count++;
            }
            else if (operation[stack_count - 1] == '*' || operation[stack_count - 1] == '/')
            {
                outputStr += input[i];
            }
        }
    }

    for(int i = operation.size(); i >= 0; i--)
    {
        outputStr += operation[i]; // move all operation marks to otput str
    }

    return outputStr;
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-22T19:56:19+00:00Added an answer on May 22, 2026 at 7:56 pm

    Your cycle here

    for(int i = operation.size(); i >= 0; i--)
    {
        outputStr += operation[i]; // move all operation marks to otput str
    }
    

    does not make any sense. You are obviously attempting to access the vector at invalid index. It is illegal to access the element at operation[i] when i is equal operation.size(). The index is out of range.

    Any self-respecting implementation would immediately report this problem with an assertion. In any case, as I said in the comment, the problems like that are resolved by debugging the code. Why are you asking other people to debug your code instead of doing it yourself?

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have my own, very fast cos function: float sine(float x) { const float
I just tried to calculate something. All my input is int. int aveScore =
I would like to calculate the color depending on a percentage value: float percentage
How can I calculate the square root of a Float in C# , similar
I have double (or float) variables that might be empty, as in holding no
I want to float a div to the right at the top of my
I am doing some float manipulation and end up with the following numbers: -0.5
If you have a float in MSSQLServer, to what do you map this in
Mine would have to be the float and margin bugs... If you float an
I want my errors to float above, left-justified, the input field that doesn't validate.

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.