output
stringlengths
52
181k
instruction
stringlengths
296
182k
#include <iostream> #include <cstdio> #include <cstring> #include <cstdlib> #include <cmath> #include <vector> #include <string> #include <map> #include <set> #include <queue> #include <stack> #include <algorithm> using namespace std; #define rep(i,j) REP((i), 0, (j)) #define REP(i,j,k) for(int i=(j);(i)<(k);++i) #define between(a,x,b) ((a)<=(x)&&(x)<=(b)) #define F first #define S second #define INF 1 << 30 char e[128]; int p; int exp(); int term(); int fact(); int exp(){ int ret = term(); while(e[p] == '+' || e[p] == '-'){ if(e[p] == '+'){ p++; ret += term(); }else if(e[p] == '-'){ p++; ret -= term(); } } return ret; } int term(){ int ret = fact(); while(e[p] == '*' || e[p] == '/'){ if(e[p] == '*'){ p++; ret *= fact(); }else if(e[p] == '/'){ p++; ret /= fact(); } } return ret; } int fact(){ int ret = 0; if(e[p] == '('){ p++; ret = exp(); p++; } while(isdigit(e[p])){ ret = ret * 10 + e[p] - '0'; p++; } return ret; } int main(){ int n; scanf("%d", &n); while(n--){ scanf("%s", e); p = 0; printf("%d\n", exp()); } return 0; }
### Prompt Develop a solution in cpp to the problem described below: Your task is to write a program which reads an expression and evaluates it. * The expression consists of numerical values, operators and parentheses, and the ends with '='. * The operators includes +, - , *, / where respectively represents, addition, subtraction, multiplication and division. * Precedence of the operators is based on usual laws. That is one should perform all multiplication and division first, then addition and subtraction. When two operators have the same precedence, they are applied from left to right. * You may assume that there is no division by zero. * All calculation is performed as integers, and after the decimal point should be truncated * Length of the expression will not exceed 100. * -1 × 109 ≤ intermediate results of computation ≤ 109 Input The input is a sequence of datasets. The first line contains an integer n which represents the number of datasets. There will be n lines where each line contains an expression. Output For each datasets, prints the result of calculation. Example Input 2 4-2*3= 4*(8+4+3)= Output -2 60 ### Response ```cpp #include <iostream> #include <cstdio> #include <cstring> #include <cstdlib> #include <cmath> #include <vector> #include <string> #include <map> #include <set> #include <queue> #include <stack> #include <algorithm> using namespace std; #define rep(i,j) REP((i), 0, (j)) #define REP(i,j,k) for(int i=(j);(i)<(k);++i) #define between(a,x,b) ((a)<=(x)&&(x)<=(b)) #define F first #define S second #define INF 1 << 30 char e[128]; int p; int exp(); int term(); int fact(); int exp(){ int ret = term(); while(e[p] == '+' || e[p] == '-'){ if(e[p] == '+'){ p++; ret += term(); }else if(e[p] == '-'){ p++; ret -= term(); } } return ret; } int term(){ int ret = fact(); while(e[p] == '*' || e[p] == '/'){ if(e[p] == '*'){ p++; ret *= fact(); }else if(e[p] == '/'){ p++; ret /= fact(); } } return ret; } int fact(){ int ret = 0; if(e[p] == '('){ p++; ret = exp(); p++; } while(isdigit(e[p])){ ret = ret * 10 + e[p] - '0'; p++; } return ret; } int main(){ int n; scanf("%d", &n); while(n--){ scanf("%s", e); p = 0; printf("%d\n", exp()); } return 0; } ```
#include <cctype> #include <cassert> #include <iostream> using namespace std; string S; size_t cur; //?§£???????§??????? int digit() { assert(isdigit(S[cur])); //S[cur]?????°?????§???????????¨????¢???? int n = S[cur] - '0'; // '0'???0????????? cur = cur + 1; //?????????????????? return n; } int number() { int n = digit(); while(cur < S.size() && isdigit(S[cur])) { //?¬??????°??????1??????????????? n = n*10 + digit(); } return n; } int expression(); //????????£?¨? int factor() { if(S[cur] != '(') { return number(); } cur += 1; int n = expression (); assert(S[cur] == ')'); cur +=1; return n; } int term() { int a = factor(); while(cur < S.size() && (S[cur] == '*' || S[cur] == '/')) { char op = S[cur++]; int b = factor(); if(op == '*') { a *= b; } else { a /= b; } } return a; } int expression() { int a = term(); while(cur < S.size() && (S[cur] == '+' || S[cur] =='-')) { //?¶???????????????????????¶??????? char op = S[cur++]; int b = term(); assert(op == '+' || op == '-'); if(op == '+') { a += b; } else { a -= b; } } return a; } int main() { int N; cin >> N; for(int i=0; i<N; ++i) { cur = 0; cin >> S; S.resize(S.size() - 1); //?????????=????????? cout << expression() << endl; } }
### Prompt Construct a CPP code solution to the problem outlined: Your task is to write a program which reads an expression and evaluates it. * The expression consists of numerical values, operators and parentheses, and the ends with '='. * The operators includes +, - , *, / where respectively represents, addition, subtraction, multiplication and division. * Precedence of the operators is based on usual laws. That is one should perform all multiplication and division first, then addition and subtraction. When two operators have the same precedence, they are applied from left to right. * You may assume that there is no division by zero. * All calculation is performed as integers, and after the decimal point should be truncated * Length of the expression will not exceed 100. * -1 × 109 ≤ intermediate results of computation ≤ 109 Input The input is a sequence of datasets. The first line contains an integer n which represents the number of datasets. There will be n lines where each line contains an expression. Output For each datasets, prints the result of calculation. Example Input 2 4-2*3= 4*(8+4+3)= Output -2 60 ### Response ```cpp #include <cctype> #include <cassert> #include <iostream> using namespace std; string S; size_t cur; //?§£???????§??????? int digit() { assert(isdigit(S[cur])); //S[cur]?????°?????§???????????¨????¢???? int n = S[cur] - '0'; // '0'???0????????? cur = cur + 1; //?????????????????? return n; } int number() { int n = digit(); while(cur < S.size() && isdigit(S[cur])) { //?¬??????°??????1??????????????? n = n*10 + digit(); } return n; } int expression(); //????????£?¨? int factor() { if(S[cur] != '(') { return number(); } cur += 1; int n = expression (); assert(S[cur] == ')'); cur +=1; return n; } int term() { int a = factor(); while(cur < S.size() && (S[cur] == '*' || S[cur] == '/')) { char op = S[cur++]; int b = factor(); if(op == '*') { a *= b; } else { a /= b; } } return a; } int expression() { int a = term(); while(cur < S.size() && (S[cur] == '+' || S[cur] =='-')) { //?¶???????????????????????¶??????? char op = S[cur++]; int b = term(); assert(op == '+' || op == '-'); if(op == '+') { a += b; } else { a -= b; } } return a; } int main() { int N; cin >> N; for(int i=0; i<N; ++i) { cur = 0; cin >> S; S.resize(S.size() - 1); //?????????=????????? cout << expression() << endl; } } ```
#include<iostream> #include<cstdlib> #include<string> using namespace std; int i; string str; int Expression(); int Term(); int Factor(); int No(int); int main(){ int n; cin >> n; while(n--){ cin >> str; i = 0; cout << Expression() << endl; } } int Expression(){ int res = Term(); while(1){ switch(str[i]){ case '+': i++; res += Term(); break; case '-': i++; res -= Term(); break; default: return res; } } } int Term(){ int res = Factor(); while(true){ switch(str[i]){ case '(': i++; res += Factor(); break; case '*': i++; res *= Factor(); break; case '/': i++; res /= Factor(); break; default: return res; } } } int Factor(){ int res = 0; switch(str[i]){ case '(': i++; res = Expression(); i++; return res; default: return No(i); } } int No(int old){ while(isdigit(str[i])) i++; return atoi((str.substr(old,i)).c_str()); }
### Prompt Please provide a CPP coded solution to the problem described below: Your task is to write a program which reads an expression and evaluates it. * The expression consists of numerical values, operators and parentheses, and the ends with '='. * The operators includes +, - , *, / where respectively represents, addition, subtraction, multiplication and division. * Precedence of the operators is based on usual laws. That is one should perform all multiplication and division first, then addition and subtraction. When two operators have the same precedence, they are applied from left to right. * You may assume that there is no division by zero. * All calculation is performed as integers, and after the decimal point should be truncated * Length of the expression will not exceed 100. * -1 × 109 ≤ intermediate results of computation ≤ 109 Input The input is a sequence of datasets. The first line contains an integer n which represents the number of datasets. There will be n lines where each line contains an expression. Output For each datasets, prints the result of calculation. Example Input 2 4-2*3= 4*(8+4+3)= Output -2 60 ### Response ```cpp #include<iostream> #include<cstdlib> #include<string> using namespace std; int i; string str; int Expression(); int Term(); int Factor(); int No(int); int main(){ int n; cin >> n; while(n--){ cin >> str; i = 0; cout << Expression() << endl; } } int Expression(){ int res = Term(); while(1){ switch(str[i]){ case '+': i++; res += Term(); break; case '-': i++; res -= Term(); break; default: return res; } } } int Term(){ int res = Factor(); while(true){ switch(str[i]){ case '(': i++; res += Factor(); break; case '*': i++; res *= Factor(); break; case '/': i++; res /= Factor(); break; default: return res; } } } int Factor(){ int res = 0; switch(str[i]){ case '(': i++; res = Expression(); i++; return res; default: return No(i); } } int No(int old){ while(isdigit(str[i])) i++; return atoi((str.substr(old,i)).c_str()); } ```
//18 #include<iostream> #include<cctype> using namespace std; char s[101]; char *p; int pp(); int pm(); int pb(); int pp(){ int a=pm(); while(*p=='+'||*p=='-'){ char o=*p++; int b=pm(); if(o=='+'){ a+=b; }else{ a-=b; } } return a; } int pm(){ int a=pb(); while(*p=='*'||*p=='/'){ char o=*p++; int b=pb(); if(o=='*'){ a*=b; }else{ a/=b; } } return a; } int pb(){ if(*p=='('){ p++; int a=pp(); p++; return a; }else{ int a=0; while(isdigit(*p)){ a=a*10+*p-'0'; p++; } return a; } } int main(){ int n; cin>>n; while(n--){ cin>>s; p=s; cout<<pp()<<endl; } return 0; }
### Prompt Create a solution in Cpp for the following problem: Your task is to write a program which reads an expression and evaluates it. * The expression consists of numerical values, operators and parentheses, and the ends with '='. * The operators includes +, - , *, / where respectively represents, addition, subtraction, multiplication and division. * Precedence of the operators is based on usual laws. That is one should perform all multiplication and division first, then addition and subtraction. When two operators have the same precedence, they are applied from left to right. * You may assume that there is no division by zero. * All calculation is performed as integers, and after the decimal point should be truncated * Length of the expression will not exceed 100. * -1 × 109 ≤ intermediate results of computation ≤ 109 Input The input is a sequence of datasets. The first line contains an integer n which represents the number of datasets. There will be n lines where each line contains an expression. Output For each datasets, prints the result of calculation. Example Input 2 4-2*3= 4*(8+4+3)= Output -2 60 ### Response ```cpp //18 #include<iostream> #include<cctype> using namespace std; char s[101]; char *p; int pp(); int pm(); int pb(); int pp(){ int a=pm(); while(*p=='+'||*p=='-'){ char o=*p++; int b=pm(); if(o=='+'){ a+=b; }else{ a-=b; } } return a; } int pm(){ int a=pb(); while(*p=='*'||*p=='/'){ char o=*p++; int b=pb(); if(o=='*'){ a*=b; }else{ a/=b; } } return a; } int pb(){ if(*p=='('){ p++; int a=pp(); p++; return a; }else{ int a=0; while(isdigit(*p)){ a=a*10+*p-'0'; p++; } return a; } } int main(){ int n; cin>>n; while(n--){ cin>>s; p=s; cout<<pp()<<endl; } return 0; } ```
#include <iostream> #include <string> #include <algorithm> #include <cctype> #include <cstdlib> using namespace std; string query; const char *p; int exprs(); int factor() { char *e; int ret; if(*p == '(') { p ++; ret = exprs(); p ++; return ret; } ret = strtol(p, &e, 10); // cout << "factor: " << ret << endl; p = e; //cout << *p << endl; return ret; } int term() { int ret = factor(); for(;;) { if(*p == '*') { p++; ret *= factor(); } else if(*p == '/') { p++; ret /= factor(); } else { break; } } return ret; } int exprs() { int ret = term(); for(;;) { if(*p == '+') { p ++; ret += term(); } else if(*p == '-') { p ++; ret -= term(); } else { break; } } return ret; } int main() { int n; cin >> n; cin.ignore(); for(int i=0; i<n; i++) { getline(cin, query); query = query.substr(0, query.size()-1); p = query.c_str(); cout << exprs() << endl; } return 0; }
### Prompt Construct a CPP code solution to the problem outlined: Your task is to write a program which reads an expression and evaluates it. * The expression consists of numerical values, operators and parentheses, and the ends with '='. * The operators includes +, - , *, / where respectively represents, addition, subtraction, multiplication and division. * Precedence of the operators is based on usual laws. That is one should perform all multiplication and division first, then addition and subtraction. When two operators have the same precedence, they are applied from left to right. * You may assume that there is no division by zero. * All calculation is performed as integers, and after the decimal point should be truncated * Length of the expression will not exceed 100. * -1 × 109 ≤ intermediate results of computation ≤ 109 Input The input is a sequence of datasets. The first line contains an integer n which represents the number of datasets. There will be n lines where each line contains an expression. Output For each datasets, prints the result of calculation. Example Input 2 4-2*3= 4*(8+4+3)= Output -2 60 ### Response ```cpp #include <iostream> #include <string> #include <algorithm> #include <cctype> #include <cstdlib> using namespace std; string query; const char *p; int exprs(); int factor() { char *e; int ret; if(*p == '(') { p ++; ret = exprs(); p ++; return ret; } ret = strtol(p, &e, 10); // cout << "factor: " << ret << endl; p = e; //cout << *p << endl; return ret; } int term() { int ret = factor(); for(;;) { if(*p == '*') { p++; ret *= factor(); } else if(*p == '/') { p++; ret /= factor(); } else { break; } } return ret; } int exprs() { int ret = term(); for(;;) { if(*p == '+') { p ++; ret += term(); } else if(*p == '-') { p ++; ret -= term(); } else { break; } } return ret; } int main() { int n; cin >> n; cin.ignore(); for(int i=0; i<n; i++) { getline(cin, query); query = query.substr(0, query.size()-1); p = query.c_str(); cout << exprs() << endl; } return 0; } ```
#include<iostream> #include<cstdlib> using namespace std; typedef pair<int,const char*>parsed; parsed expr(const char *p); parsed term(const char *p); parsed fact(const char *p); parsed expr(const char *p){ parsed r=term(p); while(*r.second=='+'||*r.second=='-'){ char op =*r.second; int tmp=r.first; r=term(r.second+1); if(op=='+')r.first=tmp+r.first; else r.first=tmp-r.first; } return r; } parsed term(const char *p){ parsed r=fact(p); while(*r.second=='*'||*r.second=='/'){ char op=*r.second; int tmp=r.first; r=fact(r.second+1); if(op=='*')r.first=tmp*r.first; else r.first=tmp/r.first; } return r; } parsed fact(const char *p){ if(isdigit(*p)){ int t=*(p++)-'0'; while(isdigit(*p))t=t*10+*(p++)-'0'; return parsed(t,p); } else if(*p=='('){ parsed r=expr(p+1); if(*r.second!=')')exit(0); return parsed(r.first,r.second+1); } else exit(0); } int main(){ string str; int n; cin>>n; while(n--){ cin>>str; cout<<expr(str.c_str()).first<<endl; } return 0; }
### Prompt Please formulate a Cpp solution to the following problem: Your task is to write a program which reads an expression and evaluates it. * The expression consists of numerical values, operators and parentheses, and the ends with '='. * The operators includes +, - , *, / where respectively represents, addition, subtraction, multiplication and division. * Precedence of the operators is based on usual laws. That is one should perform all multiplication and division first, then addition and subtraction. When two operators have the same precedence, they are applied from left to right. * You may assume that there is no division by zero. * All calculation is performed as integers, and after the decimal point should be truncated * Length of the expression will not exceed 100. * -1 × 109 ≤ intermediate results of computation ≤ 109 Input The input is a sequence of datasets. The first line contains an integer n which represents the number of datasets. There will be n lines where each line contains an expression. Output For each datasets, prints the result of calculation. Example Input 2 4-2*3= 4*(8+4+3)= Output -2 60 ### Response ```cpp #include<iostream> #include<cstdlib> using namespace std; typedef pair<int,const char*>parsed; parsed expr(const char *p); parsed term(const char *p); parsed fact(const char *p); parsed expr(const char *p){ parsed r=term(p); while(*r.second=='+'||*r.second=='-'){ char op =*r.second; int tmp=r.first; r=term(r.second+1); if(op=='+')r.first=tmp+r.first; else r.first=tmp-r.first; } return r; } parsed term(const char *p){ parsed r=fact(p); while(*r.second=='*'||*r.second=='/'){ char op=*r.second; int tmp=r.first; r=fact(r.second+1); if(op=='*')r.first=tmp*r.first; else r.first=tmp/r.first; } return r; } parsed fact(const char *p){ if(isdigit(*p)){ int t=*(p++)-'0'; while(isdigit(*p))t=t*10+*(p++)-'0'; return parsed(t,p); } else if(*p=='('){ parsed r=expr(p+1); if(*r.second!=')')exit(0); return parsed(r.first,r.second+1); } else exit(0); } int main(){ string str; int n; cin>>n; while(n--){ cin>>str; cout<<expr(str.c_str()).first<<endl; } return 0; } ```
#include<cstdio> #include<string> #include<cctype> #include<iostream> #include<algorithm> using namespace std; typedef string::const_iterator State; int expr(State &begin); int term(State &begin); int number(State &begin); int factor(State &begin); int number(State &begin){ int ret=0; while(isdigit(*begin)){ ret*=10; ret+=(*begin)-'0'; begin++; } return ret; } int term(State &begin){ int ret=factor(begin); for(;;){ if(*begin=='*'){ begin++; ret*=factor(begin); }else if(*begin=='/'){ begin++; ret/=factor(begin); }else break; } return ret; } int expr(State &begin){ int ret=term(begin); for(;;){ if(*begin=='+'){ begin++; ret+=term(begin); }else if(*begin=='-'){ begin++; ret-=term(begin); }else{ break; } } return ret; } int factor(State &begin){ if(*begin=='('){ begin++; int ret=expr(begin); begin++; return ret; }else{ return number(begin); } } int main(){ int N; cin>>N; cin.ignore(); for(int i=0;i<N;i++){ string str; getline(cin,str); State begin=str.begin(); int ans=expr(begin); cout<<ans<<endl; } return 0; }
### Prompt Your challenge is to write a cpp solution to the following problem: Your task is to write a program which reads an expression and evaluates it. * The expression consists of numerical values, operators and parentheses, and the ends with '='. * The operators includes +, - , *, / where respectively represents, addition, subtraction, multiplication and division. * Precedence of the operators is based on usual laws. That is one should perform all multiplication and division first, then addition and subtraction. When two operators have the same precedence, they are applied from left to right. * You may assume that there is no division by zero. * All calculation is performed as integers, and after the decimal point should be truncated * Length of the expression will not exceed 100. * -1 × 109 ≤ intermediate results of computation ≤ 109 Input The input is a sequence of datasets. The first line contains an integer n which represents the number of datasets. There will be n lines where each line contains an expression. Output For each datasets, prints the result of calculation. Example Input 2 4-2*3= 4*(8+4+3)= Output -2 60 ### Response ```cpp #include<cstdio> #include<string> #include<cctype> #include<iostream> #include<algorithm> using namespace std; typedef string::const_iterator State; int expr(State &begin); int term(State &begin); int number(State &begin); int factor(State &begin); int number(State &begin){ int ret=0; while(isdigit(*begin)){ ret*=10; ret+=(*begin)-'0'; begin++; } return ret; } int term(State &begin){ int ret=factor(begin); for(;;){ if(*begin=='*'){ begin++; ret*=factor(begin); }else if(*begin=='/'){ begin++; ret/=factor(begin); }else break; } return ret; } int expr(State &begin){ int ret=term(begin); for(;;){ if(*begin=='+'){ begin++; ret+=term(begin); }else if(*begin=='-'){ begin++; ret-=term(begin); }else{ break; } } return ret; } int factor(State &begin){ if(*begin=='('){ begin++; int ret=expr(begin); begin++; return ret; }else{ return number(begin); } } int main(){ int N; cin>>N; cin.ignore(); for(int i=0;i<N;i++){ string str; getline(cin,str); State begin=str.begin(); int ans=expr(begin); cout<<ans<<endl; } return 0; } ```
#include <iostream> #include <cassert> #define REP(i,l,n) for(int i=l;i<n;++i) #define rep(i,n) REP(i,0,n) #define MAX 100 using namespace std; int ans; int n,pointer; char input[MAX]; int evalE(); int evalT(); int evalF(); int main(){ cin >> n; rep(i,n){ pointer = 0; cin >> input; ans = evalE(); cout << ans <<endl; } return 0; } int evalE(){ int value = evalT(); while(true){ if(input[pointer] == '+'){ pointer++; value += evalT(); }else if(input[pointer] == '-'){ pointer++; value -= evalT(); }else{ break; } } return value; } int evalT(){ int value = evalF(); while(true){ if(input[pointer] == '*'){ pointer++; value *= evalF(); }else if(input[pointer] == '/'){ pointer++; value /= evalF(); }else{ break; } } return value; } int evalF(){ int value = 0; if(input[pointer] == '('){ pointer++; value = evalE(); assert(input[pointer] == ')'); pointer++; } while(input[pointer]>='0'&&input[pointer]<='9'){ value *= 10; value += (int)(input[pointer++] - '0'); } return value; }
### Prompt Please create a solution in CPP to the following problem: Your task is to write a program which reads an expression and evaluates it. * The expression consists of numerical values, operators and parentheses, and the ends with '='. * The operators includes +, - , *, / where respectively represents, addition, subtraction, multiplication and division. * Precedence of the operators is based on usual laws. That is one should perform all multiplication and division first, then addition and subtraction. When two operators have the same precedence, they are applied from left to right. * You may assume that there is no division by zero. * All calculation is performed as integers, and after the decimal point should be truncated * Length of the expression will not exceed 100. * -1 × 109 ≤ intermediate results of computation ≤ 109 Input The input is a sequence of datasets. The first line contains an integer n which represents the number of datasets. There will be n lines where each line contains an expression. Output For each datasets, prints the result of calculation. Example Input 2 4-2*3= 4*(8+4+3)= Output -2 60 ### Response ```cpp #include <iostream> #include <cassert> #define REP(i,l,n) for(int i=l;i<n;++i) #define rep(i,n) REP(i,0,n) #define MAX 100 using namespace std; int ans; int n,pointer; char input[MAX]; int evalE(); int evalT(); int evalF(); int main(){ cin >> n; rep(i,n){ pointer = 0; cin >> input; ans = evalE(); cout << ans <<endl; } return 0; } int evalE(){ int value = evalT(); while(true){ if(input[pointer] == '+'){ pointer++; value += evalT(); }else if(input[pointer] == '-'){ pointer++; value -= evalT(); }else{ break; } } return value; } int evalT(){ int value = evalF(); while(true){ if(input[pointer] == '*'){ pointer++; value *= evalF(); }else if(input[pointer] == '/'){ pointer++; value /= evalF(); }else{ break; } } return value; } int evalF(){ int value = 0; if(input[pointer] == '('){ pointer++; value = evalE(); assert(input[pointer] == ')'); pointer++; } while(input[pointer]>='0'&&input[pointer]<='9'){ value *= 10; value += (int)(input[pointer++] - '0'); } return value; } ```
#include<iostream> #include<string> #include<cctype> using namespace std; int number(const string &s, int &k); int fact(const string &s, int &k); int term(const string &s, int &k); int expr(const string &s, int &k){ int result = term(s,k); while(true){ if(s[k]=='+')result += term(s,++k); else if(s[k]=='-')result -= term(s,++k); else break; } return result; } int term(const string &s, int &k){ int res = fact(s,k); while(true){ if(s[k]=='*')res *= fact(s,++k); else if(s[k]=='/')res /= fact(s,++k); else break; } return res; } int fact(const string &s, int &k){ if(s[k]=='('){ int res = 0; ++k; res = expr(s,k); k++; return res; } return number(s,k); } int number(const string &s, int &k){ int res = 0; while(isdigit(s[k])){ res *= 10; res += (int)(s[k++] - '0'); } return res; } int main(void){ int n; cin >> n; while(n--){ string s; cin >> s; int k = 0; cout << expr(s, k) << endl; } return 0; }
### Prompt Please provide a CPP coded solution to the problem described below: Your task is to write a program which reads an expression and evaluates it. * The expression consists of numerical values, operators and parentheses, and the ends with '='. * The operators includes +, - , *, / where respectively represents, addition, subtraction, multiplication and division. * Precedence of the operators is based on usual laws. That is one should perform all multiplication and division first, then addition and subtraction. When two operators have the same precedence, they are applied from left to right. * You may assume that there is no division by zero. * All calculation is performed as integers, and after the decimal point should be truncated * Length of the expression will not exceed 100. * -1 × 109 ≤ intermediate results of computation ≤ 109 Input The input is a sequence of datasets. The first line contains an integer n which represents the number of datasets. There will be n lines where each line contains an expression. Output For each datasets, prints the result of calculation. Example Input 2 4-2*3= 4*(8+4+3)= Output -2 60 ### Response ```cpp #include<iostream> #include<string> #include<cctype> using namespace std; int number(const string &s, int &k); int fact(const string &s, int &k); int term(const string &s, int &k); int expr(const string &s, int &k){ int result = term(s,k); while(true){ if(s[k]=='+')result += term(s,++k); else if(s[k]=='-')result -= term(s,++k); else break; } return result; } int term(const string &s, int &k){ int res = fact(s,k); while(true){ if(s[k]=='*')res *= fact(s,++k); else if(s[k]=='/')res /= fact(s,++k); else break; } return res; } int fact(const string &s, int &k){ if(s[k]=='('){ int res = 0; ++k; res = expr(s,k); k++; return res; } return number(s,k); } int number(const string &s, int &k){ int res = 0; while(isdigit(s[k])){ res *= 10; res += (int)(s[k++] - '0'); } return res; } int main(void){ int n; cin >> n; while(n--){ string s; cin >> s; int k = 0; cout << expr(s, k) << endl; } return 0; } ```
#include <iostream> #include <cstdio> #include <cstring> #include <algorithm> #include <vector> #include <cctype> using namespace std; string s; typedef string::const_iterator State; int expression(State&); int term(State&); int number(State&); int factor(State&); int expression(State &begin){ int ret = term(begin); while(1){ if(*begin == '+'){ begin++; ret += term(begin); } else if(*begin == '-'){ begin++; ret -= term(begin); } else break; } return ret; } int term(State &begin){ int ret = factor(begin); while(1){ if(*begin == '*'){ begin++; ret *= factor(begin); } else if(*begin == '/'){ begin++; int tmp = factor(begin); ret /= tmp; } else break; } return ret; } int number(State &begin){ int ret = 0; while(isdigit(*begin)){ ret *= 10; ret += *begin-'0'; begin++; } return ret; } int factor(State &begin){ if(*begin == '('){ begin++; int ret = expression(begin); begin++; return ret; } else return number(begin); } int main(){ int n; cin >> n; for(int i = 0 ; i < n ; i++){ cin >> s; State ss = s.begin(); int ans = expression(ss); cout << ans << endl; } }
### Prompt Develop a solution in CPP to the problem described below: Your task is to write a program which reads an expression and evaluates it. * The expression consists of numerical values, operators and parentheses, and the ends with '='. * The operators includes +, - , *, / where respectively represents, addition, subtraction, multiplication and division. * Precedence of the operators is based on usual laws. That is one should perform all multiplication and division first, then addition and subtraction. When two operators have the same precedence, they are applied from left to right. * You may assume that there is no division by zero. * All calculation is performed as integers, and after the decimal point should be truncated * Length of the expression will not exceed 100. * -1 × 109 ≤ intermediate results of computation ≤ 109 Input The input is a sequence of datasets. The first line contains an integer n which represents the number of datasets. There will be n lines where each line contains an expression. Output For each datasets, prints the result of calculation. Example Input 2 4-2*3= 4*(8+4+3)= Output -2 60 ### Response ```cpp #include <iostream> #include <cstdio> #include <cstring> #include <algorithm> #include <vector> #include <cctype> using namespace std; string s; typedef string::const_iterator State; int expression(State&); int term(State&); int number(State&); int factor(State&); int expression(State &begin){ int ret = term(begin); while(1){ if(*begin == '+'){ begin++; ret += term(begin); } else if(*begin == '-'){ begin++; ret -= term(begin); } else break; } return ret; } int term(State &begin){ int ret = factor(begin); while(1){ if(*begin == '*'){ begin++; ret *= factor(begin); } else if(*begin == '/'){ begin++; int tmp = factor(begin); ret /= tmp; } else break; } return ret; } int number(State &begin){ int ret = 0; while(isdigit(*begin)){ ret *= 10; ret += *begin-'0'; begin++; } return ret; } int factor(State &begin){ if(*begin == '('){ begin++; int ret = expression(begin); begin++; return ret; } else return number(begin); } int main(){ int n; cin >> n; for(int i = 0 ; i < n ; i++){ cin >> s; State ss = s.begin(); int ans = expression(ss); cout << ans << endl; } } ```
#include <bits/stdc++.h> using namespace std; string s; int p,n; int bnf1(); int g_A(){ int r=0; if(s[p]=='(')p++,r=bnf1(),p++; else while(isdigit(s[p])) r=r*10+(s[p++]-'0'); return r; } int bnf2(){ int res=g_A(); while(s[p]=='*'||s[p]=='/'){ int t=p++; if(s[t]=='*')res*=g_A(); if(s[t]=='/')res/=g_A(); } return res; } int bnf1(){ int res=bnf2(); while(s[p]=='+'||s[p]=='-'){ int t=p++; if(s[t]=='+')res+=bnf2(); if(s[t]=='-')res-=bnf2(); } return res; } int main(){ cin>>n; while(n--){ cin>>s; p=0; cout<<bnf1()<<endl; } }
### Prompt Construct a Cpp code solution to the problem outlined: Your task is to write a program which reads an expression and evaluates it. * The expression consists of numerical values, operators and parentheses, and the ends with '='. * The operators includes +, - , *, / where respectively represents, addition, subtraction, multiplication and division. * Precedence of the operators is based on usual laws. That is one should perform all multiplication and division first, then addition and subtraction. When two operators have the same precedence, they are applied from left to right. * You may assume that there is no division by zero. * All calculation is performed as integers, and after the decimal point should be truncated * Length of the expression will not exceed 100. * -1 × 109 ≤ intermediate results of computation ≤ 109 Input The input is a sequence of datasets. The first line contains an integer n which represents the number of datasets. There will be n lines where each line contains an expression. Output For each datasets, prints the result of calculation. Example Input 2 4-2*3= 4*(8+4+3)= Output -2 60 ### Response ```cpp #include <bits/stdc++.h> using namespace std; string s; int p,n; int bnf1(); int g_A(){ int r=0; if(s[p]=='(')p++,r=bnf1(),p++; else while(isdigit(s[p])) r=r*10+(s[p++]-'0'); return r; } int bnf2(){ int res=g_A(); while(s[p]=='*'||s[p]=='/'){ int t=p++; if(s[t]=='*')res*=g_A(); if(s[t]=='/')res/=g_A(); } return res; } int bnf1(){ int res=bnf2(); while(s[p]=='+'||s[p]=='-'){ int t=p++; if(s[t]=='+')res+=bnf2(); if(s[t]=='-')res-=bnf2(); } return res; } int main(){ cin>>n; while(n--){ cin>>s; p=0; cout<<bnf1()<<endl; } } ```
#include<iostream> #include<sstream> #include<algorithm> #include<numeric> #include<vector> #include<map> #include<set> #include<queue> #include<cstdio> #include<cmath> #include<cstdlib> #include<cstring> #include<cassert> #define rep(i,n) for(int i=0;i<n;i++) #define all(c) (c).begin(),(c).end() #define fr(i,c) for(__typeof((c).begin()) i=(c).begin();i!=(c).end();i++) #define mp make_pair #define pb push_back #define dbg(x) cerr<<#x<<" = "<<(x)<<endl using namespace std; typedef long long ll; typedef vector<int> vi; typedef pair<int,int> pi; const int inf=1<<28; const double INF=1e12,EPS=1e-9; string in; int n; int eval(int s,int t){ int i,d=0; for(i=t-1;i>=s;i--){ if(in[i]==')')d++; else if(in[i]=='(')d--; if(d==0&&(in[i]=='+'||in[i]=='-'))break; } if(i>=s){ int a=eval(s,i),b=eval(i+1,t); return in[i]=='+'?a+b:a-b; } d=0; for(i=t-1;i>=s;i--){ if(in[i]==')')d++; else if(in[i]=='(')d--; if(d==0&&(in[i]=='*'||in[i]=='/'))break; } if(i>=s){ int a=eval(s,i),b=eval(i+1,t); return in[i]=='*'?a*b:a/b; } if(in[s]=='('&&in[t-1]==')')return eval(s+1,t-1); return atoi(in.substr(s,t-s).c_str()); } int main() { int cs; cin>>cs; cin.ignore(); rep(i,cs){ getline(cin,in); n=in.size(); cout<<eval(0,n-1)<<endl; } return 0; }
### Prompt Please create a solution in Cpp to the following problem: Your task is to write a program which reads an expression and evaluates it. * The expression consists of numerical values, operators and parentheses, and the ends with '='. * The operators includes +, - , *, / where respectively represents, addition, subtraction, multiplication and division. * Precedence of the operators is based on usual laws. That is one should perform all multiplication and division first, then addition and subtraction. When two operators have the same precedence, they are applied from left to right. * You may assume that there is no division by zero. * All calculation is performed as integers, and after the decimal point should be truncated * Length of the expression will not exceed 100. * -1 × 109 ≤ intermediate results of computation ≤ 109 Input The input is a sequence of datasets. The first line contains an integer n which represents the number of datasets. There will be n lines where each line contains an expression. Output For each datasets, prints the result of calculation. Example Input 2 4-2*3= 4*(8+4+3)= Output -2 60 ### Response ```cpp #include<iostream> #include<sstream> #include<algorithm> #include<numeric> #include<vector> #include<map> #include<set> #include<queue> #include<cstdio> #include<cmath> #include<cstdlib> #include<cstring> #include<cassert> #define rep(i,n) for(int i=0;i<n;i++) #define all(c) (c).begin(),(c).end() #define fr(i,c) for(__typeof((c).begin()) i=(c).begin();i!=(c).end();i++) #define mp make_pair #define pb push_back #define dbg(x) cerr<<#x<<" = "<<(x)<<endl using namespace std; typedef long long ll; typedef vector<int> vi; typedef pair<int,int> pi; const int inf=1<<28; const double INF=1e12,EPS=1e-9; string in; int n; int eval(int s,int t){ int i,d=0; for(i=t-1;i>=s;i--){ if(in[i]==')')d++; else if(in[i]=='(')d--; if(d==0&&(in[i]=='+'||in[i]=='-'))break; } if(i>=s){ int a=eval(s,i),b=eval(i+1,t); return in[i]=='+'?a+b:a-b; } d=0; for(i=t-1;i>=s;i--){ if(in[i]==')')d++; else if(in[i]=='(')d--; if(d==0&&(in[i]=='*'||in[i]=='/'))break; } if(i>=s){ int a=eval(s,i),b=eval(i+1,t); return in[i]=='*'?a*b:a/b; } if(in[s]=='('&&in[t-1]==')')return eval(s+1,t-1); return atoi(in.substr(s,t-s).c_str()); } int main() { int cs; cin>>cs; cin.ignore(); rep(i,cs){ getline(cin,in); n=in.size(); cout<<eval(0,n-1)<<endl; } return 0; } ```
#include <cstdio> #include <iostream> #include <algorithm> #include <cstdlib> #include <cmath> #include <vector> #include <ctime> #include <string> #include <map> #include <stack> #include <queue> #include <complex> #include <cctype> #include <cassert> using namespace std; int n; string S; int cur; int digit(){ assert(isdigit(S[cur])); int n = S[cur] - '0'; cur++; return n; } int number(){ int n = digit(); while(cur < S.size() && isdigit(S[cur])){ n = n * 10 + digit(); } return n; } int expression(); int factor(){ if (S[cur] != '(') return number(); cur++; int n = expression(); cur++; return n; } int term(){ int sum = factor(); while(cur < S.size() && (S[cur] == '*' || S[cur] == '/')){ char op = S[cur]; cur++; int b = factor(); if(op == '*') sum *= b; else sum /= b; } return sum; } int expression(){ int sum = term(); while(cur < S.size() && (S[cur] == '+' || S[cur] == '-')){ char op = S[cur]; cur++; int b = term(); if(op == '+') sum += b; else sum -= b; } return sum; } int main(void){ cin >> n; for(int i = 0; i < n; i++){ cur = 0; cin >> S; S.resize(S.size()-1); cout << expression() << endl; } return 0; }
### Prompt Create a solution in CPP for the following problem: Your task is to write a program which reads an expression and evaluates it. * The expression consists of numerical values, operators and parentheses, and the ends with '='. * The operators includes +, - , *, / where respectively represents, addition, subtraction, multiplication and division. * Precedence of the operators is based on usual laws. That is one should perform all multiplication and division first, then addition and subtraction. When two operators have the same precedence, they are applied from left to right. * You may assume that there is no division by zero. * All calculation is performed as integers, and after the decimal point should be truncated * Length of the expression will not exceed 100. * -1 × 109 ≤ intermediate results of computation ≤ 109 Input The input is a sequence of datasets. The first line contains an integer n which represents the number of datasets. There will be n lines where each line contains an expression. Output For each datasets, prints the result of calculation. Example Input 2 4-2*3= 4*(8+4+3)= Output -2 60 ### Response ```cpp #include <cstdio> #include <iostream> #include <algorithm> #include <cstdlib> #include <cmath> #include <vector> #include <ctime> #include <string> #include <map> #include <stack> #include <queue> #include <complex> #include <cctype> #include <cassert> using namespace std; int n; string S; int cur; int digit(){ assert(isdigit(S[cur])); int n = S[cur] - '0'; cur++; return n; } int number(){ int n = digit(); while(cur < S.size() && isdigit(S[cur])){ n = n * 10 + digit(); } return n; } int expression(); int factor(){ if (S[cur] != '(') return number(); cur++; int n = expression(); cur++; return n; } int term(){ int sum = factor(); while(cur < S.size() && (S[cur] == '*' || S[cur] == '/')){ char op = S[cur]; cur++; int b = factor(); if(op == '*') sum *= b; else sum /= b; } return sum; } int expression(){ int sum = term(); while(cur < S.size() && (S[cur] == '+' || S[cur] == '-')){ char op = S[cur]; cur++; int b = term(); if(op == '+') sum += b; else sum -= b; } return sum; } int main(void){ cin >> n; for(int i = 0; i < n; i++){ cur = 0; cin >> S; S.resize(S.size()-1); cout << expression() << endl; } return 0; } ```
#include <iostream> #include <string> #include <cctype> using namespace std; typedef string::const_iterator Iter; int expr(Iter &p); int term(Iter &p); int factor(Iter &p); int number(Iter &p); int expr(Iter &p) { int r = term(p); for(;;) { if(*p == '+') { p++; int rs = term(p); r += rs; } else if(*p == '-') { p++; int rs = term(p); r -= rs; } else break; } return r; } int term(Iter &p) { int r = factor(p); for(;;) { if(*p == '*') { p++; int rs = factor(p); r *= rs; } else if(*p == '/') { p++; int rs = factor(p); r /= rs; } else break; } return r; } int factor(Iter &p) { if(*p == '(') { p++; int r = expr(p); p++; return r; } else return number(p); } int number(Iter &p) { int r = 0; while(isdigit(*p)) { r *= 10; r += *p - '0'; p++; } return r; } int main() { int N; cin >> N; cin.ignore(); while(N--) { string line; getline(cin, line); line = line += "|"; Iter begin = line.begin(); cout << expr(begin) << endl; } }
### Prompt In cpp, your task is to solve the following problem: Your task is to write a program which reads an expression and evaluates it. * The expression consists of numerical values, operators and parentheses, and the ends with '='. * The operators includes +, - , *, / where respectively represents, addition, subtraction, multiplication and division. * Precedence of the operators is based on usual laws. That is one should perform all multiplication and division first, then addition and subtraction. When two operators have the same precedence, they are applied from left to right. * You may assume that there is no division by zero. * All calculation is performed as integers, and after the decimal point should be truncated * Length of the expression will not exceed 100. * -1 × 109 ≤ intermediate results of computation ≤ 109 Input The input is a sequence of datasets. The first line contains an integer n which represents the number of datasets. There will be n lines where each line contains an expression. Output For each datasets, prints the result of calculation. Example Input 2 4-2*3= 4*(8+4+3)= Output -2 60 ### Response ```cpp #include <iostream> #include <string> #include <cctype> using namespace std; typedef string::const_iterator Iter; int expr(Iter &p); int term(Iter &p); int factor(Iter &p); int number(Iter &p); int expr(Iter &p) { int r = term(p); for(;;) { if(*p == '+') { p++; int rs = term(p); r += rs; } else if(*p == '-') { p++; int rs = term(p); r -= rs; } else break; } return r; } int term(Iter &p) { int r = factor(p); for(;;) { if(*p == '*') { p++; int rs = factor(p); r *= rs; } else if(*p == '/') { p++; int rs = factor(p); r /= rs; } else break; } return r; } int factor(Iter &p) { if(*p == '(') { p++; int r = expr(p); p++; return r; } else return number(p); } int number(Iter &p) { int r = 0; while(isdigit(*p)) { r *= 10; r += *p - '0'; p++; } return r; } int main() { int N; cin >> N; cin.ignore(); while(N--) { string line; getline(cin, line); line = line += "|"; Iter begin = line.begin(); cout << expr(begin) << endl; } } ```
#include <bits/stdc++.h> using namespace std; string::const_iterator it; int number(); int factor(); int term(); int expression(); int number() { int ret = 0; while(isdigit(*it)) { ret *= 10; ret += *it - '0'; it++; } return ret; } int factor() { int ret; if (*it == '(') { it++; ret = expression(); it++; } else { return number(); } return ret; } int term() { int ret = factor(); for (;;) { if (*it == '*') { it++; ret *= factor(); } else if (*it == '/') { it++; ret /= factor(); } else { break; } } return ret; } int expression() { int ret = term(); for (;;) { if (*it == '+') { it++; ret += term(); } else if (*it == '-') { it++; ret -= term(); } else { break; } } return ret; } int main() { int data_num; cin >> data_num; cin.ignore(); for (int i = 0; i < data_num; ++i) { string calc_formula; cin >> calc_formula; it = calc_formula.begin(); cout << expression() << endl; } return 0; }
### Prompt Generate a cpp solution to the following problem: Your task is to write a program which reads an expression and evaluates it. * The expression consists of numerical values, operators and parentheses, and the ends with '='. * The operators includes +, - , *, / where respectively represents, addition, subtraction, multiplication and division. * Precedence of the operators is based on usual laws. That is one should perform all multiplication and division first, then addition and subtraction. When two operators have the same precedence, they are applied from left to right. * You may assume that there is no division by zero. * All calculation is performed as integers, and after the decimal point should be truncated * Length of the expression will not exceed 100. * -1 × 109 ≤ intermediate results of computation ≤ 109 Input The input is a sequence of datasets. The first line contains an integer n which represents the number of datasets. There will be n lines where each line contains an expression. Output For each datasets, prints the result of calculation. Example Input 2 4-2*3= 4*(8+4+3)= Output -2 60 ### Response ```cpp #include <bits/stdc++.h> using namespace std; string::const_iterator it; int number(); int factor(); int term(); int expression(); int number() { int ret = 0; while(isdigit(*it)) { ret *= 10; ret += *it - '0'; it++; } return ret; } int factor() { int ret; if (*it == '(') { it++; ret = expression(); it++; } else { return number(); } return ret; } int term() { int ret = factor(); for (;;) { if (*it == '*') { it++; ret *= factor(); } else if (*it == '/') { it++; ret /= factor(); } else { break; } } return ret; } int expression() { int ret = term(); for (;;) { if (*it == '+') { it++; ret += term(); } else if (*it == '-') { it++; ret -= term(); } else { break; } } return ret; } int main() { int data_num; cin >> data_num; cin.ignore(); for (int i = 0; i < data_num; ++i) { string calc_formula; cin >> calc_formula; it = calc_formula.begin(); cout << expression() << endl; } return 0; } ```
#include <iostream> #include <string> using namespace std; string s; int single(int& pos); int figure(int& pos); int solve(int& pos); int main(){ int T; cin>>T; for(int i = 0; i < T; i++){ cin>>s; int pos = 0; cout<<solve(pos)<<endl; } return 0; } int single(int& pos){//??°??????????????????????????¢??° if(s[pos]!='('){ int res = 0; while('0'<=s[pos] && s[pos]<='9'){ res *= 10; res += s[pos]-48; pos++; } return res; }else{ return solve(++pos); } } int figure(int& pos){//????????????????????????????????¢??° int res = single(pos); while(1){ if(s[pos]=='*'){ res *= single(++pos); }else if(s[pos]=='/'){ res /= single(++pos); }else { return res; } } } int solve(int& pos){//?????¨???????¨????????????¢??°(????????????????¨?????????????????????¨?????????) int res = figure(pos); while(s[pos]!='=' && s[pos]!=')'){ if(s[pos]=='+') res += figure(++pos); else if(s[pos]=='-') res -= figure(++pos); } pos++; return res; }
### Prompt Your task is to create a CPP solution to the following problem: Your task is to write a program which reads an expression and evaluates it. * The expression consists of numerical values, operators and parentheses, and the ends with '='. * The operators includes +, - , *, / where respectively represents, addition, subtraction, multiplication and division. * Precedence of the operators is based on usual laws. That is one should perform all multiplication and division first, then addition and subtraction. When two operators have the same precedence, they are applied from left to right. * You may assume that there is no division by zero. * All calculation is performed as integers, and after the decimal point should be truncated * Length of the expression will not exceed 100. * -1 × 109 ≤ intermediate results of computation ≤ 109 Input The input is a sequence of datasets. The first line contains an integer n which represents the number of datasets. There will be n lines where each line contains an expression. Output For each datasets, prints the result of calculation. Example Input 2 4-2*3= 4*(8+4+3)= Output -2 60 ### Response ```cpp #include <iostream> #include <string> using namespace std; string s; int single(int& pos); int figure(int& pos); int solve(int& pos); int main(){ int T; cin>>T; for(int i = 0; i < T; i++){ cin>>s; int pos = 0; cout<<solve(pos)<<endl; } return 0; } int single(int& pos){//??°??????????????????????????¢??° if(s[pos]!='('){ int res = 0; while('0'<=s[pos] && s[pos]<='9'){ res *= 10; res += s[pos]-48; pos++; } return res; }else{ return solve(++pos); } } int figure(int& pos){//????????????????????????????????¢??° int res = single(pos); while(1){ if(s[pos]=='*'){ res *= single(++pos); }else if(s[pos]=='/'){ res /= single(++pos); }else { return res; } } } int solve(int& pos){//?????¨???????¨????????????¢??°(????????????????¨?????????????????????¨?????????) int res = figure(pos); while(s[pos]!='=' && s[pos]!=')'){ if(s[pos]=='+') res += figure(++pos); else if(s[pos]=='-') res -= figure(++pos); } pos++; return res; } ```
#include<iostream> #include<cstdio> #include<cctype> #include<cstdlib> #include<string> using namespace std; class Parsing{ private: string parse; int pos; public: Parsing(string s){ parse = s; pos = 0; } int fact(){ if(parse[pos] == '('){ pos++; int p = expression(); pos++; return p; }else{ int p=0; while('0' <= parse[pos] && parse[pos] <= '9'){ p *= 10; p += parse[pos]-'0'; pos++; } return p; } } int term(){ int p = fact(); while(parse[pos] == '*' || parse[pos] == '/'){ if(parse[pos] == '*'){pos++;p *= fact();} else {pos++;p /= fact();} } return p; } int expression(){ int p = term(); while(parse[pos] == '+' || parse[pos] == '-'){ if(parse[pos] == '+'){pos++;p+=term();} else {pos++;p-=term();} } return p; } }; int main(){ string s; int N; cin >> N; while(N-- > 0){ cin >> s; Parsing par = Parsing(s.substr(0,s.length()-1)); cout << par.expression() << endl; } return 0; }
### Prompt Please provide a CPP coded solution to the problem described below: Your task is to write a program which reads an expression and evaluates it. * The expression consists of numerical values, operators and parentheses, and the ends with '='. * The operators includes +, - , *, / where respectively represents, addition, subtraction, multiplication and division. * Precedence of the operators is based on usual laws. That is one should perform all multiplication and division first, then addition and subtraction. When two operators have the same precedence, they are applied from left to right. * You may assume that there is no division by zero. * All calculation is performed as integers, and after the decimal point should be truncated * Length of the expression will not exceed 100. * -1 × 109 ≤ intermediate results of computation ≤ 109 Input The input is a sequence of datasets. The first line contains an integer n which represents the number of datasets. There will be n lines where each line contains an expression. Output For each datasets, prints the result of calculation. Example Input 2 4-2*3= 4*(8+4+3)= Output -2 60 ### Response ```cpp #include<iostream> #include<cstdio> #include<cctype> #include<cstdlib> #include<string> using namespace std; class Parsing{ private: string parse; int pos; public: Parsing(string s){ parse = s; pos = 0; } int fact(){ if(parse[pos] == '('){ pos++; int p = expression(); pos++; return p; }else{ int p=0; while('0' <= parse[pos] && parse[pos] <= '9'){ p *= 10; p += parse[pos]-'0'; pos++; } return p; } } int term(){ int p = fact(); while(parse[pos] == '*' || parse[pos] == '/'){ if(parse[pos] == '*'){pos++;p *= fact();} else {pos++;p /= fact();} } return p; } int expression(){ int p = term(); while(parse[pos] == '+' || parse[pos] == '-'){ if(parse[pos] == '+'){pos++;p+=term();} else {pos++;p-=term();} } return p; } }; int main(){ string s; int N; cin >> N; while(N-- > 0){ cin >> s; Parsing par = Parsing(s.substr(0,s.length()-1)); cout << par.expression() << endl; } return 0; } ```
#include <iostream> #include <string> using namespace std; string S; int expr(int& p); int term(int& p); int factor(int& p); int number(int& p); int expr(int& p) { int v1 = term(p); while(S[p] == '+' || S[p] == '-') { char op = S[p]; p++; int v2 = term(p); if(op == '+') { v1 += v2; } else { v1 -= v2; } } return v1; } int term(int& p) { int v1 = factor(p); while(S[p] == '*' || S[p] == '/') { char op = S[p]; p++; int v2 = factor(p); if(op == '*') { v1 *= v2; } else { v1 /= v2; } } return v1; } int factor(int& p) { if(isdigit(S[p])) { return number(p); } p++; int res = expr(p); p++; return res; } int number(int& p) { int res = 0; while(isdigit(S[p])) { res *= 10; res += S[p] - '0'; ++p; } return res; } int main() { int n; cin >> n; for(int i=0; i<n; ++i) { cin >> S; int p = 0; cout << expr(p) << endl; } }
### Prompt Construct a Cpp code solution to the problem outlined: Your task is to write a program which reads an expression and evaluates it. * The expression consists of numerical values, operators and parentheses, and the ends with '='. * The operators includes +, - , *, / where respectively represents, addition, subtraction, multiplication and division. * Precedence of the operators is based on usual laws. That is one should perform all multiplication and division first, then addition and subtraction. When two operators have the same precedence, they are applied from left to right. * You may assume that there is no division by zero. * All calculation is performed as integers, and after the decimal point should be truncated * Length of the expression will not exceed 100. * -1 × 109 ≤ intermediate results of computation ≤ 109 Input The input is a sequence of datasets. The first line contains an integer n which represents the number of datasets. There will be n lines where each line contains an expression. Output For each datasets, prints the result of calculation. Example Input 2 4-2*3= 4*(8+4+3)= Output -2 60 ### Response ```cpp #include <iostream> #include <string> using namespace std; string S; int expr(int& p); int term(int& p); int factor(int& p); int number(int& p); int expr(int& p) { int v1 = term(p); while(S[p] == '+' || S[p] == '-') { char op = S[p]; p++; int v2 = term(p); if(op == '+') { v1 += v2; } else { v1 -= v2; } } return v1; } int term(int& p) { int v1 = factor(p); while(S[p] == '*' || S[p] == '/') { char op = S[p]; p++; int v2 = factor(p); if(op == '*') { v1 *= v2; } else { v1 /= v2; } } return v1; } int factor(int& p) { if(isdigit(S[p])) { return number(p); } p++; int res = expr(p); p++; return res; } int number(int& p) { int res = 0; while(isdigit(S[p])) { res *= 10; res += S[p] - '0'; ++p; } return res; } int main() { int n; cin >> n; for(int i=0; i<n; ++i) { cin >> S; int p = 0; cout << expr(p) << endl; } } ```
#include <iostream> #include <string> using namespace std; class calc{ string s; string::iterator it; public: calc(const string&str){ s = str; it = s.begin(); } int fact(){ if(*it == '('){ ++it; int ret = exp(); ++it; return ret; } else{ int ret = 0; while('0' <= *it && *it <= '9'){ ret *= 10; ret += *it - '0'; ++it; } return ret; } } int term(){ int ret = fact(); while(*it == '*' || *it == '/'){ if(*it == '*'){ ++it; ret *= fact(); } else if(*it == '/'){ ++it; ret /= fact(); } } return ret; } int exp(){ int ret = term(); while(*it == '+' || *it == '-'){ if(*it == '+'){ ++it; ret += term(); } else if(*it == '-'){ ++it; ret -= term(); } } return ret; } }; int main(){ string str; int n; cin >> n; while(n--){ cin >> str; calc ans(str); cout << ans.exp() << endl; } return 0; }
### Prompt Construct a CPP code solution to the problem outlined: Your task is to write a program which reads an expression and evaluates it. * The expression consists of numerical values, operators and parentheses, and the ends with '='. * The operators includes +, - , *, / where respectively represents, addition, subtraction, multiplication and division. * Precedence of the operators is based on usual laws. That is one should perform all multiplication and division first, then addition and subtraction. When two operators have the same precedence, they are applied from left to right. * You may assume that there is no division by zero. * All calculation is performed as integers, and after the decimal point should be truncated * Length of the expression will not exceed 100. * -1 × 109 ≤ intermediate results of computation ≤ 109 Input The input is a sequence of datasets. The first line contains an integer n which represents the number of datasets. There will be n lines where each line contains an expression. Output For each datasets, prints the result of calculation. Example Input 2 4-2*3= 4*(8+4+3)= Output -2 60 ### Response ```cpp #include <iostream> #include <string> using namespace std; class calc{ string s; string::iterator it; public: calc(const string&str){ s = str; it = s.begin(); } int fact(){ if(*it == '('){ ++it; int ret = exp(); ++it; return ret; } else{ int ret = 0; while('0' <= *it && *it <= '9'){ ret *= 10; ret += *it - '0'; ++it; } return ret; } } int term(){ int ret = fact(); while(*it == '*' || *it == '/'){ if(*it == '*'){ ++it; ret *= fact(); } else if(*it == '/'){ ++it; ret /= fact(); } } return ret; } int exp(){ int ret = term(); while(*it == '+' || *it == '-'){ if(*it == '+'){ ++it; ret += term(); } else if(*it == '-'){ ++it; ret -= term(); } } return ret; } }; int main(){ string str; int n; cin >> n; while(n--){ cin >> str; calc ans(str); cout << ans.exp() << endl; } return 0; } ```
#include<iostream> using namespace std; string str; int p; int exp(); int term(); int factor(); int main(){ int n; cin>>n; for(int i=0;i<n;i++){ p=0; cin>>str; cout<<exp()<<endl; } return 0; } int exp(){ int val=term(); while(str[p]=='+'||str[p]=='-'){ if(str[p]=='+'){ p++; val+=term(); } else if(str[p]=='-'){ p++; val-=term(); } } return val; } int term(){ int val=factor(); while(str[p]=='*'||str[p]=='/'){ if(str[p]=='*'){ p++; val*=factor(); } else if(str[p]=='/'){ p++; val/=factor(); } } return val; } int factor(){ int val=0; if(str[p]=='('){ p++; val=exp(); p++; } else{ while('0'<=str[p]&&str[p]<='9'){ val*=10; val+=(str[p]-'0'); p++; } } return val; }
### Prompt Develop a solution in cpp to the problem described below: Your task is to write a program which reads an expression and evaluates it. * The expression consists of numerical values, operators and parentheses, and the ends with '='. * The operators includes +, - , *, / where respectively represents, addition, subtraction, multiplication and division. * Precedence of the operators is based on usual laws. That is one should perform all multiplication and division first, then addition and subtraction. When two operators have the same precedence, they are applied from left to right. * You may assume that there is no division by zero. * All calculation is performed as integers, and after the decimal point should be truncated * Length of the expression will not exceed 100. * -1 × 109 ≤ intermediate results of computation ≤ 109 Input The input is a sequence of datasets. The first line contains an integer n which represents the number of datasets. There will be n lines where each line contains an expression. Output For each datasets, prints the result of calculation. Example Input 2 4-2*3= 4*(8+4+3)= Output -2 60 ### Response ```cpp #include<iostream> using namespace std; string str; int p; int exp(); int term(); int factor(); int main(){ int n; cin>>n; for(int i=0;i<n;i++){ p=0; cin>>str; cout<<exp()<<endl; } return 0; } int exp(){ int val=term(); while(str[p]=='+'||str[p]=='-'){ if(str[p]=='+'){ p++; val+=term(); } else if(str[p]=='-'){ p++; val-=term(); } } return val; } int term(){ int val=factor(); while(str[p]=='*'||str[p]=='/'){ if(str[p]=='*'){ p++; val*=factor(); } else if(str[p]=='/'){ p++; val/=factor(); } } return val; } int factor(){ int val=0; if(str[p]=='('){ p++; val=exp(); p++; } else{ while('0'<=str[p]&&str[p]<='9'){ val*=10; val+=(str[p]-'0'); p++; } } return val; } ```
#include <iostream> #include <cstdlib> #include <string> using namespace std; string s; int id; int exp(); int term(); int fact(); int exp() { int r = term(); while(true) { char c = s[id++]; if(c == '+')r += term(); else if(c == '-')r -= term(); else break; } return r; } int term() { int r = fact(); while(true) { char c = s[id++]; if(c=='*') r*=fact(); else if(c=='/')r/=fact(); else break; } id--; return r; } int fact() { char c = s[id++]; if(c=='(')return exp(); if(c=='-') { return -fact(); } if(c=='+') { return fact(); } int x = c-'0'; while(true) { c = s[id++]; if('0' <= c && c <= '9') { x *= 10; x += c-'0'; } else break; } id--; return x; } void solve() { int N; scanf("%d\n", &N); while(N--) { getline(cin, s); id = 0; cout << exp() << endl; } } int main() { solve(); return(0); }
### Prompt Create a solution in CPP for the following problem: Your task is to write a program which reads an expression and evaluates it. * The expression consists of numerical values, operators and parentheses, and the ends with '='. * The operators includes +, - , *, / where respectively represents, addition, subtraction, multiplication and division. * Precedence of the operators is based on usual laws. That is one should perform all multiplication and division first, then addition and subtraction. When two operators have the same precedence, they are applied from left to right. * You may assume that there is no division by zero. * All calculation is performed as integers, and after the decimal point should be truncated * Length of the expression will not exceed 100. * -1 × 109 ≤ intermediate results of computation ≤ 109 Input The input is a sequence of datasets. The first line contains an integer n which represents the number of datasets. There will be n lines where each line contains an expression. Output For each datasets, prints the result of calculation. Example Input 2 4-2*3= 4*(8+4+3)= Output -2 60 ### Response ```cpp #include <iostream> #include <cstdlib> #include <string> using namespace std; string s; int id; int exp(); int term(); int fact(); int exp() { int r = term(); while(true) { char c = s[id++]; if(c == '+')r += term(); else if(c == '-')r -= term(); else break; } return r; } int term() { int r = fact(); while(true) { char c = s[id++]; if(c=='*') r*=fact(); else if(c=='/')r/=fact(); else break; } id--; return r; } int fact() { char c = s[id++]; if(c=='(')return exp(); if(c=='-') { return -fact(); } if(c=='+') { return fact(); } int x = c-'0'; while(true) { c = s[id++]; if('0' <= c && c <= '9') { x *= 10; x += c-'0'; } else break; } id--; return x; } void solve() { int N; scanf("%d\n", &N); while(N--) { getline(cin, s); id = 0; cout << exp() << endl; } } int main() { solve(); return(0); } ```
#include <iostream> #include <string> #include <cctype> #include <cassert> using namespace std; string S; size_t cur = 0; int digit() { assert(isdigit(S[cur])); int n = S[cur++] - '0'; return n; } int number() { int n = digit(); while(cur < S.size() && isdigit(S[cur])) { n = n*10 + digit(); } return n; } int expression(); int factor() { //Factor := '(' Expression ')' | Number if(S[cur] != '(') return number(); cur++; int n = expression(); assert(S[cur] == ')'); cur++; return n; } int term() { //Term := Factor { ('*'|'/') Factor } int a = factor(); while(cur < S.size() && (S[cur] == '*' || S[cur] == '/')) { char op = S[cur++]; int b = factor(); if(op =='*') { a *= b; } else { a /= b; } } return a; } int expression() { //Expression := Term { ('+'|'-') Term} int a = term(); while(cur < S.size() && (S[cur] == '+' || S[cur] == '-')) { char op = S[cur++]; int b = term(); if(op == '+') { a += b; } else { a -= b;} } return a; } int main() { int N; cin >> N; for(int i=0; i<N; i++) { cur = 0; cin >> S; S.resize(S.size() - 1); cout << expression() << endl; } }
### Prompt Your task is to create a Cpp solution to the following problem: Your task is to write a program which reads an expression and evaluates it. * The expression consists of numerical values, operators and parentheses, and the ends with '='. * The operators includes +, - , *, / where respectively represents, addition, subtraction, multiplication and division. * Precedence of the operators is based on usual laws. That is one should perform all multiplication and division first, then addition and subtraction. When two operators have the same precedence, they are applied from left to right. * You may assume that there is no division by zero. * All calculation is performed as integers, and after the decimal point should be truncated * Length of the expression will not exceed 100. * -1 × 109 ≤ intermediate results of computation ≤ 109 Input The input is a sequence of datasets. The first line contains an integer n which represents the number of datasets. There will be n lines where each line contains an expression. Output For each datasets, prints the result of calculation. Example Input 2 4-2*3= 4*(8+4+3)= Output -2 60 ### Response ```cpp #include <iostream> #include <string> #include <cctype> #include <cassert> using namespace std; string S; size_t cur = 0; int digit() { assert(isdigit(S[cur])); int n = S[cur++] - '0'; return n; } int number() { int n = digit(); while(cur < S.size() && isdigit(S[cur])) { n = n*10 + digit(); } return n; } int expression(); int factor() { //Factor := '(' Expression ')' | Number if(S[cur] != '(') return number(); cur++; int n = expression(); assert(S[cur] == ')'); cur++; return n; } int term() { //Term := Factor { ('*'|'/') Factor } int a = factor(); while(cur < S.size() && (S[cur] == '*' || S[cur] == '/')) { char op = S[cur++]; int b = factor(); if(op =='*') { a *= b; } else { a /= b; } } return a; } int expression() { //Expression := Term { ('+'|'-') Term} int a = term(); while(cur < S.size() && (S[cur] == '+' || S[cur] == '-')) { char op = S[cur++]; int b = term(); if(op == '+') { a += b; } else { a -= b;} } return a; } int main() { int N; cin >> N; for(int i=0; i<N; i++) { cur = 0; cin >> S; S.resize(S.size() - 1); cout << expression() << endl; } } ```
#include<iostream> #include <string> using namespace std; int weak(string& s, int& i); int strong(string& s, int& i); int fc(string& s, int& i); int inte(string& s, int& i); int weak(string& s, int& i) { int buf = strong(s, i); while (s[i] == '+' || s[i] == '-') { if (s[i] == '+') { i++; buf += strong(s, i); } else { i++; buf -= strong(s, i); } } return buf; } int strong(string& s, int& i) { int buf = fc(s, i); while (s[i] == '*' || s[i] == '/') { if (s[i] == '*') { i++; buf *= fc(s, i); } else { i++; buf /= fc(s, i); } } return buf; } int fc(string& s, int& i) { if ('0' <= s[i] && '9' >= s[i])return inte(s, i); else { i++; int re = weak(s, i); i++; return re; } } int inte(string& s, int& i) { int buf = s[i] - '0'; i++; while ('0' <= s[i] && '9' >= s[i]) { buf = buf * 10 + s[i] - '0'; i++; } return buf; } int main() { int n; cin >> n; for (int i = 0; i < n; i++) { string s; int j = 0; cin >> s; cout << weak(s, j) << endl; } }
### Prompt Your challenge is to write a Cpp solution to the following problem: Your task is to write a program which reads an expression and evaluates it. * The expression consists of numerical values, operators and parentheses, and the ends with '='. * The operators includes +, - , *, / where respectively represents, addition, subtraction, multiplication and division. * Precedence of the operators is based on usual laws. That is one should perform all multiplication and division first, then addition and subtraction. When two operators have the same precedence, they are applied from left to right. * You may assume that there is no division by zero. * All calculation is performed as integers, and after the decimal point should be truncated * Length of the expression will not exceed 100. * -1 × 109 ≤ intermediate results of computation ≤ 109 Input The input is a sequence of datasets. The first line contains an integer n which represents the number of datasets. There will be n lines where each line contains an expression. Output For each datasets, prints the result of calculation. Example Input 2 4-2*3= 4*(8+4+3)= Output -2 60 ### Response ```cpp #include<iostream> #include <string> using namespace std; int weak(string& s, int& i); int strong(string& s, int& i); int fc(string& s, int& i); int inte(string& s, int& i); int weak(string& s, int& i) { int buf = strong(s, i); while (s[i] == '+' || s[i] == '-') { if (s[i] == '+') { i++; buf += strong(s, i); } else { i++; buf -= strong(s, i); } } return buf; } int strong(string& s, int& i) { int buf = fc(s, i); while (s[i] == '*' || s[i] == '/') { if (s[i] == '*') { i++; buf *= fc(s, i); } else { i++; buf /= fc(s, i); } } return buf; } int fc(string& s, int& i) { if ('0' <= s[i] && '9' >= s[i])return inte(s, i); else { i++; int re = weak(s, i); i++; return re; } } int inte(string& s, int& i) { int buf = s[i] - '0'; i++; while ('0' <= s[i] && '9' >= s[i]) { buf = buf * 10 + s[i] - '0'; i++; } return buf; } int main() { int n; cin >> n; for (int i = 0; i < n; i++) { string s; int j = 0; cin >> s; cout << weak(s, j) << endl; } } ```
#include <iostream> #include <string> using namespace std; int expression(); int term(); int factor(); int number(); int now; string s; int expression(){ int res=term(); while(true){ if(s[now]=='+'){ now++; res+=term(); }else if(s[now]=='-'){ now++; res-=term(); }else break; } return res; } int term(){ int res=factor(); while(true){ if(s[now]=='(') res+=factor(); else if(s[now]=='*'){ now++; res*=factor(); }else if(s[now]=='/'){ now++; res/=factor(); }else break; } return res; } int factor(){ int res=0; if(s[now]=='('){ now++; res=expression(); now++; }else return number(); return res; } int number(){ int res=0; while('0'<=s[now] && s[now]<='9'){ res*=10; res+=s[now++]-'0'; } return res; } int main(void){ int n; cin >> n; while(n--){ cin >> s; now=0; cout << expression() << endl; } return 0; }
### Prompt Construct a cpp code solution to the problem outlined: Your task is to write a program which reads an expression and evaluates it. * The expression consists of numerical values, operators and parentheses, and the ends with '='. * The operators includes +, - , *, / where respectively represents, addition, subtraction, multiplication and division. * Precedence of the operators is based on usual laws. That is one should perform all multiplication and division first, then addition and subtraction. When two operators have the same precedence, they are applied from left to right. * You may assume that there is no division by zero. * All calculation is performed as integers, and after the decimal point should be truncated * Length of the expression will not exceed 100. * -1 × 109 ≤ intermediate results of computation ≤ 109 Input The input is a sequence of datasets. The first line contains an integer n which represents the number of datasets. There will be n lines where each line contains an expression. Output For each datasets, prints the result of calculation. Example Input 2 4-2*3= 4*(8+4+3)= Output -2 60 ### Response ```cpp #include <iostream> #include <string> using namespace std; int expression(); int term(); int factor(); int number(); int now; string s; int expression(){ int res=term(); while(true){ if(s[now]=='+'){ now++; res+=term(); }else if(s[now]=='-'){ now++; res-=term(); }else break; } return res; } int term(){ int res=factor(); while(true){ if(s[now]=='(') res+=factor(); else if(s[now]=='*'){ now++; res*=factor(); }else if(s[now]=='/'){ now++; res/=factor(); }else break; } return res; } int factor(){ int res=0; if(s[now]=='('){ now++; res=expression(); now++; }else return number(); return res; } int number(){ int res=0; while('0'<=s[now] && s[now]<='9'){ res*=10; res+=s[now++]-'0'; } return res; } int main(void){ int n; cin >> n; while(n--){ cin >> s; now=0; cout << expression() << endl; } return 0; } ```
// http://judge.u-aizu.ac.jp/onlinejudge/description.jsp?id=0109&lang=jp // Appendix?????????????????? #include <bits/stdc++.h> using namespace std; int n, cur; string str; int expression(); int factor(); int digit() { assert(isdigit(str[cur])); int n = str[cur] - '0'; cur++; return n; } int number() { int n = digit(); while (cur < str.size() && isdigit(str[cur])) n = n * 10 + digit(); return n; } int factor() { if (str[cur] != '(') return number(); cur++; int n = expression(); assert(str[cur] == ')'); cur++; return n; } int term() { int a = factor(); while (cur < str.size() && (str[cur] == '*' || str[cur] == '/')) { char op = str[cur++]; int b = factor(); if (op == '*') a *= b; else a /= b; } return a; } int expression() { int a = term(); while (cur < str.size() && (str[cur] == '+' || str[cur] == '-')) { char op = str[cur++]; int b = term(); if (op == '+') a += b; else a -= b; } return a; } int main() { scanf("%d", &n); for (int i=0; i<n; i++) { cur = 0; cin >> str; str.resize(str.size() - 1); printf("%d\n", expression()); } }
### Prompt Please formulate a cpp solution to the following problem: Your task is to write a program which reads an expression and evaluates it. * The expression consists of numerical values, operators and parentheses, and the ends with '='. * The operators includes +, - , *, / where respectively represents, addition, subtraction, multiplication and division. * Precedence of the operators is based on usual laws. That is one should perform all multiplication and division first, then addition and subtraction. When two operators have the same precedence, they are applied from left to right. * You may assume that there is no division by zero. * All calculation is performed as integers, and after the decimal point should be truncated * Length of the expression will not exceed 100. * -1 × 109 ≤ intermediate results of computation ≤ 109 Input The input is a sequence of datasets. The first line contains an integer n which represents the number of datasets. There will be n lines where each line contains an expression. Output For each datasets, prints the result of calculation. Example Input 2 4-2*3= 4*(8+4+3)= Output -2 60 ### Response ```cpp // http://judge.u-aizu.ac.jp/onlinejudge/description.jsp?id=0109&lang=jp // Appendix?????????????????? #include <bits/stdc++.h> using namespace std; int n, cur; string str; int expression(); int factor(); int digit() { assert(isdigit(str[cur])); int n = str[cur] - '0'; cur++; return n; } int number() { int n = digit(); while (cur < str.size() && isdigit(str[cur])) n = n * 10 + digit(); return n; } int factor() { if (str[cur] != '(') return number(); cur++; int n = expression(); assert(str[cur] == ')'); cur++; return n; } int term() { int a = factor(); while (cur < str.size() && (str[cur] == '*' || str[cur] == '/')) { char op = str[cur++]; int b = factor(); if (op == '*') a *= b; else a /= b; } return a; } int expression() { int a = term(); while (cur < str.size() && (str[cur] == '+' || str[cur] == '-')) { char op = str[cur++]; int b = term(); if (op == '+') a += b; else a -= b; } return a; } int main() { scanf("%d", &n); for (int i=0; i<n; i++) { cur = 0; cin >> str; str.resize(str.size() - 1); printf("%d\n", expression()); } } ```
#include <iostream> #include <string> using namespace std; int hyoka(string); int isNum(int); int main(){ int n,i; string s; cin>>n; for(i=0;i<n;i++){ cin>>s; cout<<hyoka(s)<<endl; } return 0; } int hyoka(string s){ long int total=0,work=0; long int tmp1,tmp2; int oi=0; char oc; int n; string cs; if(s[oi]=='(' || isNum(s[oi])!=-1){s="+"+s;} //cout<<s<<endl; while(1){ oc=s[oi];if(oc=='='){total+=work;break;} if(s[oi+1]!='('){ //??°??????????????? tmp1=0; while(1){ oi++; tmp2=isNum(s[oi]); if(tmp2!=-1){tmp1=tmp1*10+tmp2;}else{break;} } }else{ //??????????????° cs="";n=1;oi++; while(1){ oi++; switch(s[oi]){ case '(':n++;break; case ')':n--;break; } if(n!=0){cs+=s[oi];}else{break;} } cs+='='; //cout<<cs<<endl; tmp1=hyoka(cs); } switch(oc){ case '+':total+=work;work=tmp1;break; case '-':total+=work;work=-tmp1;break; case '*':work*=tmp1;break; case '/':work/=tmp1;break; } } return total; } int isNum(int c){ if(48<=c && c<=57){ return c-48; } return -1; }
### Prompt In Cpp, your task is to solve the following problem: Your task is to write a program which reads an expression and evaluates it. * The expression consists of numerical values, operators and parentheses, and the ends with '='. * The operators includes +, - , *, / where respectively represents, addition, subtraction, multiplication and division. * Precedence of the operators is based on usual laws. That is one should perform all multiplication and division first, then addition and subtraction. When two operators have the same precedence, they are applied from left to right. * You may assume that there is no division by zero. * All calculation is performed as integers, and after the decimal point should be truncated * Length of the expression will not exceed 100. * -1 × 109 ≤ intermediate results of computation ≤ 109 Input The input is a sequence of datasets. The first line contains an integer n which represents the number of datasets. There will be n lines where each line contains an expression. Output For each datasets, prints the result of calculation. Example Input 2 4-2*3= 4*(8+4+3)= Output -2 60 ### Response ```cpp #include <iostream> #include <string> using namespace std; int hyoka(string); int isNum(int); int main(){ int n,i; string s; cin>>n; for(i=0;i<n;i++){ cin>>s; cout<<hyoka(s)<<endl; } return 0; } int hyoka(string s){ long int total=0,work=0; long int tmp1,tmp2; int oi=0; char oc; int n; string cs; if(s[oi]=='(' || isNum(s[oi])!=-1){s="+"+s;} //cout<<s<<endl; while(1){ oc=s[oi];if(oc=='='){total+=work;break;} if(s[oi+1]!='('){ //??°??????????????? tmp1=0; while(1){ oi++; tmp2=isNum(s[oi]); if(tmp2!=-1){tmp1=tmp1*10+tmp2;}else{break;} } }else{ //??????????????° cs="";n=1;oi++; while(1){ oi++; switch(s[oi]){ case '(':n++;break; case ')':n--;break; } if(n!=0){cs+=s[oi];}else{break;} } cs+='='; //cout<<cs<<endl; tmp1=hyoka(cs); } switch(oc){ case '+':total+=work;work=tmp1;break; case '-':total+=work;work=-tmp1;break; case '*':work*=tmp1;break; case '/':work/=tmp1;break; } } return total; } int isNum(int c){ if(48<=c && c<=57){ return c-48; } return -1; } ```
#include <iostream> #include <string> #include <cctype> using namespace std; using State = string::const_iterator; int expression(State& begin); int term(State& begin); int factor(State& begin); int number(State& begin); int expression(State& begin){ int ret = term(begin); while (true){ if (*begin == '+'){ begin++; ret += term(begin); } else if (*begin == '-'){ begin++; ret -= term(begin); } else { break; } } return ret; } int term(State& begin){ int ret = factor(begin); while (true){ if (*begin == '*'){ begin++; ret *= factor(begin); } else if (*begin == '/'){ begin++; ret /= factor(begin); } else { break; } } return ret; } int factor(State& begin){ int ret; if (*begin == '('){ begin++; ret = expression(begin); begin++; } else { ret = number(begin); } return ret; } int number(State& begin){ int ret = 0; while (isdigit(*begin)){ ret *= 10; ret += *begin - '0'; begin++; } return ret; } int main(){ int n; string expr; cin >> n; cin.ignore(); for (int i = 0; i < n; i++){ getline(cin, expr); State begin = expr.begin(); int ans = expression(begin); cout << ans << endl; } return 0; }
### Prompt In cpp, your task is to solve the following problem: Your task is to write a program which reads an expression and evaluates it. * The expression consists of numerical values, operators and parentheses, and the ends with '='. * The operators includes +, - , *, / where respectively represents, addition, subtraction, multiplication and division. * Precedence of the operators is based on usual laws. That is one should perform all multiplication and division first, then addition and subtraction. When two operators have the same precedence, they are applied from left to right. * You may assume that there is no division by zero. * All calculation is performed as integers, and after the decimal point should be truncated * Length of the expression will not exceed 100. * -1 × 109 ≤ intermediate results of computation ≤ 109 Input The input is a sequence of datasets. The first line contains an integer n which represents the number of datasets. There will be n lines where each line contains an expression. Output For each datasets, prints the result of calculation. Example Input 2 4-2*3= 4*(8+4+3)= Output -2 60 ### Response ```cpp #include <iostream> #include <string> #include <cctype> using namespace std; using State = string::const_iterator; int expression(State& begin); int term(State& begin); int factor(State& begin); int number(State& begin); int expression(State& begin){ int ret = term(begin); while (true){ if (*begin == '+'){ begin++; ret += term(begin); } else if (*begin == '-'){ begin++; ret -= term(begin); } else { break; } } return ret; } int term(State& begin){ int ret = factor(begin); while (true){ if (*begin == '*'){ begin++; ret *= factor(begin); } else if (*begin == '/'){ begin++; ret /= factor(begin); } else { break; } } return ret; } int factor(State& begin){ int ret; if (*begin == '('){ begin++; ret = expression(begin); begin++; } else { ret = number(begin); } return ret; } int number(State& begin){ int ret = 0; while (isdigit(*begin)){ ret *= 10; ret += *begin - '0'; begin++; } return ret; } int main(){ int n; string expr; cin >> n; cin.ignore(); for (int i = 0; i < n; i++){ getline(cin, expr); State begin = expr.begin(); int ans = expression(begin); cout << ans << endl; } return 0; } ```
#include <cstdio> #include <cstdint> #include <cctype> #include <cassert> #include <vector> #include <algorithm> #include <string> int parse_int(std::string const& s, size_t& i) { int res = s[i]-'0'; while (isdigit(s[++i])) res = res*10 + s[i]-'0'; return res; } int apply(int lhs, char op, int rhs) { if (op == '+') return lhs + rhs; if (op == '-') return lhs - rhs; if (op == '*') return lhs * rhs; if (op == '/') return lhs / rhs; assert(false); } const std::vector<std::string> ops = {"+-", "*/"}; int parse(std::string const& s, size_t& i, size_t preced = 0) { if (ops.size() == preced) { if (isdigit(s[i])) return parse_int(s, i); if (s[i] == '(') { int res = parse(s, ++i, 0); assert(s[i] == ')'); ++i; return res; } assert(false); } int res = parse(s, i, preced+1); while (i < s.length()) { char op = s[i]; if (!std::count(ops[preced].begin(), ops[preced].end(), op)) break; res = apply(res, op, parse(s, ++i, preced+1)); } return res; } int main() { size_t n; scanf("%zu", &n); for (size_t i = 0; i < n; ++i) { char buf[1024]; scanf("%s", buf); std::string s = buf; if (s.back() != '=') { printf("%s\n", s.c_str()); continue; } size_t j = 0; printf("%d\n", parse(s, j)); } }
### Prompt Create a solution in CPP for the following problem: Your task is to write a program which reads an expression and evaluates it. * The expression consists of numerical values, operators and parentheses, and the ends with '='. * The operators includes +, - , *, / where respectively represents, addition, subtraction, multiplication and division. * Precedence of the operators is based on usual laws. That is one should perform all multiplication and division first, then addition and subtraction. When two operators have the same precedence, they are applied from left to right. * You may assume that there is no division by zero. * All calculation is performed as integers, and after the decimal point should be truncated * Length of the expression will not exceed 100. * -1 × 109 ≤ intermediate results of computation ≤ 109 Input The input is a sequence of datasets. The first line contains an integer n which represents the number of datasets. There will be n lines where each line contains an expression. Output For each datasets, prints the result of calculation. Example Input 2 4-2*3= 4*(8+4+3)= Output -2 60 ### Response ```cpp #include <cstdio> #include <cstdint> #include <cctype> #include <cassert> #include <vector> #include <algorithm> #include <string> int parse_int(std::string const& s, size_t& i) { int res = s[i]-'0'; while (isdigit(s[++i])) res = res*10 + s[i]-'0'; return res; } int apply(int lhs, char op, int rhs) { if (op == '+') return lhs + rhs; if (op == '-') return lhs - rhs; if (op == '*') return lhs * rhs; if (op == '/') return lhs / rhs; assert(false); } const std::vector<std::string> ops = {"+-", "*/"}; int parse(std::string const& s, size_t& i, size_t preced = 0) { if (ops.size() == preced) { if (isdigit(s[i])) return parse_int(s, i); if (s[i] == '(') { int res = parse(s, ++i, 0); assert(s[i] == ')'); ++i; return res; } assert(false); } int res = parse(s, i, preced+1); while (i < s.length()) { char op = s[i]; if (!std::count(ops[preced].begin(), ops[preced].end(), op)) break; res = apply(res, op, parse(s, ++i, preced+1)); } return res; } int main() { size_t n; scanf("%zu", &n); for (size_t i = 0; i < n; ++i) { char buf[1024]; scanf("%s", buf); std::string s = buf; if (s.back() != '=') { printf("%s\n", s.c_str()); continue; } size_t j = 0; printf("%d\n", parse(s, j)); } } ```
#if 0 #endif #include <bits/stdc++.h> using namespace std; typedef long long int ll; string S; size_t cur; char readchar() { assert(cur < S.size()); return S[cur++]; } char peek() { assert(cur < S.size()); return S[cur]; } int digit() { assert(isdigit(peek())); int n = readchar() - '0'; return n; } int number() { int n = digit(); while (cur < S.size() && isdigit(peek())) n = n*10 + digit(); return n; } int expression(); int factor() { if (peek() != '(') return number(); cur += 1; int n = expression(); assert(peek() == ')'); cur += 1; return n; } int term() { int a = factor(); while (cur < S.size() && (peek() == '*' || peek() == '/')) { char op = readchar(); int b = factor(); if (op == '*') a *= b; else a /= b; } return a; } int expression() { int a = term(); while (cur < S.size() && (peek() == '+' || peek() == '-')) { char op = readchar(); int b = term(); if (op == '+') a += b; else a -= b; } return a; } int main() { int N; cin >> N; for (int i = 0; i < N; i++) { cur = 0; cin >> S; S.resize(S.size()-1); cout << expression() << endl; } }
### Prompt Please formulate a cpp solution to the following problem: Your task is to write a program which reads an expression and evaluates it. * The expression consists of numerical values, operators and parentheses, and the ends with '='. * The operators includes +, - , *, / where respectively represents, addition, subtraction, multiplication and division. * Precedence of the operators is based on usual laws. That is one should perform all multiplication and division first, then addition and subtraction. When two operators have the same precedence, they are applied from left to right. * You may assume that there is no division by zero. * All calculation is performed as integers, and after the decimal point should be truncated * Length of the expression will not exceed 100. * -1 × 109 ≤ intermediate results of computation ≤ 109 Input The input is a sequence of datasets. The first line contains an integer n which represents the number of datasets. There will be n lines where each line contains an expression. Output For each datasets, prints the result of calculation. Example Input 2 4-2*3= 4*(8+4+3)= Output -2 60 ### Response ```cpp #if 0 #endif #include <bits/stdc++.h> using namespace std; typedef long long int ll; string S; size_t cur; char readchar() { assert(cur < S.size()); return S[cur++]; } char peek() { assert(cur < S.size()); return S[cur]; } int digit() { assert(isdigit(peek())); int n = readchar() - '0'; return n; } int number() { int n = digit(); while (cur < S.size() && isdigit(peek())) n = n*10 + digit(); return n; } int expression(); int factor() { if (peek() != '(') return number(); cur += 1; int n = expression(); assert(peek() == ')'); cur += 1; return n; } int term() { int a = factor(); while (cur < S.size() && (peek() == '*' || peek() == '/')) { char op = readchar(); int b = factor(); if (op == '*') a *= b; else a /= b; } return a; } int expression() { int a = term(); while (cur < S.size() && (peek() == '+' || peek() == '-')) { char op = readchar(); int b = term(); if (op == '+') a += b; else a -= b; } return a; } int main() { int N; cin >> N; for (int i = 0; i < N; i++) { cur = 0; cin >> S; S.resize(S.size()-1); cout << expression() << endl; } } ```
#include<iostream> #include<string> using namespace std; int p; string exp; int term(); int expression(); int factor(); int expression(){ int value; value=term(); while(exp[p]=='+' || exp[p]=='-'){ if(exp[p]=='+'){ p++; value+=term(); } else{ p++; value-=term(); } } return value; } int term(){ int value; value=factor(); while(exp[p]=='*' || exp[p]=='/'){ if(exp[p]=='*'){ p++; value*=factor(); } else{ p++; value/=factor(); } } return value; } int factor(){ int value=0; if(exp[p]=='('){ p++; value=expression(); p++; } else{ while(isdigit(exp[p])){ value=value*10+exp[p++]-'0'; } } return value; } int main() { int i,c; cin>>c; for(i=0;i<c;i++){ cin>>exp; p=0; cout<<expression()<<"\n"; } return 0; }
### Prompt Generate a cpp solution to the following problem: Your task is to write a program which reads an expression and evaluates it. * The expression consists of numerical values, operators and parentheses, and the ends with '='. * The operators includes +, - , *, / where respectively represents, addition, subtraction, multiplication and division. * Precedence of the operators is based on usual laws. That is one should perform all multiplication and division first, then addition and subtraction. When two operators have the same precedence, they are applied from left to right. * You may assume that there is no division by zero. * All calculation is performed as integers, and after the decimal point should be truncated * Length of the expression will not exceed 100. * -1 × 109 ≤ intermediate results of computation ≤ 109 Input The input is a sequence of datasets. The first line contains an integer n which represents the number of datasets. There will be n lines where each line contains an expression. Output For each datasets, prints the result of calculation. Example Input 2 4-2*3= 4*(8+4+3)= Output -2 60 ### Response ```cpp #include<iostream> #include<string> using namespace std; int p; string exp; int term(); int expression(); int factor(); int expression(){ int value; value=term(); while(exp[p]=='+' || exp[p]=='-'){ if(exp[p]=='+'){ p++; value+=term(); } else{ p++; value-=term(); } } return value; } int term(){ int value; value=factor(); while(exp[p]=='*' || exp[p]=='/'){ if(exp[p]=='*'){ p++; value*=factor(); } else{ p++; value/=factor(); } } return value; } int factor(){ int value=0; if(exp[p]=='('){ p++; value=expression(); p++; } else{ while(isdigit(exp[p])){ value=value*10+exp[p++]-'0'; } } return value; } int main() { int i,c; cin>>c; for(i=0;i<c;i++){ cin>>exp; p=0; cout<<expression()<<"\n"; } return 0; } ```
/* Expr = Term { (+|-) Term} Term = Fact { (*|/) Fact} Fact = (Expr) | number */ #include<iostream> #include<cstdlib> using namespace std; typedef pair<int,const char*>parsed; parsed expr(const char *p); parsed term(const char *p); parsed fact(const char *p); parsed expr(const char *p) { parsed r=term(p); while(*r.second=='+'||*r.second=='-'){ char op =*r.second; int tmp=r.first; r=term(r.second+1); if(op=='+')r.first=tmp+r.first; else r.first=tmp-r.first; } return r; } parsed term(const char *p) { parsed r=fact(p); while(*r.second=='*'||*r.second=='/'){ char op=*r.second; int tmp=r.first; r=fact(r.second+1); if(op=='*')r.first=tmp*r.first; else r.first=tmp/r.first; } return r; } parsed fact(const char *p) { if(isdigit(*p)){ int t=*(p++)-'0'; while(isdigit(*p))t=t*10+*(p++)-'0'; return parsed(t,p); } else if(*p=='('){ parsed r=expr(p+1); if(*r.second!=')')exit(0); return parsed(r.first,r.second+1); } else exit(0); } int main() { string str; int n; cin>>n; while(n--){ cin>>str; cout<<expr(str.c_str()).first<<endl; } return 0; }
### Prompt Generate a cpp solution to the following problem: Your task is to write a program which reads an expression and evaluates it. * The expression consists of numerical values, operators and parentheses, and the ends with '='. * The operators includes +, - , *, / where respectively represents, addition, subtraction, multiplication and division. * Precedence of the operators is based on usual laws. That is one should perform all multiplication and division first, then addition and subtraction. When two operators have the same precedence, they are applied from left to right. * You may assume that there is no division by zero. * All calculation is performed as integers, and after the decimal point should be truncated * Length of the expression will not exceed 100. * -1 × 109 ≤ intermediate results of computation ≤ 109 Input The input is a sequence of datasets. The first line contains an integer n which represents the number of datasets. There will be n lines where each line contains an expression. Output For each datasets, prints the result of calculation. Example Input 2 4-2*3= 4*(8+4+3)= Output -2 60 ### Response ```cpp /* Expr = Term { (+|-) Term} Term = Fact { (*|/) Fact} Fact = (Expr) | number */ #include<iostream> #include<cstdlib> using namespace std; typedef pair<int,const char*>parsed; parsed expr(const char *p); parsed term(const char *p); parsed fact(const char *p); parsed expr(const char *p) { parsed r=term(p); while(*r.second=='+'||*r.second=='-'){ char op =*r.second; int tmp=r.first; r=term(r.second+1); if(op=='+')r.first=tmp+r.first; else r.first=tmp-r.first; } return r; } parsed term(const char *p) { parsed r=fact(p); while(*r.second=='*'||*r.second=='/'){ char op=*r.second; int tmp=r.first; r=fact(r.second+1); if(op=='*')r.first=tmp*r.first; else r.first=tmp/r.first; } return r; } parsed fact(const char *p) { if(isdigit(*p)){ int t=*(p++)-'0'; while(isdigit(*p))t=t*10+*(p++)-'0'; return parsed(t,p); } else if(*p=='('){ parsed r=expr(p+1); if(*r.second!=')')exit(0); return parsed(r.first,r.second+1); } else exit(0); } int main() { string str; int n; cin>>n; while(n--){ cin>>str; cout<<expr(str.c_str()).first<<endl; } return 0; } ```
#include <iostream> #include <string> using namespace std; string input; int pos; int fact(); int term(); int expr(); int fact() { int result = 0; if (input[pos] == '(') { pos++; result = expr(); pos++; } else { while (isdigit(input[pos])) { result = result * 10 + input[pos] - '0'; pos++; } } // cout << "fact: " << result << endl; return result; } int term() { int result = fact(); while (input[pos] == '*' || input[pos] == '/') { if (input[pos] == '*') { pos++; result *= fact(); } else { pos++; result /= fact(); } } // cout << "term: " << result << endl; return result; } int expr() { int result = term(); while (input[pos] == '+' || input[pos] == '-') { if (input[pos] == '+') { pos++; result += term(); } else { pos++; result -= term(); } } // cout << "expr: " << result << endl; return result; } int main() { int n; cin >> n; while (n--) { cin >> input; pos = 0; cout << expr() << endl; } return 0; }
### Prompt Generate a Cpp solution to the following problem: Your task is to write a program which reads an expression and evaluates it. * The expression consists of numerical values, operators and parentheses, and the ends with '='. * The operators includes +, - , *, / where respectively represents, addition, subtraction, multiplication and division. * Precedence of the operators is based on usual laws. That is one should perform all multiplication and division first, then addition and subtraction. When two operators have the same precedence, they are applied from left to right. * You may assume that there is no division by zero. * All calculation is performed as integers, and after the decimal point should be truncated * Length of the expression will not exceed 100. * -1 × 109 ≤ intermediate results of computation ≤ 109 Input The input is a sequence of datasets. The first line contains an integer n which represents the number of datasets. There will be n lines where each line contains an expression. Output For each datasets, prints the result of calculation. Example Input 2 4-2*3= 4*(8+4+3)= Output -2 60 ### Response ```cpp #include <iostream> #include <string> using namespace std; string input; int pos; int fact(); int term(); int expr(); int fact() { int result = 0; if (input[pos] == '(') { pos++; result = expr(); pos++; } else { while (isdigit(input[pos])) { result = result * 10 + input[pos] - '0'; pos++; } } // cout << "fact: " << result << endl; return result; } int term() { int result = fact(); while (input[pos] == '*' || input[pos] == '/') { if (input[pos] == '*') { pos++; result *= fact(); } else { pos++; result /= fact(); } } // cout << "term: " << result << endl; return result; } int expr() { int result = term(); while (input[pos] == '+' || input[pos] == '-') { if (input[pos] == '+') { pos++; result += term(); } else { pos++; result -= term(); } } // cout << "expr: " << result << endl; return result; } int main() { int n; cin >> n; while (n--) { cin >> input; pos = 0; cout << expr() << endl; } return 0; } ```
#include <iostream> #include <cstdio> #include <cctype> #include <string> using namespace std; int exp(string &s, int &i); int term(string &s, int &i); int factor(string &s, int &i); int number(string &s, int &i); int exp(string &s, int &i) { int val = term(s, i); while(s[i] == '+' || s[i] == '-'){ char op = s[i]; i++; int val2 = term(s, i); if(op == '+') val += val2; else val -= val2; } return val; } int term(string &s, int &i) { int val = factor(s, i); while(s[i] == '*' || s[i] == '/'){ char op = s[i]; i++; int val2 = factor(s, i); if(op == '*') val *= val2; else val /= val2; } return val; } int factor(string &s, int &i) { if(isdigit(s[i])) return number(s, i); i++; int val = exp(s, i); i++; return val; } int number(string &s, int &i) { int n = s[i] - '0'; i++; while(isdigit(s[i])){ n = n * 10 + (s[i] - '0'); i++; } return n; } int main() { int n; cin >> n; for(int j = 0; j < n; j++){ string s; cin >> s; int i = 0; cout << exp(s, i) << endl; } }
### Prompt Please provide a Cpp coded solution to the problem described below: Your task is to write a program which reads an expression and evaluates it. * The expression consists of numerical values, operators and parentheses, and the ends with '='. * The operators includes +, - , *, / where respectively represents, addition, subtraction, multiplication and division. * Precedence of the operators is based on usual laws. That is one should perform all multiplication and division first, then addition and subtraction. When two operators have the same precedence, they are applied from left to right. * You may assume that there is no division by zero. * All calculation is performed as integers, and after the decimal point should be truncated * Length of the expression will not exceed 100. * -1 × 109 ≤ intermediate results of computation ≤ 109 Input The input is a sequence of datasets. The first line contains an integer n which represents the number of datasets. There will be n lines where each line contains an expression. Output For each datasets, prints the result of calculation. Example Input 2 4-2*3= 4*(8+4+3)= Output -2 60 ### Response ```cpp #include <iostream> #include <cstdio> #include <cctype> #include <string> using namespace std; int exp(string &s, int &i); int term(string &s, int &i); int factor(string &s, int &i); int number(string &s, int &i); int exp(string &s, int &i) { int val = term(s, i); while(s[i] == '+' || s[i] == '-'){ char op = s[i]; i++; int val2 = term(s, i); if(op == '+') val += val2; else val -= val2; } return val; } int term(string &s, int &i) { int val = factor(s, i); while(s[i] == '*' || s[i] == '/'){ char op = s[i]; i++; int val2 = factor(s, i); if(op == '*') val *= val2; else val /= val2; } return val; } int factor(string &s, int &i) { if(isdigit(s[i])) return number(s, i); i++; int val = exp(s, i); i++; return val; } int number(string &s, int &i) { int n = s[i] - '0'; i++; while(isdigit(s[i])){ n = n * 10 + (s[i] - '0'); i++; } return n; } int main() { int n; cin >> n; for(int j = 0; j < n; j++){ string s; cin >> s; int i = 0; cout << exp(s, i) << endl; } } ```
#include<iostream> #include<string> #include<cassert> #include<cctype> #include<stdio.h> #include<stdlib.h> //#include<math.h> using namespace std; string S; size_t cur = 0; int term(); int factor(); int expression(); int digit(){ assert(isdigit(S[cur])); int n = S[cur] - '0'; cur = cur + 1; return n; } int number(){ int n = digit(); while (cur < S.size() && isdigit(S[cur])) n = n * 10 + digit(); return n; } int expression(){ int a = term(); while (cur < S.size() && (S[cur] == '+' || S[cur] == '-')){ char op = S[cur]; cur = cur + 1; int b = term(); if (op == '+') a += b; else a -= b; } return a; } int term(){ int a = factor(); while (cur < S.size() && (S[cur] == '*' || S[cur] == '/')){ char op = S[cur]; cur = cur + 1; int b = factor(); if (op == '*') a *= b; else a /= b; } return a; } int factor(){ if (S[cur] != '(') return number(); cur += 1; int n = expression(); assert(S[cur] == ')'); cur += 1; return n; } int main(){ int N; cin >> N; for (int i = 0; i < N; i++){ cur = 0; cin >> S; S.resize(S.size() - 1); cout << expression() << endl; } return 0; }
### Prompt Please create a solution in CPP to the following problem: Your task is to write a program which reads an expression and evaluates it. * The expression consists of numerical values, operators and parentheses, and the ends with '='. * The operators includes +, - , *, / where respectively represents, addition, subtraction, multiplication and division. * Precedence of the operators is based on usual laws. That is one should perform all multiplication and division first, then addition and subtraction. When two operators have the same precedence, they are applied from left to right. * You may assume that there is no division by zero. * All calculation is performed as integers, and after the decimal point should be truncated * Length of the expression will not exceed 100. * -1 × 109 ≤ intermediate results of computation ≤ 109 Input The input is a sequence of datasets. The first line contains an integer n which represents the number of datasets. There will be n lines where each line contains an expression. Output For each datasets, prints the result of calculation. Example Input 2 4-2*3= 4*(8+4+3)= Output -2 60 ### Response ```cpp #include<iostream> #include<string> #include<cassert> #include<cctype> #include<stdio.h> #include<stdlib.h> //#include<math.h> using namespace std; string S; size_t cur = 0; int term(); int factor(); int expression(); int digit(){ assert(isdigit(S[cur])); int n = S[cur] - '0'; cur = cur + 1; return n; } int number(){ int n = digit(); while (cur < S.size() && isdigit(S[cur])) n = n * 10 + digit(); return n; } int expression(){ int a = term(); while (cur < S.size() && (S[cur] == '+' || S[cur] == '-')){ char op = S[cur]; cur = cur + 1; int b = term(); if (op == '+') a += b; else a -= b; } return a; } int term(){ int a = factor(); while (cur < S.size() && (S[cur] == '*' || S[cur] == '/')){ char op = S[cur]; cur = cur + 1; int b = factor(); if (op == '*') a *= b; else a /= b; } return a; } int factor(){ if (S[cur] != '(') return number(); cur += 1; int n = expression(); assert(S[cur] == ')'); cur += 1; return n; } int main(){ int N; cin >> N; for (int i = 0; i < N; i++){ cur = 0; cin >> S; S.resize(S.size() - 1); cout << expression() << endl; } return 0; } ```
#include<iostream> #include<string> #include<sstream> using namespace std; string s; int conv_stoi(string str){ stringstream ss(str); int n; ss >> n; return n; } int parse(int l, int r){ int n = 0; for(int i=r-1;i>=l;i--){ if(s[i] == ')') n++; if(s[i] == '(') n--; if(!n){ if(s[i] == '+') return parse(l,i) + parse(i+1,r); if(s[i] == '-') return parse(l,i) - parse(i+1,r); } } for(int i=r-1;i>=l;i--){ if(s[i] == ')') n++; if(s[i] == '(') n--; if(!n){ if(s[i] == '*') return parse(l,i) * parse(i+1,r); if(s[i] == '/') return parse(l,i) / parse(i+1,r); } } if(s[l] == '(' && s[r-1] == ')') return parse(l+1,r-1); return conv_stoi(s.substr(l,r-l)); } int main(){ int n; cin >> n; while(n--){ cin >> s; cout << parse(0,(int)s.size()-1) << endl; } }
### Prompt Your task is to create a Cpp solution to the following problem: Your task is to write a program which reads an expression and evaluates it. * The expression consists of numerical values, operators and parentheses, and the ends with '='. * The operators includes +, - , *, / where respectively represents, addition, subtraction, multiplication and division. * Precedence of the operators is based on usual laws. That is one should perform all multiplication and division first, then addition and subtraction. When two operators have the same precedence, they are applied from left to right. * You may assume that there is no division by zero. * All calculation is performed as integers, and after the decimal point should be truncated * Length of the expression will not exceed 100. * -1 × 109 ≤ intermediate results of computation ≤ 109 Input The input is a sequence of datasets. The first line contains an integer n which represents the number of datasets. There will be n lines where each line contains an expression. Output For each datasets, prints the result of calculation. Example Input 2 4-2*3= 4*(8+4+3)= Output -2 60 ### Response ```cpp #include<iostream> #include<string> #include<sstream> using namespace std; string s; int conv_stoi(string str){ stringstream ss(str); int n; ss >> n; return n; } int parse(int l, int r){ int n = 0; for(int i=r-1;i>=l;i--){ if(s[i] == ')') n++; if(s[i] == '(') n--; if(!n){ if(s[i] == '+') return parse(l,i) + parse(i+1,r); if(s[i] == '-') return parse(l,i) - parse(i+1,r); } } for(int i=r-1;i>=l;i--){ if(s[i] == ')') n++; if(s[i] == '(') n--; if(!n){ if(s[i] == '*') return parse(l,i) * parse(i+1,r); if(s[i] == '/') return parse(l,i) / parse(i+1,r); } } if(s[l] == '(' && s[r-1] == ')') return parse(l+1,r-1); return conv_stoi(s.substr(l,r-l)); } int main(){ int n; cin >> n; while(n--){ cin >> s; cout << parse(0,(int)s.size()-1) << endl; } } ```
#include <iostream> #include <string> #include <cctype> using namespace std; typedef string::const_iterator State; int factor(State &begin); int number(State &begin) { int ret = 0; while(isdigit(*begin)) { ret *= 10; ret += *begin - '0'; begin++; } return ret; } int term(State &begin) { int ret = factor(begin); while(true) { if(*begin == '*') { begin++; ret *= factor(begin); } else if(*begin == '/') { begin++; ret /= factor(begin); } else { break; } } return ret; } int expression(State &begin) { int ret = term(begin); while(true) { if(*begin == '+') { begin++; ret += term(begin); } else if(*begin == '-') { begin++; ret -= term(begin); } else { break; } } return ret; } int factor(State &begin) { int ret; if(*begin == '(') { begin++; ret = expression(begin); begin++; } else { ret = number(begin); } return ret; } int main() { int n; cin >> n; cin.ignore(); while(n--) { string s; getline(cin,s); State begin = s.begin(); int ans = expression(begin); cout << ans << endl; } return 0; }
### Prompt Create a solution in CPP for the following problem: Your task is to write a program which reads an expression and evaluates it. * The expression consists of numerical values, operators and parentheses, and the ends with '='. * The operators includes +, - , *, / where respectively represents, addition, subtraction, multiplication and division. * Precedence of the operators is based on usual laws. That is one should perform all multiplication and division first, then addition and subtraction. When two operators have the same precedence, they are applied from left to right. * You may assume that there is no division by zero. * All calculation is performed as integers, and after the decimal point should be truncated * Length of the expression will not exceed 100. * -1 × 109 ≤ intermediate results of computation ≤ 109 Input The input is a sequence of datasets. The first line contains an integer n which represents the number of datasets. There will be n lines where each line contains an expression. Output For each datasets, prints the result of calculation. Example Input 2 4-2*3= 4*(8+4+3)= Output -2 60 ### Response ```cpp #include <iostream> #include <string> #include <cctype> using namespace std; typedef string::const_iterator State; int factor(State &begin); int number(State &begin) { int ret = 0; while(isdigit(*begin)) { ret *= 10; ret += *begin - '0'; begin++; } return ret; } int term(State &begin) { int ret = factor(begin); while(true) { if(*begin == '*') { begin++; ret *= factor(begin); } else if(*begin == '/') { begin++; ret /= factor(begin); } else { break; } } return ret; } int expression(State &begin) { int ret = term(begin); while(true) { if(*begin == '+') { begin++; ret += term(begin); } else if(*begin == '-') { begin++; ret -= term(begin); } else { break; } } return ret; } int factor(State &begin) { int ret; if(*begin == '(') { begin++; ret = expression(begin); begin++; } else { ret = number(begin); } return ret; } int main() { int n; cin >> n; cin.ignore(); while(n--) { string s; getline(cin,s); State begin = s.begin(); int ans = expression(begin); cout << ans << endl; } return 0; } ```
#include<iostream> #include<string> #include<vector> using namespace std; string S; char U[11]="0123456789"; int junjo[4]={0,0,1,1}; long long STOI(string V){ int LEN=V.size(); if(LEN==0){return 0;} return (V[LEN - 1] - '0') + 10 * ( STOI(V.substr(0, LEN - 1)) ); } long long calc(int L,int R){ int K=0; vector<int>junjo2[4]; for(int i=0;i<4;i++){ junjo2[junjo[i]].push_back(i); } for(int i=0;i<4;i++){ if(junjo2[i].size()>=1){ for(int j=R-1;j>=L;j--){ if(S[j]=='('){K++;} if(S[j]==')'){K--;} for(int k=0;k<junjo2[i].size();k++){ if(junjo2[i][k]==0){ if(S[j]=='+' && K==0){ return calc(L,j)+calc(j+1,R); } } if(junjo2[i][k]==1){ if(S[j]=='-' && K==0){ return calc(L,j)-calc(j+1,R); } } if(junjo2[i][k]==2){ if(S[j]=='*' && K==0){ return calc(L,j)*calc(j+1,R); } } if(junjo2[i][k]==3){ if(S[j]=='/' && K==0){ return calc(L,j)/calc(j+1,R); } } } } } } if(S[L]=='(' && S[R-1]==')'){return calc(L+1,R-1);} return STOI(S.substr(L,R-L)); } int main(){ int n; cin>>n; for(int i=0;i<n;i++){ cin>>S; cout<<calc(0,S.size()-1)<<endl; } }
### Prompt Generate a cpp solution to the following problem: Your task is to write a program which reads an expression and evaluates it. * The expression consists of numerical values, operators and parentheses, and the ends with '='. * The operators includes +, - , *, / where respectively represents, addition, subtraction, multiplication and division. * Precedence of the operators is based on usual laws. That is one should perform all multiplication and division first, then addition and subtraction. When two operators have the same precedence, they are applied from left to right. * You may assume that there is no division by zero. * All calculation is performed as integers, and after the decimal point should be truncated * Length of the expression will not exceed 100. * -1 × 109 ≤ intermediate results of computation ≤ 109 Input The input is a sequence of datasets. The first line contains an integer n which represents the number of datasets. There will be n lines where each line contains an expression. Output For each datasets, prints the result of calculation. Example Input 2 4-2*3= 4*(8+4+3)= Output -2 60 ### Response ```cpp #include<iostream> #include<string> #include<vector> using namespace std; string S; char U[11]="0123456789"; int junjo[4]={0,0,1,1}; long long STOI(string V){ int LEN=V.size(); if(LEN==0){return 0;} return (V[LEN - 1] - '0') + 10 * ( STOI(V.substr(0, LEN - 1)) ); } long long calc(int L,int R){ int K=0; vector<int>junjo2[4]; for(int i=0;i<4;i++){ junjo2[junjo[i]].push_back(i); } for(int i=0;i<4;i++){ if(junjo2[i].size()>=1){ for(int j=R-1;j>=L;j--){ if(S[j]=='('){K++;} if(S[j]==')'){K--;} for(int k=0;k<junjo2[i].size();k++){ if(junjo2[i][k]==0){ if(S[j]=='+' && K==0){ return calc(L,j)+calc(j+1,R); } } if(junjo2[i][k]==1){ if(S[j]=='-' && K==0){ return calc(L,j)-calc(j+1,R); } } if(junjo2[i][k]==2){ if(S[j]=='*' && K==0){ return calc(L,j)*calc(j+1,R); } } if(junjo2[i][k]==3){ if(S[j]=='/' && K==0){ return calc(L,j)/calc(j+1,R); } } } } } } if(S[L]=='(' && S[R-1]==')'){return calc(L+1,R-1);} return STOI(S.substr(L,R-L)); } int main(){ int n; cin>>n; for(int i=0;i<n;i++){ cin>>S; cout<<calc(0,S.size()-1)<<endl; } } ```
#include <iostream> #include <string> using namespace std; class calc{ string s; string::iterator it; public: calc(string str){ s = str; it = str.begin(); } int fact(){ if(*it == '('){ *it++; int ret = exp(); *it++; return ret; } else { int ret = 0; while('0' <= *it && *it <= '9'){ ret *= 10; ret += *it-'0'; ++it; } return ret; } } int turm(){ int ret = fact(); while(*it == '*' || *it == '/'){ if(*it == '*'){ *it++; ret *= fact(); } else if(*it == '/'){ *it++; ret /= fact(); } } return ret; } int exp(){ int ret = turm(); while(*it == '+' || *it == '-'){ if(*it == '+'){ *it++; ret += turm(); } else if(*it == '-'){ *it++; ret -= turm(); } } return ret; } }; int main(){ int n; cin >> n; while(n--){ string str; cin >> str; calc ans(str); cout << ans.exp() << endl; } return 0; }
### Prompt Your challenge is to write a CPP solution to the following problem: Your task is to write a program which reads an expression and evaluates it. * The expression consists of numerical values, operators and parentheses, and the ends with '='. * The operators includes +, - , *, / where respectively represents, addition, subtraction, multiplication and division. * Precedence of the operators is based on usual laws. That is one should perform all multiplication and division first, then addition and subtraction. When two operators have the same precedence, they are applied from left to right. * You may assume that there is no division by zero. * All calculation is performed as integers, and after the decimal point should be truncated * Length of the expression will not exceed 100. * -1 × 109 ≤ intermediate results of computation ≤ 109 Input The input is a sequence of datasets. The first line contains an integer n which represents the number of datasets. There will be n lines where each line contains an expression. Output For each datasets, prints the result of calculation. Example Input 2 4-2*3= 4*(8+4+3)= Output -2 60 ### Response ```cpp #include <iostream> #include <string> using namespace std; class calc{ string s; string::iterator it; public: calc(string str){ s = str; it = str.begin(); } int fact(){ if(*it == '('){ *it++; int ret = exp(); *it++; return ret; } else { int ret = 0; while('0' <= *it && *it <= '9'){ ret *= 10; ret += *it-'0'; ++it; } return ret; } } int turm(){ int ret = fact(); while(*it == '*' || *it == '/'){ if(*it == '*'){ *it++; ret *= fact(); } else if(*it == '/'){ *it++; ret /= fact(); } } return ret; } int exp(){ int ret = turm(); while(*it == '+' || *it == '-'){ if(*it == '+'){ *it++; ret += turm(); } else if(*it == '-'){ *it++; ret -= turm(); } } return ret; } }; int main(){ int n; cin >> n; while(n--){ string str; cin >> str; calc ans(str); cout << ans.exp() << endl; } return 0; } ```
#include <iostream> #include <string> #include <cctype> using namespace std; string str; int cur = 0; int expression(); int term(); int factor(); int expression(); int digit() { int a = str[cur] - '0'; cur++; return a; } int number() { int a = digit(); while (cur < str.size() && isdigit(str[cur])) { a = a * 10 + digit(); } return a; } int expression() { int a = term(); while (cur < str.size() && (str[cur] == '+' || str[cur] == '-')) { if (str[cur] == '+') { cur++; int b = term(); a += b; } else if (str[cur] == '-') { cur++; int b = term(); a -= b; } } return a; } int term() { int a = factor(); while (cur < str.size() && (str[cur] == '*' || str[cur] == '/')) { if (str[cur] == '*') { cur++; int b = factor(); a *= b; } else if (str[cur] == '/') { cur++; int b = factor(); a /= b; } } return a; } int factor() { if (str[cur] != '(') return number(); cur++; int a = expression(); cur++; return a; } int main() { int N = 0; cin>>N; for (int n = 0; n < N; n++) { cin>>str; cur = 0; cout<<expression()<<endl; } }
### Prompt Create a solution in CPP for the following problem: Your task is to write a program which reads an expression and evaluates it. * The expression consists of numerical values, operators and parentheses, and the ends with '='. * The operators includes +, - , *, / where respectively represents, addition, subtraction, multiplication and division. * Precedence of the operators is based on usual laws. That is one should perform all multiplication and division first, then addition and subtraction. When two operators have the same precedence, they are applied from left to right. * You may assume that there is no division by zero. * All calculation is performed as integers, and after the decimal point should be truncated * Length of the expression will not exceed 100. * -1 × 109 ≤ intermediate results of computation ≤ 109 Input The input is a sequence of datasets. The first line contains an integer n which represents the number of datasets. There will be n lines where each line contains an expression. Output For each datasets, prints the result of calculation. Example Input 2 4-2*3= 4*(8+4+3)= Output -2 60 ### Response ```cpp #include <iostream> #include <string> #include <cctype> using namespace std; string str; int cur = 0; int expression(); int term(); int factor(); int expression(); int digit() { int a = str[cur] - '0'; cur++; return a; } int number() { int a = digit(); while (cur < str.size() && isdigit(str[cur])) { a = a * 10 + digit(); } return a; } int expression() { int a = term(); while (cur < str.size() && (str[cur] == '+' || str[cur] == '-')) { if (str[cur] == '+') { cur++; int b = term(); a += b; } else if (str[cur] == '-') { cur++; int b = term(); a -= b; } } return a; } int term() { int a = factor(); while (cur < str.size() && (str[cur] == '*' || str[cur] == '/')) { if (str[cur] == '*') { cur++; int b = factor(); a *= b; } else if (str[cur] == '/') { cur++; int b = factor(); a /= b; } } return a; } int factor() { if (str[cur] != '(') return number(); cur++; int a = expression(); cur++; return a; } int main() { int N = 0; cin>>N; for (int n = 0; n < N; n++) { cin>>str; cur = 0; cout<<expression()<<endl; } } ```
#include <iostream> #include <cassert> #include <cctype> using namespace std; string S; size_t cur; int digit() { assert(isdigit(S[cur])); int n = S[cur] - '0'; // ASCII code -> digit cur = cur+1; return n; } int number() { int n = digit(); while (cur<S.size() && isdigit(S[cur])) { n = n*10 + digit(); } return n; } int expression(); int factor() { if (S[cur] != '(') return number(); cur += 1; int n = expression(); assert(S[cur] == ')'); cur += 1; return n; } int term() { int a = factor(); while (cur<S.size() && (S[cur]=='*' || S[cur]=='/')) { char op = S[cur++]; int b = factor(); if (op=='*') a *= b; else a /= b; } return a; } int expression() { int a = term(); while (cur<S.size() && (S[cur]=='+' || S[cur]=='-')) { char op = S[cur++]; int b = term(); if (op == '+') a += b; else a -= b; } return a; } #if 0 int parse() {return expression();} int main() { int a = parse(); cout << a << endl; assert(a == 4); assert(cur == S.size()); return 0; } #endif int main() { int N; cin >> N; for (int i=0; i<N; i++){ cur = 0; cin >> S; S.resize(S.size()-1); // delete '=' cout << expression() << endl; } return 0; }
### Prompt In cpp, your task is to solve the following problem: Your task is to write a program which reads an expression and evaluates it. * The expression consists of numerical values, operators and parentheses, and the ends with '='. * The operators includes +, - , *, / where respectively represents, addition, subtraction, multiplication and division. * Precedence of the operators is based on usual laws. That is one should perform all multiplication and division first, then addition and subtraction. When two operators have the same precedence, they are applied from left to right. * You may assume that there is no division by zero. * All calculation is performed as integers, and after the decimal point should be truncated * Length of the expression will not exceed 100. * -1 × 109 ≤ intermediate results of computation ≤ 109 Input The input is a sequence of datasets. The first line contains an integer n which represents the number of datasets. There will be n lines where each line contains an expression. Output For each datasets, prints the result of calculation. Example Input 2 4-2*3= 4*(8+4+3)= Output -2 60 ### Response ```cpp #include <iostream> #include <cassert> #include <cctype> using namespace std; string S; size_t cur; int digit() { assert(isdigit(S[cur])); int n = S[cur] - '0'; // ASCII code -> digit cur = cur+1; return n; } int number() { int n = digit(); while (cur<S.size() && isdigit(S[cur])) { n = n*10 + digit(); } return n; } int expression(); int factor() { if (S[cur] != '(') return number(); cur += 1; int n = expression(); assert(S[cur] == ')'); cur += 1; return n; } int term() { int a = factor(); while (cur<S.size() && (S[cur]=='*' || S[cur]=='/')) { char op = S[cur++]; int b = factor(); if (op=='*') a *= b; else a /= b; } return a; } int expression() { int a = term(); while (cur<S.size() && (S[cur]=='+' || S[cur]=='-')) { char op = S[cur++]; int b = term(); if (op == '+') a += b; else a -= b; } return a; } #if 0 int parse() {return expression();} int main() { int a = parse(); cout << a << endl; assert(a == 4); assert(cur == S.size()); return 0; } #endif int main() { int N; cin >> N; for (int i=0; i<N; i++){ cur = 0; cin >> S; S.resize(S.size()-1); // delete '=' cout << expression() << endl; } return 0; } ```
#include <bits/stdc++.h> using namespace std; typedef long long ll; string s; int N; int c; int expr(); int term(); int factor(); int num(); void debug(string text){ return; cout << text + " "; for(int i = 0; i < N; i++){ if(i == c) cout << '[' << s[i] << ']'; else cout << s[i]; } cout << endl; } int expr(){ debug("expr"); int x = term(); while(c < N){ if(s[c] == '+'){ c++; x += term(); } else if(s[c] == '-'){ c++; x -= term(); } else break; } return x; } int term(){ debug("term"); int x = factor(); while(c < N){ if(s[c] == '*'){ c++; x *= factor(); } else if(s[c] == '/'){ c++; x /= factor(); } else break; } return x; } int factor(){ debug("factor"); if(s[c] == '('){ c++; int ret = expr(); assert(s[c] == ')'); c++; return ret; } return num(); } int num(){ debug("num"); int ret = 0; while(c < N && isdigit(s[c])){ ret = ret * 10 + s[c] - '0'; c++; } return ret; } int main(){ cin.tie(0); ios::sync_with_stdio(false); #ifdef LOCAL std::ifstream in("in"); std::cin.rdbuf(in.rdbuf()); #endif int n; cin >> n; while(n--){ cin >> s; s.pop_back(); N = s.size(); c = 0; cout << expr() << endl; } }
### Prompt Develop a solution in CPP to the problem described below: Your task is to write a program which reads an expression and evaluates it. * The expression consists of numerical values, operators and parentheses, and the ends with '='. * The operators includes +, - , *, / where respectively represents, addition, subtraction, multiplication and division. * Precedence of the operators is based on usual laws. That is one should perform all multiplication and division first, then addition and subtraction. When two operators have the same precedence, they are applied from left to right. * You may assume that there is no division by zero. * All calculation is performed as integers, and after the decimal point should be truncated * Length of the expression will not exceed 100. * -1 × 109 ≤ intermediate results of computation ≤ 109 Input The input is a sequence of datasets. The first line contains an integer n which represents the number of datasets. There will be n lines where each line contains an expression. Output For each datasets, prints the result of calculation. Example Input 2 4-2*3= 4*(8+4+3)= Output -2 60 ### Response ```cpp #include <bits/stdc++.h> using namespace std; typedef long long ll; string s; int N; int c; int expr(); int term(); int factor(); int num(); void debug(string text){ return; cout << text + " "; for(int i = 0; i < N; i++){ if(i == c) cout << '[' << s[i] << ']'; else cout << s[i]; } cout << endl; } int expr(){ debug("expr"); int x = term(); while(c < N){ if(s[c] == '+'){ c++; x += term(); } else if(s[c] == '-'){ c++; x -= term(); } else break; } return x; } int term(){ debug("term"); int x = factor(); while(c < N){ if(s[c] == '*'){ c++; x *= factor(); } else if(s[c] == '/'){ c++; x /= factor(); } else break; } return x; } int factor(){ debug("factor"); if(s[c] == '('){ c++; int ret = expr(); assert(s[c] == ')'); c++; return ret; } return num(); } int num(){ debug("num"); int ret = 0; while(c < N && isdigit(s[c])){ ret = ret * 10 + s[c] - '0'; c++; } return ret; } int main(){ cin.tie(0); ios::sync_with_stdio(false); #ifdef LOCAL std::ifstream in("in"); std::cin.rdbuf(in.rdbuf()); #endif int n; cin >> n; while(n--){ cin >> s; s.pop_back(); N = s.size(); c = 0; cout << expr() << endl; } } ```
#include <iostream> #include <sstream> #include <iomanip> #include <algorithm> #include <cmath> #include <string> #include <vector> #include <list> #include <queue> #include <stack> #include <set> #include <map> #include <bitset> #include <numeric> #include <climits> #include <cfloat> using namespace std; int solve(string& s, int& i) { int a = 0; int b = 0; char prev = '+'; for(;;){ int x = 0; if(s[i] == '('){ ++ i; x += solve(s, i); ++ i; }else{ while('0' <= s[i] && s[i] <= '9'){ x *= 10; x += s[i] - '0'; ++ i; } } if(prev == '+'){ a += b; b = x; }else if(prev == '-'){ a += b; b = -x; }else if(prev == '*'){ b *= x; }else{ b /= x; } if(s[i] == ')' || s[i] == '=') break; prev = s[i]; ++ i; } return a + b; } int main() { int n; cin >> n; while(--n >= 0){ string s; cin >> s; int i = 0; cout << solve(s, i) << endl; } return 0; }
### Prompt Your challenge is to write a cpp solution to the following problem: Your task is to write a program which reads an expression and evaluates it. * The expression consists of numerical values, operators and parentheses, and the ends with '='. * The operators includes +, - , *, / where respectively represents, addition, subtraction, multiplication and division. * Precedence of the operators is based on usual laws. That is one should perform all multiplication and division first, then addition and subtraction. When two operators have the same precedence, they are applied from left to right. * You may assume that there is no division by zero. * All calculation is performed as integers, and after the decimal point should be truncated * Length of the expression will not exceed 100. * -1 × 109 ≤ intermediate results of computation ≤ 109 Input The input is a sequence of datasets. The first line contains an integer n which represents the number of datasets. There will be n lines where each line contains an expression. Output For each datasets, prints the result of calculation. Example Input 2 4-2*3= 4*(8+4+3)= Output -2 60 ### Response ```cpp #include <iostream> #include <sstream> #include <iomanip> #include <algorithm> #include <cmath> #include <string> #include <vector> #include <list> #include <queue> #include <stack> #include <set> #include <map> #include <bitset> #include <numeric> #include <climits> #include <cfloat> using namespace std; int solve(string& s, int& i) { int a = 0; int b = 0; char prev = '+'; for(;;){ int x = 0; if(s[i] == '('){ ++ i; x += solve(s, i); ++ i; }else{ while('0' <= s[i] && s[i] <= '9'){ x *= 10; x += s[i] - '0'; ++ i; } } if(prev == '+'){ a += b; b = x; }else if(prev == '-'){ a += b; b = -x; }else if(prev == '*'){ b *= x; }else{ b /= x; } if(s[i] == ')' || s[i] == '=') break; prev = s[i]; ++ i; } return a + b; } int main() { int n; cin >> n; while(--n >= 0){ string s; cin >> s; int i = 0; cout << solve(s, i) << endl; } return 0; } ```
#include <iostream> #include <string> #include <cctype> using namespace std; typedef string::const_iterator State; struct Parsing { int expression(State& it) { int res = term(it); while (true) { if (*it == '+') res += term(++it); else if (*it == '-') res -= term(++it); else break; } return res; } int factor(State& it) { int res = 0; if (*it == '(') { res = expression(++it); ++it; } else res = number(it); return res; } int term(State& it) { int res = factor(it); while (true) { if (*it == '*') res *= factor(++it); else if (*it == '/') res /= factor(++it); else break; } return res; } int number(State& it) { int res = 0; while (isdigit(*it)) { res *= 10; res += *it - '0'; ++it; } return res; } }; int main() { int n; cin >> n; Parsing solve; while (n--) { string s; cin >> s; State begin = s.begin(); cout << solve.expression(begin) << endl; } return 0; }
### Prompt Develop a solution in CPP to the problem described below: Your task is to write a program which reads an expression and evaluates it. * The expression consists of numerical values, operators and parentheses, and the ends with '='. * The operators includes +, - , *, / where respectively represents, addition, subtraction, multiplication and division. * Precedence of the operators is based on usual laws. That is one should perform all multiplication and division first, then addition and subtraction. When two operators have the same precedence, they are applied from left to right. * You may assume that there is no division by zero. * All calculation is performed as integers, and after the decimal point should be truncated * Length of the expression will not exceed 100. * -1 × 109 ≤ intermediate results of computation ≤ 109 Input The input is a sequence of datasets. The first line contains an integer n which represents the number of datasets. There will be n lines where each line contains an expression. Output For each datasets, prints the result of calculation. Example Input 2 4-2*3= 4*(8+4+3)= Output -2 60 ### Response ```cpp #include <iostream> #include <string> #include <cctype> using namespace std; typedef string::const_iterator State; struct Parsing { int expression(State& it) { int res = term(it); while (true) { if (*it == '+') res += term(++it); else if (*it == '-') res -= term(++it); else break; } return res; } int factor(State& it) { int res = 0; if (*it == '(') { res = expression(++it); ++it; } else res = number(it); return res; } int term(State& it) { int res = factor(it); while (true) { if (*it == '*') res *= factor(++it); else if (*it == '/') res /= factor(++it); else break; } return res; } int number(State& it) { int res = 0; while (isdigit(*it)) { res *= 10; res += *it - '0'; ++it; } return res; } }; int main() { int n; cin >> n; Parsing solve; while (n--) { string s; cin >> s; State begin = s.begin(); cout << solve.expression(begin) << endl; } return 0; } ```
#include <iostream> #include <string> #include <cassert> using namespace std; string S; size_t cur = 0; int digit(){ assert(isdigit(S[cur])); int n = S[cur] - '0'; cur += 1; return n; } int number(){ int n = digit(); while(cur<S.size()&&isdigit(S[cur])){ n = n*10 + digit(); } return n; } int factor(); int term(){ int a = factor(); while(cur<S.size()&&(S[cur] == '*' || S[cur] == '/')){ char op = S[cur]; cur += 1; int b = factor(); if(op == '*') a *= b; else a /= b; } return a; } int expression(){ int a = term(); while(cur<S.size()&&(S[cur] == '+' || S[cur] == '-')){ char op = S[cur]; cur += 1; int b = term(); if(op == '+') a += b; else a -= b; } return a; } int factor(){ if(S[cur]!='(') return number(); cur += 1; int n = expression(); assert(S[cur]==')'); cur += 1; return n; } int parse(){ cur = 0; return expression(); } int main(){ int n; cin >> n; for(int i=0;i<n;++i){ cin >> S; S.pop_back(); int a = parse(); cout << a << endl; } }
### Prompt Please provide a cpp coded solution to the problem described below: Your task is to write a program which reads an expression and evaluates it. * The expression consists of numerical values, operators and parentheses, and the ends with '='. * The operators includes +, - , *, / where respectively represents, addition, subtraction, multiplication and division. * Precedence of the operators is based on usual laws. That is one should perform all multiplication and division first, then addition and subtraction. When two operators have the same precedence, they are applied from left to right. * You may assume that there is no division by zero. * All calculation is performed as integers, and after the decimal point should be truncated * Length of the expression will not exceed 100. * -1 × 109 ≤ intermediate results of computation ≤ 109 Input The input is a sequence of datasets. The first line contains an integer n which represents the number of datasets. There will be n lines where each line contains an expression. Output For each datasets, prints the result of calculation. Example Input 2 4-2*3= 4*(8+4+3)= Output -2 60 ### Response ```cpp #include <iostream> #include <string> #include <cassert> using namespace std; string S; size_t cur = 0; int digit(){ assert(isdigit(S[cur])); int n = S[cur] - '0'; cur += 1; return n; } int number(){ int n = digit(); while(cur<S.size()&&isdigit(S[cur])){ n = n*10 + digit(); } return n; } int factor(); int term(){ int a = factor(); while(cur<S.size()&&(S[cur] == '*' || S[cur] == '/')){ char op = S[cur]; cur += 1; int b = factor(); if(op == '*') a *= b; else a /= b; } return a; } int expression(){ int a = term(); while(cur<S.size()&&(S[cur] == '+' || S[cur] == '-')){ char op = S[cur]; cur += 1; int b = term(); if(op == '+') a += b; else a -= b; } return a; } int factor(){ if(S[cur]!='(') return number(); cur += 1; int n = expression(); assert(S[cur]==')'); cur += 1; return n; } int parse(){ cur = 0; return expression(); } int main(){ int n; cin >> n; for(int i=0;i<n;++i){ cin >> S; S.pop_back(); int a = parse(); cout << a << endl; } } ```
#include <iostream> #include <cctype> #include <string> #include <cassert> using namespace std; string S; size_t cur; int x; char readchar(){ assert(cur < S.size()); return S[cur++]; } char peek(){ assert(cur < S.size()); return S[cur]; } int digit(){ assert(isdigit(peek())); int n = readchar() - '0'; return n; } int number(){ int n = digit(); while (cur < S.size() && isdigit(peek())){ n = n*10 + digit(); } return n; } int expression(); int factor(){ if (peek() != '(') return number(); cur += 1; int n = expression(); assert(peek()==')'); cur+=1; return n; } int term(){ int a = factor(); while(cur < S.size() && (peek()=='*' || peek()=='/')){ char op = readchar(); int b = factor(); if (op=='*'){ a = a*b; }else{ a = a/b; } } return a; } int expression(){ int a = term(); while(cur < S.size() && (peek()=='+' || peek()=='-')){ char op = readchar(); int b = term(); if (op == '+'){ a = a+b; }else{ a = a-b; } } return a; } int main() { cin >> x; for (int i=1; i<=x; i++){ cin >> S; cur = 0; S.resize(S.size()-1); cout << expression() << endl; } }
### Prompt Create a solution in Cpp for the following problem: Your task is to write a program which reads an expression and evaluates it. * The expression consists of numerical values, operators and parentheses, and the ends with '='. * The operators includes +, - , *, / where respectively represents, addition, subtraction, multiplication and division. * Precedence of the operators is based on usual laws. That is one should perform all multiplication and division first, then addition and subtraction. When two operators have the same precedence, they are applied from left to right. * You may assume that there is no division by zero. * All calculation is performed as integers, and after the decimal point should be truncated * Length of the expression will not exceed 100. * -1 × 109 ≤ intermediate results of computation ≤ 109 Input The input is a sequence of datasets. The first line contains an integer n which represents the number of datasets. There will be n lines where each line contains an expression. Output For each datasets, prints the result of calculation. Example Input 2 4-2*3= 4*(8+4+3)= Output -2 60 ### Response ```cpp #include <iostream> #include <cctype> #include <string> #include <cassert> using namespace std; string S; size_t cur; int x; char readchar(){ assert(cur < S.size()); return S[cur++]; } char peek(){ assert(cur < S.size()); return S[cur]; } int digit(){ assert(isdigit(peek())); int n = readchar() - '0'; return n; } int number(){ int n = digit(); while (cur < S.size() && isdigit(peek())){ n = n*10 + digit(); } return n; } int expression(); int factor(){ if (peek() != '(') return number(); cur += 1; int n = expression(); assert(peek()==')'); cur+=1; return n; } int term(){ int a = factor(); while(cur < S.size() && (peek()=='*' || peek()=='/')){ char op = readchar(); int b = factor(); if (op=='*'){ a = a*b; }else{ a = a/b; } } return a; } int expression(){ int a = term(); while(cur < S.size() && (peek()=='+' || peek()=='-')){ char op = readchar(); int b = term(); if (op == '+'){ a = a+b; }else{ a = a-b; } } return a; } int main() { cin >> x; for (int i=1; i<=x; i++){ cin >> S; cur = 0; S.resize(S.size()-1); cout << expression() << endl; } } ```
#include <cstdio> #include <cctype> #include <cassert> #include <algorithm> #include <string> #include <vector> int apply(int lhs, char op, int rhs) { switch (op) { case '+': return lhs+rhs; case '-': return lhs-rhs; case '*': return lhs*rhs; case '/': return lhs/rhs; } assert(false); } int parse(const std::string &s, size_t &i, size_t preced=0) { const std::vector<std::string> ops={"+-", "*/"}; if (preced == ops.size()) { if (s[i] == '(') { int res=parse(s, ++i, 0); assert(s[i] == ')'); ++i; return res; } if (isdigit(s[i])) { int res=s[i]-'0'; while (++i < s.length() && isdigit(s[i])) res = res*10+s[i]-'0'; return res; } assert(false); } int res=parse(s, i, preced+1); while (i < s.length()) { char op=s[i]; if (!std::count(ops[preced].begin(), ops[preced].end(), op)) break; int tmp=parse(s, ++i, preced+1); res = apply(res, op, tmp); } return res; } int main() { int n; scanf("%d", &n); for (int i=0; i<n; ++i) { char buf[128]; scanf("%s", buf); std::string s=buf; size_t j=0; printf("%d\n", parse(s, j)); } }
### Prompt Please provide a cpp coded solution to the problem described below: Your task is to write a program which reads an expression and evaluates it. * The expression consists of numerical values, operators and parentheses, and the ends with '='. * The operators includes +, - , *, / where respectively represents, addition, subtraction, multiplication and division. * Precedence of the operators is based on usual laws. That is one should perform all multiplication and division first, then addition and subtraction. When two operators have the same precedence, they are applied from left to right. * You may assume that there is no division by zero. * All calculation is performed as integers, and after the decimal point should be truncated * Length of the expression will not exceed 100. * -1 × 109 ≤ intermediate results of computation ≤ 109 Input The input is a sequence of datasets. The first line contains an integer n which represents the number of datasets. There will be n lines where each line contains an expression. Output For each datasets, prints the result of calculation. Example Input 2 4-2*3= 4*(8+4+3)= Output -2 60 ### Response ```cpp #include <cstdio> #include <cctype> #include <cassert> #include <algorithm> #include <string> #include <vector> int apply(int lhs, char op, int rhs) { switch (op) { case '+': return lhs+rhs; case '-': return lhs-rhs; case '*': return lhs*rhs; case '/': return lhs/rhs; } assert(false); } int parse(const std::string &s, size_t &i, size_t preced=0) { const std::vector<std::string> ops={"+-", "*/"}; if (preced == ops.size()) { if (s[i] == '(') { int res=parse(s, ++i, 0); assert(s[i] == ')'); ++i; return res; } if (isdigit(s[i])) { int res=s[i]-'0'; while (++i < s.length() && isdigit(s[i])) res = res*10+s[i]-'0'; return res; } assert(false); } int res=parse(s, i, preced+1); while (i < s.length()) { char op=s[i]; if (!std::count(ops[preced].begin(), ops[preced].end(), op)) break; int tmp=parse(s, ++i, preced+1); res = apply(res, op, tmp); } return res; } int main() { int n; scanf("%d", &n); for (int i=0; i<n; ++i) { char buf[128]; scanf("%s", buf); std::string s=buf; size_t j=0; printf("%d\n", parse(s, j)); } } ```
#include <iostream> #include <string> #include <cassert> #include <cctype> using namespace std; string S; size_t cur = 0; int expression(); int digit(); int number(); int parse(); int term(); int factor(); int main(){ int n; cin >> n; for (int i=0; i<n; i++){ cin >> S; int a = parse(); // assert (a == 15); assert (cur == S.size()-1); cout << a << endl; cur = 0; } } int digit(){ assert (isdigit(S[cur])); int n = S[cur] - '0'; cur = cur+1; return n; } int number(){ int n = digit(); while(cur < S.size()-1 && isdigit(S[cur])) n = n*10 + digit(); return n; } int expression(){ int sum = term(); while (S[cur] == '+' || S[cur] == '-'){ char op = S[cur]; cur += 1; int b = term(); assert(op == '+' || op == '-'); if (op == '+') sum += b; else sum -= b; } return sum; } int term(){ int a = factor(); while ( cur < S.size()-1 && (S[cur] == '*' || S[cur] == '/') ){ char op = S[cur++]; int b = factor(); if (op == '*') a *= b; else a /= b; } return a; } int factor(){ if (S[cur] != '(') return number(); cur += 1; int n = expression(); assert(S[cur] == ')'); cur += 1; return n; } int parse(){ return expression(); }
### Prompt In CPP, your task is to solve the following problem: Your task is to write a program which reads an expression and evaluates it. * The expression consists of numerical values, operators and parentheses, and the ends with '='. * The operators includes +, - , *, / where respectively represents, addition, subtraction, multiplication and division. * Precedence of the operators is based on usual laws. That is one should perform all multiplication and division first, then addition and subtraction. When two operators have the same precedence, they are applied from left to right. * You may assume that there is no division by zero. * All calculation is performed as integers, and after the decimal point should be truncated * Length of the expression will not exceed 100. * -1 × 109 ≤ intermediate results of computation ≤ 109 Input The input is a sequence of datasets. The first line contains an integer n which represents the number of datasets. There will be n lines where each line contains an expression. Output For each datasets, prints the result of calculation. Example Input 2 4-2*3= 4*(8+4+3)= Output -2 60 ### Response ```cpp #include <iostream> #include <string> #include <cassert> #include <cctype> using namespace std; string S; size_t cur = 0; int expression(); int digit(); int number(); int parse(); int term(); int factor(); int main(){ int n; cin >> n; for (int i=0; i<n; i++){ cin >> S; int a = parse(); // assert (a == 15); assert (cur == S.size()-1); cout << a << endl; cur = 0; } } int digit(){ assert (isdigit(S[cur])); int n = S[cur] - '0'; cur = cur+1; return n; } int number(){ int n = digit(); while(cur < S.size()-1 && isdigit(S[cur])) n = n*10 + digit(); return n; } int expression(){ int sum = term(); while (S[cur] == '+' || S[cur] == '-'){ char op = S[cur]; cur += 1; int b = term(); assert(op == '+' || op == '-'); if (op == '+') sum += b; else sum -= b; } return sum; } int term(){ int a = factor(); while ( cur < S.size()-1 && (S[cur] == '*' || S[cur] == '/') ){ char op = S[cur++]; int b = factor(); if (op == '*') a *= b; else a /= b; } return a; } int factor(){ if (S[cur] != '(') return number(); cur += 1; int n = expression(); assert(S[cur] == ')'); cur += 1; return n; } int parse(){ return expression(); } ```
#include <bits/stdc++.h> using namespace std; #define FOR(i,a,b) for(int i=(a);i<(b);i++) #define REP(i,n) FOR(i,0,n) int culc1(); int culc2(); int culc3(); int culc4(); int n; string str; int cur; int main() { cin >> n; while (n--) { cin >> str; cur = 0; cout << culc1() << endl; } return 0; } int culc1() { int val = culc2(); while (str[cur] == '+' || str[cur] == '-') { char op = str[cur++]; int val2 = culc2(); if (op == '+') val += val2; else val -= val2; } return val; } int culc2() { int val = culc3(); while (str[cur] == '*' || str[cur] == '/') { char op = str[cur++]; int val2 = culc3(); if (op == '*') val *= val2; else val /= val2; } return val; } int culc3() { if (isdigit(str[cur])) return culc4(); cur++; int res = culc1(); cur++; return res; } int culc4() { int num = str[cur++] - '0'; while (isdigit(str[cur])) num = num*10 + str[cur++] - '0'; return num; }
### Prompt Develop a solution in cpp to the problem described below: Your task is to write a program which reads an expression and evaluates it. * The expression consists of numerical values, operators and parentheses, and the ends with '='. * The operators includes +, - , *, / where respectively represents, addition, subtraction, multiplication and division. * Precedence of the operators is based on usual laws. That is one should perform all multiplication and division first, then addition and subtraction. When two operators have the same precedence, they are applied from left to right. * You may assume that there is no division by zero. * All calculation is performed as integers, and after the decimal point should be truncated * Length of the expression will not exceed 100. * -1 × 109 ≤ intermediate results of computation ≤ 109 Input The input is a sequence of datasets. The first line contains an integer n which represents the number of datasets. There will be n lines where each line contains an expression. Output For each datasets, prints the result of calculation. Example Input 2 4-2*3= 4*(8+4+3)= Output -2 60 ### Response ```cpp #include <bits/stdc++.h> using namespace std; #define FOR(i,a,b) for(int i=(a);i<(b);i++) #define REP(i,n) FOR(i,0,n) int culc1(); int culc2(); int culc3(); int culc4(); int n; string str; int cur; int main() { cin >> n; while (n--) { cin >> str; cur = 0; cout << culc1() << endl; } return 0; } int culc1() { int val = culc2(); while (str[cur] == '+' || str[cur] == '-') { char op = str[cur++]; int val2 = culc2(); if (op == '+') val += val2; else val -= val2; } return val; } int culc2() { int val = culc3(); while (str[cur] == '*' || str[cur] == '/') { char op = str[cur++]; int val2 = culc3(); if (op == '*') val *= val2; else val /= val2; } return val; } int culc3() { if (isdigit(str[cur])) return culc4(); cur++; int res = culc1(); cur++; return res; } int culc4() { int num = str[cur++] - '0'; while (isdigit(str[cur])) num = num*10 + str[cur++] - '0'; return num; } ```
#include <iostream> #include <cassert> #include <cctype> using namespace std; string S; size_t cur; int digit() { assert(isdigit(S[cur])); // S[cur]が数字でなければストップ。正しく動作すれば実行されない。 int n = S[cur] - '0'; cur++; return n; } int number() { int n = digit(); while (cur < S.size() && isdigit(S[cur])) { n = n * 10 + digit(); } return n; } int factor(); int term() { int a = factor(); while (cur < S.size() && (S[cur] == '*' || S[cur] == '/')) { char op = S[cur++]; int b = factor(); if (op == '*') { a *= b; } else { a /= b; } } return a; } int expression(); int factor() { if (S[cur] != '(') return number(); assert(S[cur] == '('); cur++; int n = expression(); assert(S[cur] == ')'); cur++; return n; } int expression() { int a = term(); while (cur < S.size() && (S[cur] == '+' || S[cur] == '-')) { char op = S[cur++]; int b = term(); if (op == '+') { a += b; } else { a -= b; } } return a; } int parse() { return expression(); } int main() { int N; cin >> N; for (int i=0; i<N; i++) { cur = 0; cin >> S; S.resize(S.size()-1); // 最後の=はいらない cout << expression() << endl; } }
### Prompt Please formulate a CPP solution to the following problem: Your task is to write a program which reads an expression and evaluates it. * The expression consists of numerical values, operators and parentheses, and the ends with '='. * The operators includes +, - , *, / where respectively represents, addition, subtraction, multiplication and division. * Precedence of the operators is based on usual laws. That is one should perform all multiplication and division first, then addition and subtraction. When two operators have the same precedence, they are applied from left to right. * You may assume that there is no division by zero. * All calculation is performed as integers, and after the decimal point should be truncated * Length of the expression will not exceed 100. * -1 × 109 ≤ intermediate results of computation ≤ 109 Input The input is a sequence of datasets. The first line contains an integer n which represents the number of datasets. There will be n lines where each line contains an expression. Output For each datasets, prints the result of calculation. Example Input 2 4-2*3= 4*(8+4+3)= Output -2 60 ### Response ```cpp #include <iostream> #include <cassert> #include <cctype> using namespace std; string S; size_t cur; int digit() { assert(isdigit(S[cur])); // S[cur]が数字でなければストップ。正しく動作すれば実行されない。 int n = S[cur] - '0'; cur++; return n; } int number() { int n = digit(); while (cur < S.size() && isdigit(S[cur])) { n = n * 10 + digit(); } return n; } int factor(); int term() { int a = factor(); while (cur < S.size() && (S[cur] == '*' || S[cur] == '/')) { char op = S[cur++]; int b = factor(); if (op == '*') { a *= b; } else { a /= b; } } return a; } int expression(); int factor() { if (S[cur] != '(') return number(); assert(S[cur] == '('); cur++; int n = expression(); assert(S[cur] == ')'); cur++; return n; } int expression() { int a = term(); while (cur < S.size() && (S[cur] == '+' || S[cur] == '-')) { char op = S[cur++]; int b = term(); if (op == '+') { a += b; } else { a -= b; } } return a; } int parse() { return expression(); } int main() { int N; cin >> N; for (int i=0; i<N; i++) { cur = 0; cin >> S; S.resize(S.size()-1); // 最後の=はいらない cout << expression() << endl; } } ```
#include<iostream> #include<algorithm> #include<string> using namespace std; int expression(); int term(); int factor(); int number(); int now; string s; int expression(){ int res=term(); while(true){ if(s[now]=='+')now++,res+=term(); else if(s[now]=='-')now++,res-=term(); else break; } return res; } int term(){ int res=factor(); while(true){ if(s[now]=='(')res+=factor(); else if(s[now]=='*')now++,res*=factor(); else if(s[now]=='/')now++,res/=factor(); else break; } return res; } int factor(){ int res=0; if(s[now]=='(')now++,res=expression(),now++; else return number(); return res; } int number(){ int res=0; while('0'<=s[now] && s[now]<='9')res*=10,res+=s[now++]-'0'; return res; } int main(void){ int n; cin >> n; while(n--){ cin >> s; now=0; cout << expression() << endl; } return 0; }
### Prompt Create a solution in CPP for the following problem: Your task is to write a program which reads an expression and evaluates it. * The expression consists of numerical values, operators and parentheses, and the ends with '='. * The operators includes +, - , *, / where respectively represents, addition, subtraction, multiplication and division. * Precedence of the operators is based on usual laws. That is one should perform all multiplication and division first, then addition and subtraction. When two operators have the same precedence, they are applied from left to right. * You may assume that there is no division by zero. * All calculation is performed as integers, and after the decimal point should be truncated * Length of the expression will not exceed 100. * -1 × 109 ≤ intermediate results of computation ≤ 109 Input The input is a sequence of datasets. The first line contains an integer n which represents the number of datasets. There will be n lines where each line contains an expression. Output For each datasets, prints the result of calculation. Example Input 2 4-2*3= 4*(8+4+3)= Output -2 60 ### Response ```cpp #include<iostream> #include<algorithm> #include<string> using namespace std; int expression(); int term(); int factor(); int number(); int now; string s; int expression(){ int res=term(); while(true){ if(s[now]=='+')now++,res+=term(); else if(s[now]=='-')now++,res-=term(); else break; } return res; } int term(){ int res=factor(); while(true){ if(s[now]=='(')res+=factor(); else if(s[now]=='*')now++,res*=factor(); else if(s[now]=='/')now++,res/=factor(); else break; } return res; } int factor(){ int res=0; if(s[now]=='(')now++,res=expression(),now++; else return number(); return res; } int number(){ int res=0; while('0'<=s[now] && s[now]<='9')res*=10,res+=s[now++]-'0'; return res; } int main(void){ int n; cin >> n; while(n--){ cin >> s; now=0; cout << expression() << endl; } return 0; } ```
#include <iostream> #include <algorithm> #include <cassert> #include <cctype> #include <cstdio> #include <math.h> #include <map> #include <queue> #include <string> using namespace std; typedef long long ll; string S; size_t cur; int parse(); int digit(){ assert(isdigit(S[cur])); int n=S[cur]-'0'; cur++; return n; } int number(){ int n=digit(); while(cur<S.size()&&isdigit(S[cur])) n=n*10+digit(); return n; } int expression(); int factor(){ if(S[cur]!='(')return number(); cur++; int n=expression(); assert(S[cur]==')'); cur++; return n; } int term1(){ int a=factor(); while(cur<S.size()&&S[cur]=='^'){ cur++; int b=factor(); a=pow(a,b); } return a; } int term2(){ int a=term1(); while(cur<S.size()&&(S[cur]=='*'||S[cur]=='/')){ char op=S[cur++]; int b=term1(); if(op=='*')a*=b; else a/=b; } return a; } int expression(){ int a=term2(); while(cur<S.size()&&(S[cur]=='+'||S[cur]=='-')){ char op=S[cur++]; int b=term2(); if(op=='+')a+=b; else a-=b; } return a; } int main() { int n; cin>>n; while(cin>>S){ cur=0; cout<<expression()<<endl; } }
### Prompt In cpp, your task is to solve the following problem: Your task is to write a program which reads an expression and evaluates it. * The expression consists of numerical values, operators and parentheses, and the ends with '='. * The operators includes +, - , *, / where respectively represents, addition, subtraction, multiplication and division. * Precedence of the operators is based on usual laws. That is one should perform all multiplication and division first, then addition and subtraction. When two operators have the same precedence, they are applied from left to right. * You may assume that there is no division by zero. * All calculation is performed as integers, and after the decimal point should be truncated * Length of the expression will not exceed 100. * -1 × 109 ≤ intermediate results of computation ≤ 109 Input The input is a sequence of datasets. The first line contains an integer n which represents the number of datasets. There will be n lines where each line contains an expression. Output For each datasets, prints the result of calculation. Example Input 2 4-2*3= 4*(8+4+3)= Output -2 60 ### Response ```cpp #include <iostream> #include <algorithm> #include <cassert> #include <cctype> #include <cstdio> #include <math.h> #include <map> #include <queue> #include <string> using namespace std; typedef long long ll; string S; size_t cur; int parse(); int digit(){ assert(isdigit(S[cur])); int n=S[cur]-'0'; cur++; return n; } int number(){ int n=digit(); while(cur<S.size()&&isdigit(S[cur])) n=n*10+digit(); return n; } int expression(); int factor(){ if(S[cur]!='(')return number(); cur++; int n=expression(); assert(S[cur]==')'); cur++; return n; } int term1(){ int a=factor(); while(cur<S.size()&&S[cur]=='^'){ cur++; int b=factor(); a=pow(a,b); } return a; } int term2(){ int a=term1(); while(cur<S.size()&&(S[cur]=='*'||S[cur]=='/')){ char op=S[cur++]; int b=term1(); if(op=='*')a*=b; else a/=b; } return a; } int expression(){ int a=term2(); while(cur<S.size()&&(S[cur]=='+'||S[cur]=='-')){ char op=S[cur++]; int b=term2(); if(op=='+')a+=b; else a-=b; } return a; } int main() { int n; cin>>n; while(cin>>S){ cur=0; cout<<expression()<<endl; } } ```
#include<iostream> #include<string> #include<algorithm> #include<map> #include<set> #include<utility> #include<vector> #include<cmath> #include<cstring> #include<cstdio> #include<time.h> #define loop(i,a,b) for(int i=a;i<b;i++) #define rep(i,a) loop(i,0,a) #define pb push_back #define mp make_pair #define all(in) in.begin(),in.end() const double PI=acos(-1); const double EPS=1e-10; const int inf=1e8; using namespace std; typedef long long ll; typedef vector<int> vi; string s; int a; int func();//+- int num(); int func2();//*/ int func3();//() int num(){ int out=0; while(isdigit(s[a])){ out*=10;out+=s[a]-'0'; a++; } return out; } int func3(){ int out; if(s[a]=='('){ a++; out=func(); a++; }else out=num(); return out; } int func2(){ int out=func3(); while(1){ if(s[a]=='*'){ a++; out*=func3(); }else if(s[a]=='/'){ a++; out/=func3(); }else break; } return out; } int func(){ int out=func2(); while(1){ if(s[a]=='+'){ a++;out+=func2(); }else if(s[a]=='-'){ a++;out-=func2(); }else break; } return out; } int main(){ int n; cin>>n; while(n--){ a=0; cin>>s; //s="0+"+s; cout<<func()<<endl; } }
### Prompt Construct a cpp code solution to the problem outlined: Your task is to write a program which reads an expression and evaluates it. * The expression consists of numerical values, operators and parentheses, and the ends with '='. * The operators includes +, - , *, / where respectively represents, addition, subtraction, multiplication and division. * Precedence of the operators is based on usual laws. That is one should perform all multiplication and division first, then addition and subtraction. When two operators have the same precedence, they are applied from left to right. * You may assume that there is no division by zero. * All calculation is performed as integers, and after the decimal point should be truncated * Length of the expression will not exceed 100. * -1 × 109 ≤ intermediate results of computation ≤ 109 Input The input is a sequence of datasets. The first line contains an integer n which represents the number of datasets. There will be n lines where each line contains an expression. Output For each datasets, prints the result of calculation. Example Input 2 4-2*3= 4*(8+4+3)= Output -2 60 ### Response ```cpp #include<iostream> #include<string> #include<algorithm> #include<map> #include<set> #include<utility> #include<vector> #include<cmath> #include<cstring> #include<cstdio> #include<time.h> #define loop(i,a,b) for(int i=a;i<b;i++) #define rep(i,a) loop(i,0,a) #define pb push_back #define mp make_pair #define all(in) in.begin(),in.end() const double PI=acos(-1); const double EPS=1e-10; const int inf=1e8; using namespace std; typedef long long ll; typedef vector<int> vi; string s; int a; int func();//+- int num(); int func2();//*/ int func3();//() int num(){ int out=0; while(isdigit(s[a])){ out*=10;out+=s[a]-'0'; a++; } return out; } int func3(){ int out; if(s[a]=='('){ a++; out=func(); a++; }else out=num(); return out; } int func2(){ int out=func3(); while(1){ if(s[a]=='*'){ a++; out*=func3(); }else if(s[a]=='/'){ a++; out/=func3(); }else break; } return out; } int func(){ int out=func2(); while(1){ if(s[a]=='+'){ a++;out+=func2(); }else if(s[a]=='-'){ a++;out-=func2(); }else break; } return out; } int main(){ int n; cin>>n; while(n--){ a=0; cin>>s; //s="0+"+s; cout<<func()<<endl; } } ```
#include "bits/stdc++.h" #include<unordered_map> #pragma warning(disable:4996) using namespace std; string st; int a; int plmi(); int num() { int n = 0; while (1) { if (isdigit(st[a])) { n *= 10; n += st[a] - '0'; a++; } else { return n; } } } int siki() { int n; if (st[a] == '(') { a++; n=plmi(); a++; } else { n = num(); } return n; } int evdi() { int n = siki(); while (1) { if (st[a] == '*') { a++; n *= siki(); } else if (st[a] == '/') { a++; n /= siki(); } else { break; } } return n; } int plmi() { int n=evdi(); while (1) { if (st[a] == '+') { a++; n += evdi(); } else if (st[a] == '-') { a++; n -= evdi(); } else { break; } } return n; } int main() { int n; cin >> n; for (int i = 0; i < n; ++i) { cin >> st; a = 0; int ans=plmi(); cout << ans << endl; } return 0; }
### Prompt Construct a CPP code solution to the problem outlined: Your task is to write a program which reads an expression and evaluates it. * The expression consists of numerical values, operators and parentheses, and the ends with '='. * The operators includes +, - , *, / where respectively represents, addition, subtraction, multiplication and division. * Precedence of the operators is based on usual laws. That is one should perform all multiplication and division first, then addition and subtraction. When two operators have the same precedence, they are applied from left to right. * You may assume that there is no division by zero. * All calculation is performed as integers, and after the decimal point should be truncated * Length of the expression will not exceed 100. * -1 × 109 ≤ intermediate results of computation ≤ 109 Input The input is a sequence of datasets. The first line contains an integer n which represents the number of datasets. There will be n lines where each line contains an expression. Output For each datasets, prints the result of calculation. Example Input 2 4-2*3= 4*(8+4+3)= Output -2 60 ### Response ```cpp #include "bits/stdc++.h" #include<unordered_map> #pragma warning(disable:4996) using namespace std; string st; int a; int plmi(); int num() { int n = 0; while (1) { if (isdigit(st[a])) { n *= 10; n += st[a] - '0'; a++; } else { return n; } } } int siki() { int n; if (st[a] == '(') { a++; n=plmi(); a++; } else { n = num(); } return n; } int evdi() { int n = siki(); while (1) { if (st[a] == '*') { a++; n *= siki(); } else if (st[a] == '/') { a++; n /= siki(); } else { break; } } return n; } int plmi() { int n=evdi(); while (1) { if (st[a] == '+') { a++; n += evdi(); } else if (st[a] == '-') { a++; n -= evdi(); } else { break; } } return n; } int main() { int n; cin >> n; for (int i = 0; i < n; ++i) { cin >> st; a = 0; int ans=plmi(); cout << ans << endl; } return 0; } ```
#include <cctype> #include <string> #include <iostream> using namespace std; typedef string::const_iterator State; int number(State& it) { int result = 0; while (isdigit(*it)) { result *= 10; result += (*it) - '0'; ++it; } return result; } int expression(State&); int factor(State& it) { if (*it == '(') { ++it; int result = expression(it); ++it; return result; } else return number(it); } int term(State& it) { int result = factor(it); for (;;) { if (*it == '*') { ++it; result *= factor(it); } else if (*it == '/') { ++it; result /= factor(it); } else break; } return result; } int expression(State& it) { int result = term(it); for (;;) { if (*it == '+') { ++it; result += term(it); } else if (*it == '-') { ++it; result -= term(it); } else break; } return result; } int main () { int n; cin >> n; for (int i = 0; i < n; i++) { string line; cin >> line; State it = line.begin(); cout << expression(it) << endl; } return 0; }
### Prompt Please provide a Cpp coded solution to the problem described below: Your task is to write a program which reads an expression and evaluates it. * The expression consists of numerical values, operators and parentheses, and the ends with '='. * The operators includes +, - , *, / where respectively represents, addition, subtraction, multiplication and division. * Precedence of the operators is based on usual laws. That is one should perform all multiplication and division first, then addition and subtraction. When two operators have the same precedence, they are applied from left to right. * You may assume that there is no division by zero. * All calculation is performed as integers, and after the decimal point should be truncated * Length of the expression will not exceed 100. * -1 × 109 ≤ intermediate results of computation ≤ 109 Input The input is a sequence of datasets. The first line contains an integer n which represents the number of datasets. There will be n lines where each line contains an expression. Output For each datasets, prints the result of calculation. Example Input 2 4-2*3= 4*(8+4+3)= Output -2 60 ### Response ```cpp #include <cctype> #include <string> #include <iostream> using namespace std; typedef string::const_iterator State; int number(State& it) { int result = 0; while (isdigit(*it)) { result *= 10; result += (*it) - '0'; ++it; } return result; } int expression(State&); int factor(State& it) { if (*it == '(') { ++it; int result = expression(it); ++it; return result; } else return number(it); } int term(State& it) { int result = factor(it); for (;;) { if (*it == '*') { ++it; result *= factor(it); } else if (*it == '/') { ++it; result /= factor(it); } else break; } return result; } int expression(State& it) { int result = term(it); for (;;) { if (*it == '+') { ++it; result += term(it); } else if (*it == '-') { ++it; result -= term(it); } else break; } return result; } int main () { int n; cin >> n; for (int i = 0; i < n; i++) { string line; cin >> line; State it = line.begin(); cout << expression(it) << endl; } return 0; } ```
#include <string> #include <cctype> #include <iostream> typedef std::string::iterator State; using namespace std; int number(State &begin); int factor(State &begin); int expression(State &begin); int main(void){ int N; std::cin >> N; while(N--){ std::string s; std::cin>>s; State begin = s.begin(); int ans = expression(begin); std::cout << ans << std::endl; } return 0; } int number(State &begin){ int ret = 0; while(isdigit(*begin)){ ret *= 10; ret += *begin - '0'; begin++; } return ret; } int factor(State &begin){ if(*begin == '('){ begin++; int ret = expression(begin); begin++; return ret; }else{ return number(begin); } } int expression(State &begin){ int num = factor(begin); int sum = 0; for(;;){ char ch = *begin; begin++; if(ch == '+'){ sum += num; num = factor(begin); } else if(ch == '-'){ sum += num; num = -factor(begin); }else if(ch == '*'){ num *= factor(begin); }else if(ch == '/'){ num /= factor(begin); }else{ begin--; break; } } return sum+num; }
### Prompt Create a solution in CPP for the following problem: Your task is to write a program which reads an expression and evaluates it. * The expression consists of numerical values, operators and parentheses, and the ends with '='. * The operators includes +, - , *, / where respectively represents, addition, subtraction, multiplication and division. * Precedence of the operators is based on usual laws. That is one should perform all multiplication and division first, then addition and subtraction. When two operators have the same precedence, they are applied from left to right. * You may assume that there is no division by zero. * All calculation is performed as integers, and after the decimal point should be truncated * Length of the expression will not exceed 100. * -1 × 109 ≤ intermediate results of computation ≤ 109 Input The input is a sequence of datasets. The first line contains an integer n which represents the number of datasets. There will be n lines where each line contains an expression. Output For each datasets, prints the result of calculation. Example Input 2 4-2*3= 4*(8+4+3)= Output -2 60 ### Response ```cpp #include <string> #include <cctype> #include <iostream> typedef std::string::iterator State; using namespace std; int number(State &begin); int factor(State &begin); int expression(State &begin); int main(void){ int N; std::cin >> N; while(N--){ std::string s; std::cin>>s; State begin = s.begin(); int ans = expression(begin); std::cout << ans << std::endl; } return 0; } int number(State &begin){ int ret = 0; while(isdigit(*begin)){ ret *= 10; ret += *begin - '0'; begin++; } return ret; } int factor(State &begin){ if(*begin == '('){ begin++; int ret = expression(begin); begin++; return ret; }else{ return number(begin); } } int expression(State &begin){ int num = factor(begin); int sum = 0; for(;;){ char ch = *begin; begin++; if(ch == '+'){ sum += num; num = factor(begin); } else if(ch == '-'){ sum += num; num = -factor(begin); }else if(ch == '*'){ num *= factor(begin); }else if(ch == '/'){ num /= factor(begin); }else{ begin--; break; } } return sum+num; } ```
#include <iostream> #include <cctype> /** memo * expr ::= term { ( '+' | '-' ) term } * term ::= fact { ( '*' | '/' ) fact } * fact ::= '(' expr ')' | digit **/ using namespace std; int expr(); int term(); int fact(); char buf[101]; int p; int expr() { int v = term(); while(buf[p] == '+' || buf[p] == '-') { if(buf[p] == '+') { p++; v += term(); } else { p++; v -= term(); } } return v; } int term() { int v = fact(); while(buf[p] == '*' || buf[p] == '/') { if(buf[p] == '*') { p++; v *= fact(); } else { p++; v /= fact(); } } return v; } int fact() { int v = 0; if(buf[p] == '(') { p++; v = expr(); p++; // skip ')' } else { while(isdigit(buf[p])) { v = v * 10 + buf[p] - '0'; p++; } } return v; } int main() { int times; int ans; cin >> times; for(int i = 0; i < times; i++) { cin >> buf; p = 0; ans = expr(); cout << ans << endl; } return 0; }
### Prompt Please formulate a CPP solution to the following problem: Your task is to write a program which reads an expression and evaluates it. * The expression consists of numerical values, operators and parentheses, and the ends with '='. * The operators includes +, - , *, / where respectively represents, addition, subtraction, multiplication and division. * Precedence of the operators is based on usual laws. That is one should perform all multiplication and division first, then addition and subtraction. When two operators have the same precedence, they are applied from left to right. * You may assume that there is no division by zero. * All calculation is performed as integers, and after the decimal point should be truncated * Length of the expression will not exceed 100. * -1 × 109 ≤ intermediate results of computation ≤ 109 Input The input is a sequence of datasets. The first line contains an integer n which represents the number of datasets. There will be n lines where each line contains an expression. Output For each datasets, prints the result of calculation. Example Input 2 4-2*3= 4*(8+4+3)= Output -2 60 ### Response ```cpp #include <iostream> #include <cctype> /** memo * expr ::= term { ( '+' | '-' ) term } * term ::= fact { ( '*' | '/' ) fact } * fact ::= '(' expr ')' | digit **/ using namespace std; int expr(); int term(); int fact(); char buf[101]; int p; int expr() { int v = term(); while(buf[p] == '+' || buf[p] == '-') { if(buf[p] == '+') { p++; v += term(); } else { p++; v -= term(); } } return v; } int term() { int v = fact(); while(buf[p] == '*' || buf[p] == '/') { if(buf[p] == '*') { p++; v *= fact(); } else { p++; v /= fact(); } } return v; } int fact() { int v = 0; if(buf[p] == '(') { p++; v = expr(); p++; // skip ')' } else { while(isdigit(buf[p])) { v = v * 10 + buf[p] - '0'; p++; } } return v; } int main() { int times; int ans; cin >> times; for(int i = 0; i < times; i++) { cin >> buf; p = 0; ans = expr(); cout << ans << endl; } return 0; } ```
#include <bits/stdc++.h> using namespace std; class Calc{ private: int idx; string s; public: Calc(string s) : s(s) { idx = 0; } int fact(){ int res = 0; if(s[idx] == '('){ idx++; res = exp(); idx++; }else{ while(isdigit(s[idx])){ res *= 10; res += s[idx++]-'0'; } } return res; } int term(){ int res = fact(); while(s[idx] == '*' || s[idx] == '/'){ if(s[idx] == '*'){ idx++; res *= fact(); }else{ idx++; res /= fact(); } } return res; } int exp(){ int res = term(); while(s[idx] == '+' || s[idx] == '-'){ if(s[idx] == '+'){ idx++; res += term(); }else{ idx++; res -= term(); } } return res; } }; int main(){ int Tc; string s; cin >> Tc; while(Tc--){ cin >> s; s.resize(s.size()-1); Calc c(s); cout << c.exp() << endl; } return 0; }
### Prompt Generate a Cpp solution to the following problem: Your task is to write a program which reads an expression and evaluates it. * The expression consists of numerical values, operators and parentheses, and the ends with '='. * The operators includes +, - , *, / where respectively represents, addition, subtraction, multiplication and division. * Precedence of the operators is based on usual laws. That is one should perform all multiplication and division first, then addition and subtraction. When two operators have the same precedence, they are applied from left to right. * You may assume that there is no division by zero. * All calculation is performed as integers, and after the decimal point should be truncated * Length of the expression will not exceed 100. * -1 × 109 ≤ intermediate results of computation ≤ 109 Input The input is a sequence of datasets. The first line contains an integer n which represents the number of datasets. There will be n lines where each line contains an expression. Output For each datasets, prints the result of calculation. Example Input 2 4-2*3= 4*(8+4+3)= Output -2 60 ### Response ```cpp #include <bits/stdc++.h> using namespace std; class Calc{ private: int idx; string s; public: Calc(string s) : s(s) { idx = 0; } int fact(){ int res = 0; if(s[idx] == '('){ idx++; res = exp(); idx++; }else{ while(isdigit(s[idx])){ res *= 10; res += s[idx++]-'0'; } } return res; } int term(){ int res = fact(); while(s[idx] == '*' || s[idx] == '/'){ if(s[idx] == '*'){ idx++; res *= fact(); }else{ idx++; res /= fact(); } } return res; } int exp(){ int res = term(); while(s[idx] == '+' || s[idx] == '-'){ if(s[idx] == '+'){ idx++; res += term(); }else{ idx++; res -= term(); } } return res; } }; int main(){ int Tc; string s; cin >> Tc; while(Tc--){ cin >> s; s.resize(s.size()-1); Calc c(s); cout << c.exp() << endl; } return 0; } ```
#include <bits/stdc++.h> #define int long long using namespace std; // exp1 = exp2 [ ('+'|'-') exp2 ]* // exp2 = exp3 [ ('*'|'/') exp3 ]* // exp3 = '(' exp1 ')' | number int exp1(const string& s, int& i); int exp2(const string& s, int& i); int exp3(const string& s, int& i); int number(const string& s, int& i); int exp1(const string& s, int& i) { int acc = exp2(s, i); while (1) { if (s[i] == '+') acc += exp2(s, ++i); else if (s[i] == '-') acc -= exp2(s, ++i); else return acc; } } int exp2(const string& s, int& i) { int acc = exp3(s, i); while (1) { if (s[i] == '*') acc *= exp3(s, ++i); else if (s[i] == '/') acc /= exp3(s, ++i); else return acc; } } int exp3(const string& s, int& i) { if (s[i] == '(') { int ret = exp1(s, ++i); assert(s[i] == ')'); i++; return ret; } return number(s, i); } int number(const string& s, int& i) { int acc = 0; while (isdigit(s[i])) { acc = acc * 10 + (s[i++] - '0'); } return acc; } void solve() { string s; cin >> s; int i = 0; cout << exp1(s, i) << endl; assert(s[i] == '='); } signed main() { int T; cin >> T; while (T--) solve(); }
### Prompt Construct a Cpp code solution to the problem outlined: Your task is to write a program which reads an expression and evaluates it. * The expression consists of numerical values, operators and parentheses, and the ends with '='. * The operators includes +, - , *, / where respectively represents, addition, subtraction, multiplication and division. * Precedence of the operators is based on usual laws. That is one should perform all multiplication and division first, then addition and subtraction. When two operators have the same precedence, they are applied from left to right. * You may assume that there is no division by zero. * All calculation is performed as integers, and after the decimal point should be truncated * Length of the expression will not exceed 100. * -1 × 109 ≤ intermediate results of computation ≤ 109 Input The input is a sequence of datasets. The first line contains an integer n which represents the number of datasets. There will be n lines where each line contains an expression. Output For each datasets, prints the result of calculation. Example Input 2 4-2*3= 4*(8+4+3)= Output -2 60 ### Response ```cpp #include <bits/stdc++.h> #define int long long using namespace std; // exp1 = exp2 [ ('+'|'-') exp2 ]* // exp2 = exp3 [ ('*'|'/') exp3 ]* // exp3 = '(' exp1 ')' | number int exp1(const string& s, int& i); int exp2(const string& s, int& i); int exp3(const string& s, int& i); int number(const string& s, int& i); int exp1(const string& s, int& i) { int acc = exp2(s, i); while (1) { if (s[i] == '+') acc += exp2(s, ++i); else if (s[i] == '-') acc -= exp2(s, ++i); else return acc; } } int exp2(const string& s, int& i) { int acc = exp3(s, i); while (1) { if (s[i] == '*') acc *= exp3(s, ++i); else if (s[i] == '/') acc /= exp3(s, ++i); else return acc; } } int exp3(const string& s, int& i) { if (s[i] == '(') { int ret = exp1(s, ++i); assert(s[i] == ')'); i++; return ret; } return number(s, i); } int number(const string& s, int& i) { int acc = 0; while (isdigit(s[i])) { acc = acc * 10 + (s[i++] - '0'); } return acc; } void solve() { string s; cin >> s; int i = 0; cout << exp1(s, i) << endl; assert(s[i] == '='); } signed main() { int T; cin >> T; while (T--) solve(); } ```
#include <iostream> #include <string> #include <cctype> using namespace std; int number(string::const_iterator &c){//number int ret = 0; while(isdigit(*c)){ ret*=10; ret+=*c-'0'; c++; } return ret; } int expression(string::const_iterator&); int factor(string::const_iterator &c) { int ret; if (*c=='(') { c++; ret = expression(c); c++; }else return number(c); return ret; } int term(string::const_iterator &c){//*or/ int ret = factor(c); for(;;){ if(*c=='*'){ c++; ret*=factor(c); }else if(*c=='/'){ c++; ret/=factor(c); }else break; } return ret; } int expression(string::const_iterator &c){//all int ret = term(c); for(;;){ if(*c=='+'){ c++; ret+=term(c); }else if(*c=='-'){ c++; ret-=term(c); }else break; } return ret; } int main(){ int n; cin>>n; cin.ignore(); while(n--){ string s; getline(cin,s); string::const_iterator c=s.begin(); cout<<expression(c)<<"\n"; } return 0; }
### Prompt Please create a solution in cpp to the following problem: Your task is to write a program which reads an expression and evaluates it. * The expression consists of numerical values, operators and parentheses, and the ends with '='. * The operators includes +, - , *, / where respectively represents, addition, subtraction, multiplication and division. * Precedence of the operators is based on usual laws. That is one should perform all multiplication and division first, then addition and subtraction. When two operators have the same precedence, they are applied from left to right. * You may assume that there is no division by zero. * All calculation is performed as integers, and after the decimal point should be truncated * Length of the expression will not exceed 100. * -1 × 109 ≤ intermediate results of computation ≤ 109 Input The input is a sequence of datasets. The first line contains an integer n which represents the number of datasets. There will be n lines where each line contains an expression. Output For each datasets, prints the result of calculation. Example Input 2 4-2*3= 4*(8+4+3)= Output -2 60 ### Response ```cpp #include <iostream> #include <string> #include <cctype> using namespace std; int number(string::const_iterator &c){//number int ret = 0; while(isdigit(*c)){ ret*=10; ret+=*c-'0'; c++; } return ret; } int expression(string::const_iterator&); int factor(string::const_iterator &c) { int ret; if (*c=='(') { c++; ret = expression(c); c++; }else return number(c); return ret; } int term(string::const_iterator &c){//*or/ int ret = factor(c); for(;;){ if(*c=='*'){ c++; ret*=factor(c); }else if(*c=='/'){ c++; ret/=factor(c); }else break; } return ret; } int expression(string::const_iterator &c){//all int ret = term(c); for(;;){ if(*c=='+'){ c++; ret+=term(c); }else if(*c=='-'){ c++; ret-=term(c); }else break; } return ret; } int main(){ int n; cin>>n; cin.ignore(); while(n--){ string s; getline(cin,s); string::const_iterator c=s.begin(); cout<<expression(c)<<"\n"; } return 0; } ```
#include <bits/stdc++.h> using namespace std; int Num(); int Fact(); int Term(); int Exp(); int ite; string s; int Num() { int res = 0; while (isdigit(s[ite])) { res = res * 10 + s[ite] - '0'; ite++; } return res; } int Fact() { if (s[ite] == '(') { ite++; int res = Exp(); ite++; return res; } else { return Num(); } } int Term() { int res = Fact(); while (true) { if (s[ite] == '*') { ite++; res *= Fact(); } else if (s[ite] == '/') { ite++; res /= Fact(); } else { break; } } return res; } int Exp() { int res = Term(); while (s[ite] != '=' && s[ite] != ')') { if (s[ite] == '+') { ite++; res += Term(); } else if(s[ite] == '-') { ite++; res -= Term(); } } return res; } int main() { int n; cin >> n; while (n--) { cin >> s; ite = 0; cout << Exp() << endl; } return 0; }
### Prompt Please create a solution in CPP to the following problem: Your task is to write a program which reads an expression and evaluates it. * The expression consists of numerical values, operators and parentheses, and the ends with '='. * The operators includes +, - , *, / where respectively represents, addition, subtraction, multiplication and division. * Precedence of the operators is based on usual laws. That is one should perform all multiplication and division first, then addition and subtraction. When two operators have the same precedence, they are applied from left to right. * You may assume that there is no division by zero. * All calculation is performed as integers, and after the decimal point should be truncated * Length of the expression will not exceed 100. * -1 × 109 ≤ intermediate results of computation ≤ 109 Input The input is a sequence of datasets. The first line contains an integer n which represents the number of datasets. There will be n lines where each line contains an expression. Output For each datasets, prints the result of calculation. Example Input 2 4-2*3= 4*(8+4+3)= Output -2 60 ### Response ```cpp #include <bits/stdc++.h> using namespace std; int Num(); int Fact(); int Term(); int Exp(); int ite; string s; int Num() { int res = 0; while (isdigit(s[ite])) { res = res * 10 + s[ite] - '0'; ite++; } return res; } int Fact() { if (s[ite] == '(') { ite++; int res = Exp(); ite++; return res; } else { return Num(); } } int Term() { int res = Fact(); while (true) { if (s[ite] == '*') { ite++; res *= Fact(); } else if (s[ite] == '/') { ite++; res /= Fact(); } else { break; } } return res; } int Exp() { int res = Term(); while (s[ite] != '=' && s[ite] != ')') { if (s[ite] == '+') { ite++; res += Term(); } else if(s[ite] == '-') { ite++; res -= Term(); } } return res; } int main() { int n; cin >> n; while (n--) { cin >> s; ite = 0; cout << Exp() << endl; } return 0; } ```
#include <bits/stdc++.h> #define P int using namespace std; string s; int p; P bns1(); P g_A(){ P r=0; if(s[p]=='('){ p++; r=bns1(); p++; } else{ while(isdigit(s[p])){ r=r*10+(s[p]-'0'); p++; } } return r; } P bns2(){ P res=g_A(); while(s[p]=='*'||s[p]=='/'){ int t=p++; if(s[t]=='*')res*=g_A(); if(s[t]=='/')res/=g_A(); } return res; } P bns1(){ P res=bns2(); while(s[p]=='+'||s[p]=='-'){ int t=p++; if(s[t]=='+')res+=bns2(); if(s[t]=='-')res-=bns2(); } return res; } int main(){ int ans,n; cin>>n; while(n--){ cin>>s; p=0; P ans=bns1(); cout<<ans<<endl; } }
### Prompt Construct a cpp code solution to the problem outlined: Your task is to write a program which reads an expression and evaluates it. * The expression consists of numerical values, operators and parentheses, and the ends with '='. * The operators includes +, - , *, / where respectively represents, addition, subtraction, multiplication and division. * Precedence of the operators is based on usual laws. That is one should perform all multiplication and division first, then addition and subtraction. When two operators have the same precedence, they are applied from left to right. * You may assume that there is no division by zero. * All calculation is performed as integers, and after the decimal point should be truncated * Length of the expression will not exceed 100. * -1 × 109 ≤ intermediate results of computation ≤ 109 Input The input is a sequence of datasets. The first line contains an integer n which represents the number of datasets. There will be n lines where each line contains an expression. Output For each datasets, prints the result of calculation. Example Input 2 4-2*3= 4*(8+4+3)= Output -2 60 ### Response ```cpp #include <bits/stdc++.h> #define P int using namespace std; string s; int p; P bns1(); P g_A(){ P r=0; if(s[p]=='('){ p++; r=bns1(); p++; } else{ while(isdigit(s[p])){ r=r*10+(s[p]-'0'); p++; } } return r; } P bns2(){ P res=g_A(); while(s[p]=='*'||s[p]=='/'){ int t=p++; if(s[t]=='*')res*=g_A(); if(s[t]=='/')res/=g_A(); } return res; } P bns1(){ P res=bns2(); while(s[p]=='+'||s[p]=='-'){ int t=p++; if(s[t]=='+')res+=bns2(); if(s[t]=='-')res-=bns2(); } return res; } int main(){ int ans,n; cin>>n; while(n--){ cin>>s; p=0; P ans=bns1(); cout<<ans<<endl; } } ```
#include<iostream> #include<cassert> #include<string> #include<cctype> using namespace std; string S; size_t cur = 0; int parse(); int digit(); int number(); int expression(); int term(); int factor(); int main() { int n; cin >> n; for (int i = 0; i < n; i++) { cin >> S; cur = 0; S.resize(S.size() - 1); cout << expression() << endl; assert(cur == S.size()); } return 0; } int digit() { assert(isdigit(S[cur])); int n = S[cur] - '0'; cur++; return n; } int number() { int n = digit(); while (cur < S.size() && isdigit(S[cur])) n = n * 10 + digit(); return n; } int expression() { int a = term(); while (cur < S.size() && (S[cur] == '+' || S[cur] == '-')) { char op = S[cur++]; int b = term(); if (op == '+') a += b; else a -= b; } return a; } int parse() { return expression(); } int term() { int a = factor(); while (cur < S.size() && (S[cur] == '*' || S[cur] == '/')) { char op = S[cur++]; int b = factor(); if (op == '*') a *= b; else a /= b; } return a; } int factor() { if (S[cur] != '(') return number(); cur++; int n = expression(); assert(S[cur] == ')'); cur++; return n; }
### Prompt Your challenge is to write a cpp solution to the following problem: Your task is to write a program which reads an expression and evaluates it. * The expression consists of numerical values, operators and parentheses, and the ends with '='. * The operators includes +, - , *, / where respectively represents, addition, subtraction, multiplication and division. * Precedence of the operators is based on usual laws. That is one should perform all multiplication and division first, then addition and subtraction. When two operators have the same precedence, they are applied from left to right. * You may assume that there is no division by zero. * All calculation is performed as integers, and after the decimal point should be truncated * Length of the expression will not exceed 100. * -1 × 109 ≤ intermediate results of computation ≤ 109 Input The input is a sequence of datasets. The first line contains an integer n which represents the number of datasets. There will be n lines where each line contains an expression. Output For each datasets, prints the result of calculation. Example Input 2 4-2*3= 4*(8+4+3)= Output -2 60 ### Response ```cpp #include<iostream> #include<cassert> #include<string> #include<cctype> using namespace std; string S; size_t cur = 0; int parse(); int digit(); int number(); int expression(); int term(); int factor(); int main() { int n; cin >> n; for (int i = 0; i < n; i++) { cin >> S; cur = 0; S.resize(S.size() - 1); cout << expression() << endl; assert(cur == S.size()); } return 0; } int digit() { assert(isdigit(S[cur])); int n = S[cur] - '0'; cur++; return n; } int number() { int n = digit(); while (cur < S.size() && isdigit(S[cur])) n = n * 10 + digit(); return n; } int expression() { int a = term(); while (cur < S.size() && (S[cur] == '+' || S[cur] == '-')) { char op = S[cur++]; int b = term(); if (op == '+') a += b; else a -= b; } return a; } int parse() { return expression(); } int term() { int a = factor(); while (cur < S.size() && (S[cur] == '*' || S[cur] == '/')) { char op = S[cur++]; int b = factor(); if (op == '*') a *= b; else a /= b; } return a; } int factor() { if (S[cur] != '(') return number(); cur++; int n = expression(); assert(S[cur] == ')'); cur++; return n; } ```
#include <bits/stdc++.h> #define REP(i, n) for (int i = 0; i < (int)(n); i++) using namespace std; string s; int p; int expr(); int num() { int res = 0; while (p < s.size() && '0' <= s[p] && s[p] <= '9') { res *= 10; res += s[p] - '0'; p++; } return res; } int factor() { int res = 0; if (s[p] == '(') { p++; res = expr(); p++; } else { res = num(); } return res; } int term() { int res = factor(); while (p < s.size()) { if (s[p] == '*') { p++; res *= factor(); } else if (s[p] == '/') { p++; res /= factor(); } else { break; } } return res; } int expr() { int res = term(); while (p < s.size()) { if (s[p] == '+') { p++; res += term(); } else if (s[p] == '-') { p++; res -= term(); } else { break; } } return res; } int main() { int n; cin >> n; REP(i, n) { cin >> s; p = 0; cout << expr() << endl; } }
### Prompt Please create a solution in Cpp to the following problem: Your task is to write a program which reads an expression and evaluates it. * The expression consists of numerical values, operators and parentheses, and the ends with '='. * The operators includes +, - , *, / where respectively represents, addition, subtraction, multiplication and division. * Precedence of the operators is based on usual laws. That is one should perform all multiplication and division first, then addition and subtraction. When two operators have the same precedence, they are applied from left to right. * You may assume that there is no division by zero. * All calculation is performed as integers, and after the decimal point should be truncated * Length of the expression will not exceed 100. * -1 × 109 ≤ intermediate results of computation ≤ 109 Input The input is a sequence of datasets. The first line contains an integer n which represents the number of datasets. There will be n lines where each line contains an expression. Output For each datasets, prints the result of calculation. Example Input 2 4-2*3= 4*(8+4+3)= Output -2 60 ### Response ```cpp #include <bits/stdc++.h> #define REP(i, n) for (int i = 0; i < (int)(n); i++) using namespace std; string s; int p; int expr(); int num() { int res = 0; while (p < s.size() && '0' <= s[p] && s[p] <= '9') { res *= 10; res += s[p] - '0'; p++; } return res; } int factor() { int res = 0; if (s[p] == '(') { p++; res = expr(); p++; } else { res = num(); } return res; } int term() { int res = factor(); while (p < s.size()) { if (s[p] == '*') { p++; res *= factor(); } else if (s[p] == '/') { p++; res /= factor(); } else { break; } } return res; } int expr() { int res = term(); while (p < s.size()) { if (s[p] == '+') { p++; res += term(); } else if (s[p] == '-') { p++; res -= term(); } else { break; } } return res; } int main() { int n; cin >> n; REP(i, n) { cin >> s; p = 0; cout << expr() << endl; } } ```
#include <bits/stdc++.h> using namespace std; #define REP(i,n) for(int i=0;i<(int)(n);i++) typedef long long ll; char s[125]; int it; int dfs(){ int ret = 0; int tail = 0; int sign = 1; // first operand int a = 0; if(s[it]=='('){ it++; a = dfs(); assert(s[it]==')'); it++; }else{ while(isdigit(s[it])){ a *= 10; a += s[it]-'0'; it++; } } tail = a; while(true){ // operator char op = s[it]; if(op == '=' || op == ')'){ // end ret += sign * tail; return ret; } it++; // second operand int b = 0; if(s[it]=='('){ it++; b = dfs(); assert(s[it]==')'); it++; }else{ while(isdigit(s[it])){ b *= 10; b += s[it]-'0'; it++; } } if(op == '+' || op == '-'){ // tail end ret += sign * tail; tail = b; sign = op=='+' ? 1 : -1; }else if(op == '*'){ tail *= b; }else if(op == '/'){ tail /= b; } } } void solve(){ REP(i,125)s[i] = '\0'; scanf("%s",s); it = 0; int ans = dfs(); printf("%d\n",ans); } int main(){ int t; scanf("%d",&t); while(t--)solve(); }
### Prompt Construct a Cpp code solution to the problem outlined: Your task is to write a program which reads an expression and evaluates it. * The expression consists of numerical values, operators and parentheses, and the ends with '='. * The operators includes +, - , *, / where respectively represents, addition, subtraction, multiplication and division. * Precedence of the operators is based on usual laws. That is one should perform all multiplication and division first, then addition and subtraction. When two operators have the same precedence, they are applied from left to right. * You may assume that there is no division by zero. * All calculation is performed as integers, and after the decimal point should be truncated * Length of the expression will not exceed 100. * -1 × 109 ≤ intermediate results of computation ≤ 109 Input The input is a sequence of datasets. The first line contains an integer n which represents the number of datasets. There will be n lines where each line contains an expression. Output For each datasets, prints the result of calculation. Example Input 2 4-2*3= 4*(8+4+3)= Output -2 60 ### Response ```cpp #include <bits/stdc++.h> using namespace std; #define REP(i,n) for(int i=0;i<(int)(n);i++) typedef long long ll; char s[125]; int it; int dfs(){ int ret = 0; int tail = 0; int sign = 1; // first operand int a = 0; if(s[it]=='('){ it++; a = dfs(); assert(s[it]==')'); it++; }else{ while(isdigit(s[it])){ a *= 10; a += s[it]-'0'; it++; } } tail = a; while(true){ // operator char op = s[it]; if(op == '=' || op == ')'){ // end ret += sign * tail; return ret; } it++; // second operand int b = 0; if(s[it]=='('){ it++; b = dfs(); assert(s[it]==')'); it++; }else{ while(isdigit(s[it])){ b *= 10; b += s[it]-'0'; it++; } } if(op == '+' || op == '-'){ // tail end ret += sign * tail; tail = b; sign = op=='+' ? 1 : -1; }else if(op == '*'){ tail *= b; }else if(op == '/'){ tail /= b; } } } void solve(){ REP(i,125)s[i] = '\0'; scanf("%s",s); it = 0; int ans = dfs(); printf("%d\n",ans); } int main(){ int t; scanf("%d",&t); while(t--)solve(); } ```
#include <iostream> #include <string> #include <cctype> using namespace std; typedef string::const_iterator State; int expression(State &begin); int number(State &begin) { int ret = 0; for(; isdigit(*begin); begin++) { ret = ret * 10 + *begin - '0'; } return ret; } int factor(State &begin) { if(*begin == '(') { begin++; int ret = expression(begin); begin++; return ret; } else { return number(begin); } } int term(State &begin) { int ret = factor(begin); while(true) { if (*begin == '*') { begin++; ret *= factor(begin); } else if (*begin == '/') { begin++; ret /= factor(begin); } else { break; } } return ret; } int expression(State &begin) { int ret = term(begin); while(true) { if (*begin == '+') { begin++; ret += term(begin); } else if (*begin == '-') { begin++; ret -= term(begin); } else { break; } } return ret; } int main() { int N; cin >> N; cin.ignore(); for (int i = 0; i < N; i++) { string s; getline(cin, s); State begin = s.begin(); cout << expression(begin) << endl; } return 0; }
### Prompt Construct a cpp code solution to the problem outlined: Your task is to write a program which reads an expression and evaluates it. * The expression consists of numerical values, operators and parentheses, and the ends with '='. * The operators includes +, - , *, / where respectively represents, addition, subtraction, multiplication and division. * Precedence of the operators is based on usual laws. That is one should perform all multiplication and division first, then addition and subtraction. When two operators have the same precedence, they are applied from left to right. * You may assume that there is no division by zero. * All calculation is performed as integers, and after the decimal point should be truncated * Length of the expression will not exceed 100. * -1 × 109 ≤ intermediate results of computation ≤ 109 Input The input is a sequence of datasets. The first line contains an integer n which represents the number of datasets. There will be n lines where each line contains an expression. Output For each datasets, prints the result of calculation. Example Input 2 4-2*3= 4*(8+4+3)= Output -2 60 ### Response ```cpp #include <iostream> #include <string> #include <cctype> using namespace std; typedef string::const_iterator State; int expression(State &begin); int number(State &begin) { int ret = 0; for(; isdigit(*begin); begin++) { ret = ret * 10 + *begin - '0'; } return ret; } int factor(State &begin) { if(*begin == '(') { begin++; int ret = expression(begin); begin++; return ret; } else { return number(begin); } } int term(State &begin) { int ret = factor(begin); while(true) { if (*begin == '*') { begin++; ret *= factor(begin); } else if (*begin == '/') { begin++; ret /= factor(begin); } else { break; } } return ret; } int expression(State &begin) { int ret = term(begin); while(true) { if (*begin == '+') { begin++; ret += term(begin); } else if (*begin == '-') { begin++; ret -= term(begin); } else { break; } } return ret; } int main() { int N; cin >> N; cin.ignore(); for (int i = 0; i < N; i++) { string s; getline(cin, s); State begin = s.begin(); cout << expression(begin) << endl; } return 0; } ```
#include <bits/stdc++.h> using namespace std; typedef long long ll; string s; int N; int c; int expr(); int term(); int factor(); int num(); void debug(string text){ return; cout << text + " "; for(int i = 0; i < N; i++){ if(i == c) cout << '[' << s[i] << ']'; else cout << s[i]; } cout << endl; } int expr(){ debug("expr"); int x = term(); while(c < N && (s[c] == '+' || s[c] == '-')){ if(s[c] == '+'){ c++; x += term(); } else{ c++; x -= term(); } } return x; } int term(){ debug("term"); int x = factor(); while(c < N && (s[c] == '*' || s[c] == '/')){ if(s[c] == '*'){ c++; x *= factor(); } else{ c++; x /= factor(); } } return x; } int factor(){ debug("factor"); if(s[c] == '('){ c++; int ret = expr(); assert(s[c] == ')'); c++; return ret; } return num(); } int num(){ debug("num"); int ret = 0; while(c < N && isdigit(s[c])){ ret = ret * 10 + s[c] - '0'; c++; } return ret; } int main(){ cin.tie(0); ios::sync_with_stdio(false); #ifdef LOCAL std::ifstream in("in"); std::cin.rdbuf(in.rdbuf()); #endif int n; cin >> n; while(n--){ cin >> s; s.pop_back(); N = s.size(); c = 0; cout << expr() << endl; } }
### Prompt Develop a solution in cpp to the problem described below: Your task is to write a program which reads an expression and evaluates it. * The expression consists of numerical values, operators and parentheses, and the ends with '='. * The operators includes +, - , *, / where respectively represents, addition, subtraction, multiplication and division. * Precedence of the operators is based on usual laws. That is one should perform all multiplication and division first, then addition and subtraction. When two operators have the same precedence, they are applied from left to right. * You may assume that there is no division by zero. * All calculation is performed as integers, and after the decimal point should be truncated * Length of the expression will not exceed 100. * -1 × 109 ≤ intermediate results of computation ≤ 109 Input The input is a sequence of datasets. The first line contains an integer n which represents the number of datasets. There will be n lines where each line contains an expression. Output For each datasets, prints the result of calculation. Example Input 2 4-2*3= 4*(8+4+3)= Output -2 60 ### Response ```cpp #include <bits/stdc++.h> using namespace std; typedef long long ll; string s; int N; int c; int expr(); int term(); int factor(); int num(); void debug(string text){ return; cout << text + " "; for(int i = 0; i < N; i++){ if(i == c) cout << '[' << s[i] << ']'; else cout << s[i]; } cout << endl; } int expr(){ debug("expr"); int x = term(); while(c < N && (s[c] == '+' || s[c] == '-')){ if(s[c] == '+'){ c++; x += term(); } else{ c++; x -= term(); } } return x; } int term(){ debug("term"); int x = factor(); while(c < N && (s[c] == '*' || s[c] == '/')){ if(s[c] == '*'){ c++; x *= factor(); } else{ c++; x /= factor(); } } return x; } int factor(){ debug("factor"); if(s[c] == '('){ c++; int ret = expr(); assert(s[c] == ')'); c++; return ret; } return num(); } int num(){ debug("num"); int ret = 0; while(c < N && isdigit(s[c])){ ret = ret * 10 + s[c] - '0'; c++; } return ret; } int main(){ cin.tie(0); ios::sync_with_stdio(false); #ifdef LOCAL std::ifstream in("in"); std::cin.rdbuf(in.rdbuf()); #endif int n; cin >> n; while(n--){ cin >> s; s.pop_back(); N = s.size(); c = 0; cout << expr() << endl; } } ```
#include<iostream> #include<vector> #include<algorithm> #include<string> using namespace std; int expression(); int term(); int factor(); int number(); int now; string s; int expression(){ int res=term(); while(true){ if(s[now]=='+')now++,res+=term(); else if(s[now]=='-')now++,res-=term(); else break; } return res; } int term(){ int res=factor(); while(true){ if(s[now]=='(')res+=factor(); else if(s[now]=='*')now++,res*=factor(); else if(s[now]=='/')now++,res/=factor(); else break; } return res; } int factor(){ int res=0; if(s[now]=='('){ now++; res=expression(); now++; } else return number(); return res; } int number(){ int res=0; while('0'<=s[now] && s[now]<='9'){ res*=10; res+=s[now++]-'0'; } return res; } int main(void){ int n; cin >> n; while(n--){ cin >> s; now=0; cout << expression() << endl; } return 0; }
### Prompt Please formulate a cpp solution to the following problem: Your task is to write a program which reads an expression and evaluates it. * The expression consists of numerical values, operators and parentheses, and the ends with '='. * The operators includes +, - , *, / where respectively represents, addition, subtraction, multiplication and division. * Precedence of the operators is based on usual laws. That is one should perform all multiplication and division first, then addition and subtraction. When two operators have the same precedence, they are applied from left to right. * You may assume that there is no division by zero. * All calculation is performed as integers, and after the decimal point should be truncated * Length of the expression will not exceed 100. * -1 × 109 ≤ intermediate results of computation ≤ 109 Input The input is a sequence of datasets. The first line contains an integer n which represents the number of datasets. There will be n lines where each line contains an expression. Output For each datasets, prints the result of calculation. Example Input 2 4-2*3= 4*(8+4+3)= Output -2 60 ### Response ```cpp #include<iostream> #include<vector> #include<algorithm> #include<string> using namespace std; int expression(); int term(); int factor(); int number(); int now; string s; int expression(){ int res=term(); while(true){ if(s[now]=='+')now++,res+=term(); else if(s[now]=='-')now++,res-=term(); else break; } return res; } int term(){ int res=factor(); while(true){ if(s[now]=='(')res+=factor(); else if(s[now]=='*')now++,res*=factor(); else if(s[now]=='/')now++,res/=factor(); else break; } return res; } int factor(){ int res=0; if(s[now]=='('){ now++; res=expression(); now++; } else return number(); return res; } int number(){ int res=0; while('0'<=s[now] && s[now]<='9'){ res*=10; res+=s[now++]-'0'; } return res; } int main(void){ int n; cin >> n; while(n--){ cin >> s; now=0; cout << expression() << endl; } return 0; } ```
#include<string> #include<cstdlib> #include<iostream> using namespace std; int prior[128]; long long parse(string s) { if(s[0]=='(' && s[s.length()-1]==')'){ bool f=true; for(int i=1,d=1;i<s.length()-1;i++){ if (s[i]=='(') d++; else if(s[i]==')') d--; if(d==0){ f=false; break; } } if(f) return parse(s.substr(1,s.length()-2)); } int divprr=3,divpos=10000000; for(int i=s.length()-1,d=0;i>=1;i--){ if (s[i]==')') d--; else if(s[i]=='(') d++; else if(d==0 && prior[s[i]]!=0){ if(divprr>prior[s[i]]) divprr=prior[s[i]],divpos=i; } } if(divpos==10000000) return atoll(s.c_str()); switch(s[divpos]){ case '+': return parse(s.substr(0,divpos)) + parse(s.substr(divpos+1)); case '-': return parse(s.substr(0,divpos)) - parse(s.substr(divpos+1)); case '*': return parse(s.substr(0,divpos)) * parse(s.substr(divpos+1)); case '/': return parse(s.substr(0,divpos)) / parse(s.substr(divpos+1)); } while(1); } int main() { prior['+']=prior['-']=1; prior['*']=prior['/']=2; int n; cin>>n; while(n--){ string s; cin>>s; s=s.substr(0,s.length()-1); cout<<parse(s)<<endl; } return 0; }
### Prompt Please create a solution in CPP to the following problem: Your task is to write a program which reads an expression and evaluates it. * The expression consists of numerical values, operators and parentheses, and the ends with '='. * The operators includes +, - , *, / where respectively represents, addition, subtraction, multiplication and division. * Precedence of the operators is based on usual laws. That is one should perform all multiplication and division first, then addition and subtraction. When two operators have the same precedence, they are applied from left to right. * You may assume that there is no division by zero. * All calculation is performed as integers, and after the decimal point should be truncated * Length of the expression will not exceed 100. * -1 × 109 ≤ intermediate results of computation ≤ 109 Input The input is a sequence of datasets. The first line contains an integer n which represents the number of datasets. There will be n lines where each line contains an expression. Output For each datasets, prints the result of calculation. Example Input 2 4-2*3= 4*(8+4+3)= Output -2 60 ### Response ```cpp #include<string> #include<cstdlib> #include<iostream> using namespace std; int prior[128]; long long parse(string s) { if(s[0]=='(' && s[s.length()-1]==')'){ bool f=true; for(int i=1,d=1;i<s.length()-1;i++){ if (s[i]=='(') d++; else if(s[i]==')') d--; if(d==0){ f=false; break; } } if(f) return parse(s.substr(1,s.length()-2)); } int divprr=3,divpos=10000000; for(int i=s.length()-1,d=0;i>=1;i--){ if (s[i]==')') d--; else if(s[i]=='(') d++; else if(d==0 && prior[s[i]]!=0){ if(divprr>prior[s[i]]) divprr=prior[s[i]],divpos=i; } } if(divpos==10000000) return atoll(s.c_str()); switch(s[divpos]){ case '+': return parse(s.substr(0,divpos)) + parse(s.substr(divpos+1)); case '-': return parse(s.substr(0,divpos)) - parse(s.substr(divpos+1)); case '*': return parse(s.substr(0,divpos)) * parse(s.substr(divpos+1)); case '/': return parse(s.substr(0,divpos)) / parse(s.substr(divpos+1)); } while(1); } int main() { prior['+']=prior['-']=1; prior['*']=prior['/']=2; int n; cin>>n; while(n--){ string s; cin>>s; s=s.substr(0,s.length()-1); cout<<parse(s)<<endl; } return 0; } ```
#include <bits/stdc++.h> using namespace std; #define rep(i,a,b) for(int i=a;i<b;i++) bool match(char c, string s) { rep(i, 0, s.length()) if (s[i] == c) return true; return false; } string op[2] = { "+-", "*/" }; int parse(string s, int &i, int x) { if (x == 2) { if (s[i] == '(') { i++; int ans = parse(s, i, 0); i++; return ans; } else { int ret = 0; while ('0' <= s[i] && s[i] <= '9') { ret = ret * 10 + s[i] - '0'; i++; } return ret; } } else { int ans = parse(s, i, x + 1); while (op[x].find(s[i]) != string::npos) { switch (s[i]) { case '+': i++; ans += parse(s, i, x + 1); break; case '-': i++; ans -= parse(s, i, x + 1); break; case '*': i++; ans *= parse(s, i, x + 1); break; case '/': i++; ans /= parse(s, i, x + 1); break; } } return ans; } } int main() { int n; cin >> n; rep(_n, 0, n) { string s; cin >> s; int i = 0; cout << parse(s, i, 0) << endl; } }
### Prompt Create a solution in cpp for the following problem: Your task is to write a program which reads an expression and evaluates it. * The expression consists of numerical values, operators and parentheses, and the ends with '='. * The operators includes +, - , *, / where respectively represents, addition, subtraction, multiplication and division. * Precedence of the operators is based on usual laws. That is one should perform all multiplication and division first, then addition and subtraction. When two operators have the same precedence, they are applied from left to right. * You may assume that there is no division by zero. * All calculation is performed as integers, and after the decimal point should be truncated * Length of the expression will not exceed 100. * -1 × 109 ≤ intermediate results of computation ≤ 109 Input The input is a sequence of datasets. The first line contains an integer n which represents the number of datasets. There will be n lines where each line contains an expression. Output For each datasets, prints the result of calculation. Example Input 2 4-2*3= 4*(8+4+3)= Output -2 60 ### Response ```cpp #include <bits/stdc++.h> using namespace std; #define rep(i,a,b) for(int i=a;i<b;i++) bool match(char c, string s) { rep(i, 0, s.length()) if (s[i] == c) return true; return false; } string op[2] = { "+-", "*/" }; int parse(string s, int &i, int x) { if (x == 2) { if (s[i] == '(') { i++; int ans = parse(s, i, 0); i++; return ans; } else { int ret = 0; while ('0' <= s[i] && s[i] <= '9') { ret = ret * 10 + s[i] - '0'; i++; } return ret; } } else { int ans = parse(s, i, x + 1); while (op[x].find(s[i]) != string::npos) { switch (s[i]) { case '+': i++; ans += parse(s, i, x + 1); break; case '-': i++; ans -= parse(s, i, x + 1); break; case '*': i++; ans *= parse(s, i, x + 1); break; case '/': i++; ans /= parse(s, i, x + 1); break; } } return ans; } } int main() { int n; cin >> n; rep(_n, 0, n) { string s; cin >> s; int i = 0; cout << parse(s, i, 0) << endl; } } ```
#include <iostream> #include <string> using namespace std; int exp(); int term(); int factor(); int number(); int digit(); string str; int p,len,val; int exp(){ int val = term(); while( str[p] == '+' || str[p] == '-' ){ if( str[p] == '+' ){ p++; val += term(); }else if( str[p] == '-' ){ p++; val -= term(); } } return val; } int term(){ int val=factor(); while( str[p] == '*' || str[p] == '/' ){ if( str[p] == '*' ){ p++; val *= factor(); }else if( str[p] == '/' ){ p++; val /= factor(); } } return val; } int factor(){ int val; if( str[p] == '(' ){ p++; val = exp(); p++; }else{ val = number(); } return val; } int number(){ int val = 0; while( digit() ){ int num = str[p] - '0'; val = val * 10 + num; p++; } return val; } int digit(){ if( str[p] == '1' || str[p] == '2' || str[p] == '3' || str[p] == '4' || str[p] == '5' || str[p] == '6' || str[p] == '7' || str[p] == '8' || str[p] == '9' || str[p] == '0' ) return 1; return 0; } int main(){ int n; cin >> n; for(int i = 0; i < n; i++ ){ p = 0; cin >> str; int ans = exp(); cout << ans << endl; } return 0; }
### Prompt Create a solution in cpp for the following problem: Your task is to write a program which reads an expression and evaluates it. * The expression consists of numerical values, operators and parentheses, and the ends with '='. * The operators includes +, - , *, / where respectively represents, addition, subtraction, multiplication and division. * Precedence of the operators is based on usual laws. That is one should perform all multiplication and division first, then addition and subtraction. When two operators have the same precedence, they are applied from left to right. * You may assume that there is no division by zero. * All calculation is performed as integers, and after the decimal point should be truncated * Length of the expression will not exceed 100. * -1 × 109 ≤ intermediate results of computation ≤ 109 Input The input is a sequence of datasets. The first line contains an integer n which represents the number of datasets. There will be n lines where each line contains an expression. Output For each datasets, prints the result of calculation. Example Input 2 4-2*3= 4*(8+4+3)= Output -2 60 ### Response ```cpp #include <iostream> #include <string> using namespace std; int exp(); int term(); int factor(); int number(); int digit(); string str; int p,len,val; int exp(){ int val = term(); while( str[p] == '+' || str[p] == '-' ){ if( str[p] == '+' ){ p++; val += term(); }else if( str[p] == '-' ){ p++; val -= term(); } } return val; } int term(){ int val=factor(); while( str[p] == '*' || str[p] == '/' ){ if( str[p] == '*' ){ p++; val *= factor(); }else if( str[p] == '/' ){ p++; val /= factor(); } } return val; } int factor(){ int val; if( str[p] == '(' ){ p++; val = exp(); p++; }else{ val = number(); } return val; } int number(){ int val = 0; while( digit() ){ int num = str[p] - '0'; val = val * 10 + num; p++; } return val; } int digit(){ if( str[p] == '1' || str[p] == '2' || str[p] == '3' || str[p] == '4' || str[p] == '5' || str[p] == '6' || str[p] == '7' || str[p] == '8' || str[p] == '9' || str[p] == '0' ) return 1; return 0; } int main(){ int n; cin >> n; for(int i = 0; i < n; i++ ){ p = 0; cin >> str; int ans = exp(); cout << ans << endl; } return 0; } ```
#include<bits/stdc++.h> using namespace std; int p=0; int formula(string& s); int num(string& s){ int res=0; int op=1; if(s[p]=='-'){ op=-1; p++; } else if(s[p]=='+'){ op=1; p++; } while(isdigit(s[p])){ res*=10; res+=s[p]-'0'; p++; } return op*res; } int term(string & s){ if(s[p]=='('){ p++; int res=formula(s); p++; return res; } return num(s); } int section(string& s){ int res=term(s); while(p!=s.size() && s[p]!=')' && s[p]!='+' && s[p]!='-'){ char op=s[p]; p++; int next=term(s); if(op=='*') res*=next; else res/=next; } return res; } int formula(string& s){ int res=section(s); while(p!=s.size() && s[p]!=')'){ char op=s[p]; p++; int next=section(s); if(op=='+') res+=next; else res-=next; } return res; } int main(){ int n; cin>>n; for(int i=0;i<n;i++){ string s; cin>>s; s.pop_back(); p=0; cout<<formula(s)<<endl; } return 0; }
### Prompt Generate a CPP solution to the following problem: Your task is to write a program which reads an expression and evaluates it. * The expression consists of numerical values, operators and parentheses, and the ends with '='. * The operators includes +, - , *, / where respectively represents, addition, subtraction, multiplication and division. * Precedence of the operators is based on usual laws. That is one should perform all multiplication and division first, then addition and subtraction. When two operators have the same precedence, they are applied from left to right. * You may assume that there is no division by zero. * All calculation is performed as integers, and after the decimal point should be truncated * Length of the expression will not exceed 100. * -1 × 109 ≤ intermediate results of computation ≤ 109 Input The input is a sequence of datasets. The first line contains an integer n which represents the number of datasets. There will be n lines where each line contains an expression. Output For each datasets, prints the result of calculation. Example Input 2 4-2*3= 4*(8+4+3)= Output -2 60 ### Response ```cpp #include<bits/stdc++.h> using namespace std; int p=0; int formula(string& s); int num(string& s){ int res=0; int op=1; if(s[p]=='-'){ op=-1; p++; } else if(s[p]=='+'){ op=1; p++; } while(isdigit(s[p])){ res*=10; res+=s[p]-'0'; p++; } return op*res; } int term(string & s){ if(s[p]=='('){ p++; int res=formula(s); p++; return res; } return num(s); } int section(string& s){ int res=term(s); while(p!=s.size() && s[p]!=')' && s[p]!='+' && s[p]!='-'){ char op=s[p]; p++; int next=term(s); if(op=='*') res*=next; else res/=next; } return res; } int formula(string& s){ int res=section(s); while(p!=s.size() && s[p]!=')'){ char op=s[p]; p++; int next=section(s); if(op=='+') res+=next; else res-=next; } return res; } int main(){ int n; cin>>n; for(int i=0;i<n;i++){ string s; cin>>s; s.pop_back(); p=0; cout<<formula(s)<<endl; } return 0; } ```
#include<iostream> #include<stack> #include<string> #include <sstream> using namespace std; string s; int idx; int term(); int expr(); int fact(); int number(); // <term> = <fact> { (‘*’|‘/’) <fact> } int term(){ int res = fact(); while(s[idx] == '*' || s[idx] == '/'){ char op = s[idx++]; int tmp = fact(); if(op == '*') res *= tmp; else res /= tmp; } return res; } // <expr> = <term> { (‘+’|‘-’) <term> } int expr(){ int res = term(); while(s[idx] == '+' || s[idx] == '-'){ char op = s[idx++]; int tmp = term(); if(op == '+') res += tmp; else res -= tmp; } return res; } // <number> = { <digit> } // <digit> = ‘0’ | ‘1’ | ... | ‘9’ int number(){ int res = 0; while('0' <= s[idx] && s[idx] <= '9'){ res = res*10 + s[idx] - '0'; idx++; } return res; } // <fact> = ‘(’ <expr> ‘)’ | <number> int fact(){ int res; if(s[idx] == '('){ idx++; res = expr(); //assert( s[idx] == ')' ); idx++; } else{ res = number(); } return res; } int main() { int n; cin>>n; for(;n--;) { idx=0; cin>>s; cout << expr()<<endl; } }
### Prompt Your challenge is to write a CPP solution to the following problem: Your task is to write a program which reads an expression and evaluates it. * The expression consists of numerical values, operators and parentheses, and the ends with '='. * The operators includes +, - , *, / where respectively represents, addition, subtraction, multiplication and division. * Precedence of the operators is based on usual laws. That is one should perform all multiplication and division first, then addition and subtraction. When two operators have the same precedence, they are applied from left to right. * You may assume that there is no division by zero. * All calculation is performed as integers, and after the decimal point should be truncated * Length of the expression will not exceed 100. * -1 × 109 ≤ intermediate results of computation ≤ 109 Input The input is a sequence of datasets. The first line contains an integer n which represents the number of datasets. There will be n lines where each line contains an expression. Output For each datasets, prints the result of calculation. Example Input 2 4-2*3= 4*(8+4+3)= Output -2 60 ### Response ```cpp #include<iostream> #include<stack> #include<string> #include <sstream> using namespace std; string s; int idx; int term(); int expr(); int fact(); int number(); // <term> = <fact> { (‘*’|‘/’) <fact> } int term(){ int res = fact(); while(s[idx] == '*' || s[idx] == '/'){ char op = s[idx++]; int tmp = fact(); if(op == '*') res *= tmp; else res /= tmp; } return res; } // <expr> = <term> { (‘+’|‘-’) <term> } int expr(){ int res = term(); while(s[idx] == '+' || s[idx] == '-'){ char op = s[idx++]; int tmp = term(); if(op == '+') res += tmp; else res -= tmp; } return res; } // <number> = { <digit> } // <digit> = ‘0’ | ‘1’ | ... | ‘9’ int number(){ int res = 0; while('0' <= s[idx] && s[idx] <= '9'){ res = res*10 + s[idx] - '0'; idx++; } return res; } // <fact> = ‘(’ <expr> ‘)’ | <number> int fact(){ int res; if(s[idx] == '('){ idx++; res = expr(); //assert( s[idx] == ')' ); idx++; } else{ res = number(); } return res; } int main() { int n; cin>>n; for(;n--;) { idx=0; cin>>s; cout << expr()<<endl; } } ```
#include<bits/stdc++.h> using namespace std; char* p; int number(); int term(); int expression(); int program(); int main(){ int n; cin>>n; char x[100]; for(int i=0;i<n;i++){ scanf("%s",x); p=x; cout<<expression()<<endl; } return 0; } int program(){ int num; if(*p=='('){ p++; num=expression(); p++; }else num=number(); return num; } int number(){ int num=0; while('0'<=*p &&*p<='9'){ num*=10; num+=(*p-'0'); p++; } return num; } int expression(){ int num=term(); while(1){ if(*p=='+'){ p++; num+=term(); }else if(*p=='-'){ p++; num-=term(); }else break; } return num; } int term(){ int num=program(); while(1){ if(*p=='*'){ p++; num*=program(); }else if(*p=='/'){ p++; num/=program(); }else break; } return num; }
### Prompt Please formulate a CPP solution to the following problem: Your task is to write a program which reads an expression and evaluates it. * The expression consists of numerical values, operators and parentheses, and the ends with '='. * The operators includes +, - , *, / where respectively represents, addition, subtraction, multiplication and division. * Precedence of the operators is based on usual laws. That is one should perform all multiplication and division first, then addition and subtraction. When two operators have the same precedence, they are applied from left to right. * You may assume that there is no division by zero. * All calculation is performed as integers, and after the decimal point should be truncated * Length of the expression will not exceed 100. * -1 × 109 ≤ intermediate results of computation ≤ 109 Input The input is a sequence of datasets. The first line contains an integer n which represents the number of datasets. There will be n lines where each line contains an expression. Output For each datasets, prints the result of calculation. Example Input 2 4-2*3= 4*(8+4+3)= Output -2 60 ### Response ```cpp #include<bits/stdc++.h> using namespace std; char* p; int number(); int term(); int expression(); int program(); int main(){ int n; cin>>n; char x[100]; for(int i=0;i<n;i++){ scanf("%s",x); p=x; cout<<expression()<<endl; } return 0; } int program(){ int num; if(*p=='('){ p++; num=expression(); p++; }else num=number(); return num; } int number(){ int num=0; while('0'<=*p &&*p<='9'){ num*=10; num+=(*p-'0'); p++; } return num; } int expression(){ int num=term(); while(1){ if(*p=='+'){ p++; num+=term(); }else if(*p=='-'){ p++; num-=term(); }else break; } return num; } int term(){ int num=program(); while(1){ if(*p=='*'){ p++; num*=program(); }else if(*p=='/'){ p++; num/=program(); }else break; } return num; } ```
#include <iostream> #include <string> #include <cctype> #define rep(i,n) for(int i=0;i<n;++i) using namespace std; string src; int pos; bool end() { return pos >= src.length(); } char peek() { return src[pos]; } void succ() { if (!end()) ++pos; } int parse_expr(); int lex_number() { int val = 0; char c = peek(); do { val = 10 * val + (c - '0'); succ(); } while (!end() && isdigit(c = peek())); return val; } int parse_primary() { int ret; char c = peek(); switch (c) { case '(': succ(); ret = parse_expr(); succ(); break; default: ret = lex_number(); break; } return ret; } int parse_term() { int x = parse_primary(); while (!end() && (peek() == '*' || peek() == '/')) { char op = peek(); succ(); int y = parse_primary(); x = (op == '*' ? x * y : x / y); } return x; } int parse_expr() { int x = parse_term(); while (!end() && (peek() == '+' || peek() == '-')) { char op = peek(); succ(); int y = parse_term(); x = (op == '+' ? x + y : x - y); } return x; } int parse() { pos = 0; return parse_expr(); } int main() { int N; cin>>N; while(N--) { cin >> src; cout << parse() << endl; } return 0; }
### Prompt Your challenge is to write a cpp solution to the following problem: Your task is to write a program which reads an expression and evaluates it. * The expression consists of numerical values, operators and parentheses, and the ends with '='. * The operators includes +, - , *, / where respectively represents, addition, subtraction, multiplication and division. * Precedence of the operators is based on usual laws. That is one should perform all multiplication and division first, then addition and subtraction. When two operators have the same precedence, they are applied from left to right. * You may assume that there is no division by zero. * All calculation is performed as integers, and after the decimal point should be truncated * Length of the expression will not exceed 100. * -1 × 109 ≤ intermediate results of computation ≤ 109 Input The input is a sequence of datasets. The first line contains an integer n which represents the number of datasets. There will be n lines where each line contains an expression. Output For each datasets, prints the result of calculation. Example Input 2 4-2*3= 4*(8+4+3)= Output -2 60 ### Response ```cpp #include <iostream> #include <string> #include <cctype> #define rep(i,n) for(int i=0;i<n;++i) using namespace std; string src; int pos; bool end() { return pos >= src.length(); } char peek() { return src[pos]; } void succ() { if (!end()) ++pos; } int parse_expr(); int lex_number() { int val = 0; char c = peek(); do { val = 10 * val + (c - '0'); succ(); } while (!end() && isdigit(c = peek())); return val; } int parse_primary() { int ret; char c = peek(); switch (c) { case '(': succ(); ret = parse_expr(); succ(); break; default: ret = lex_number(); break; } return ret; } int parse_term() { int x = parse_primary(); while (!end() && (peek() == '*' || peek() == '/')) { char op = peek(); succ(); int y = parse_primary(); x = (op == '*' ? x * y : x / y); } return x; } int parse_expr() { int x = parse_term(); while (!end() && (peek() == '+' || peek() == '-')) { char op = peek(); succ(); int y = parse_term(); x = (op == '+' ? x + y : x - y); } return x; } int parse() { pos = 0; return parse_expr(); } int main() { int N; cin>>N; while(N--) { cin >> src; cout << parse() << endl; } return 0; } ```
#include<iostream> #include<string> #include<cstddef> #include <cassert> #include<cctype> using namespace std; string S; size_t cur=0; int n; void init_S(){ cur=0; } char readchar(){ assert(cur<S.size()); char ret=S[cur]; cur+=1; return ret; } char peek(){ return S[cur]; } int digit(){ assert(isdigit(peek())); int n=readchar() - '0'; return n; } int number(){ int n=digit(); while(cur<S.size()&&isdigit(peek())) n=n*10+digit(); return n; } int factor(); int term(){ int sum=factor(); while(cur<S.size()&&(peek()=='*'||peek()=='/')){ char op=readchar(); int b=factor(); if(op=='*')sum*=b; else sum/=b; } return sum; } int b; int expression(){ int sum=term(); while(cur<S.size() && (peek()=='+'||peek()=='-')){ char op=readchar(); int b=term(); if(op=='+')sum+=b; else sum-=b; } return sum; } int factor(){ if(peek()!='('){ return number(); } cur++; int n=expression(); assert(peek()==')'); cur++; return n; } int parse(){ return expression(); } int main(){ cin>>n; for(int i=0;i<n;i++){ cin>>S; int a=parse(); cout<<a<<endl; S.clear(); init_S(); } return 0; }
### Prompt Generate a cpp solution to the following problem: Your task is to write a program which reads an expression and evaluates it. * The expression consists of numerical values, operators and parentheses, and the ends with '='. * The operators includes +, - , *, / where respectively represents, addition, subtraction, multiplication and division. * Precedence of the operators is based on usual laws. That is one should perform all multiplication and division first, then addition and subtraction. When two operators have the same precedence, they are applied from left to right. * You may assume that there is no division by zero. * All calculation is performed as integers, and after the decimal point should be truncated * Length of the expression will not exceed 100. * -1 × 109 ≤ intermediate results of computation ≤ 109 Input The input is a sequence of datasets. The first line contains an integer n which represents the number of datasets. There will be n lines where each line contains an expression. Output For each datasets, prints the result of calculation. Example Input 2 4-2*3= 4*(8+4+3)= Output -2 60 ### Response ```cpp #include<iostream> #include<string> #include<cstddef> #include <cassert> #include<cctype> using namespace std; string S; size_t cur=0; int n; void init_S(){ cur=0; } char readchar(){ assert(cur<S.size()); char ret=S[cur]; cur+=1; return ret; } char peek(){ return S[cur]; } int digit(){ assert(isdigit(peek())); int n=readchar() - '0'; return n; } int number(){ int n=digit(); while(cur<S.size()&&isdigit(peek())) n=n*10+digit(); return n; } int factor(); int term(){ int sum=factor(); while(cur<S.size()&&(peek()=='*'||peek()=='/')){ char op=readchar(); int b=factor(); if(op=='*')sum*=b; else sum/=b; } return sum; } int b; int expression(){ int sum=term(); while(cur<S.size() && (peek()=='+'||peek()=='-')){ char op=readchar(); int b=term(); if(op=='+')sum+=b; else sum-=b; } return sum; } int factor(){ if(peek()!='('){ return number(); } cur++; int n=expression(); assert(peek()==')'); cur++; return n; } int parse(){ return expression(); } int main(){ cin>>n; for(int i=0;i<n;i++){ cin>>S; int a=parse(); cout<<a<<endl; S.clear(); init_S(); } return 0; } ```
#include<iostream> #include<string> #include<sstream> using namespace std; string in; int calc(int l,int r) { int pa=0; for(int i = r; i >= l; --i) { if(in[i]==')') pa++; else if(in[i]=='(') pa--; else if(!pa) { if(in[i]=='+') return calc(l,i-1)+calc(i+1,r); if(in[i]=='-') return calc(l,i-1)-calc(i+1,r); } } pa=0; for(int i = r; i >= l; --i) { if(in[i]==')') pa++; else if(in[i]=='(') pa--; else if(!pa) { if(in[i]=='*') return calc(l,i-1)*calc(i+1,r); if(in[i]=='/') return calc(l,i-1)/calc(i+1,r); } } if(in[l]=='('&&in[r]==')') return calc(l+1,r-1); stringstream ss(in.substr(l,r-l+1)); int res; ss>>res; return res; } int main() { int n; cin>>n; while(n--) { cin>>in; cout<<calc(0,in.size()-2)<<endl; } return 0; }
### Prompt Please provide a Cpp coded solution to the problem described below: Your task is to write a program which reads an expression and evaluates it. * The expression consists of numerical values, operators and parentheses, and the ends with '='. * The operators includes +, - , *, / where respectively represents, addition, subtraction, multiplication and division. * Precedence of the operators is based on usual laws. That is one should perform all multiplication and division first, then addition and subtraction. When two operators have the same precedence, they are applied from left to right. * You may assume that there is no division by zero. * All calculation is performed as integers, and after the decimal point should be truncated * Length of the expression will not exceed 100. * -1 × 109 ≤ intermediate results of computation ≤ 109 Input The input is a sequence of datasets. The first line contains an integer n which represents the number of datasets. There will be n lines where each line contains an expression. Output For each datasets, prints the result of calculation. Example Input 2 4-2*3= 4*(8+4+3)= Output -2 60 ### Response ```cpp #include<iostream> #include<string> #include<sstream> using namespace std; string in; int calc(int l,int r) { int pa=0; for(int i = r; i >= l; --i) { if(in[i]==')') pa++; else if(in[i]=='(') pa--; else if(!pa) { if(in[i]=='+') return calc(l,i-1)+calc(i+1,r); if(in[i]=='-') return calc(l,i-1)-calc(i+1,r); } } pa=0; for(int i = r; i >= l; --i) { if(in[i]==')') pa++; else if(in[i]=='(') pa--; else if(!pa) { if(in[i]=='*') return calc(l,i-1)*calc(i+1,r); if(in[i]=='/') return calc(l,i-1)/calc(i+1,r); } } if(in[l]=='('&&in[r]==')') return calc(l+1,r-1); stringstream ss(in.substr(l,r-l+1)); int res; ss>>res; return res; } int main() { int n; cin>>n; while(n--) { cin>>in; cout<<calc(0,in.size()-2)<<endl; } return 0; } ```
#include <iostream> #include <cstdlib> #include <string> using namespace std; int pos; string str; int exp(); int factor() { if(str[pos] >= '0' && str[pos] <= '9') { string tmp = ""; while(str[pos] >= '0' && str[pos] <= '9') { tmp += str[pos++]; } return atoi(tmp.c_str()); } if(str[pos] == '(') { pos++; int tmp = exp(); pos++; return tmp; } } int term() { int x = factor(); while(str[pos] == '*' || str[pos] == '/') { if(str[pos] == '*') { pos++; x *= factor(); } else if(str[pos] == '/') { pos++; x /= factor(); } } return x; } int exp() { int x = term(); while(str[pos] == '+' || str[pos] == '-') { if(str[pos] == '+') { pos++; x += term(); } else if(str[pos] == '-') { pos++; x -= term(); } } return x; } int main() { int n; cin >> n; for(int i = 0; i < n; i++) { pos = 0; cin >> str; str = str.substr(0, str.length()-1); cout << exp() << endl; } }
### Prompt Your challenge is to write a Cpp solution to the following problem: Your task is to write a program which reads an expression and evaluates it. * The expression consists of numerical values, operators and parentheses, and the ends with '='. * The operators includes +, - , *, / where respectively represents, addition, subtraction, multiplication and division. * Precedence of the operators is based on usual laws. That is one should perform all multiplication and division first, then addition and subtraction. When two operators have the same precedence, they are applied from left to right. * You may assume that there is no division by zero. * All calculation is performed as integers, and after the decimal point should be truncated * Length of the expression will not exceed 100. * -1 × 109 ≤ intermediate results of computation ≤ 109 Input The input is a sequence of datasets. The first line contains an integer n which represents the number of datasets. There will be n lines where each line contains an expression. Output For each datasets, prints the result of calculation. Example Input 2 4-2*3= 4*(8+4+3)= Output -2 60 ### Response ```cpp #include <iostream> #include <cstdlib> #include <string> using namespace std; int pos; string str; int exp(); int factor() { if(str[pos] >= '0' && str[pos] <= '9') { string tmp = ""; while(str[pos] >= '0' && str[pos] <= '9') { tmp += str[pos++]; } return atoi(tmp.c_str()); } if(str[pos] == '(') { pos++; int tmp = exp(); pos++; return tmp; } } int term() { int x = factor(); while(str[pos] == '*' || str[pos] == '/') { if(str[pos] == '*') { pos++; x *= factor(); } else if(str[pos] == '/') { pos++; x /= factor(); } } return x; } int exp() { int x = term(); while(str[pos] == '+' || str[pos] == '-') { if(str[pos] == '+') { pos++; x += term(); } else if(str[pos] == '-') { pos++; x -= term(); } } return x; } int main() { int n; cin >> n; for(int i = 0; i < n; i++) { pos = 0; cin >> str; str = str.substr(0, str.length()-1); cout << exp() << endl; } } ```
#include<iostream> #include <string> #include <cctype> using namespace std; typedef string::const_iterator State; class ParseError {}; int expression(State& base); int number(State& base){ int ret=0; while(isdigit(*base)){ ret=ret*10+(*base-'0'); base++; } return ret; } int factor(State& base){ int ret; if(*base=='('){ base++; ret=expression(base); base++; return ret; } else return number(base); } int term(State& base){ int ret=factor(base); while(1){ if(*base=='*'){ base++; ret*=factor(base); } else if(*base=='/'){ base++; ret/=factor(base); } else break; } return ret; } int expression(State& base){ int ret=term(base); while(1){ if(*base=='+'){ base++; ret+=term(base); } else if(*base=='-'){ base++; ret-=term(base); } else break; } return ret; } int main(){ int n; cin>>n; while(n--){ string str; cin>>str; State base = str.begin(); cout<<expression(base)<<endl; } }
### Prompt Please create a solution in cpp to the following problem: Your task is to write a program which reads an expression and evaluates it. * The expression consists of numerical values, operators and parentheses, and the ends with '='. * The operators includes +, - , *, / where respectively represents, addition, subtraction, multiplication and division. * Precedence of the operators is based on usual laws. That is one should perform all multiplication and division first, then addition and subtraction. When two operators have the same precedence, they are applied from left to right. * You may assume that there is no division by zero. * All calculation is performed as integers, and after the decimal point should be truncated * Length of the expression will not exceed 100. * -1 × 109 ≤ intermediate results of computation ≤ 109 Input The input is a sequence of datasets. The first line contains an integer n which represents the number of datasets. There will be n lines where each line contains an expression. Output For each datasets, prints the result of calculation. Example Input 2 4-2*3= 4*(8+4+3)= Output -2 60 ### Response ```cpp #include<iostream> #include <string> #include <cctype> using namespace std; typedef string::const_iterator State; class ParseError {}; int expression(State& base); int number(State& base){ int ret=0; while(isdigit(*base)){ ret=ret*10+(*base-'0'); base++; } return ret; } int factor(State& base){ int ret; if(*base=='('){ base++; ret=expression(base); base++; return ret; } else return number(base); } int term(State& base){ int ret=factor(base); while(1){ if(*base=='*'){ base++; ret*=factor(base); } else if(*base=='/'){ base++; ret/=factor(base); } else break; } return ret; } int expression(State& base){ int ret=term(base); while(1){ if(*base=='+'){ base++; ret+=term(base); } else if(*base=='-'){ base++; ret-=term(base); } else break; } return ret; } int main(){ int n; cin>>n; while(n--){ string str; cin>>str; State base = str.begin(); cout<<expression(base)<<endl; } } ```
#include<bits/stdc++.h> using namespace std; #define int long long 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()&&isdigit(s[p])) res=res*10+s[p++]-'0'; return res; } signed main(){ int n; cin>>n; while(n--){ string s; int p=0; cin>>s; s.pop_back(); cout<<expression(s,p)<<endl; } return 0; }
### Prompt Please provide a cpp coded solution to the problem described below: Your task is to write a program which reads an expression and evaluates it. * The expression consists of numerical values, operators and parentheses, and the ends with '='. * The operators includes +, - , *, / where respectively represents, addition, subtraction, multiplication and division. * Precedence of the operators is based on usual laws. That is one should perform all multiplication and division first, then addition and subtraction. When two operators have the same precedence, they are applied from left to right. * You may assume that there is no division by zero. * All calculation is performed as integers, and after the decimal point should be truncated * Length of the expression will not exceed 100. * -1 × 109 ≤ intermediate results of computation ≤ 109 Input The input is a sequence of datasets. The first line contains an integer n which represents the number of datasets. There will be n lines where each line contains an expression. Output For each datasets, prints the result of calculation. Example Input 2 4-2*3= 4*(8+4+3)= Output -2 60 ### Response ```cpp #include<bits/stdc++.h> using namespace std; #define int long long 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()&&isdigit(s[p])) res=res*10+s[p++]-'0'; return res; } signed main(){ int n; cin>>n; while(n--){ string s; int p=0; cin>>s; s.pop_back(); cout<<expression(s,p)<<endl; } return 0; } ```
#include <iostream> #include <cassert> #include <string> using namespace std; string S; size_t cur=0; int term(); int expression(); int digit(){ assert(isdigit(S[cur])); int n = S[cur] - '0'; cur++; return n; } int number(){ int n=digit(); while(cur <S.size() && isdigit(S[cur])) n=n*10+digit(); return n; } int factor() { if (S[cur] != '(') return number(); cur += 1; int n = expression(); assert(S[cur] == ')'); cur += 1; return n; } int term(){ int sum = factor(); while(S[cur] == '*' || S[cur] == '/'){ char op = S[cur]; cur += 1; int b = factor(); if( op == '*') sum*=b; else { assert(b); sum/=b; } } return sum; } int expression(){ int sum = term(); while(S[cur] == '+' || S[cur] == '-'){ char op = S[cur]; cur += 1; int b = term(); if( op == '+') sum+=b; else sum-=b; } return sum; } int main(void){ int n; cin >> n; for(int i=0;i<n;i++){ cin >> S; cur=0; int r; r=expression(); cout << r << endl; } return 0; }
### Prompt Construct a CPP code solution to the problem outlined: Your task is to write a program which reads an expression and evaluates it. * The expression consists of numerical values, operators and parentheses, and the ends with '='. * The operators includes +, - , *, / where respectively represents, addition, subtraction, multiplication and division. * Precedence of the operators is based on usual laws. That is one should perform all multiplication and division first, then addition and subtraction. When two operators have the same precedence, they are applied from left to right. * You may assume that there is no division by zero. * All calculation is performed as integers, and after the decimal point should be truncated * Length of the expression will not exceed 100. * -1 × 109 ≤ intermediate results of computation ≤ 109 Input The input is a sequence of datasets. The first line contains an integer n which represents the number of datasets. There will be n lines where each line contains an expression. Output For each datasets, prints the result of calculation. Example Input 2 4-2*3= 4*(8+4+3)= Output -2 60 ### Response ```cpp #include <iostream> #include <cassert> #include <string> using namespace std; string S; size_t cur=0; int term(); int expression(); int digit(){ assert(isdigit(S[cur])); int n = S[cur] - '0'; cur++; return n; } int number(){ int n=digit(); while(cur <S.size() && isdigit(S[cur])) n=n*10+digit(); return n; } int factor() { if (S[cur] != '(') return number(); cur += 1; int n = expression(); assert(S[cur] == ')'); cur += 1; return n; } int term(){ int sum = factor(); while(S[cur] == '*' || S[cur] == '/'){ char op = S[cur]; cur += 1; int b = factor(); if( op == '*') sum*=b; else { assert(b); sum/=b; } } return sum; } int expression(){ int sum = term(); while(S[cur] == '+' || S[cur] == '-'){ char op = S[cur]; cur += 1; int b = term(); if( op == '+') sum+=b; else sum-=b; } return sum; } int main(void){ int n; cin >> n; for(int i=0;i<n;i++){ cin >> S; cur=0; int r; r=expression(); cout << r << endl; } return 0; } ```
#include <iostream> #include <string> using namespace std; string str; int p; int exp(); int factor(); int term(); int factor() { int num=0; while('0' <= str[p] && str[p] <= '9') { num*=10; num += str[p] - '0'; p++; } if (str[p] == '('){ p++; num = exp(); p++; } return num; } int term(){ int val = factor(); while(str[p] == '*' || str[p] == '/') { if(str[p] == '*') { p++; val *= factor(); } else if(str[p] == '/'){ p++; val /= factor(); } } return val; } int exp() { int val = term(); while(str[p] == '+' || str[p] == '-') { if(str[p] == '+') { p++; val += term(); } else if(str[p] == '-'){ p++; val -= term(); } } return val; } int main() { int n; cin >> n; for(int i=0;i<n;i++) { p = 0; cin >> str; cout << exp() << endl; } return 0; }
### Prompt Your task is to create a CPP solution to the following problem: Your task is to write a program which reads an expression and evaluates it. * The expression consists of numerical values, operators and parentheses, and the ends with '='. * The operators includes +, - , *, / where respectively represents, addition, subtraction, multiplication and division. * Precedence of the operators is based on usual laws. That is one should perform all multiplication and division first, then addition and subtraction. When two operators have the same precedence, they are applied from left to right. * You may assume that there is no division by zero. * All calculation is performed as integers, and after the decimal point should be truncated * Length of the expression will not exceed 100. * -1 × 109 ≤ intermediate results of computation ≤ 109 Input The input is a sequence of datasets. The first line contains an integer n which represents the number of datasets. There will be n lines where each line contains an expression. Output For each datasets, prints the result of calculation. Example Input 2 4-2*3= 4*(8+4+3)= Output -2 60 ### Response ```cpp #include <iostream> #include <string> using namespace std; string str; int p; int exp(); int factor(); int term(); int factor() { int num=0; while('0' <= str[p] && str[p] <= '9') { num*=10; num += str[p] - '0'; p++; } if (str[p] == '('){ p++; num = exp(); p++; } return num; } int term(){ int val = factor(); while(str[p] == '*' || str[p] == '/') { if(str[p] == '*') { p++; val *= factor(); } else if(str[p] == '/'){ p++; val /= factor(); } } return val; } int exp() { int val = term(); while(str[p] == '+' || str[p] == '-') { if(str[p] == '+') { p++; val += term(); } else if(str[p] == '-'){ p++; val -= term(); } } return val; } int main() { int n; cin >> n; for(int i=0;i<n;i++) { p = 0; cin >> str; cout << exp() << endl; } return 0; } ```
#include<iostream> #include<string> #include<vector> #include<cstdio> #include<cstring> using namespace std; char siki[200]; int cur; int digit(){ int n=siki[cur]-'0'; ++cur; return n; } int number(){ int n=digit(); while(cur<strlen(siki) && isdigit(siki[cur])) n=n*10+digit(); return n; } int factor(); int term(){ int a=factor(); while(cur<strlen(siki) && (siki[cur]=='*' || siki[cur]=='/')){ char op=siki[cur]; ++cur; int b=factor(); if(op=='*')a*=b;else a/=b; } return a; } int expression(){ int a=term(); while(cur<strlen(siki) && (siki[cur]=='+' || siki[cur]=='-')){ char op=siki[cur]; cur++; int b=term(); if(op=='+')a+=b;else a-=b; } return a; } int factor(){ if(siki[cur]!='(')return number(); cur++; int n=expression(); cur++; return n; } int main(){ int n; cin>>n; for(int i=0;i<n;i++){ cin>>siki; cur=0; cout<<expression()<<endl; } }
### Prompt Please formulate a cpp solution to the following problem: Your task is to write a program which reads an expression and evaluates it. * The expression consists of numerical values, operators and parentheses, and the ends with '='. * The operators includes +, - , *, / where respectively represents, addition, subtraction, multiplication and division. * Precedence of the operators is based on usual laws. That is one should perform all multiplication and division first, then addition and subtraction. When two operators have the same precedence, they are applied from left to right. * You may assume that there is no division by zero. * All calculation is performed as integers, and after the decimal point should be truncated * Length of the expression will not exceed 100. * -1 × 109 ≤ intermediate results of computation ≤ 109 Input The input is a sequence of datasets. The first line contains an integer n which represents the number of datasets. There will be n lines where each line contains an expression. Output For each datasets, prints the result of calculation. Example Input 2 4-2*3= 4*(8+4+3)= Output -2 60 ### Response ```cpp #include<iostream> #include<string> #include<vector> #include<cstdio> #include<cstring> using namespace std; char siki[200]; int cur; int digit(){ int n=siki[cur]-'0'; ++cur; return n; } int number(){ int n=digit(); while(cur<strlen(siki) && isdigit(siki[cur])) n=n*10+digit(); return n; } int factor(); int term(){ int a=factor(); while(cur<strlen(siki) && (siki[cur]=='*' || siki[cur]=='/')){ char op=siki[cur]; ++cur; int b=factor(); if(op=='*')a*=b;else a/=b; } return a; } int expression(){ int a=term(); while(cur<strlen(siki) && (siki[cur]=='+' || siki[cur]=='-')){ char op=siki[cur]; cur++; int b=term(); if(op=='+')a+=b;else a-=b; } return a; } int factor(){ if(siki[cur]!='(')return number(); cur++; int n=expression(); cur++; return n; } int main(){ int n; cin>>n; for(int i=0;i<n;i++){ cin>>siki; cur=0; cout<<expression()<<endl; } } ```
// 再帰的下向き構文解析 #include <iostream> #include <string> #include <algorithm> #include <cctype> using namespace std; int expression(void); string str; int pos; int factor(void){ int num = 0; if( isdigit( str[pos] ) ){ // '0' <= str[pos] && str[pos] <= '9' while( isdigit( str[pos] ) ){ num *= 10; num += str[pos] - '0'; pos++; } return num; }else if( str[pos] == '(' ){ pos++; int res = expression(); pos++; // ')' の分 return res; } } int term(void){ int res = factor(); while(1){ if( str[pos] == '*' ){ pos++; res *= factor(); }else if( str[pos] == '/' ){ pos++; res /= factor(); }else break; } return res; } int expression(void){ int res = term(); while(1){ if( str[pos] == '+' ){ pos++; res += term(); }else if( str[pos] == '-' ){ pos++; res -= term(); }else break; } return res; } int main(void){ int dn; cin >> dn; while(dn--){ pos = 0; cin >> str; cout << expression() << endl; } return 0; }
### Prompt Please provide a Cpp coded solution to the problem described below: Your task is to write a program which reads an expression and evaluates it. * The expression consists of numerical values, operators and parentheses, and the ends with '='. * The operators includes +, - , *, / where respectively represents, addition, subtraction, multiplication and division. * Precedence of the operators is based on usual laws. That is one should perform all multiplication and division first, then addition and subtraction. When two operators have the same precedence, they are applied from left to right. * You may assume that there is no division by zero. * All calculation is performed as integers, and after the decimal point should be truncated * Length of the expression will not exceed 100. * -1 × 109 ≤ intermediate results of computation ≤ 109 Input The input is a sequence of datasets. The first line contains an integer n which represents the number of datasets. There will be n lines where each line contains an expression. Output For each datasets, prints the result of calculation. Example Input 2 4-2*3= 4*(8+4+3)= Output -2 60 ### Response ```cpp // 再帰的下向き構文解析 #include <iostream> #include <string> #include <algorithm> #include <cctype> using namespace std; int expression(void); string str; int pos; int factor(void){ int num = 0; if( isdigit( str[pos] ) ){ // '0' <= str[pos] && str[pos] <= '9' while( isdigit( str[pos] ) ){ num *= 10; num += str[pos] - '0'; pos++; } return num; }else if( str[pos] == '(' ){ pos++; int res = expression(); pos++; // ')' の分 return res; } } int term(void){ int res = factor(); while(1){ if( str[pos] == '*' ){ pos++; res *= factor(); }else if( str[pos] == '/' ){ pos++; res /= factor(); }else break; } return res; } int expression(void){ int res = term(); while(1){ if( str[pos] == '+' ){ pos++; res += term(); }else if( str[pos] == '-' ){ pos++; res -= term(); }else break; } return res; } int main(void){ int dn; cin >> dn; while(dn--){ pos = 0; cin >> str; cout << expression() << endl; } return 0; } ```
#include <iostream> #include <string> using namespace std; string s; int si(int&); int fi(int&); int so(int&); int main(){ int n; cin >> n; for (int i = 0; i < n; i++){ cin >> s; int pos = 0; cout << so(pos) << endl; } return 0; } int so(int& pos){ int ans = fi(pos); while (s[pos] != '=' && s[pos] != ')'){ if (s[pos] == '+'){ ans += fi(++pos); } else if (s[pos] == '-'){ ans -= fi(++pos); } } pos++; return ans; } int fi(int& pos){ int ans = si(pos); while (1){ if (s[pos] == '*'){ ans *= si(++pos); } else if (s[pos] == '/'){ ans /= si(++pos); } else { return ans; } } } int si(int& pos){ if (s[pos] != '('){ int ans = 0; while ('0' <= s[pos] && s[pos] <= '9'){ ans *= 10; ans += s[pos] - 48; pos++; } return ans; } else{ return so(++pos); } }
### Prompt In cpp, your task is to solve the following problem: Your task is to write a program which reads an expression and evaluates it. * The expression consists of numerical values, operators and parentheses, and the ends with '='. * The operators includes +, - , *, / where respectively represents, addition, subtraction, multiplication and division. * Precedence of the operators is based on usual laws. That is one should perform all multiplication and division first, then addition and subtraction. When two operators have the same precedence, they are applied from left to right. * You may assume that there is no division by zero. * All calculation is performed as integers, and after the decimal point should be truncated * Length of the expression will not exceed 100. * -1 × 109 ≤ intermediate results of computation ≤ 109 Input The input is a sequence of datasets. The first line contains an integer n which represents the number of datasets. There will be n lines where each line contains an expression. Output For each datasets, prints the result of calculation. Example Input 2 4-2*3= 4*(8+4+3)= Output -2 60 ### Response ```cpp #include <iostream> #include <string> using namespace std; string s; int si(int&); int fi(int&); int so(int&); int main(){ int n; cin >> n; for (int i = 0; i < n; i++){ cin >> s; int pos = 0; cout << so(pos) << endl; } return 0; } int so(int& pos){ int ans = fi(pos); while (s[pos] != '=' && s[pos] != ')'){ if (s[pos] == '+'){ ans += fi(++pos); } else if (s[pos] == '-'){ ans -= fi(++pos); } } pos++; return ans; } int fi(int& pos){ int ans = si(pos); while (1){ if (s[pos] == '*'){ ans *= si(++pos); } else if (s[pos] == '/'){ ans /= si(++pos); } else { return ans; } } } int si(int& pos){ if (s[pos] != '('){ int ans = 0; while ('0' <= s[pos] && s[pos] <= '9'){ ans *= 10; ans += s[pos] - 48; pos++; } return ans; } else{ return so(++pos); } } ```
#include <iostream> #include <vector> #include <cstdio> #include <map> #include <queue> #include <deque> #include <stack> #include <algorithm> using namespace std; #define INF (1<<25) #define P(a,b) make_pair(a,b) int id = 0; string s; int exp(); int term(); int fact(); int exp(){ int ans = term(); while(true){ char c = s[id++]; if(c == '+'){ ans += term(); }else if(c=='-'){ ans -= term(); }else{ return ans; } } } int term(){ int ans = fact(); while(true){ char c = s[id++]; if(c == '*'){ ans *= fact(); }else if(c=='/'){ ans /= fact(); }else{ id--; return ans; } } } int fact(){ char c = s[id++]; if(c=='(') return exp(); if(c=='-') return -exp(); if(c=='+') return exp(); int x = c - '0'; while(true){ c = s[id++]; if('0' <= c && c <= '9'){ x = x*10 + c - '0'; }else{ id--; return x; } } } int main(){ int n; cin >> n; while(n--){ cin >> s; id = 0; cout << exp() << endl; } }
### Prompt Generate a cpp solution to the following problem: Your task is to write a program which reads an expression and evaluates it. * The expression consists of numerical values, operators and parentheses, and the ends with '='. * The operators includes +, - , *, / where respectively represents, addition, subtraction, multiplication and division. * Precedence of the operators is based on usual laws. That is one should perform all multiplication and division first, then addition and subtraction. When two operators have the same precedence, they are applied from left to right. * You may assume that there is no division by zero. * All calculation is performed as integers, and after the decimal point should be truncated * Length of the expression will not exceed 100. * -1 × 109 ≤ intermediate results of computation ≤ 109 Input The input is a sequence of datasets. The first line contains an integer n which represents the number of datasets. There will be n lines where each line contains an expression. Output For each datasets, prints the result of calculation. Example Input 2 4-2*3= 4*(8+4+3)= Output -2 60 ### Response ```cpp #include <iostream> #include <vector> #include <cstdio> #include <map> #include <queue> #include <deque> #include <stack> #include <algorithm> using namespace std; #define INF (1<<25) #define P(a,b) make_pair(a,b) int id = 0; string s; int exp(); int term(); int fact(); int exp(){ int ans = term(); while(true){ char c = s[id++]; if(c == '+'){ ans += term(); }else if(c=='-'){ ans -= term(); }else{ return ans; } } } int term(){ int ans = fact(); while(true){ char c = s[id++]; if(c == '*'){ ans *= fact(); }else if(c=='/'){ ans /= fact(); }else{ id--; return ans; } } } int fact(){ char c = s[id++]; if(c=='(') return exp(); if(c=='-') return -exp(); if(c=='+') return exp(); int x = c - '0'; while(true){ c = s[id++]; if('0' <= c && c <= '9'){ x = x*10 + c - '0'; }else{ id--; return x; } } } int main(){ int n; cin >> n; while(n--){ cin >> s; id = 0; cout << exp() << endl; } } ```
#include <iostream> #include <cstring> #include <cctype> #include <cassert> using namespace std; string S; size_t cur = 0; char readchar() { assert(cur < S.size()); char ret = S[cur]; cur++; return ret; } char peek() { assert(cur < S.size()); return S[cur]; } int digit() { assert(isdigit(peek())); int n = readchar() - '0'; return n; } int number() { int n = digit(); while(cur < S.size() && isdigit(peek())) n = n*10 + digit(); return n; } int expression(); int factor() { if(peek() != '(') return number(); cur++; int n = expression(); assert(peek() == ')'); cur++; return n; } int term() { int a = factor(); while(cur < S.size() && (peek() == '*' || peek() == '/')) { char op = readchar(); int b = factor(); if(op == '*') a *= b; else a /= b; } return a; } int expression() { int sum = term(); while(cur < S.size() && (peek() == '+' || peek() == '-')) { char op = readchar(); int b = term(); if(op == '+') sum += b; else sum -= b; } return sum; } int parse() { return expression(); } int main() { int n; cin >> n; for(int i = 0; i < n; i++) { cin >> S; int a = parse(); cout << a << endl; cur = 0; } return 0; }
### Prompt Develop a solution in Cpp to the problem described below: Your task is to write a program which reads an expression and evaluates it. * The expression consists of numerical values, operators and parentheses, and the ends with '='. * The operators includes +, - , *, / where respectively represents, addition, subtraction, multiplication and division. * Precedence of the operators is based on usual laws. That is one should perform all multiplication and division first, then addition and subtraction. When two operators have the same precedence, they are applied from left to right. * You may assume that there is no division by zero. * All calculation is performed as integers, and after the decimal point should be truncated * Length of the expression will not exceed 100. * -1 × 109 ≤ intermediate results of computation ≤ 109 Input The input is a sequence of datasets. The first line contains an integer n which represents the number of datasets. There will be n lines where each line contains an expression. Output For each datasets, prints the result of calculation. Example Input 2 4-2*3= 4*(8+4+3)= Output -2 60 ### Response ```cpp #include <iostream> #include <cstring> #include <cctype> #include <cassert> using namespace std; string S; size_t cur = 0; char readchar() { assert(cur < S.size()); char ret = S[cur]; cur++; return ret; } char peek() { assert(cur < S.size()); return S[cur]; } int digit() { assert(isdigit(peek())); int n = readchar() - '0'; return n; } int number() { int n = digit(); while(cur < S.size() && isdigit(peek())) n = n*10 + digit(); return n; } int expression(); int factor() { if(peek() != '(') return number(); cur++; int n = expression(); assert(peek() == ')'); cur++; return n; } int term() { int a = factor(); while(cur < S.size() && (peek() == '*' || peek() == '/')) { char op = readchar(); int b = factor(); if(op == '*') a *= b; else a /= b; } return a; } int expression() { int sum = term(); while(cur < S.size() && (peek() == '+' || peek() == '-')) { char op = readchar(); int b = term(); if(op == '+') sum += b; else sum -= b; } return sum; } int parse() { return expression(); } int main() { int n; cin >> n; for(int i = 0; i < n; i++) { cin >> S; int a = parse(); cout << a << endl; cur = 0; } return 0; } ```
#include <iostream> #include <cassert> #include <string> #include <cctype> using namespace std; int cur = 0; string S; int expression(); int term(); int factor(); int main() { int times; cin >>times; for(int i = 0 ; i < times; i++){ cin >> S; cur = 0; S.resize(S.size()-1); cout << expression() <<endl; } } int digit() { assert(isdigit(S[cur])); int n = S[cur] -'0'; cur = cur+1; return n; } int number() { int n = digit(); while(cur <S.size() && isdigit(S[cur])){ n = n*10 + digit(); } return n; } int expression() { int sum = term(); while (S[cur] == '+' || S[cur] == '-'){ char op = S[cur]; cur +=1; int b = term(); if(op == '+') sum +=b; else sum -= b; } return sum; } int term() { int a = factor(); while(cur < S.size() && (S[cur] == '*' || S[cur] == '/')){ char op = S[cur]; cur +=1; int b = factor(); if(op == '*') a *=b; else a/=b; } return a; } int factor() { if(S[cur] != '(' ) return number(); cur +=1; int n =expression(); assert(S[cur] == ')'); cur +=1; return n; }
### Prompt In Cpp, your task is to solve the following problem: Your task is to write a program which reads an expression and evaluates it. * The expression consists of numerical values, operators and parentheses, and the ends with '='. * The operators includes +, - , *, / where respectively represents, addition, subtraction, multiplication and division. * Precedence of the operators is based on usual laws. That is one should perform all multiplication and division first, then addition and subtraction. When two operators have the same precedence, they are applied from left to right. * You may assume that there is no division by zero. * All calculation is performed as integers, and after the decimal point should be truncated * Length of the expression will not exceed 100. * -1 × 109 ≤ intermediate results of computation ≤ 109 Input The input is a sequence of datasets. The first line contains an integer n which represents the number of datasets. There will be n lines where each line contains an expression. Output For each datasets, prints the result of calculation. Example Input 2 4-2*3= 4*(8+4+3)= Output -2 60 ### Response ```cpp #include <iostream> #include <cassert> #include <string> #include <cctype> using namespace std; int cur = 0; string S; int expression(); int term(); int factor(); int main() { int times; cin >>times; for(int i = 0 ; i < times; i++){ cin >> S; cur = 0; S.resize(S.size()-1); cout << expression() <<endl; } } int digit() { assert(isdigit(S[cur])); int n = S[cur] -'0'; cur = cur+1; return n; } int number() { int n = digit(); while(cur <S.size() && isdigit(S[cur])){ n = n*10 + digit(); } return n; } int expression() { int sum = term(); while (S[cur] == '+' || S[cur] == '-'){ char op = S[cur]; cur +=1; int b = term(); if(op == '+') sum +=b; else sum -= b; } return sum; } int term() { int a = factor(); while(cur < S.size() && (S[cur] == '*' || S[cur] == '/')){ char op = S[cur]; cur +=1; int b = factor(); if(op == '*') a *=b; else a/=b; } return a; } int factor() { if(S[cur] != '(' ) return number(); cur +=1; int n =expression(); assert(S[cur] == ')'); cur +=1; return n; } ```
#include<iostream> #define INF (1<<26) using namespace std; int n; int compute(int a,int b,string str){ int i=a,sum=0,tmp=INF,tmp2=INF,cnt=0; while(1){ //cout<<a<<' '<<b<<endl; sum=0; if(str[i]=='('){ sum=compute(i+1,0,str); //cout<<sum<<endl; cnt=0; for(i=i;i<(int)str.size();i++){ if(str[i]==')')cnt--; if(str[i]=='(')cnt++; if(cnt==0)break; } i++; }else{ while(1){ if('0'<=str[i]&&str[i]<='9'){ sum*=10; sum+=(str[i]-'0'); i++; }else{ break; } } } if(b==1){ sum*=-1; b=0; } if(tmp!=INF){ sum*=tmp; tmp=INF; } if(tmp2!=INF){ sum=tmp2/sum; tmp2=INF; } //if(a==8)cout<<sum<<endl; if(str[i]=='\0'||str[i]==')'||str[i]=='='||i==(int)str.size())return sum; if(str[i]=='+')return sum + compute(i+1,0,str); if(str[i]=='-')return sum + compute(i+1,1,str); if(str[i]=='*'){ i++; tmp=sum; }else if(str[i]=='/'){ i++; tmp2=sum; } } } int main(){ string str; cin>>n; while(n--){ cin>>str; //cout<<str<<endl; cout<<compute(0,0,str)<<endl; } return 0; }
### Prompt Create a solution in Cpp for the following problem: Your task is to write a program which reads an expression and evaluates it. * The expression consists of numerical values, operators and parentheses, and the ends with '='. * The operators includes +, - , *, / where respectively represents, addition, subtraction, multiplication and division. * Precedence of the operators is based on usual laws. That is one should perform all multiplication and division first, then addition and subtraction. When two operators have the same precedence, they are applied from left to right. * You may assume that there is no division by zero. * All calculation is performed as integers, and after the decimal point should be truncated * Length of the expression will not exceed 100. * -1 × 109 ≤ intermediate results of computation ≤ 109 Input The input is a sequence of datasets. The first line contains an integer n which represents the number of datasets. There will be n lines where each line contains an expression. Output For each datasets, prints the result of calculation. Example Input 2 4-2*3= 4*(8+4+3)= Output -2 60 ### Response ```cpp #include<iostream> #define INF (1<<26) using namespace std; int n; int compute(int a,int b,string str){ int i=a,sum=0,tmp=INF,tmp2=INF,cnt=0; while(1){ //cout<<a<<' '<<b<<endl; sum=0; if(str[i]=='('){ sum=compute(i+1,0,str); //cout<<sum<<endl; cnt=0; for(i=i;i<(int)str.size();i++){ if(str[i]==')')cnt--; if(str[i]=='(')cnt++; if(cnt==0)break; } i++; }else{ while(1){ if('0'<=str[i]&&str[i]<='9'){ sum*=10; sum+=(str[i]-'0'); i++; }else{ break; } } } if(b==1){ sum*=-1; b=0; } if(tmp!=INF){ sum*=tmp; tmp=INF; } if(tmp2!=INF){ sum=tmp2/sum; tmp2=INF; } //if(a==8)cout<<sum<<endl; if(str[i]=='\0'||str[i]==')'||str[i]=='='||i==(int)str.size())return sum; if(str[i]=='+')return sum + compute(i+1,0,str); if(str[i]=='-')return sum + compute(i+1,1,str); if(str[i]=='*'){ i++; tmp=sum; }else if(str[i]=='/'){ i++; tmp2=sum; } } } int main(){ string str; cin>>n; while(n--){ cin>>str; //cout<<str<<endl; cout<<compute(0,0,str)<<endl; } return 0; } ```
#include <iostream> #include <string> using namespace std; int k,n,ans; string s; int calc1(); int calc2(); int calc3(); int calc1(){ int a; a=calc2(); while(s[k]=='+' ||s[k]=='-'){ if(s[k]=='+'){ k++; a+=calc2(); }else if(s[k]=='-'){ k++; a-=calc2(); } } return a; } int calc2(){ int a; a=calc3(); while(s[k]=='*' ||s[k]=='/'){ if(s[k]=='*'){ k++; a*=calc3(); }else if(s[k]=='/'){ k++; a/=calc3(); } } return a; } int calc3(){ int a; if(s[k]=='('){ k++; a=calc1(); k++; }else{ a=0; while(s[k]>='0' && s[k]<='9'){ a*=10; a+=s[k]-'0'; k++; } } return a; } int main(){ cin >> n; while(n--){ ans=0; k=0; cin >> s; ans=calc1(); cout << ans <<endl; s.clear(); } return 0; }
### Prompt Please formulate a Cpp solution to the following problem: Your task is to write a program which reads an expression and evaluates it. * The expression consists of numerical values, operators and parentheses, and the ends with '='. * The operators includes +, - , *, / where respectively represents, addition, subtraction, multiplication and division. * Precedence of the operators is based on usual laws. That is one should perform all multiplication and division first, then addition and subtraction. When two operators have the same precedence, they are applied from left to right. * You may assume that there is no division by zero. * All calculation is performed as integers, and after the decimal point should be truncated * Length of the expression will not exceed 100. * -1 × 109 ≤ intermediate results of computation ≤ 109 Input The input is a sequence of datasets. The first line contains an integer n which represents the number of datasets. There will be n lines where each line contains an expression. Output For each datasets, prints the result of calculation. Example Input 2 4-2*3= 4*(8+4+3)= Output -2 60 ### Response ```cpp #include <iostream> #include <string> using namespace std; int k,n,ans; string s; int calc1(); int calc2(); int calc3(); int calc1(){ int a; a=calc2(); while(s[k]=='+' ||s[k]=='-'){ if(s[k]=='+'){ k++; a+=calc2(); }else if(s[k]=='-'){ k++; a-=calc2(); } } return a; } int calc2(){ int a; a=calc3(); while(s[k]=='*' ||s[k]=='/'){ if(s[k]=='*'){ k++; a*=calc3(); }else if(s[k]=='/'){ k++; a/=calc3(); } } return a; } int calc3(){ int a; if(s[k]=='('){ k++; a=calc1(); k++; }else{ a=0; while(s[k]>='0' && s[k]<='9'){ a*=10; a+=s[k]-'0'; k++; } } return a; } int main(){ cin >> n; while(n--){ ans=0; k=0; cin >> s; ans=calc1(); cout << ans <<endl; s.clear(); } return 0; } ```
#include <iostream> #include <string> #include <cassert> #include <cctype> using namespace std; string S=""; int cur=0; char readchar() { assert(cur<S.size()); return S[cur++]; } char peek() { assert(cur<S.size()); return S[cur]; } int digit() { assert(isdigit(peek())); int n=readchar()-'0'; return n; } int number() { int n=digit(); while(cur<S.size()&&isdigit(peek())) { n=n*10+digit(); } return n; } int expression(); int factor() { if(peek()!='(') return number(); cur+=1; int n=expression(); assert(peek()==')'); cur+=1; return n; } int term() { int a=factor(); while(cur<S.size()&&(peek()=='*'||peek()=='/')) { char op=readchar(); int b=factor(); if(op=='*') { a*=b; } else { a/=b; } } return a; } int expression() { int sum=term(); while(cur<S.size()&&(peek()=='+'||peek()=='-')) { char op=readchar(); int b=term(); if(op=='+') { sum+=b; } else { sum-=b; } } return sum; } int main() { int n; cin>>n; for(int i=0;i<n;i++) { cur=0; cin>>S; S.resize(S.size()-1); cout<<expression()<<endl; } return 0; }
### Prompt Develop a solution in Cpp to the problem described below: Your task is to write a program which reads an expression and evaluates it. * The expression consists of numerical values, operators and parentheses, and the ends with '='. * The operators includes +, - , *, / where respectively represents, addition, subtraction, multiplication and division. * Precedence of the operators is based on usual laws. That is one should perform all multiplication and division first, then addition and subtraction. When two operators have the same precedence, they are applied from left to right. * You may assume that there is no division by zero. * All calculation is performed as integers, and after the decimal point should be truncated * Length of the expression will not exceed 100. * -1 × 109 ≤ intermediate results of computation ≤ 109 Input The input is a sequence of datasets. The first line contains an integer n which represents the number of datasets. There will be n lines where each line contains an expression. Output For each datasets, prints the result of calculation. Example Input 2 4-2*3= 4*(8+4+3)= Output -2 60 ### Response ```cpp #include <iostream> #include <string> #include <cassert> #include <cctype> using namespace std; string S=""; int cur=0; char readchar() { assert(cur<S.size()); return S[cur++]; } char peek() { assert(cur<S.size()); return S[cur]; } int digit() { assert(isdigit(peek())); int n=readchar()-'0'; return n; } int number() { int n=digit(); while(cur<S.size()&&isdigit(peek())) { n=n*10+digit(); } return n; } int expression(); int factor() { if(peek()!='(') return number(); cur+=1; int n=expression(); assert(peek()==')'); cur+=1; return n; } int term() { int a=factor(); while(cur<S.size()&&(peek()=='*'||peek()=='/')) { char op=readchar(); int b=factor(); if(op=='*') { a*=b; } else { a/=b; } } return a; } int expression() { int sum=term(); while(cur<S.size()&&(peek()=='+'||peek()=='-')) { char op=readchar(); int b=term(); if(op=='+') { sum+=b; } else { sum-=b; } } return sum; } int main() { int n; cin>>n; for(int i=0;i<n;i++) { cur=0; cin>>S; S.resize(S.size()-1); cout<<expression()<<endl; } return 0; } ```
#include <iostream> #include <stack> #include <stdio.h> #include <stdlib.h> #include <string.h> using namespace std; int main() { char c[100],*st; int n,f,ca1,ca2,d; stack<int> as; stack<char> bs; cin >> n; for (n;n>0;n--) { cin >> c; st=c; while(true) { if (st[0]=='=') break; f=0; d=0; if (st[0]=='-') if (st==c) d=1; else if ((st-1)[0]=='(') d=1; if ((st[0]>='0' && st[0]<='9') || d==1) { as.push(atoi(st)); f=1; st++; for (st;st[0]>='0' && st[0]<='9';st++); } else { if (st[0]==')') { bs.pop(); f=1;} else bs.push(st[0]); st++;} if (bs.empty() || f==0) continue; while(true){ if (bs.empty()) break; else if (bs.top()=='(') break; if (bs.top()=='*' || bs.top()=='/') { ca2=as.top(); as.pop(); ca1=as.top(); as.pop(); if (bs.top()=='*') ca1=ca1*ca2; else ca1=ca1/ca2; as.push(ca1); bs.pop(); } else if (st[0]!='*' && st[0]!='/') { ca2=as.top(); as.pop(); ca1=as.top(); as.pop(); if (bs.top()=='+') ca1=ca1+ca2; else ca1=ca1-ca2; as.push(ca1); bs.pop(); } else break; } } cout << as.top() << endl; as.pop(); } return 0; }
### Prompt Create a solution in CPP for the following problem: Your task is to write a program which reads an expression and evaluates it. * The expression consists of numerical values, operators and parentheses, and the ends with '='. * The operators includes +, - , *, / where respectively represents, addition, subtraction, multiplication and division. * Precedence of the operators is based on usual laws. That is one should perform all multiplication and division first, then addition and subtraction. When two operators have the same precedence, they are applied from left to right. * You may assume that there is no division by zero. * All calculation is performed as integers, and after the decimal point should be truncated * Length of the expression will not exceed 100. * -1 × 109 ≤ intermediate results of computation ≤ 109 Input The input is a sequence of datasets. The first line contains an integer n which represents the number of datasets. There will be n lines where each line contains an expression. Output For each datasets, prints the result of calculation. Example Input 2 4-2*3= 4*(8+4+3)= Output -2 60 ### Response ```cpp #include <iostream> #include <stack> #include <stdio.h> #include <stdlib.h> #include <string.h> using namespace std; int main() { char c[100],*st; int n,f,ca1,ca2,d; stack<int> as; stack<char> bs; cin >> n; for (n;n>0;n--) { cin >> c; st=c; while(true) { if (st[0]=='=') break; f=0; d=0; if (st[0]=='-') if (st==c) d=1; else if ((st-1)[0]=='(') d=1; if ((st[0]>='0' && st[0]<='9') || d==1) { as.push(atoi(st)); f=1; st++; for (st;st[0]>='0' && st[0]<='9';st++); } else { if (st[0]==')') { bs.pop(); f=1;} else bs.push(st[0]); st++;} if (bs.empty() || f==0) continue; while(true){ if (bs.empty()) break; else if (bs.top()=='(') break; if (bs.top()=='*' || bs.top()=='/') { ca2=as.top(); as.pop(); ca1=as.top(); as.pop(); if (bs.top()=='*') ca1=ca1*ca2; else ca1=ca1/ca2; as.push(ca1); bs.pop(); } else if (st[0]!='*' && st[0]!='/') { ca2=as.top(); as.pop(); ca1=as.top(); as.pop(); if (bs.top()=='+') ca1=ca1+ca2; else ca1=ca1-ca2; as.push(ca1); bs.pop(); } else break; } } cout << as.top() << endl; as.pop(); } return 0; } ```
#include <iostream> #include <string> using namespace std; string str; int pos; int exp(); int term(); int factor(); int henkan(string); int ruijou(int); int main(){ int n; cin >> n; for(int i=0;i<n;i++){ str=""; getchar(); while(1){ char temp; cin >> temp; if(temp=='=') break; str+=temp; } pos=0; int ans=0; ans=exp(); cout << ans << endl; } return 0; } int exp(){ int x=term(); while(str[pos]=='+' || str[pos]=='-'){ char op=str[pos]; pos++; if(op=='+'){ x+=term(); } else{ x-=term(); } } return x; } int term(){ int x=factor(); while(str[pos]=='*' || str[pos]=='/'){ char op=str[pos]; pos++; if(op=='*') x*=factor(); else x/=factor(); } return x; } int factor(){ int x; if(str[pos]=='('){ pos++; x=exp(); pos++; } else{ string number=""; int i=pos; while(str[i]>='0' && str[i]<='9'){ number+=str[i]; i++; } pos=i; x=henkan(number); } return x; } int henkan(string number){ int size=number.size(); int sum=0; for(int i=0;i<size;i++){ sum+=(number[i]-'0')*ruijou(size-i-1); } return sum; } int ruijou(int n){ int sum=1; for(int i=0;i<n;i++){ sum*=10; } return sum; }
### Prompt In CPP, your task is to solve the following problem: Your task is to write a program which reads an expression and evaluates it. * The expression consists of numerical values, operators and parentheses, and the ends with '='. * The operators includes +, - , *, / where respectively represents, addition, subtraction, multiplication and division. * Precedence of the operators is based on usual laws. That is one should perform all multiplication and division first, then addition and subtraction. When two operators have the same precedence, they are applied from left to right. * You may assume that there is no division by zero. * All calculation is performed as integers, and after the decimal point should be truncated * Length of the expression will not exceed 100. * -1 × 109 ≤ intermediate results of computation ≤ 109 Input The input is a sequence of datasets. The first line contains an integer n which represents the number of datasets. There will be n lines where each line contains an expression. Output For each datasets, prints the result of calculation. Example Input 2 4-2*3= 4*(8+4+3)= Output -2 60 ### Response ```cpp #include <iostream> #include <string> using namespace std; string str; int pos; int exp(); int term(); int factor(); int henkan(string); int ruijou(int); int main(){ int n; cin >> n; for(int i=0;i<n;i++){ str=""; getchar(); while(1){ char temp; cin >> temp; if(temp=='=') break; str+=temp; } pos=0; int ans=0; ans=exp(); cout << ans << endl; } return 0; } int exp(){ int x=term(); while(str[pos]=='+' || str[pos]=='-'){ char op=str[pos]; pos++; if(op=='+'){ x+=term(); } else{ x-=term(); } } return x; } int term(){ int x=factor(); while(str[pos]=='*' || str[pos]=='/'){ char op=str[pos]; pos++; if(op=='*') x*=factor(); else x/=factor(); } return x; } int factor(){ int x; if(str[pos]=='('){ pos++; x=exp(); pos++; } else{ string number=""; int i=pos; while(str[i]>='0' && str[i]<='9'){ number+=str[i]; i++; } pos=i; x=henkan(number); } return x; } int henkan(string number){ int size=number.size(); int sum=0; for(int i=0;i<size;i++){ sum+=(number[i]-'0')*ruijou(size-i-1); } return sum; } int ruijou(int n){ int sum=1; for(int i=0;i<n;i++){ sum*=10; } return sum; } ```
#include <iostream> #include <string> #include <map> using namespace std; typedef pair<int,int> result; #define value first #define p second result equation(const string &s, int p = 0); result factor(const string &s, int p=0); result term(const string &s, int p=0); result equation(const string &s,int p) { result r = factor(s,p); while(s[r.p] == '+' || s[r.p] == '-') { result r_ = factor(s, r.p+1); if(s[r.p] == '+') r.value += r_.value; else r.value -= r_.value; r.p = r_.p; } return r; } result factor(const string &s, int p) { result r = term(s,p); while(s[r.p] == '*' || s[r.p] == '/') { result r_ = term(s, r.p+1); if(s[r.p] == '*') r.value *= r_.value; else r.value /= r_.value; r.p = r_.p; } return r; } result term(const string &s, int p) { if(s[p] == '(') { result r = equation(s,p+1); r.p += 1; return r; }else{ int value = 0; while(isdigit(s[p])) value = value*10 + (s[p++] - '0'); return result(value,p); } } int main() { int n; cin>>n; string s; for(int i=0; i<n; ++i) { cin>>s; result r = equation(s); cout<<r.value<<endl; } }
### Prompt Please provide a CPP coded solution to the problem described below: Your task is to write a program which reads an expression and evaluates it. * The expression consists of numerical values, operators and parentheses, and the ends with '='. * The operators includes +, - , *, / where respectively represents, addition, subtraction, multiplication and division. * Precedence of the operators is based on usual laws. That is one should perform all multiplication and division first, then addition and subtraction. When two operators have the same precedence, they are applied from left to right. * You may assume that there is no division by zero. * All calculation is performed as integers, and after the decimal point should be truncated * Length of the expression will not exceed 100. * -1 × 109 ≤ intermediate results of computation ≤ 109 Input The input is a sequence of datasets. The first line contains an integer n which represents the number of datasets. There will be n lines where each line contains an expression. Output For each datasets, prints the result of calculation. Example Input 2 4-2*3= 4*(8+4+3)= Output -2 60 ### Response ```cpp #include <iostream> #include <string> #include <map> using namespace std; typedef pair<int,int> result; #define value first #define p second result equation(const string &s, int p = 0); result factor(const string &s, int p=0); result term(const string &s, int p=0); result equation(const string &s,int p) { result r = factor(s,p); while(s[r.p] == '+' || s[r.p] == '-') { result r_ = factor(s, r.p+1); if(s[r.p] == '+') r.value += r_.value; else r.value -= r_.value; r.p = r_.p; } return r; } result factor(const string &s, int p) { result r = term(s,p); while(s[r.p] == '*' || s[r.p] == '/') { result r_ = term(s, r.p+1); if(s[r.p] == '*') r.value *= r_.value; else r.value /= r_.value; r.p = r_.p; } return r; } result term(const string &s, int p) { if(s[p] == '(') { result r = equation(s,p+1); r.p += 1; return r; }else{ int value = 0; while(isdigit(s[p])) value = value*10 + (s[p++] - '0'); return result(value,p); } } int main() { int n; cin>>n; string s; for(int i=0; i<n; ++i) { cin>>s; result r = equation(s); cout<<r.value<<endl; } } ```
#include <iostream> #include <cassert> #include <string> #include <cctype> using namespace std; string S; size_t cur=0; int digit() { assert(isdigit(S[cur])); int n = S[cur] - '0'; cur++; return n; } int number() { int n = digit(); while(cur < S.size() && isdigit(S[cur])) { n = n*10 + digit(); } return n; } int expression(); int factor() { if (S[cur] != '(') return number(); cur++; int n = expression(); assert(S[cur] == ')'); cur++; return n; } int term() { int a = factor(); while (cur < S.size() && (S[cur] == '*' || S[cur] == '/')) { char op = S[cur++]; int b = factor(); if (op == '*') a *= b; else a /= b; } return a; } int expression() { int a = term(); while (cur < S.size() && (S[cur] == '+' || S[cur] == '-')) { char op = S[cur++]; int b = term(); if (op == '+') a += b; else a -= b; } return a; } int main() { int N; cin >> N; for (int i=0; i<N; i++) { cur = 0; cin >> S; S.resize(S.size() - 1); // '='????????? cout << expression() << endl; } } // accepted! #if 0 ???????????????pdf?????????????????????????????§?????¨?????§45????????? ?????°??????????????¨??§?????????????????????????°?????????????????????¨???????????§????????? #endif
### Prompt Develop a solution in CPP to the problem described below: Your task is to write a program which reads an expression and evaluates it. * The expression consists of numerical values, operators and parentheses, and the ends with '='. * The operators includes +, - , *, / where respectively represents, addition, subtraction, multiplication and division. * Precedence of the operators is based on usual laws. That is one should perform all multiplication and division first, then addition and subtraction. When two operators have the same precedence, they are applied from left to right. * You may assume that there is no division by zero. * All calculation is performed as integers, and after the decimal point should be truncated * Length of the expression will not exceed 100. * -1 × 109 ≤ intermediate results of computation ≤ 109 Input The input is a sequence of datasets. The first line contains an integer n which represents the number of datasets. There will be n lines where each line contains an expression. Output For each datasets, prints the result of calculation. Example Input 2 4-2*3= 4*(8+4+3)= Output -2 60 ### Response ```cpp #include <iostream> #include <cassert> #include <string> #include <cctype> using namespace std; string S; size_t cur=0; int digit() { assert(isdigit(S[cur])); int n = S[cur] - '0'; cur++; return n; } int number() { int n = digit(); while(cur < S.size() && isdigit(S[cur])) { n = n*10 + digit(); } return n; } int expression(); int factor() { if (S[cur] != '(') return number(); cur++; int n = expression(); assert(S[cur] == ')'); cur++; return n; } int term() { int a = factor(); while (cur < S.size() && (S[cur] == '*' || S[cur] == '/')) { char op = S[cur++]; int b = factor(); if (op == '*') a *= b; else a /= b; } return a; } int expression() { int a = term(); while (cur < S.size() && (S[cur] == '+' || S[cur] == '-')) { char op = S[cur++]; int b = term(); if (op == '+') a += b; else a -= b; } return a; } int main() { int N; cin >> N; for (int i=0; i<N; i++) { cur = 0; cin >> S; S.resize(S.size() - 1); // '='????????? cout << expression() << endl; } } // accepted! #if 0 ???????????????pdf?????????????????????????????§?????¨?????§45????????? ?????°??????????????¨??§?????????????????????????°?????????????????????¨???????????§????????? #endif ```
#include<iostream> #include<cassert> #include<cctype> using namespace std; string S; int cur; int expression(); int digit(){ assert(isdigit(S[cur])); int n=S[cur]-'0'; cur=cur+1; return n; } int number(){ int n=digit(); while(cur<S.size()&&isdigit(S[cur])){ n=n*10+digit(); } return n; } int factor(){ if(S[cur]!='(') return number(); cur+=1; int n=expression(); assert(S[cur]==')'); cur+=1; return n; } int term(){ int a=factor(); while(cur<S.size()&&(S[cur]=='*'||S[cur]=='/')){ char op=S[cur++]; int b=factor(); if(op=='*'){ a*=b; } else a/=b; } return a; } int expression() { int sum=term(); while(cur<S.size()&&(S[cur]=='+'||S[cur]=='-')){ char op=S[cur++]; int b=term(); if(op=='+'){ sum+=b; } else sum-=b; } return sum; } int main(){ int N; cin>>N; for(int i=0;i<N;++i){ cur=0; cin>>S; cout<<expression()<<endl; } return 0; }
### Prompt Please formulate a CPP solution to the following problem: Your task is to write a program which reads an expression and evaluates it. * The expression consists of numerical values, operators and parentheses, and the ends with '='. * The operators includes +, - , *, / where respectively represents, addition, subtraction, multiplication and division. * Precedence of the operators is based on usual laws. That is one should perform all multiplication and division first, then addition and subtraction. When two operators have the same precedence, they are applied from left to right. * You may assume that there is no division by zero. * All calculation is performed as integers, and after the decimal point should be truncated * Length of the expression will not exceed 100. * -1 × 109 ≤ intermediate results of computation ≤ 109 Input The input is a sequence of datasets. The first line contains an integer n which represents the number of datasets. There will be n lines where each line contains an expression. Output For each datasets, prints the result of calculation. Example Input 2 4-2*3= 4*(8+4+3)= Output -2 60 ### Response ```cpp #include<iostream> #include<cassert> #include<cctype> using namespace std; string S; int cur; int expression(); int digit(){ assert(isdigit(S[cur])); int n=S[cur]-'0'; cur=cur+1; return n; } int number(){ int n=digit(); while(cur<S.size()&&isdigit(S[cur])){ n=n*10+digit(); } return n; } int factor(){ if(S[cur]!='(') return number(); cur+=1; int n=expression(); assert(S[cur]==')'); cur+=1; return n; } int term(){ int a=factor(); while(cur<S.size()&&(S[cur]=='*'||S[cur]=='/')){ char op=S[cur++]; int b=factor(); if(op=='*'){ a*=b; } else a/=b; } return a; } int expression() { int sum=term(); while(cur<S.size()&&(S[cur]=='+'||S[cur]=='-')){ char op=S[cur++]; int b=term(); if(op=='+'){ sum+=b; } else sum-=b; } return sum; } int main(){ int N; cin>>N; for(int i=0;i<N;++i){ cur=0; cin>>S; cout<<expression()<<endl; } return 0; } ```
#include <cassert> #include <cctype> #include <vector> #include <string> #include <iostream> #include <algorithm> using namespace std; string S = ""; int cur; int digit(){ assert(isdigit(S[cur])); int n = S[cur] - '0'; cur = cur+1; return n; } int number(){ int n = digit(); while(cur < S.size() && isdigit(S[cur])) n = n*10 + digit(); return n; } int factor(); int term(){ int a = factor(); while(cur < S.size() && (S[cur] == '*' || S[cur] == '/')){ char op = S[cur++]; int b = factor(); if(op == '*'){ a *= b; }else{ a /= b; } } return a; } int expression(){ int sum = term(); while (cur < S.size() && (S[cur] == '+' || S[cur] == '-')){ char op = S[cur]; cur++; int b = term(); if(op == '+'){ sum += b; }else{ sum -= b; } } return sum; } int factor(){ if(S[cur] != '('){ return number(); } cur += 1; int n = expression(); assert(S[cur] == ')'); cur += 1; return n; } int main(){ int N; cin >> N; for(int i = 0;i<N;i++){ cur = 0; cin >> S; S.erase(S.size()-1); cout << expression() << endl; } }
### Prompt Please provide a cpp coded solution to the problem described below: Your task is to write a program which reads an expression and evaluates it. * The expression consists of numerical values, operators and parentheses, and the ends with '='. * The operators includes +, - , *, / where respectively represents, addition, subtraction, multiplication and division. * Precedence of the operators is based on usual laws. That is one should perform all multiplication and division first, then addition and subtraction. When two operators have the same precedence, they are applied from left to right. * You may assume that there is no division by zero. * All calculation is performed as integers, and after the decimal point should be truncated * Length of the expression will not exceed 100. * -1 × 109 ≤ intermediate results of computation ≤ 109 Input The input is a sequence of datasets. The first line contains an integer n which represents the number of datasets. There will be n lines where each line contains an expression. Output For each datasets, prints the result of calculation. Example Input 2 4-2*3= 4*(8+4+3)= Output -2 60 ### Response ```cpp #include <cassert> #include <cctype> #include <vector> #include <string> #include <iostream> #include <algorithm> using namespace std; string S = ""; int cur; int digit(){ assert(isdigit(S[cur])); int n = S[cur] - '0'; cur = cur+1; return n; } int number(){ int n = digit(); while(cur < S.size() && isdigit(S[cur])) n = n*10 + digit(); return n; } int factor(); int term(){ int a = factor(); while(cur < S.size() && (S[cur] == '*' || S[cur] == '/')){ char op = S[cur++]; int b = factor(); if(op == '*'){ a *= b; }else{ a /= b; } } return a; } int expression(){ int sum = term(); while (cur < S.size() && (S[cur] == '+' || S[cur] == '-')){ char op = S[cur]; cur++; int b = term(); if(op == '+'){ sum += b; }else{ sum -= b; } } return sum; } int factor(){ if(S[cur] != '('){ return number(); } cur += 1; int n = expression(); assert(S[cur] == ')'); cur += 1; return n; } int main(){ int N; cin >> N; for(int i = 0;i<N;i++){ cur = 0; cin >> S; S.erase(S.size()-1); cout << expression() << endl; } } ```
#include <iostream> #include <string> using namespace std; int hyoka(string); int isNum(int); int main(){ int n,i; string s; cin>>n; for(i=0;i<n;i++){ cin>>s; cout<<hyoka(s)<<endl; } return 0; } int hyoka(string s){ int total=0,work=0; int tmp1,tmp2; int oi=0; char oc; int n; string cs; if(s[oi]=='(' || isNum(s[oi])!=-1){s='+'+s;} //cout<<s<<endl; while(1){ oc=s[oi];if(oc=='='){total+=work;break;} if(s[oi+1]!='('){ //??°??????????????? tmp1=0; while(1){ oi++; tmp2=isNum(s[oi]); if(tmp2!=-1){tmp1=tmp1*10+tmp2;}else{break;} } }else{ //??????????????° cs="";n=1;oi++; while(1){ oi++; switch(s[oi]){ case '(':n++;break; case ')':n--;break; } if(n!=0){cs+=s[oi];}else{break;} } cs+='='; //cout<<cs<<endl; tmp1=hyoka(cs); } switch(oc){ case '+':total+=work;work=tmp1;break; case '-':total+=work;work=-tmp1;break; case '*':work*=tmp1;break; case '/':work/=tmp1;break; } } return total; } int isNum(int c){ if(48<=c && c<=57){ return c-48; } return -1; }
### Prompt Create a solution in CPP for the following problem: Your task is to write a program which reads an expression and evaluates it. * The expression consists of numerical values, operators and parentheses, and the ends with '='. * The operators includes +, - , *, / where respectively represents, addition, subtraction, multiplication and division. * Precedence of the operators is based on usual laws. That is one should perform all multiplication and division first, then addition and subtraction. When two operators have the same precedence, they are applied from left to right. * You may assume that there is no division by zero. * All calculation is performed as integers, and after the decimal point should be truncated * Length of the expression will not exceed 100. * -1 × 109 ≤ intermediate results of computation ≤ 109 Input The input is a sequence of datasets. The first line contains an integer n which represents the number of datasets. There will be n lines where each line contains an expression. Output For each datasets, prints the result of calculation. Example Input 2 4-2*3= 4*(8+4+3)= Output -2 60 ### Response ```cpp #include <iostream> #include <string> using namespace std; int hyoka(string); int isNum(int); int main(){ int n,i; string s; cin>>n; for(i=0;i<n;i++){ cin>>s; cout<<hyoka(s)<<endl; } return 0; } int hyoka(string s){ int total=0,work=0; int tmp1,tmp2; int oi=0; char oc; int n; string cs; if(s[oi]=='(' || isNum(s[oi])!=-1){s='+'+s;} //cout<<s<<endl; while(1){ oc=s[oi];if(oc=='='){total+=work;break;} if(s[oi+1]!='('){ //??°??????????????? tmp1=0; while(1){ oi++; tmp2=isNum(s[oi]); if(tmp2!=-1){tmp1=tmp1*10+tmp2;}else{break;} } }else{ //??????????????° cs="";n=1;oi++; while(1){ oi++; switch(s[oi]){ case '(':n++;break; case ')':n--;break; } if(n!=0){cs+=s[oi];}else{break;} } cs+='='; //cout<<cs<<endl; tmp1=hyoka(cs); } switch(oc){ case '+':total+=work;work=tmp1;break; case '-':total+=work;work=-tmp1;break; case '*':work*=tmp1;break; case '/':work/=tmp1;break; } } return total; } int isNum(int c){ if(48<=c && c<=57){ return c-48; } return -1; } ```
#include <iostream> #include <string> #include <cctype> #include <cassert> #include <algorithm> using namespace std; size_t cur; string s; int expression(); int digit(){ assert(cur < s.size()); return s[cur++] - '0'; } int number(){ int n = 0.0; assert(cur < s.size()); while(cur < s.size() && isdigit(s[cur])){ n = n * 10 + digit(); } return n; } int factor(){ assert(cur < s.size()); if(isdigit(s[cur])){ return number(); }else{ cur++; int a = expression(); assert(s[cur] == ')'); cur++; return a; } } int term(){ assert(cur < s.size()); int a = factor(); while(cur < s.size() && (s[cur] == '*' || s[cur] == '/')){ char op = s[cur++]; int b = factor(); if(op == '*') a *= b; else a /= b; } return a; } int expression(){ assert(cur < s.size()); int a = term(); while(cur < s.size() && (s[cur] == '-' || s[cur] == '+')){ char op = s[cur++]; int b = term(); if(op == '-') a -= b; else a += b; } return a; } int main(){ int n; cin >> n; for(int i = 0; i < n; i++){ cur = 0; cin >> s; cout << expression() << endl; } return 0; }
### Prompt Please create a solution in cpp to the following problem: Your task is to write a program which reads an expression and evaluates it. * The expression consists of numerical values, operators and parentheses, and the ends with '='. * The operators includes +, - , *, / where respectively represents, addition, subtraction, multiplication and division. * Precedence of the operators is based on usual laws. That is one should perform all multiplication and division first, then addition and subtraction. When two operators have the same precedence, they are applied from left to right. * You may assume that there is no division by zero. * All calculation is performed as integers, and after the decimal point should be truncated * Length of the expression will not exceed 100. * -1 × 109 ≤ intermediate results of computation ≤ 109 Input The input is a sequence of datasets. The first line contains an integer n which represents the number of datasets. There will be n lines where each line contains an expression. Output For each datasets, prints the result of calculation. Example Input 2 4-2*3= 4*(8+4+3)= Output -2 60 ### Response ```cpp #include <iostream> #include <string> #include <cctype> #include <cassert> #include <algorithm> using namespace std; size_t cur; string s; int expression(); int digit(){ assert(cur < s.size()); return s[cur++] - '0'; } int number(){ int n = 0.0; assert(cur < s.size()); while(cur < s.size() && isdigit(s[cur])){ n = n * 10 + digit(); } return n; } int factor(){ assert(cur < s.size()); if(isdigit(s[cur])){ return number(); }else{ cur++; int a = expression(); assert(s[cur] == ')'); cur++; return a; } } int term(){ assert(cur < s.size()); int a = factor(); while(cur < s.size() && (s[cur] == '*' || s[cur] == '/')){ char op = s[cur++]; int b = factor(); if(op == '*') a *= b; else a /= b; } return a; } int expression(){ assert(cur < s.size()); int a = term(); while(cur < s.size() && (s[cur] == '-' || s[cur] == '+')){ char op = s[cur++]; int b = term(); if(op == '-') a -= b; else a += b; } return a; } int main(){ int n; cin >> n; for(int i = 0; i < n; i++){ cur = 0; cin >> s; cout << expression() << endl; } return 0; } ```
#include <bits/stdc++.h> using namespace std; using State = string::const_iterator; int expression(State& s); int number(State& s) { int res = 0; while (isdigit(*s)) { res *= 10; res += *s - '0'; s++; } return res; } int factor(State& s) { if (*s == '(') { s++; int res = expression(s); s++; return res; } else { return number(s); } } int term(State& s) { int res = factor(s); while (true) { if (*s == '*') { s++; res *= factor(s); } else if (*s == '/') { s++; res /= factor(s); } else { break; } } return res; } int expression(State& s) { int res = term(s); while (true) { if (*s == '+') { s++; res += term(s); } else if (*s == '-') { s++; res -= term(s); } else { break; } } return res; } int main() { int q; cin >> q; while (q--) { string st; cin >> st; State s = st.begin(); cout << expression(s) << endl; } }
### Prompt Develop a solution in Cpp to the problem described below: Your task is to write a program which reads an expression and evaluates it. * The expression consists of numerical values, operators and parentheses, and the ends with '='. * The operators includes +, - , *, / where respectively represents, addition, subtraction, multiplication and division. * Precedence of the operators is based on usual laws. That is one should perform all multiplication and division first, then addition and subtraction. When two operators have the same precedence, they are applied from left to right. * You may assume that there is no division by zero. * All calculation is performed as integers, and after the decimal point should be truncated * Length of the expression will not exceed 100. * -1 × 109 ≤ intermediate results of computation ≤ 109 Input The input is a sequence of datasets. The first line contains an integer n which represents the number of datasets. There will be n lines where each line contains an expression. Output For each datasets, prints the result of calculation. Example Input 2 4-2*3= 4*(8+4+3)= Output -2 60 ### Response ```cpp #include <bits/stdc++.h> using namespace std; using State = string::const_iterator; int expression(State& s); int number(State& s) { int res = 0; while (isdigit(*s)) { res *= 10; res += *s - '0'; s++; } return res; } int factor(State& s) { if (*s == '(') { s++; int res = expression(s); s++; return res; } else { return number(s); } } int term(State& s) { int res = factor(s); while (true) { if (*s == '*') { s++; res *= factor(s); } else if (*s == '/') { s++; res /= factor(s); } else { break; } } return res; } int expression(State& s) { int res = term(s); while (true) { if (*s == '+') { s++; res += term(s); } else if (*s == '-') { s++; res -= term(s); } else { break; } } return res; } int main() { int q; cin >> q; while (q--) { string st; cin >> st; State s = st.begin(); cout << expression(s) << endl; } } ```
#include<iostream> #include<cctype> #include<string> using namespace std; typedef string::const_iterator State; int expression(State &); int term(State &); int factor(State &); int number(State &); int main(void){ int N; cin >> N; cin.ignore(); for(int i=0;i<N;i++){ string s; getline(cin,s); State begin=s.begin(); int ans=expression(begin); cout << ans << endl; } return 0; } int expression(State &begin){ int ret=term(begin); while(1){ if(*begin=='+'){ begin++; ret+=term(begin); } else if(*begin=='-'){ begin++; ret-=term(begin); } else break; } return ret; } int term(State &begin){ int ret=factor(begin); while(1){ if(*begin=='*'){ begin++; ret*=factor(begin); } else if(*begin=='/'){ begin++; ret/=factor(begin); } else break; } return ret; } int factor(State &begin){ if(*begin=='('){ begin++; int ret=expression(begin); begin++; return ret; } if(*begin=='-'){ begin++; return -factor(begin); } return number(begin); } int number(State &begin){ int ret=0; while(isdigit(*begin)){ ret*=10; ret+=*begin-'0'; begin++; } return ret; }
### Prompt Create a solution in CPP for the following problem: Your task is to write a program which reads an expression and evaluates it. * The expression consists of numerical values, operators and parentheses, and the ends with '='. * The operators includes +, - , *, / where respectively represents, addition, subtraction, multiplication and division. * Precedence of the operators is based on usual laws. That is one should perform all multiplication and division first, then addition and subtraction. When two operators have the same precedence, they are applied from left to right. * You may assume that there is no division by zero. * All calculation is performed as integers, and after the decimal point should be truncated * Length of the expression will not exceed 100. * -1 × 109 ≤ intermediate results of computation ≤ 109 Input The input is a sequence of datasets. The first line contains an integer n which represents the number of datasets. There will be n lines where each line contains an expression. Output For each datasets, prints the result of calculation. Example Input 2 4-2*3= 4*(8+4+3)= Output -2 60 ### Response ```cpp #include<iostream> #include<cctype> #include<string> using namespace std; typedef string::const_iterator State; int expression(State &); int term(State &); int factor(State &); int number(State &); int main(void){ int N; cin >> N; cin.ignore(); for(int i=0;i<N;i++){ string s; getline(cin,s); State begin=s.begin(); int ans=expression(begin); cout << ans << endl; } return 0; } int expression(State &begin){ int ret=term(begin); while(1){ if(*begin=='+'){ begin++; ret+=term(begin); } else if(*begin=='-'){ begin++; ret-=term(begin); } else break; } return ret; } int term(State &begin){ int ret=factor(begin); while(1){ if(*begin=='*'){ begin++; ret*=factor(begin); } else if(*begin=='/'){ begin++; ret/=factor(begin); } else break; } return ret; } int factor(State &begin){ if(*begin=='('){ begin++; int ret=expression(begin); begin++; return ret; } if(*begin=='-'){ begin++; return -factor(begin); } return number(begin); } int number(State &begin){ int ret=0; while(isdigit(*begin)){ ret*=10; ret+=*begin-'0'; begin++; } return ret; } ```