I'm using Microsoft Visual Studios 2017. I have tried this code for ours but I donno what's wrong!
Here's the error pic:
The code works fine without the
Code:
bool equal_to_operator_flag = false;
So that seems to be the fault over here but I have no clue why.
Here's the full code:
Code:
#include <conio.h>
#include <iostream>
#include <ctype.h>
/* For character comparisions */
#include <vector>
#include <string>
#include <windows.h>
/* For Sleep.. :D */
#include <algorithm>
/* For replace */
#include <sstream>
/* For splitting input to terms */
int main() {
using namespace std;
using std::cout;
// Temporary
/*-------------------------------------------------------------------------*/
cout << " Please type an expression \n (each term has one variable, alpha-numerical only, \n operations and variables separated by spaces)\n\n";
string input_expression;
getline(cin, input_expression);
// input
{
int length = (int)input_expression.length();
for (int i = 0; i < length; i++) {
if (input_expression[i] == '+' || input_expression[i] == '/' || input_expression[i] == '-' || input_expression[i] == '*' || input_expression[i] == '^' || input_expression[i] == '%') {
input_expression.insert(i, " ");
input_expression.insert(i + 2, " ");
}
}
}
std::replace(input_expression.begin(), input_expression.end(), '\t', ' ');
//Trims Tabspaces
std::string::size_type pos;
while ((pos = input_expression.find(" ")) != std::string::npos)
{
input_expression.replace(pos, 2, " ");
}// Trims multiple white spaces to just one
cout << "You entered the expression: "<<input_expression<<endl<<endl;
/*-------------------------------------------------------------------------*/
string buf;
stringstream ss(input_expression);
vector <string> terms;
vector <string> numeric;
vector <string> alpha_numeric;
vector <string> term_operator;
vector <string> invalid;
vector <string> sorted_terms;
vector <int> term_type;
// 0 - alpha-numeric
// 1 - numeric
// 2 - ^
// % - 3
// ( - 4, ) - 5
// / - 6
// * - 7
// + - 8
// - - 9
// = - 10
while (ss >> buf)
terms.push_back(buf);
cout << "Terms: ";
for (int i = 0; i < (int)terms.size(); i++)
cout << terms[i] << " | ";
cout << "\n\n\n";
/*-------------------------------------------------------------------------*/
// input =
// -A 636 / a5 6& -24 -5.5 -6a 9! 5..4 = .5 AA5 = 5aa 53a53 53a a3 =
bool equal_to_operator_flag = false;
for (int i = 0; i < (int) terms.size() ; i++) {
bool term_operator_flag = false;
bool numeric_flag = true;
bool valid_flag = true;
int decimal = 0;
int alpha = 0;
bool alpha_taken_flag = false;
// bool equal_to_operator_flag = false;
string term = (string) terms[i];
// ^ - 2
// % - 3
// / - 6
// * - 7
// + - 8
// - - 9
// = - 10
if (term == "=") {
term_operator_flag = true;
if(equal_to_operator_flag)
invalid.push_back(terms[i]);
else {
term_operator.push_back(terms[i]);
term_type.push_back(10);
equal_to_operator_flag = true;
}
}
else if (term == "^") {
term_operator.push_back(terms[i]);
term_type.push_back(2);
term_operator_flag = true;
}
else if (term == "%") {
term_operator.push_back(terms[i]);
term_type.push_back(3);
term_operator_flag = true;
}
else if (term == "/") {
term_operator.push_back(terms[i]);
term_type.push_back(6);
term_operator_flag = true;
}
else if (term == "*") {
term_operator.push_back(terms[i]);
term_type.push_back(7);
term_operator_flag = true;
}
else if (term == "+") {
term_operator.push_back(terms[i]);
term_type.push_back(8);
term_operator_flag = true;
}
else if (term == "-") {
term_operator.push_back(terms[i]);
term_type.push_back(9);
term_operator_flag = true;
}
for (int i = 0; i < (int) term.length(); i++) {
if (i == 0) {
if (!(isdigit(term[0]))) { // If it's not a digit
if (term[0] != '-') // If first value isn't '-'
if ((term[0] != '.')) // If first value isn't '.'
numeric_flag = false;
else
decimal++;
}
if (!(isalnum(term[0]))) // If it's not alpha-numerical
if (term[0] != '-') // if first value isn't '-'
valid_flag = false;
if (isalpha(term[0]))
alpha++;
}
else {
if (isalpha(term[i])) { // If it's an alphabet
alpha++;
alpha_taken_flag = true;
}
if ((isdigit(term[i])) && alpha_taken_flag)
valid_flag = false;
if (!(isdigit(term[i]))) { // If it's not a digit
if ((term[i] != '.')) // If it isn't '.'
numeric_flag = false;
else
decimal++;
if (decimal > 1) // If more than one decimal
numeric_flag = false;
}
if (!(isalnum(term[i]))) // If it's not alpha-numerical
valid_flag = false;
else if (alpha > 1) // If there's more than one alphabet in the term
valid_flag = false;
}
}
if (term_operator_flag) {
//term_operator.push_back(terms[i]);
//if(terms[i]== "(")
term_operator_flag = false;
}
else if (numeric_flag) {
numeric.push_back(terms[i]);
term_type.push_back(1);
}
else if (valid_flag) {
alpha_numeric.push_back(terms[i]);
term_type.push_back(0);
}
else
invalid.push_back(terms[i]);
}
cout << endl << "Operator Terms:" << endl << "\t";
for (int i = 0; i < (int)term_operator.size(); i++) {
cout << term_operator[i] << " | ";
}
cout << endl<< "Numeric Terms:"<<endl<<"\t";
for (int i = 0; i < (int)numeric.size(); i++) {
cout << numeric[i]<<" | ";
}
cout << endl << "Alpha-Numeric Terms:" << endl << "\t";
for (int i = 0; i < (int)alpha_numeric.size(); i++) {
cout << alpha_numeric[i] << " | ";
}
cout << endl << "Invalid Terms:" << endl<<"\t";
for (int i = 0; i < (int)invalid.size(); i++) {
cout << invalid[i] << " | ";
}
cout << endl << endl << "Inputted Terms were:" << endl<<"\t";
for (int i = 0; i < (int)terms.size(); i++) {
cout << terms[i] << " ";
}
cout << endl << endl;
cout << "Terms after sorting:" << endl;
for (int i = 0; i < (int)terms.size(); i++)
if ((std::find(numeric.begin(), numeric.end(), terms[i]) != numeric.end()) || (std::find(alpha_numeric.begin(), alpha_numeric.end(), terms[i]) != alpha_numeric.end()) || (std::find(term_operator.begin(), term_operator.end(), terms[i]) != term_operator.end()))
sorted_terms.push_back(terms[i]);
for (int i = 0; i < (int) sorted_terms.size(); i++) {
cout << sorted_terms[i] << "(" << term_type[i] << ") | ";
}
_getch();
return 0;
}