Hi.
I'm trying to solve this problem (spoj.pl/problems/ARITH2) but i give Run Time Error and i'm not have idea
My code
#include <cstdlib>
#include <string>
#include <cstdio>
#include <cstring>
#include <vector>
#include <iostream>
#include <map>
#include <algorithm>
#include <cstdio>
#include <cctype>
#include <climits>
using namespace std;
char buffer[600], b2[600];
long long str_to_n (const char* str) {
long long t;
sscanf (str, "%lld", &t);
return t;
}
/*
long long evaluate (vector<string> n, vector<char> o, long long res, int k = 0) {
if (k == n.size() || k == o.size())
return res;
long h = str_to_n (n[k].c_str());
if (o[k] == '+') return evaluate (n, o, res + h, k + 1);
if (o[k] == '-') return evaluate (n, o, res - h, k + 1);
if (o[k] == '*') return evaluate (n, o, res * h, k + 1);
if (o[k] == '/') return evaluate (n, o, res / h, k + 1);
}*/
int main () {
int N;
gets (buffer);
sscanf (buffer, "%d", &N);
while (N--) {
gets (buffer);
gets (buffer);
vector<string> n;
vector<char> o;
strcpy (b2, buffer);
for (char* ptr = strtok (buffer, "+-/*= "); ptr != 0; n.push_back (ptr), ptr = strtok (0, "+-/*= "));
for (char* ptr = strtok (b2, "0123456789= "); ptr != 0; o.push_back (ptr[0]), ptr = strtok (0, "0123456789= "));
long long h = str_to_n (n[0].c_str());
n.erase (n.begin(), n.begin() + 1);
for (size_t i = 0; i < n.size() && i < o.size(); ++i) {
if (o[i] == '+') h = h + str_to_n (n[i].c_str());
else if (o[i] == '*') h = h * str_to_n (n[i].c_str());
else if (o[i] == '/') h = h / str_to_n (n[i].c_str());
else if (o[i] == '-') h = h - str_to_n (n[i].c_str());
}
printf ("%lld\n", h);
}
}