void calculate() // Performs calculations
{
	while(true) try {
		cout << prompt; // Output '>' symbol
		Token t = ts.get(); // Get a new character
		if (t.kind == help) { // If help was entered open help menu
		cout << "Welcome to Awesome Calculator " << version << " ! Here is the list of the commands:\n"
			"Available operators are '+','-','/','*'. The program is able to work with floating-point numbers\n"
			"'Expression' + ';' - Prints the result (always use it at the end of the statement)\n"
			"'let' + 'name of variable' + '='' + 'value' - defines a variable\n"
			"'const' + 'name of constant' + '='' + 'value' - defines a constant\n"
			"'name of variable' + '=' + 'new value' - assigns a new value to the variable\n"
			"'pow(x,y)' - multiplies x by y times (y must be of type int)\n"
			"'sqrt(x)' - returns square root of x\n"
			"'h' or 'help' - opens this menu\n"
			"'quit' - closes the program\n";
			continue; // Move to next iteration
		}
		while (t.kind == print) t=ts.get(); // Read all ';'
		if (t.kind == quit) return; // Close the program if exit was entered
		ts.unget(t); // Return a character into the stream
		cout << result << statement() << endl; // Output the result
	}
	catch(runtime_error& e) {
		cerr << e.what() << endl; // Cout the error if the exception was thrown
		clean_up_mess(); // Ignores all characters till the ';'
	}
}
Пример #2
0
void calculate()
{
	const string prompt = "> ";
	const string result = "= ";
	while (cin) {
		try {
			bool displayed_help = false;
			cout << prompt;
			Token t = ts.get();
			while (t.kind == print) t = ts.get();
			if (t.kind == quit) return;
			if (t.kind == help_lower) {
				display_help();
				displayed_help = true;
			}
			if (displayed_help == false) {
			ts.putback(t);
			cout << result << statement() << endl;
			}
		}
		catch (exception& e) {
			cerr << e.what() << endl;
			clean_up_mess();
		}
	}
}
Пример #3
0
double calculateLoop()
{
  double result = 0;

  cout << "> ";
  while (cin)
  {

    try
    {
      Token t = ts.get();
      if(t.kind == QUIT)
        break;
      if(t.kind == PRINT) {
        cout << "= " << result << endl << "> ";
        while(t.kind == PRINT)
            t = ts.get();
        ts.putback(t);
        continue;
      }
      ts.putback(t);
      result = statement();
    }
    catch(exception &ex)
    {
      cerr << ex.what() << endl;
      clean_up_mess();
      cout << "> ";
    }
  }
  return result;
}
Пример #4
0
//----------------------------------------------------------------------
void calculate(){
while(cin)try{
	cout << '>';
	Token t = ts.get();
	if(t.kind == print) t=ts.get();
	if(t.kind == 'q')return;
	ts.putback(t);
	cout<<statement()<<'\n';
	}
catch(exception& e){
	cerr<<e.what()<<'\n';
	clean_up_mess();
	}
}
void calculate()
{
  while(true) try {
    cout << prompt;
    Token t = ts.get();
    while (t.kind == print) t=ts.get();
    if (t.kind == quit) return;
    ts.unget(t);
    cout << result << statement() << endl;
  }
  catch(runtime_error& e) {
    cerr << e.what() << endl;
    clean_up_mess();
  }
}
Пример #6
0
void calculate()
{
  while (cin)
    try {
      cout << prompt;
      Token t = ts.get();
      while (t.kind == print) t = ts.get();
      if (t.kind == quit) return;
      ts.putback(t);
      cout << result << statement() << endl;
    } catch (exception& e) {
      cerr << e.what() << endl;
      clean_up_mess();
    }
}
void calculate() // Performs calculations
{
	while(true) try {
		cout << prompt; // Output '>' symbol
		Token t = ts.get(); // Get a new character
		while (t.kind == print) t=ts.get(); // Read all ';'
		if (t.kind == quit) return; // Close the program if exit was entered
		ts.unget(t); // Return a character into the stream
		cout << result << statement() << endl; // Output the result
	}
	catch(runtime_error& e) {
		cerr << e.what() << endl; // Cout the error if the exception was thrown
		clean_up_mess(); // Ignores all characters till the ';'
	}
}
Пример #8
0
void calculate()
{
    while (cin)
      try {
        cout << prompt;
        Token t = ts.get();
        while (t.kind == print) t=ts.get();    // first discard all "prints"
        if (t.kind == quit) return;        // quit
        ts.putback(t);
        cout << result << statement() << endl;
    }
    catch (exception& e) {
        cerr << e.what() << endl;        // write error message
        clean_up_mess();
    }
}
Пример #9
0
void calculate()		// expression evaluation loop
{
  while (cin) 
    try{
      cout << prompt;
      Token t = ts.get();
      while (t.kind == print) t = ts.get(); // first discard all "prints"
      if (t.kind == quit) return;
      ts.putback(t);
      cout << result << expression() << '\n';
    }
    catch (exception& e) {
      cerr << e.what() << '\n';	// write error message
      clean_up_mess();
    }
}
void calculate()
{
	while(true) try {
		cout << PROMPT;
		Token t = ts.get();
		while (t.kind == PRINT) t=ts.get();
		if (t.kind == QUIT) return;
		if (t.kind == HELP) help();
		ts.unget(t);
		cout << RESULT << statement() << endl;
	}
	catch(runtime_error& e) {
		cerr << e.what() << endl;
		clean_up_mess();
	}
}
Пример #11
0
void calculate()
{
	while(true) try {
		cout << prompt;
		Token t = ts.get();
		while (t.kind == print) t=ts.get();	// 出力トーンを飲み込む
		if (t.kind == quit) return;			// 終了トークンの場合は終了する
		ts.unget(t);
		cout << result << statement() << endl;
	}
	catch(runtime_error& e) {
		cerr << e.what() << endl;
		if(!cin)							// cinエラー後の復帰
			cin.clear();
		clean_up_mess();
	}
}
Пример #12
0
void calculate(Token_stream& ts)
{
	const string prompt = "> ";
	const string result = "= ";
	while (cin) {
		try {
			cout << prompt;
			Token t = ts.get();
			while (t.kind == print) t = ts.get();
			if (t.kind == quit) return;
			ts.putback(t);
			cout << result << statement(ts) << endl;
		}
		catch (exception& e) {
			cerr << e.what() << endl;
			clean_up_mess(ts);
		}
	}
}
Пример #13
0
void calculate(){
	while (cin){
		try{
	    	cout << prompt; 						// プロンプトを表示する
	    	Token t = ts.get();
	    	while(t.kind == print) t = ts.get();	// すべてのprintを破棄する
			
	    	if(t.kind == help){		// ヘルプを表示する
				cout << "Simple calculator help text" << endl;
				continue;
	    	}	

	    	if(t.kind == quit) return;
			ts.putback(t);
	    	cout << result << statement() << endl;		    		
		}
		catch(exception& e){
			cerr << e.what() << endl;
			clean_up_mess();
		}
    }
}
Пример #14
0
void calculate()
    /*Expression evaluation loop*/
{   
    while (cin) {
        try {
            cout << prompt;
            Token t = ts.get();

            while (t.kind == print)
                t = ts.get();
            if (t.kind == quit)
                return;

            ts.putback(t);
            cout << result << statement() << "\n";
        }
        catch(runtime_error& e) {
            cerr << "ERROR: " << e.what() << '\n'; //Write error message
            clean_up_mess();
        }
    }//End while
}
Пример #15
0
void calculate()
{
    //while there is input, check if it's an exit command (q), an execute command (;) or values for our token
    while (cin)
        try {
            cout << prompt; //prompt to user

            Token t = tokenStream.get();

            while (t.kind == printResult) t = tokenStream.get();
            if (t.kind == quit) return;

            tokenStream.putback(t);
            cout << result << statement() << endl;

        }

        catch(exception& e)
        {
            cerr<< e.what() << endl;
            clean_up_mess();
        }
}