Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include<bits/stdc++.h>
- #define pb push_back
- #define err (s.size() == 0x11 || s.size() == 0x0b)
- using namespace std;
- typedef enum{
- OPERATOR,
- INT,
- LINEENTER
- } Symbol;
- Symbol sym;
- int t = 0 , num , only_int = 0 , aaa = 0;
- char op;
- bool error = 0 , error2 = 0 , error3 = 0 , zero = 0;
- vector<string> p;
- string s;
- bool is_op( char c ){ return c == '+' || c == '-' || c == '*' || c == '/'; }
- bool is_num( char c ){ return c >= '0' && c <= '9'; }
- bool is_int(){
- int tmp = t;
- if( (s[t] == '+' || s[t] == '-') && is_num( s[t+1] ) ) return 1;
- else if( is_num( s[t] ) ) return 1;
- return 0;
- }
- Symbol peek(){
- while( s[t] == ' ' && ++t );
- if( is_int() ) return INT;
- else if( is_op( s[t] ) ) return OPERATOR;
- else if( s[t] == '\n' || s[t] == '\0' ) return LINEENTER;
- }
- int match( Symbol sym ){
- while( s[t] == ' ' && ++t );
- if( sym == OPERATOR ){
- op = s[t++];
- return 2147483647;
- }
- else if( sym == INT ){
- if( !is_int() ) error = 1;
- int neg = 1 , sum = 0;
- if( s[t] == '-' ) ++t , neg = -1;
- if( s[t] == '+' ) ++t;
- while( is_num( s[t] ) ){
- sum *= 10;
- sum += s[t++] - 48;
- }
- num = sum * neg;
- }
- else if( sym == LINEENTER ){
- }
- }
- bool ReadFormula(){
- stringstream ss;
- ss << s;
- string tmp;
- while( ss >> tmp ){
- for( int i = 0 ; i < tmp.size() ; ++i ){
- //cout << tmp[i] << endl;
- if( !is_op( tmp[i] ) && !is_num( tmp[i] ) ){
- int j = i - 1 , k = i + 1;
- bool bb = 1;
- while( !is_num( tmp[j] ) && !is_op( tmp[j] ) && j > -1 ) --j;
- if( is_num( tmp[j] ) ){
- while( is_num( tmp[j] ) || ( (tmp[j] == '+' || tmp[j] == '-') && bb ) ) {
- if( tmp[j] == '+' || tmp[j] == '-' ) bb = 0;
- --j;
- }
- }
- while( !is_num( tmp[k] ) && !is_op( tmp[k] ) && k < tmp.size() ) ++k;
- while( is_num( tmp[k] ) || is_op( tmp[k] ) ) ++k;
- cout << "Error: Unknown token ";
- for( int w = j + 1 ; w < k ; ++w ) cout << tmp[w];
- puts("");
- return 0;
- }
- }
- }
- return 1;
- }
- int exp(){
- if( peek() == INT ){
- match( INT );
- return num;
- }
- else if( peek() == OPERATOR ){
- //puts( "OPERA" );
- error2 = 1;
- match( OPERATOR );
- char o = op;
- int a , b;
- a = exp();
- b = exp();
- match( LINEENTER );
- if( b == 2147483647 ){
- if( o != '-' || o != '+' ) error = error3 = 1;
- if( o == '-' ) return a * -1;
- else return a;
- }
- //cout << a << o << b << endl;
- switch( o ){
- case '+':
- return a + b;
- break;
- case '-':
- return a - b;
- break;
- case '*':
- return a * b;
- break;
- case '/':
- if( !b ){
- //printf( "aaa->%d error->%d error2->%d error3->%d only_int->%d zero -> %d\n" , aaa,error,error2,error3,only_int,zero );
- if( !( error || only_int > 1 || aaa > 1 ) ) zero = 1;
- return 0;
- }
- return a / b;
- break;
- }
- }
- else {
- //error = 1;
- return 2147483647;
- //puts( "exp error" );
- }
- return 0;
- }
- int exps(){
- if( peek() == OPERATOR || peek() == INT ){
- error2 = 0;
- ++aaa;
- int ans = exp();
- if( !error2 ) ++only_int;
- exps();
- return ans;
- }
- else{
- // LAMDA
- }
- }
- int prog(){
- if( peek() == OPERATOR || peek() == INT ){
- return exps();
- }
- else{
- error = 1;
- //puts( "prog error" );
- }
- }
- int EvalFormula(){
- return prog();
- }
- int main(){
- puts( "Welcome use our calculator!" );
- cout << "> ";
- while( getline( cin , s ) ){
- error = error2 = error3 = only_int = zero = aaa = t = 0;
- while( s[t] == ' ' && ++t );
- if( t == s.size() && cin.eof() ) break;
- if( s == "" ) {
- cout << "> ";
- continue;
- }
- if( !ReadFormula() ) {
- cout << "> ";
- continue;
- }
- int ans = EvalFormula();
- //printf( "aaa->%d error->%d error2->%d error3->%d only_int->%d zero -> %d\n" , aaa,error,error2,error3,only_int,zero );
- if( aaa > 1 ) {
- if( aaa == 2 && only_int == 1 && err ) puts( "Error: Divide by ZERO!" );
- else puts("Error: Illegal formula!");
- }
- else if( error || only_int > 1 && !zero ) puts("Error: Illegal formula!");
- else if( zero ) puts( "Error: Divide by ZERO!" );
- else{
- cout << ans << endl;
- }
- if( cin.eof() ) break;
- cout << "> ";
- }
- puts( "ByeBye~" );
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement