Question No. 4

You might also like

Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 5

Question No.

4
The first step in building a parse tree is to break up the expression string into a list of tokens. There
are four different kinds of tokens to consider: left parentheses, right parentheses, operators, and
operands. We know that whenever we read a left parenthesis we are starting a new expression, and
hence we should create a new tree to correspond to that expression. Conversely, whenever we read
a right parenthesis, we have finished an expression. We also know that operands are going to be
leaf nodes and children of their operators. Finally, we know that every operator is going to have
both a left and a right child.

Using the information from above, we can define four rules as follows:

1. If the current token is a '(', add a new node as the left child of the current node, and descend
to the left child.
2. If the current token is in the list ['+','-','/','*'], set the root value of the current node to the
operator represented by the current token. Add a new node as the right child of the current
node and descend to the right child.
3. If the current token is a number, set the root value of the current node to the number and
return to the parent.
4. If the current token is a ')', go to the parent of the current node.
#include<iostream>
#include<string>
#include<string.h>
using namespace std;
string ass_code = "";
int i = 0;
float calculate(float value1, float value2, char op) {
switch (op) {
case '+':return (value1 + value2);
case '-':return (value1 - value2);
case '*':return (value1 * value2);
case '/':return (value1 / value2);
}
}
bool isoperator(char c) {
bool flag = false;
string exp = "+*/-";
for (int i = 0; i < 4; i++) {
if (c == exp[i])
flag = true;
}
return flag;
}
int persidenceof(char c) {
if (c == '*' || c == '/') {
return 2;
}
else if (c == '-' || c == '+') {
return 1;
}
else
return -1;
}
class stackNum {
private:
int top;
float Arr[20];
public:
stackNum() {
top = -1;
}
void push(float i) {
Arr[++top] = i;
}
float pop() {
return Arr[top--];
}
float get_Top() {
if (top != -1 || top < 20) {
return Arr[top];
}
}
};
class stackChar {
private:
int top;
char Arr[20];
public:
stackChar() {
top = -1;
}
void push(char i) {
Arr[++top] = i;
}
char pop() {
if (top != -1 || top < 20) {
return Arr[top--];
}
}
char get_Top() {
if (top != -1 || top < 20) {
return Arr[top];
}
}
int isEmpty() {
if (top == -1) {
return 1;
}
else
return 0;
}
};

int getResult(string exp) {


stackChar op;
stackNum dig;
string var = "";
float num_exp = 0;
exp = '(' + exp + ')';
int i = 0;
for (; i < exp.length();) {
int val = 0;
if (exp[i] == '(') {
op.push(exp[i]);
i++;
}
else if (isdigit(exp[i]) || exp[i] == '.'){
while (isdigit(exp[i]) || exp[i] == '.') {
var = var + exp[i];
i++;
}
num_exp = (float)atof(var.c_str());
dig.push(num_exp);
num_exp = 0;
var = "";
}
else if (isoperator(exp[i])) {
if (persidenceof(op.get_Top()) >= persidenceof(exp[i])) {
float value1 = dig.pop();
float value2 = dig.pop();
char opr = op.pop();
dig.push(calculate(value2, value1, opr));
op.push(exp[i]);
}
else {
op.push(exp[i]);
}

i++;
}
else if (exp[i] == ')') {
while (op.get_Top() != '(') {
float value1 = dig.pop();
float value2 = dig.pop();
char opr = op.pop();
dig.push(calculate(value2, value1, opr));
}
op.pop();
i++;
}
}
if (op.isEmpty() != 1) {
while (op.get_Top() != '(') {
float value1 = dig.pop();
float value2 = dig.pop();
char opr = op.pop();
dig.push(calculate(value2, value1, opr));
}
op.pop();
}
return dig.pop();
}
int main() {
int i = 0;
while (i != -1) {
ass_code = "";
system("cls");
string exp = "";
cout << "Enter Value: ";
cin >> exp;
cout << "Answer: " << getResult(exp) << endl;
cout << "Enter 1 To continue -1 to exit: "; cin >> i;
}
cout << "Thanks For Using :)\n";
system("pause");
}

You might also like