C++ Program for String Calculator [DEVCPP/GCC]

PROGRAM

// C++ Program for String Calculator

#include <iostream>
#include <string>
#include <cctype>

using namespace std;

class Program
{
private:

    string expression;
    int indexOfPreviousOperator;
    int indexOfNextOperator;

public:

Program(string ex)
    {
        expression = ex;
    }

    void Evaluate()
    {
        int result = 0;

        if ((expression.find('+') == string::npos && expression.find('-') == string::npos && expression.find('*') == string::npos && expression.find('/') == string::npos))
{
                cout<<stoi(expression);
        }
     
        for (int i = 0; i < expression.size(); i++)
{
            if (expression[i] == '/')
{
                result = getFirstOperand(i) / getSecondOperand(i);
                expression.erase(indexOfPreviousOperator, indexOfNextOperator - indexOfPreviousOperator);
                expression.insert(indexOfPreviousOperator, to_string(result));
                Evaluate();
            }
            else if (expression[i] == '*')
{
                result = getFirstOperand(i) * getSecondOperand(i);
                expression.erase(indexOfPreviousOperator, indexOfNextOperator - indexOfPreviousOperator);
                expression.insert(indexOfPreviousOperator, to_string(result));
                Evaluate();
            }
            else if (expression[i] == '+' && expression.find('/') == string::npos && expression.find('*') == string::npos)
{
                result = getFirstOperand(i) + getSecondOperand(i);
                expression.erase(indexOfPreviousOperator, (indexOfNextOperator - indexOfPreviousOperator));
expression.insert(indexOfPreviousOperator, to_string(result));
                Evaluate();
            }
            else if (expression[i] == '-' && expression.find('/') == string::npos && expression.find('*') == string::npos)
{
                result = getFirstOperand(i) - getSecondOperand(i);
                expression.erase(indexOfPreviousOperator, indexOfNextOperator - indexOfPreviousOperator);
                expression.insert(indexOfPreviousOperator, to_string(result));
                Evaluate();
            }
        }
    }

    int getFirstOperand(int index)
    {
        string operand = "";
        indexOfPreviousOperator = 0;
     
        for (int i = (index - 1); i >= 0; i--)
{
            if (!isdigit(expression[i]))
{
                indexOfPreviousOperator = i;
                break;
            }
        }
        operand = expression.substr(indexOfPreviousOperator, (index - indexOfPreviousOperator));
        return stoi(operand);
    }

    int getSecondOperand(int index)
    {
        string operand = "";
        indexOfNextOperator = expression.size();
     
        for (int i = (index + 1); i < expression.size(); i++)
{
            if (!isdigit(expression[i]))
            {
                indexOfNextOperator = i;
                break;
            }
        }
        operand = expression.substr((index + 1), (indexOfNextOperator - index - 1));
        return stoi(operand);
    }
};

int main()
{
string inputExpression;

cout<<"ENTER EXPRESSION TO BE EVALUATED: ";
      cin>>inputExpression;
 
      Program obj(inputExpression);
obj.Evaluate();

      return 0;
}

OUTPUT



C++ Program to check given number is Pernicious or not [DEVCPP/GCC]

PERNICIOUS NUMBER

pernicious number is a positive integer which has prime number of ones in its binary representation.

PROGRAM

// C++ Program to check given number is Pernicious or not

#include <iostream>
#include <bitset>
#include <cassert>

using namespace std;

bool isPrime(int num)
{
    if(num == 1)
        return false;
   
    for(int j=2 ; j<=(num/2) ; j++)
    {
        if(num % j == 0)
            return false;
    }
   
    return true;
}

int main()
{
    long num, count = 0;
   
    cout << "ENTER A NUMBER : ";
    cin>>num;
 
    assert(num > 0);
 
    string binary = bitset<8>(num).to_string();
   
    for(int i=0 ; i < binary.length(); i++)
    {
        if(binary[i] == '1')
            count++;
    }
   
    if(isPrime(count))
        cout<<endl<<num<<" IS A PERNICIOUS NUMBER";
    else
        cout<<endl<<num<<" IS NOT A PERNICIOUS NUMBER";
 
   return 0;
}

OUTPUT


FIND US ON FACEBOOK!