Please note, this is a STATIC archive of website www.tutorialspoint.com from 11 May 2019, cach3.com does not collect or store any user information, there is no "phishing" involved.
Tutorialspoint

expression_evaluation

import 'dart:io';
void main()
{
    double first, second, result;
    print("Enter the Expression:");
    String exp = stdin.readLineSync();
    exp = exp.trim();
    print(exp);
    Label1:
    for(int i=0;i<exp.length;i++)
    {
        if(exp[i]=='(')
        {
            exp = brac(exp);
        }
        continue Label1;
    }
    Label2:
    for(int i=0;i<exp.length;i++)
    {
        if(exp[i]=='/')
        {
            exp = spli(exp,'/');
        }
        continue Label2;
    }
    Label2:
    for(int i=0;i<exp.length;i++)
    {
        if(exp[i]=='*')
        {
            exp = spli(exp,'*');
        }
        continue Label2;
    }
    Label2:
    for(int i=0;i<exp.length;i++)
    {
        if(exp[i]=='+')
        {
            exp = spli(exp,'+');
        }
        continue Label2;
    }
    Label2:
    for(int i=0;i<exp.length;i++)
    {
        if(exp[i]=='-')
        {
            exp = spli(exp,'-');
        }
        continue Label2;
    }
    var ops =['+','-','*','/','%'];
    for(int i=0;i<exp.length;i++)
    {
      if(ops.contains(exp[i])) 
        exp = fin(exp);  
    }
    print(exp);  
}    

String fin(String exp)
 {
    int i1,i2,i3;
    String n1,n2,nu ='',ope;
    var nums = ['1','2','3','4','5','6','7','8','9','0'];
    var ops =['+','-','*','/','%'];
    for(int i=0;i<exp.length;i++)
    {
      if(nums.contains(exp[i]))  
      {
          nu = nu + exp[i];
      }
      else if(ops.contains(exp[i]))
      {
          ope = exp[i];
          n1=nu;
          nu ='';
      }
    }
    n2 = nu;
    i1 = int.parse(n1);
    i2 = int.parse(n2);
    i3 =calc(i1,i2,ope);
    exp = i3.toString();
    return exp;
 }  
    
String brac(String e)
{
    int st=0,ed=0;
    for (int i=0;i<e.length; i++)
    {
        if(e[i]=='(')
        {
            st=i;
            continue;
        }
        if(e[i]==')')
        {
            ed=i;
            break;
        }
        
    }
    st=st+1;
    var s = e.substring(st,ed);
   var number='',num1,num2,oper;
    int a,b;
   var numli = ['1','2','3','4','5','6','7','8','9','0'];
   var op =['+','-','*','/','%'];
    for(int i=0;i<s.length;i++)
    {
      if(numli.contains(s[i]))  
      {
          number = number + s[i];
      }
      else if(op.contains(s[i]))
      {
          oper = s[i];
          num1=number;
          number ='';
      }
    }
    num2 = number;
    a = int.parse(num1);
    b = int.parse(num2);
    int c =calc(a,b,oper);
    String s2=c.toString();
    e = e.replaceAll('('+s+')',s2);
    return e;
}

String spli(String s,String operate)
    {
        var nu='',num1,num2,oper='',nu1='',nu2='';
        int a,b,pt=0;
        var numli = ['1','2','3','4','5','6','7','8','9','0'];
        int p,l;
        lable:
        for(int i=0;i<s.length;i++)
         {
            if(s[i]==operate)
            {
                for(int j=i-1;j>=0;j--)
                {
                    if(numli.contains(s[j]))
                    {
                        nu1 =nu1 +s[j];
                        p=j;
                    }
                    else
                        break;
                }
                for(int k=i+1;k<s.length;k++)
                {
                    if(numli.contains(s[k]))
                    {
                        nu2 =nu2 +s[k];
                        l=k;
                        
                    }
                    else
                        break;
                }
                
                 var n1='';
                for(int i=nu1.length-1;i>=0;i--)
                {
                  n1=n1+nu1[i];  
                }
                int res;
                int val1=int.parse(n1);
                int val2=int.parse(nu2);
                if(operate=='/')
                    res = val1 ~/ val2;
                else if(operate=='*')
                    res = val1 * val2;
                else if(operate=='+')
                    res = val1 + val2;
                else if(operate=='-')
                   res = val1- val2;
                String su = s.substring(p,l+1);
                s=s.replaceAll(su,res.toString());
                return s;
            }
            continue lable;
        }
    }

int calc(int a, int b, String oper)
{
    int result;
    switch(oper)
    {
        case '+':
            return a + b;
            break;
        case '-':
            return a - b;
            break;
        case '*':
            return a * b;
            break;
        case '/':
            return a ~/ b;
            break;
        case '%':
            return a % b;
            break;
        default:
            return 0;
    }
}

Advertisements
Loading...

We use cookies to provide and improve our services. By using our site, you consent to our Cookies Policy.