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 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231
|
/*
* main.cpp
*
* Created on: 29 janv. 2009
* Author: Philippe
*/
#include "main.h"
using namespace std;
bool isOperator(string op)
{
bool temp = false;
for(int i = 0; i < nbOper; i++)
{
if(op == listOperators[i])
temp = true;
}
return temp;
}
bool isVariable(string var)
{
bool temp = false;
for(int i = 0; i < nbVar; i++)
{
if(var == listVariables[i])
temp = true;
}
return temp;
}
bool isNumber(string var)
{
if(var == "0" || var == "1" || var == "2" || var == "3" || var == "4" || var == "5"
|| var == "6" || var == "7" || var == "8" || var == "9")
return true;
else
return false;
}
void showVectorStr(vector<string> vec)
{
for(unsigned int i(0);i<vec.size();++i)
{
cout << vec[i] << " ";
}
}
void showVectorInt(vector<int> vec)
{
for(unsigned int i(0);i<vec.size();++i)
{
cout << vec[i] << " ";
}
}
void scanString(string strInput)
{
for(unsigned int i(0); i < strInput.length(); i++)
{
string read = strInput.substr(i,1);
if(read == " ")
{
//Space... Don't do anything.
}
else if(isOperator(read))
{
operatorFromInput.push_back(read);
indexOpFromInput.push_back(i);
}
else if(isVariable(read))
{
varsFromInput.push_back(read);
indexVarFromInput.push_back(i);
}
else if(isNumber(read))
{
string nbTemp = "";
int j = i;
while(isNumber(strInput.substr(i,1)))
{
nbTemp += strInput.substr(i,1);
i++;
}
varsFromInput.push_back(nbTemp);
indexVarFromInput.push_back(j);
}
else
{
if(isalpha(read[0]))
{
cout << "Unknown variable name " << read[0] << endl;
}
else
{
cout << "Unknown operator " << read[0] << endl;
}
}
}
}
int getPriority(string op)
{
if(op == "=")
return 1;
if(op == "-" || op == "+")
return 2;
if(op == "*" || op == "/")
return 3;
if(op == "(")
return 9; // Pour l'instnat seulement. apres sans doute 2
return 9;
}
int getLowestPriorityIndex(vector<string> vect)
{
int lowestPrio = 7;
int index = 0;
for(unsigned int i(0); i < vect.size(); i++)
{
if(getPriority(vect.at(i)) < lowestPrio)
{
lowestPrio = getPriority(vect.at(i));
index = i;
}
}
return index;
}
void insertInTree(tref &root, string ope, int index)
{
tref newStuff;
newStuff = new cell;
// First treat the very first insertion
if(root == NULL)
{
newStuff->value = ope;
newStuff->index = index;
newStuff->left = NULL;
newStuff->right = NULL;
root = newStuff;
}
else
{
if(index < (root->index))
insertInTree(root->left, ope, index);
else
insertInTree(root->right, ope, index);
}
}
void buildTree()
{
root = NULL;
int lowestPriority;
while(!operatorFromInput.empty())
{
lowestPriority = getLowestPriorityIndex(operatorFromInput);
//Parcourir vecteur pour prendre priorité minimum
string ope = operatorFromInput.at(lowestPriority);
//cout << "ope: " << ope << endl;
//Prendre son index
//Appeler constuction arbre avec ce vecteur et son index
insertInTree(root, ope, indexOpFromInput.at(lowestPriority));
//cout << "Insertion OK: " << ope << " => " << indexOpFromInput.at(lowestPriority) << endl;
//retirer cet operateur
operatorFromInput.erase((operatorFromInput.begin())+lowestPriority);
//retirer son index.
indexOpFromInput.erase((indexOpFromInput.begin())+lowestPriority);
}
//Faire pareil pour variables.
while(!varsFromInput.empty())
{
insertInTree(root, varsFromInput.at(0), indexVarFromInput.at(0));
varsFromInput.erase(varsFromInput.begin());
indexVarFromInput.erase(indexVarFromInput.begin());
}
}
void NLR(tref root)
{
if(root != NULL)
{
cout << root->value << " ";
NLR(root->left);
cout << " ";
NLR(root->right);
cout << " ";
}
}
int main(int argc, char **argv)
{
string str = "";
do
{
cout << "Expression: " << endl;
getline(cin, str);
if(str != "QUIT")
{
scanString(str);
cout << "operateurs:" << endl;
showVectorStr(operatorFromInput);
cout << endl;
// cout << "index operateurs:" << endl;
// showVectorInt(indexOpFromInput);
// cout << endl;
//
// cout << "var et chiffres:" << endl;
// showVectorStr(varsFromInput);
// cout << endl;
// cout << "index var et chiffres:" << endl;
// showVectorInt(indexVarFromInput);
// cout << endl;
buildTree();
NLR(root);
}
}while(str != "QUIT");
} |