Last active
April 26, 2017 19:47
-
-
Save dhairyagabha/c7f12ab203457bd106500a624cb6364d to your computer and use it in GitHub Desktop.
Context Free Grammer
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// | |
// main.cpp | |
// Context-Free-Grammer | |
// | |
// Created by Dhairya Gabhawala on 4/18/17. | |
// Copyright © 2017 Dhairya Gabhawala. All rights reserved. | |
// | |
#include <iostream> | |
#include <string> | |
#include <math.h> | |
#include <algorithm> | |
using namespace std; | |
float x = 0,y = 0,z = 0; | |
int count = 0; | |
string expr, term, factor, id, expression, input; | |
float Expression(string expression),Term(string expression), Factor(string expression); | |
float calculate(float a, float b, char opp){ | |
switch (opp) { | |
case '+': return a+b; | |
case '-':return a-b; | |
case '*':return a*b; | |
case '/':return a/b; | |
} | |
return 0; | |
} | |
bool expoperator(char check){ | |
switch (check) { | |
case '+': return true; | |
case '-': return true; | |
} | |
return false; | |
} | |
bool termoperator(char check){ | |
switch (check) { | |
case '*': return true; | |
case '/': return true; | |
} | |
return false; | |
} | |
float Id(char expression){ | |
switch (expression) { | |
case 'x': return x; break; | |
case 'y': return y; break; | |
case 'z': return z; break; | |
default: break; | |
} | |
return 0; | |
} | |
float Factor(string expression){ | |
int i = expression.length()-1; | |
if (expression.find('(') < expression.find(')')){ | |
cout << "Factor:" << expression.substr(expression.find_first_of('(')+1, expression.find_last_of(')')-1) << endl; | |
return Expression(expression.substr(expression.find_first_of('(')+1, expression.find_last_of(')')-1)); | |
} else if (expression[i] == 'x' || expression[i] == 'y' || expression[i] =='z'){ | |
cout << "Id:" << expression[i] << endl; | |
return Id(expression[i]); | |
} | |
return 0; | |
} | |
float Term(string expression){ | |
int i = expression.length()-1; | |
for(i = i; i >= 0; i--){ | |
if (expression[i] == ')'){ | |
i = expression.substr(0,i-1).find_first_of('('); | |
if ( i == 0){ //check if closing parentheses is ever before opening parentheses | |
return Factor(expression); | |
} | |
} else if (termoperator(expression[i])){ | |
cout << "Term:" << expression.substr(0,i) << endl; // Remove this | |
cout << "Operator:" << expression[i]<< endl; // Remove this | |
cout << "Factor:" << expression.substr(i+1,expression.length()) << endl; // Remove this | |
return calculate(Term(expression.substr(0,i)), Factor(expression.substr(i+1,expression.length())), expression[i]); | |
break; | |
} else if ((expression.find('*') > expression.length()) && (expression.find('/') > expression.length())){ | |
return Factor(expression); | |
} | |
} | |
return 0; | |
} | |
float Expression(string expression) { | |
int i = expression.length() - 1; | |
for (i = i; i >= 0; i--) { | |
if (expression[i] == ')') { | |
i = expression.substr(0, i - 1).find_first_of('('); | |
if (i == 0) { | |
return Term(expression); | |
} | |
} | |
else if (expoperator(expression[i])) { | |
cout << "Expression:" << expression.substr(0, i) << endl; // Remove this | |
cout << "Operator:" << expression[i] << endl; // Remove this | |
cout << "Term:" << expression.substr(i + 1, expression.length()) << endl; // Remove this | |
return calculate(Expression(expression.substr(0, i)), Term(expression.substr(i + 1, expression.length())), expression[i]); | |
} | |
else if ((expression.find('+') > expression.length()) && (expression.find('-') > expression.length())) { | |
if (expression[0] == '(' && expression[expression.length() - 1] == ')') { | |
expression = expression.substr(1, expression.length()); | |
} | |
return Term(expression); | |
} | |
else | |
if (i == 0) { | |
return Term(expression); | |
} | |
} | |
return 0; | |
} | |
int main(int argc, const char * argv[]) { | |
int i = 1; | |
cout << "*********Context Free Grammer*********" << '\n'; | |
while(i > 0){ | |
std::getline(std::cin, input); | |
if ((input.find("x") < input.length()) && (input.find("=") < input.length())) { | |
x = std::stof(input.substr(input.find_last_of("=")+1,input.length())); | |
} else if ((input.find("y") < input.length())&& (input.find("=") < input.length())){ | |
y = std::stof(input.substr(input.find_last_of("=")+1,input.length())); | |
} else if ((input.find("z") < input.length()) && (input.find("=") < input.length())){ | |
z = std::stof(input.substr(input.find_last_of("=")+1,input.length())); | |
} else if (((input.find("x") < input.length()) || (input.find("y") < input.length()) || (input.find("z") < input.length())) && (input.find("=") > input.length())){ | |
std::string::iterator end_pos = std::remove(input.begin(), input.end(), ' '); | |
input.erase(end_pos, input.end()); | |
expression = input; | |
cout << expression << endl; | |
cout << Expression(expression) << '\n' << endl; | |
} | |
}; | |
return 0; | |
} |
jglewis56
commented
Apr 26, 2017
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment