library

This documentation is automatically generated by online-judge-tools/verification-helper

View the Project on GitHub beet-aizu/library

:heavy_check_mark: test/aoj/0109.test.cpp

Depends on

Code

// verification-helper: PROBLEM http://judge.u-aizu.ac.jp/onlinejudge/description.jsp?id=0109

#include <bits/stdc++.h>
using namespace std;

#define call_from_test
#include "../../string/parse.cpp"
#undef call_from_test

signed main(){
  cin.tie(0);
  ios::sync_with_stdio(0);

  int n;
  cin>>n;
  while(n--){
    string s;
    int p=0;
    cin>>s;
    s.pop_back();
    cout<<expression(s,p)<<endl;
  }
  return 0;
}
#line 1 "test/aoj/0109.test.cpp"
// verification-helper: PROBLEM http://judge.u-aizu.ac.jp/onlinejudge/description.jsp?id=0109

#include <bits/stdc++.h>
using namespace std;

#define call_from_test
#line 1 "string/parse.cpp"

#line 3 "string/parse.cpp"
using namespace std;
#endif
//BEGIN CUT HERE
int expression(string&,int&);
int term(string&,int&);
int factor(string&,int&);
int number(string&,int&);

bool f;

int expression(string& s,int& p){
  int res=term(s,p);
  while(p<(int)s.size()){
    if(s[p]=='+'){
      p++;
      res+=term(s,p);
      continue;
    }
    if(s[p]=='-'){
      p++;
      res-=term(s,p);
      continue;
    }
    break;
  }
  return res;
}

int term(string& s,int& p){
  int res=factor(s,p);
  while(p<(int)s.size()){
    if(s[p]=='*'){
      p++;
      res*=factor(s,p);
      continue;
    }
    if(s[p]=='/'){
      p++;
      int tmp=factor(s,p);
      if(tmp==0){
        f=1;
        break;
      }
      res/=tmp;
      continue;
    }
    break;
  }
  return res;
}

int factor(string& s,int& p){
  int res;
  if(s[p]=='('){
    p++;
    res=expression(s,p);
    p++;
  }else{
    res=number(s,p);
  }
  return res;
}

int number(string& s,int& p){
  int res=0;
  while(p<(int)s.size() and isdigit(s[p]))
    res=res*10+s[p++]-'0';
  return res;
}
//END CUT HERE
#ifndef call_from_test
signed main(){
  return 0;
}
#endif
#line 8 "test/aoj/0109.test.cpp"
#undef call_from_test

signed main(){
  cin.tie(0);
  ios::sync_with_stdio(0);

  int n;
  cin>>n;
  while(n--){
    string s;
    int p=0;
    cin>>s;
    s.pop_back();
    cout<<expression(s,p)<<endl;
  }
  return 0;
}
Back to top page