Advertisement
Guest User

Untitled

a guest
Nov 12th, 2018
93
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.15 KB | None | 0 0
  1. import java.io.BufferedReader;
  2. import java.io.InputStreamReader;
  3.  
  4. public class ArithmeticExpression {
  5.  
  6. static int presmetaj(char c[], int l, int r) {
  7. int suma=0;
  8. if(r-l==4) {
  9. int prvbroj=c[l+1]-'0';
  10. int vtorbroj=c[r-1]-'0';
  11. if(c[l+2]=='+') suma+=(prvbroj+vtorbroj);
  12. else if(c[l+2]=='-') suma+=(prvbroj-vtorbroj);
  13. return suma;
  14. }
  15. int brojac=0;
  16. for(int i=l;i<=r;i++)
  17. {
  18. if(c[i]=='(') brojac++;
  19. else if(c[i]==')') brojac--;
  20. else if(brojac==1&&c[i]=='+') return presmetaj(c,l+1,i-1)+presmetaj(c,i+1,r-1);
  21. else if(brojac==1&&c[i]=='-') return presmetaj(c,l+1,i-1)-presmetaj(c,i+1,r-1);
  22. }
  23. return 0;
  24. }
  25.  
  26. public static void main(String[] args) throws Exception {
  27. int i,j,k;
  28.  
  29. BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
  30.  
  31. String expression = br.readLine();
  32. char exp[] = expression.toCharArray();
  33.  
  34. int rez = presmetaj(exp, 0, exp.length-1);
  35. System.out.println(rez);
  36.  
  37. br.close();
  38.  
  39. }
  40.  
  41. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement