testing-repository/cpp/calc.cpp

64 lines
1.7 KiB
C++

#include <iostream>
#include<cstdlib>
#include<cstring>
static int pos = 0;
int calc_expression_value(const char * expression);
int calc_factor_value(const char * factor){
int result = 0;
char c = factor[pos];
if( c == '('){ // if expression has '(', calculate the expression in the '()'
pos++;
result = calc_expression_value(factor);
pos++;
}else{
while(isdigit(c)){ // Get the number to calculate
result = 10 * result + c - '0';
pos++;
c = factor[pos];
}
}
return result;
}
int calc_term_value(const char * term){
int result = calc_factor_value(term); // Get the first number
while(true){
//pos++; // Calc the multiplication and divide
char op = term[pos];
if(op == '*' || op == '/'){
pos++;
int value = calc_factor_value(term); // Get another value
if(op == '*') result *= value;
else result /= value;
}else{
break;
}
}
return result;
}
int calc_expression_value(const char * expression){
int result = calc_term_value(expression); // Calc the result of expression
//bool more = true;
while(true){
char op = expression[pos];
if(op == '+' || op == '-'){ // Calculate for add and subb
pos++;
int value = calc_term_value(expression);
if( op == '+') result += value;
else result -= value;
}else{
break;
}
}
return result;
}
int main(int argc,char ** argv){
char expression[1000];
std::cin.getline(expression,sizeof(expression));
std::cout<<calc_expression_value(expression)<<std::endl;
return 0;
}