hey yall , so i have to make a logical calculator with :
(& and , | or , ~ not ,> if)
i made this with (+,-,*,/) for logical calculator (with parenthesis)it takes a INFIX turns it to POSTFIX then takes the values and calculates the final answer :
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109
|
#include<iostream>
#include<stack>
using namespace std;
int prec(char c)
{
if (c == '^')
return 3;
else if (c == '*' || c == '/')
return 2;
else if (c == '+' || c == '-')
return 1;
else
return -1;
}
string infixToPostfix(string s)
{
std::stack<char> st;
st.push('N');
int l = s.length();
string ns;
for (int i = 0; i < l; i++)
{
if ((s[i] >= 'a' && s[i] <= 'z') ||
(s[i] >= 'A' && s[i] <= 'Z'))
ns += s[i];
else if (s[i] == '(')
st.push('(');
else if (s[i] == ')')
{
while (st.top() != 'N' && st.top() != '(')
{
char c = st.top();
st.pop();
ns += c;
}
if (st.top() == '(')
{
char c = st.top();
st.pop();
}
}
else {
while (st.top() != 'N' && prec(s[i]) <=
prec(st.top()))
{
char c = st.top();
st.pop();
ns += c;
}
st.push(s[i]);
}
}
while (st.top() != 'N')
{
char c = st.top();
st.pop();
ns += c;
}
return ns;
}
int E(int x, int y, char z)
{
switch (z)
{
case '+':
return x + y;
case '-':
return x - y;
case '/':
return x / y;
case '*':
return x * y;
case '^':
return pow(x,y);
default:
break;
}
}
int main()
{
string exp; cin >> exp;
int TV, X, Y, Z;
string S1 = infixToPostfix(exp);
stack<int> STACK1;
int LS1 = S1.length();
for (int i = 0; i < LS1; i++)
{
if (isalpha(S1[i]))
{
cout << "Enter the value of ( " << S1[i] << " ) : ";
cin >> TV;
STACK1.push(TV);
}
else
{
X = STACK1.top(); STACK1.pop();
Y = STACK1.top(); STACK1.pop();
Z = E(Y, X, S1[i]);
STACK1.push(Z);
}
}
cout << "\n" << "Ur answer is : ( " << STACK1.top() << " ) \n"; STACK1.pop();
return 0;
}
|
can someone please turn this code into (& and , | or , ~ not ,> if),(the values for input variables needs to be 1 or 0) ?
example of input :
~p & q <== example 1
p > q (p if q) <== example 2
(p & q) & r <== example 3
lets say p = 1 and q = 0 : p&q = 0 , p|q =1 p>q = 1 <== example
THANK YOU IN ADVANCE