logical calculator

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
Last edited on
it works exactly the same way.
case '+':
return x + y;

becomes
case '&':
return (unsigned int)x & (unsigned int)y; //some compilers insist on unsigned for logical operations. Some do not care. The back casting to signed is up to you on the result.

boolean works too. you can return x==y (it will be 0 or 1 as an integer, 0 being false).
Last edited on
hey jonnin thank you for ur response

when i try to replace + with | or * with & the parenthesis keep printing along side with everything else , also i dont know how to show ~ (not) when theres a equation like this :

~p & q or like this ~(p|q)&r
Every time I run it I get a big fat nothing. It just asks what the value of x is, or whatever, but no answer printed or anything.

what input are you feeding it before you changed it?

isnt it just computing a result, so would it not just print the value of ~(p|q)&r .. its just an integer...

if you don't see it give us the new code and the input that does not work exactly as you typed it and the expected output.
Last edited on
the code above takes an infix converts it to postfix and then calculates the answer like this :

INPUT = (a+b*c/d) turns it to postfix : abc*d/+ then asks for the value of variables like :

a=2 b=3 c=4 d=4

then the final answer: 5



i want the code to be like this :

takes something like p&q

after converting it to postfix,

ask for the values like : p=1 q=0

then the answer : p&q = 0 or false
Nothing I have tried so far prints the final answer or the RP operations.

~p & q or like this ~(p|q)&r

This would do it on my calculator, but not sure its officially postfix. Maybe it helps?

p~q&

pq|~r&
hey guys ,i would really appreciate any help

thank you in advance
post your current code. I can't make sense of the above vs what you have now and need.
delete the classes bring the infix part in main function also u dont need the e class
and swap the +/-* with whatever u want
Topic archived. No new replies allowed.