import java.util.*; public class Main { public static void main(String[] args) { Scanner in =new Scanner(System.in); int t =in.nextInt(); for(int i =0;i<t;i++) { char[] ch = in.next().toCharArray(); int len = ch.length; int sum=0;int flag=1; for(int j =0;j<len;j++) { if(ch[j]>='0'&&ch[j]<='9'){ int b = 0; while(j<len && ch[j]>='0'&&ch[j]<='9'){ b = b*10+(ch[j]-'0'); ++j; } sum += flag*b; --j; }else if (ch[j] == '+') { flag = 1; }else if (ch[j] == '-') { flag = -1; } } System.out.println(sum); } } } |
Double click to view unformatted code.