Ejemplo n.º 1
0
// Copies the entire cache to a MathTree
MathTree* Parser::cache_to_tree(Node* root)
{
    MathTree* tree = new MathTree();
    list<Node*>::iterator it;
    for (unsigned weight = 0; weight < node_cache.size(); ++weight)
        for (int op = 0; op < LAST_OP; ++op)
            for (it = node_cache[weight][op].begin();
                 it != node_cache[weight][op].end(); ++it)
                tree->add(*it);
    tree->pack();
    tree->set_root(root);
    cout << "Done." << endl;
    return tree;
}
Ejemplo n.º 2
0
int main(int argc, char** argv)
{
    if (argc < 3) {
        print_help();
        exit(1);
    }

    argv++;
    argc--; // Remove executable name from argv
    FabVars v(OUTPUT_SVG, argc, argv);

    if (v.mode != SOLVE_BOOL && v.mode != SOLVE_REAL) {
        cerr << "Error:  math_svg only works on Boolean or Real math strings." << endl;
        exit(4);
    }

    Parser p;
    MathTree* tree = p.parse(v.math_string, v.mode);
    if (!tree)
        return 1;

    cout << "Nodes in tree: " << tree->node_count() << endl
         << "Tree depth: " << tree->root->get_weight() << endl
         << "Evaluating (region size = " << v.ni << " x " << v.nj
         << " x " << v.nk << ")"
         << endl;
#if MULTITHREADED
    ThreadManager<EdgeSolver> tm(v);
    tm.evaluate(tree);
#else
    EdgeSolver s(tree, v);
    s.evaluate_region(Region(v));
    s.save();
#endif

    cout << "\nWriting SVG." << endl;
    v.write_svg();

    delete tree;
    return 0;
}
Ejemplo n.º 3
0
int main (int argc, char * argv [])
	{
	WCS_String temp;
	char op;
	char character;
	long i;
	long val;
	MathTree mathTree;
	Token token;

	cout<<"********************** Welcome to the MathTree Calculator!! ********************" << endl;
	cout<<"Enter 'C' followed by a whole number to enter a constant in the tree" <<endl;
	cout<<"Enter 'V' followed by two digits to enter a variable into the tree" <<endl;
	cout<<"Enter 'v' followed by two digits, a space, and then up to three digits"<<endl;
	cout<<"To set a value to your variable" <<endl;
	cout<<"Enter 'O' followed by and operator (+ - * /) followed by a digit "<<endl;
	cout<<"To insert an operator into the tree with a percedence set by the digit"<<endl;
	cout<<"Enter 'E' to evaluate the tree and to see what the answer is" <<endl;
	cout<<"Enter 'D' to delete the tree" <<endl;
	cout<<"Enter 'X' to exit the program"<<endl;
	cout<<endl;
	cout<<"\t\t\t    ENTER A VALID IMPUT " <<endl;
	for(;;)
		{
		character = cin.get();
		switch (character)
			{
		case 'c':
		case 'C':
			cin >> i;
			cin.ignore ();
			cout<<"you entered a constant "<< i << endl;
			cout<<"\n"<<endl;
			token.SetType (Token::ConstantToken);	// move SetType from private to public in tokenn class
			token.SetValue (i);
			mathTree.InsertOperand (token);
			break;
		case 'V':
			cin >> i;
			cin.ignore ();
			cout<<"You want to enter the variable " << i <<endl;
			token.SetType (Token::VariableToken);
			token.SetWhich (i);
			mathTree.InsertOperand (token);
			cout<<"You entered the variable "<<token.GetWhich() <<endl;
			cout<<"\n"<<endl;
			break;
		case 'e':
		case 'E':
			try{
				cout << "Answer is " << mathTree.Evaluate () << endl;
				cout<<"\n"<<endl;
				}
			catch(MathTree::Exceptions E){
				switch (E){
				case 0:
					cout<<"cannot divide by 0 exiting program.. "<< endl;
					exit (0);
					}
			}
		break;
		case 'v':
			cin >> i;
			cin >> val;
			cin.ignore ();
			cout<<"You want to set the variable " << i << " To the value " << val <<endl;
			token.SetType (Token::VariableToken);
			token.SetWhich (i);
			token.SetValue (val);
			cout<<"the variables value is "<<token.GetValue()<<endl;
			cout<<"\n"<<endl;
			break;
		case 'X':
		case 'x':
			exit (0);
			break;
		case 'O':
		case 'o':
			cin >> op;
			cin >> val;
			cin.ignore ();
			cout<<"You Entered a " << op << " Operator " << endl;
			cout<<"You want to set the " << op << " Operator's precedence to " << val << endl;
			if (op == '+'){
				token.SetType(Token::OperatorPlusToken);
				token.SetPrecedence((Operator::Precedence)(val));
				mathTree.InsertBinaryOperator (token);
				cout<<"operators precedence is : " << token.GetPrecedence() << endl;
				cout<<"\n"<<endl;
				}
			else
				if (op =='-'){
					token.SetType(Token::OperatorMinusToken);
					token.SetPrecedence((Operator::Precedence)(val));
					mathTree.InsertBinaryOperator (token);
					cout<<"operators precedence is : " << token.GetPrecedence() << endl;
					cout<<"\n"<<endl;
					}
				else 
					if (op == '*'){
						token.SetType(Token::OperatorAsteriskToken);
						token.SetPrecedence((Operator::Precedence)(val));
						mathTree.InsertBinaryOperator (token);
						cout<<"operators precedence is : " << token.GetPrecedence() << endl;
						cout<<"\n"<<endl;
						}
					else
						if (op == '/'){
							token.SetType(Token::OperatorSlashToken);
							token.SetPrecedence((Operator::Precedence)(val));
							mathTree.InsertBinaryOperator (token);
							cout<<"operators precedence is : " << token.GetPrecedence() << endl;
							cout<<"\n"<<endl;
							}
						break;

		case 'd':
		case 'D':
			cout<< "Deleting the tree " << endl;
			cout<<"Keep in mind that.... "<< endl;
			cout<<" Variable Token's Enumeration value is 2"<<endl;
			cout<<" Constant Token's Enumeration value is 3"<<endl;
			cout<<" BinaryPlus Token's Enumeration value is 7"<<endl;
			cout<<" BinaryMinus Token's Enumeration value is 8"<<endl;
			cout<<" BinaryAsterisk Token's Enumeration value is 9"<<endl;
			cout<<" BinarySlash Token's Enumeration value is 10"<<endl;
			mathTree.Delete();	
                    break;
        case 'P':
        case 'p':
                    cout<<"Printing your Tree " <<endl;
                    mathTree.walkingPrint();

		}	
	}
        return 0;
	}
Ejemplo n.º 4
0
void main ()
	{
	char		c;
	int			i;
	int			numParen;
	char		op;
	long		Value;
	MathTree	MT;
	Token		Toke;

	Token::TokenType DetermineTokenType(char);
	Operator::Precedence DetermineLePrecedence(char, int);
	void Halp();

	Halp();
	for (;;)
		{
			c = cin.get (); //get the first char
		switch (c)
			{
			case 'c':
			case 'C':
				cin >> i; // we are just going to ASSume this input is perrrrfect.
				Toke.SetType (Token::ConstantToken);	
				Toke.SetValue (i);
				MT.InsertOperand (Toke);
				break;
			case 'o':
			case 'O':
				cin >> op;
				cin >> numParen;
				Toke.SetType(	DetermineTokenType(op)	);
				Toke.SetPrecedence(  DetermineLePrecedence(op, numParen)  );
				MT.InsertBinaryOperator(Toke);
				break;
			case 'V':
				cin >> i;
				cin.ignore ();
				Toke.SetType (Token::VariableToken);
				Toke.SetWhich (i);
				MT.InsertOperand (Toke);
				break;
			case 'e':
			case 'E':
				 try{
						cout << "Answer is " << MT.Evaluate () << endl;
					}
				 catch(...)
					{
						cout << "Diving by zero is bad. mmmkay?." << endl;
						cout << "Deleting tree" << endl;
						MT.DeleteTree();
						cout << "Tree deleted, try again." << endl;
					}
				break;
			case 'v':
				cin >> i;
				cin >> Value;
				cin.ignore ();
				Toke.SetType (Token::VariableToken);
				Toke.SetWhich (i);
				Toke.SetValue (Value);
				break;
			case 'd':
			case 'D':
				MT.DeleteTree();
				break;
			case 'x':
			case 'X':
				exit(0);
				break;
			case 'h':
			case 'H':
				Halp();
			}	
		}
	}