示例#1
0
文件: ex3.cpp 项目: qiuqiyuan/ppp
// deal with numbers and parentheses
double primary()
{
    Token t = ts.get();
    switch (t.kind) {
        case '{':    // handle '{' expression '}'
            {    
                double d = expression();
                t = ts.get();
                if (t.kind != '}') error("'}' expected");
                return d;
            }
        case '(':    // handle '(' expression ')'
            {    
                double d = expression();
                t = ts.get();
                if (t.kind != ')') error("')' expected");
                return d;
            }
        case '8':            // we use '8' to represent a number
            {
                Token next_t = ts.get();
                if (next_t.kind == '!')
                    return factorial(narrow_cast<int>(t.value));
                else
                    ts.putback(next_t);
                return t.value;  // return the number's value
            }
        default:
            error("primary expected");
    }
}
示例#2
0
double func()
{
	Token t = ts.get();
	if (ts.get().kind == '(') {
		if(t.name == "sqrt"){
			double d = expression();
			t = ts.get();
			if (t.kind != ')') error("')' expected");
			if (d < 0) error("sqrt() parameter is negative number ");
			return sqrt(d);			// 平方根を返す
		}
		else if(t.name == "pow"){
			double d1 = expression();
			t = ts.get();
			if (t.kind != ',') error("',' expected");
			double d2 = expression();
			int i2 = int(d2);
			if (i2 < 0) error("second parameter is negative number ");
			if (i2 != d2) error("second parameter is not integer");
			t = ts.get();
			if (t.kind != ')') error("')' expected");
			return pow(d1, i2);		// べき乗を返す
		}
	}
	else
		error("'(' expected");
}
示例#3
0
文件: e7-1.cpp 项目: hizcode/ppp
// deal with numbers and parentheses
double primary()
{
    Token t = ts.get();
    switch (t.kind) {
    case '(':           // handle '(' expression ')'
        {
            double d = expression();
            t = ts.get();
            if (t.kind != ')') error("')' expected");
            return d;
        }
    case number:    
        return t.value;    // return the number's value
    case name:
		{
			Token next = ts.get();
			if (next.kind == '=') {	// handle name = expression
				double d = expression();
				set_value(t.name,d);
				return d;
			}
			else {
				ts.putback(next);		// not an assignment: return the value
				return get_value(t.name); // return the variable's value
			}
		}
    case '-':
        return - primary();
    case '+':
        return primary();
    default:
        error("primary expected");
    }
}
示例#4
0
double primary(){
	Token t = ts.get();
	switch(t.kind){
		case '(':				// '(' Expression ')'を処理する
			{
				double d = expression();
				t = ts.get();
				if(t.kind != ')') error("')' expected");
				return d;			
			}
		case number:					// 数字を表す
			return t.value;				// 数字の値を返す
		case name:
			{
				Token t2 = ts.get();
				if(t2.kind == '='){				// 変数に値を代入
					double d = expression();
					st.set_value(t.name, d);
					return d;
				}			
				else{
					ts.putback(t2);
					return st.get_value(t.name);	// 変数の値を返す
				}
			}
		case '-':
			return -primary();
		case '+':
			return primary();
		default:
			error("primary expected");
	}
}
double primary()
{
	Token t = ts.get();
	switch (t.kind) {
	case '(':
	{	double d = expression();
		t = ts.get();
		if (t.kind != ')') error("'(' expected");
		return d;
	}
	case sqrtsymb:
        return get_square();
    case powsymb:
        return get_pow();
	case '-':
		return - primary();
    case name:
    {
        t2 = ts.get();
        cin >>
        if(t.kind == '=') return set_value();
        return get_value(t.name);
    }

    case '+':
        return primary();
	case number:
        return t.value;
	default:
		error("primary expected");
	}
}
int main()
try
{
    while (cin) {
        cout << "> ";
        Token t = ts.get();
        while (t.kind == ';') t=ts.get();    // eat ';'
        if (t.kind == 'q') {
            keep_window_open();
            return 0;
        }
        ts.putback(t);
        cout << "= " << expression() << endl;
    }
    keep_window_open();
    return 0;
}
catch (exception& e) {
    cerr << e.what() << endl;
    keep_window_open("~~");
    return 1;
}
catch (...) {
    cerr << "exception \n";
    keep_window_open("~~");
    return 2;
}
示例#7
0
文件: 7-1.cpp 项目: JayDz/PPP-answers
double term()
{
	double left = primary();
	Token t = ts.get();
	while (true) {
		switch (t.kind) {
		case '*':
			left *= primary();
			t = ts.get();
			break;
		case '/':
			{
				double d = primary();
				if (d==0) error("Division by zero.");
				left /= d;
				t = ts.get();
				break;
			}
		case '%':
			{
				int i1 = narrow_cast<int>(left);
				int i2 = narrow_cast<int>(term());
				if (i2 == 0) error("%: divide by zero");
				left = i1 % i2;
				t = ts.get();
				break;
			}
		default:
			ts.putback(t);
			return left;
		}
	}
}
double factorial()
{
    double left = primary();
    Token t = ts.get();        // get the next token from token stream

    while(true) {
        switch (t.kind) {
        case '!':
            {   
                int x = left;
                int count = left;
                for (int i = 1; i < count; i++) {
                    x*=i;
                    //cout << left << "\n" << i << "\n";
                }
                if (x == 0) left = 1;
                else left = x;
                t = ts.get();
                break;
            }
        default: 
            ts.putback(t);     // put t back into the token stream
            return left;
        }
    }
}
// deal with numbers and parentheses
double primary()
{
    Token t = ts.get();
    switch (t.kind) {
    case '(':    // handle '(' expression ')'
        {
            double d = expression();
            t = ts.get();
            if (t.kind != ')') error("')' expected");
            return d;
        }
    case '{':    // handle '(' expression ')'
        {
            double d = expression();
            t = ts.get();
            if (t.kind != '}') error("'}' expected");
            return d;
        }
    case '8':            // we use '8' to represent a number
        return t.value;  // return the number's value
    case 'x':
      error("x");
    default:
        error("primary expected");
    }
}
示例#10
0
文件: ex3.cpp 项目: kaustubhb/PPPCpp
double primary()
{
  Token t = ts.get();
  if(t.kind == '(')
  {
    double val = expression();
    t = ts.get();
    if (t.kind != ')')
      error ("Closing parenthesis expected, but not found");
    return val;
  }
  else if(t.kind == '{')
  {
    double val = expression();
    t = ts.get();
    if(t.kind != '}')
      error ("Closing brace expected, but not found");
    return val;
  }
  else if (t.kind == '8')
    return t.value;
  error("Primary expected, but found something else");

  // can never reach here
  cerr << "Should never reach here" << endl;
  return 0;
}
示例#11
0
// deal with *, /, and %
double term()
{
	double left = primary();
	Token t = ts.get();        // get the next token from token stream

	while (true) {
		switch (t.kind) {
		case '*':
			left *= primary();
			t = ts.get();
			break;		//added break and bracket
		
		case '/':
		{
			double d = primary();
			if (d == 0) error("divide by zero");
			left /= d;
			t = ts.get();
			break;
		}
		default:
			ts.putback(t);     // put t back into the token stream
			return left;
		}
	}
}
示例#12
0
double primary(){
	Token t = ts.get();
	switch(t.kind){
		case '(':				// '(' Expression ')'を処理する
			{
				double d = expression();
				t = ts.get();
				if(t.kind != ')') error("')' expected");
				return d;			
			}
		case '{':				// '{' Expression '}'を処理する
			{
				double d = expression();
				t = ts.get();
				if(t.kind != '}') error("'}' expected");
				return d;			
			}		
		case '8':				// '8'を使って数字を表す
			return t.value;		// 数字の値を返す
		case 'q':				// 追加
			keep_window_open("~0");
			exit(EXIT_SUCCESS);
		default:
			error("primary expected");
	}
}
示例#13
0
文件: 7-9.cpp 项目: JayDz/PPP-answers
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();
		}
	}
}
示例#14
0
文件: ex3.cpp 项目: qiuqiyuan/ppp
int main()
    try
{
    cout << WELCOME << endl;
    cout << INSTRUCT << endl;

    double val = 0;
    while (cin) {
        Token t = ts.get();

        if (t.kind == ';')        // ';' for "print now"
            cout << "=" << val << '\n';
        else
            ts.putback(t);

        t = ts.get();
        if (t.kind == 'x') break; // 'x' for quit
        else
            ts.putback(t);
        val = expression();
    }
    keep_window_open();
}
catch (exception& e) {
    cerr << "error: " << e.what() << '\n'; 
    keep_window_open();
    return 1;
}
catch (...) {
    cerr << "Oops: unknown exception!\n"; 
    keep_window_open();
    return 2;
}
// deal with numbers and parentheses
double primary()
{
    Token t = ts.get();
    switch (t.kind) {
	case '{':
		{
			double e=expression();
			t=ts.get();
			if(t.kind!='}') error("'}' expected");
			return suffix(e);
		}
    case '(':    // handle '(' expression ')'
        {    
            double d = expression();
            t = ts.get();
            if (t.kind != ')') error("')' expected");
            return suffix(d);
        }
    case '8':            // we use '8' to represent a number
        return suffix(t.value);  // return the number's value
	case END:
	case QUIT:
		ts.putback(t);
		return 0;
    default:
		ts.putback(t);
        error("primary expected");
		return t.value;
    }
}
// deal with '!' - factorial. do the conversion to int first. 
double factorial()
{
    double left = primary();
    Token t = ts.get();        // get the next token from token stream

    while(true) {
        switch (t.kind) {
        case '!':
            {
                int n = abs (left); // convert to int
                long int fac = 1;  // 0!=1
                while (n>1) {
                  fac *= n--;  // 1*n*(n-1)*.. *3*2
                }
                if (left < 0) {
                  left = -fac;  // we want to handle the case -5!
                } else {
                  left = fac;
                }                
                t = ts.get();
                break;
            }
        case 'x':
          error("x");
          break;
        default:
            ts.putback(t);     // put t back into the token stream
            return left;
        }
    }
}
示例#17
0
double prim(bool get) {
	if (get) ts.get();
	switch (ts.current().kind) {
	case Kind::number:
	{
						 double v = ts.current().number_value;
						 ts.get();
						 return v;
	}
	case Kind::name:
	{
					   double &v = table[ts.current().string_value];
					   if (ts.get().kind == Kind::assign) v = expr(true);
					   return v;
	}
	case Kind::minus:
	{
						return -prim(true);
	}
	case Kind::lp:
	{
					 auto e = expr(true);
					 if (ts.current().kind != Kind::rp)
						 return error("'(' expected");
	}
	default:
		return error("primary expected");
	}
}
// deal with *, /, and %
double term()
{
    //double left = primary();
    double left = factorial();
    Token t = ts.get();        // get the next token from token stream

    while(true) {
        switch (t.kind) {
        case '*':
            //left *= primary();
            left *= factorial();
            t = ts.get();
            break;
        case '/':
            {
                //double d = primary();
                double d = factorial();
                if (d == 0) error("divide by zero");
                left /= d;
                t = ts.get();
                break;
            }
        case 'x':
          error("x");
          break;
        default:
            ts.putback(t);     // put t back into the token stream
            return left;
        }
    }
}
示例#19
0
文件: 7-1.cpp 项目: JayDz/PPP-answers
double primary()
{
	Token t = ts.get();
	switch (t.kind) {
	case '(':
		{
			double d = expression();
			t = ts.get();
			if (t.kind != ')') error("expected ')'");
			return d;
		}
	case '{':
		{
			double d = expression();
			t = ts.get();
			if (t.kind != '}') error("expected '}'");
			return d;
		}
	case number:
		return t.value;
	case '-':
		return - primary();
	case '+':
		return primary();
	case name:
		return get_value(t.name);
	default:
		error("primary expected");
	}
}
double func(const char func_kind)
{
	Token t {ts.get()};
	if (t.kind != '(')
        error("function expects a '(' as opening");

    switch (func_kind) {
        case sqrt_kind:
            {
                ts.unget(t);
                double d {expression()};
                if (d < 0)
                    error("negative value for sqrt()");
                return sqrt(d);
            }
        case power_kind:
            {
                double d1 {expression()};
                t = ts.get();
                if (t.kind != ',')
                    error("',' expected");
                double d2 {expression()};
                return pow(d1, d2);
            }
        default:
            return func(func_kind);
    }
}
示例#21
0
// deal with numbers and parentheses
double primary()
{
    Token t = ts.get();
    switch (t.kind) {
    case '(':    // handle '(' expression ')'
        {
            double d = expression();
            t = ts.get();
            if (t.kind != ')') error("')' expected");

            Token next = ts.get();
            if(next.kind != '!'){
                ts.putback(next);
                return d;  // return the number's value
            }
            else
                return get_factorical(int(d));
        }
    case '8':            // we use '8' to represent a number
        {
            Token next = ts.get();
            if(next.kind != '!'){
                ts.putback(next);
                return t.value;  // return the number's value
            }
            else
                return get_factorical(int(t.value));
        }
    default:
        error("primary expected");
    }
}
示例#22
0
double factorial()
    //find and calculate factorials
{
    double left = primary();
    Token t = ts.get();

    while(true) {
        switch(t.kind) {
        case '!':
        {  
            if (left == 0)
                left = 1; //By def. of factorial
            else {
                int n = (int)left;
                int ans = n;
                for(int i = 1; i < n; i++)
                    ans = ans * (n - i);
                left = ans;
                t = ts.get();
            }
            break;
        }
        default:
            ts.putback(t);
            return left; //no more '!'
        }//end switch
    }//end while
}
示例#23
0
double term(){
	double left = primary();
	Token t = ts.get();
	while(true){
		switch(t.kind){
			case '*':
				left *= primary();
				t = ts.get();
				break;
			case '/':
				{
					double d = primary();
					if(d == 0) error("divide by zero");
					left /= d;
					t = ts.get();
					break;
				}
			case '%':
				{
					double d = primary();
					int i1 = int(left);
					if(i1 != left) error("left-hand operand of % not int");
					int i2 = int(d);
					if(i2 != d) error("right-hand operand of % not int");
					if(i2 == 0) error("%: divide by zero");
					left = i1 % i2;
					t = ts.get();
					break;
				}
			default:
				ts.putback(t);
				return left;
		}
	}
}
示例#24
0
double declaration(bool is_const)
    //Handle 'name = expression'
    //Declare a variable called "name" with initial value "expresion"
{
    Token t = ts.get();
    if (t.kind != name)
        error("name expected in declaration");
    string var_name = t.name;
    Token t2 = ts.get();
    if (t2.kind != '=')
        error("= missing in declaration of ",var_name);

    double d = expression();
    switch (is_const) {
        case true:
            symtable.declare(var_name, d, true);
            break;
        default:
            if (symtable.is_declared(var_name))
                symtable.set(var_name, d);
            else
                symtable.declare(var_name, d, false);
    }
    return d;
}
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 ';'
	}
}
示例#26
0
double term()
{
  double left = factTerm();
  Token t = ts.get();

  while(true)
  {
    switch(t.kind)
    {
    case '*':
      left *= factTerm();
      t = ts.get();
      break;
    case '/':
      {
        double right = factTerm();
        if(right == 0)
          error("Division by 0 error");
        left /= right;   
        t = ts.get();
      }
      break;
    default:
      ts.putback(t);
      return left;
    }
  }
}
示例#27
0
文件: calc4.cpp 项目: ychmrsk/cpp
/* Term
     Primary
     Term '*' Primary
     Term '/' Primary
     Term '%' Primary
*/
double term()
{
  double left = primary();
  Token t = ts.get();
  while (true) {
    switch (t.kind) {
    case '*':
      left *= primary();
      t = ts.get();
      break;
    case '/':
      {  // to define a variable in case, need { }
        double d = primary();
        if (d == 0) error("divide by zero");
        left /= d;
        t = ts.get();
        break;
      }
//    case '%':
//      left %= primary();
//      t = ts.get();
//      break;
    default:
      ts.putback(t);
      return left;
    }
  }
}
示例#28
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;
}
示例#29
0
文件: e7-1.cpp 项目: hizcode/ppp
// deal with *, /, and %
double term()
{
    double left = primary();
    Token t = ts.get(); // get the next token from token stream

    while(true) {
        switch (t.kind) {
        case '*':
            left *= primary();
            t = ts.get();
            break;
        case '/':
            {    
                double d = primary();
                if (d == 0) error("divide by zero");
                left /= d; 
                t = ts.get();
                break;
            }
        case '%':
            {    
                int i1 = narrow_cast<int>(left);
                int i2 = narrow_cast<int>(term());
                if (i2 == 0) error("%: divide by zero");
                left = i1%i2; 
                t = ts.get();
                break;
            }
        default: 
            ts.putback(t);        // put t back into the token stream
            return left;
        }
    }
}
示例#30
0
static double expression(Token_stream& ts)
{
    double left=term(ts);
	Token t=ts.get();

	double rez;
	bool isdone=false;
	do {
		switch(t.kind){
		case '+':
            left+=term(ts);
			t=ts.get();
			break;
		case '-':
            left-=term(ts);
			t=ts.get();
			break;
		default:
			ts.putback(t);
			isdone=true;
			rez= left;
		}
	}while (!isdone);

	return rez;
}