Untitled
unknown
c_cpp
3 years ago
20 kB
5
Indexable
#include <sstream> #include <string> #include <iostream> #include <cmath> #include <iomanip> class FixedPoint { public: FixedPoint() : repr_{0} {} FixedPoint(double val) { repr_ = std::round(val * 100); } explicit FixedPoint(long long repr) : repr_{repr} { } std::string ToString() const { std::ostringstream out; out.precision(2); out << std::fixed << (repr_ / 100.); return out.str(); } long long GetRepr() const { return repr_; } private: long long repr_; }; FixedPoint operator+(const FixedPoint& lhs, const FixedPoint& rhs) { return FixedPoint{lhs.GetRepr() + rhs.GetRepr()}; } FixedPoint operator-(const FixedPoint& lhs, FixedPoint& rhs) { return FixedPoint{lhs.GetRepr()- rhs.GetRepr()}; } FixedPoint operator*(const FixedPoint& lhs, const FixedPoint& rhs) { long long repr = lhs.GetRepr() * rhs.GetRepr(); if (repr >= 0) { return FixedPoint{(repr + 50) / 100}; } return FixedPoint{(repr - 50) / 100}; } FixedPoint operator/(const FixedPoint& lhs, const FixedPoint& rhs) { long long repr = lhs.GetRepr() * 1000 / rhs.GetRepr(); if (repr >= 0) { return FixedPoint{(repr + 5) / 10}; } return FixedPoint{(repr - 5) / 10}; } bool operator>(const FixedPoint& lhs, const FixedPoint& rhs) { return lhs.GetRepr() > rhs.GetRepr(); } bool operator<(const FixedPoint& lhs, const FixedPoint& rhs) { return lhs.GetRepr() < rhs.GetRepr(); } bool operator==(const FixedPoint& lhs, const FixedPoint& rhs) { return lhs.GetRepr() == rhs.GetRepr(); } std::ostream& operator<<(std::ostream& os, const FixedPoint& p) { return os << p.ToString(); } std::istream& operator>>(std::istream& is, FixedPoint& p) { double val; is >> val; p = FixedPoint{val}; return is; } std::string output [1002][27] ={}; long long result[1002][27] = {}; short check[1002][27] = {}; int main() { std::string line; int row_number = 0 , column_numbeR_max = -1; while(getline(std::cin, line)) { int Column_number = 0; size_t begin_pos = 0; while (true) { size_t end_pos = line.find(',', begin_pos); output[row_number][Column_number] = line.substr(begin_pos, end_pos - begin_pos); Column_number++; if (end_pos == std::string::npos) { break; } begin_pos = end_pos+1; } row_number++; if(Column_number > column_numbeR_max) column_numbeR_max = Column_number; } std::stringstream ss1; for(int i = 0 ; i < row_number ; i++) { for(int j = 0 ; j < column_numbeR_max ; j++) { if(output[i][j][0] == '#') { output[i][j] = output[i][j].substr(1,output[i][j].length() - 1); check[i][j] = -1; } else if(output[i][j][0] != '=' ) { int z = 0; if(output[i][j][0] == '-') z = 1; std::string s = output[i][j]; if(z) s = output[i][j].substr(1,output[i][j].length() - 1); check[i][j] = 1; ss1 << s; float f = 0; ss1 >> f; ss1.clear(); if(z) f *= -1; long long ll = llround(100*f); result[i][j] = ll; } else { std::string s = output[i][j]; std::string delim = "+-*/><"; int char_place = 0; char c = '0'; bool flag = 0; for(size_t k = 2; k < s.length() ; k++) { for(int m = 0; m < 6; m++) { if(s[k] == delim[m]) { c = delim[m]; char_place = k; flag = 1; break; } if(flag) break; } } long long lhs = 0, rhs = 0; int lhs_index = 0, rhs_index = 0; if(isalpha(s[1])) { int index = s[1] - 'A'; for(int k = 2; k < char_place ; k++) { lhs_index = 10 * lhs_index + (s[k] - '0'); } if(index >= column_numbeR_max || lhs_index > row_number) { check[i][j] = -1; output[i][j] = "ERROR"; continue; } else if(lhs_index - 1 == i && index == j) { check[i][j] = -1; output[i][j] = "ERROR"; continue; } else if(check[lhs_index - 1][index] == -1) { check[i][j] = -1; output[i][j] = "ERROR"; continue; } else if (check[lhs_index - 1][index] == 0) { continue; } else { lhs = result[lhs_index - 1][index]; } } else { int z = 0; if(s[1] == '-') z = 1; std::string ssss = output[i][j].substr(1+z, char_place - 1 - z); float f ; ss1 << ssss; ss1 >> f; ss1.clear(); lhs = llround(100*f); if(z) lhs*= -1; } if(isalpha(s[char_place + 1])) { int index = s[char_place + 1] - 'A'; for(size_t k = char_place + 2; k < s.length() ; k++) { rhs_index = 10 * rhs_index + (s[k] - '0'); } if(index >= column_numbeR_max || rhs_index > row_number ) { check[i][j] = -1; output[i][j] = "ERROR"; continue; } else if(rhs_index - 1 == i && index == j) { check[i][j] = -1; output[i][j] = "ERROR"; continue; } else if(check[rhs_index - 1][index] == -1) { check[i][j] = -1; output[i][j] = "ERROR"; continue; } else if (check[rhs_index - 1][index] == 0) { continue; } else { rhs = result[rhs_index - 1][index]; } } else { int z1 = 0; if(s[char_place + 1] == '-') z1++; std::string sss = output[i][j].substr(char_place + 1 + z1, s.length() - char_place - 1 - z1); float f ; ss1 << sss; ss1 >> f; ss1.clear(); rhs = llround(100*f); if(z1) rhs *= -1; } FixedPoint num1(lhs); FixedPoint num2(rhs); if(c == '+') { long long result_temp = (num1+num2).GetRepr(); if(llabs(result_temp) > 10000000) { check[i][j] = -1; output[i][j] = "ERROR"; continue; } else { output[i][j] = (num1+num2).ToString(); result[i][j] = result_temp; check[i][j] = 1; } } if(c == '-') { long long result_temp = (num1-num2).GetRepr(); if(llabs(result_temp) > 10000000) { check[i][j] = -1; output[i][j] = "ERROR"; continue; } else { output[i][j] = (num1-num2).ToString(); result[i][j] = result_temp; check[i][j] = 1; } } if(c == '*') { long long result_temp = (num1*num2).GetRepr(); if(llabs(result_temp) > 10000000) { check[i][j] = -1; output[i][j] = "ERROR"; continue; } else { output[i][j] = (num1*num2).ToString(); result[i][j] = result_temp; check[i][j] = 1; } } if(c == '/') { if(num2.GetRepr() == 0) { check[i][j] = -1; output[i][j] = "ERROR"; continue; } long long result_temp = (num1/num2).GetRepr(); if(llabs(result_temp) > 10000000) { check[i][j] = -1; output[i][j] = "ERROR"; continue; } else { output[i][j] = (num1/num2).ToString(); result[i][j] = result_temp; check[i][j] = 1; } } if(c == '<') { if(num1 < num2) { check[i][j] = -1; output[i][j] = "true"; continue; } else { check[i][j] = -1; output[i][j] = "false"; continue; } } if(c == '>') { if(num1 > num2) { check[i][j] = -1; output[i][j] = "true"; continue; } else { check[i][j] = -1; output[i][j] = "false"; continue; } } } } } int flag_big = 1; unsigned long long error = 0, error1 = -1; while(flag_big) { if(error1 == error) break; error1 = error; error = 0; flag_big = 0; for(int i = 0 ; i < row_number ; i++) { for(int j = 0 ; j < column_numbeR_max ; j++) { if(output[i][j][0] == '=') { std::string s = output[i][j]; std::string delim = "+-*/><"; int char_place = 0; char c = '0'; bool flag = 0; for(size_t k = 2; k < s.length() ; k++) { for(int m = 0; m < 6; m++) { if(s[k] == delim[m]) { c = delim[m]; char_place = k; flag = 1; break; } if(flag) break; } } long long lhs = 0, rhs = 0; int lhs_index = 0, rhs_index = 0; if(isalpha(s[1])) { int index = s[1] - 'A'; for(int k = 2; k < char_place ; k++) { lhs_index = 10 * lhs_index + (s[k] - '0'); } if(index >= column_numbeR_max || lhs_index > row_number) { check[i][j] = -1; output[i][j] = "ERROR"; continue; } else if(lhs_index - 1 == i && index == j) { check[i][j] = -1; output[i][j] = "ERROR"; continue; } else if(check[lhs_index - 1][index] == -1) { check[i][j] = -1; output[i][j] = "ERROR"; continue; } else if (check[lhs_index - 1][index] == 0) { error = error + lhs_index + index + 1; flag_big = 1; continue; } else { lhs = result[lhs_index - 1][index]; } } else { int z2 = 0; if(s[1] == '-') z2++; std::string sss = output[i][j].substr(1 + z2, char_place - 1 - z2); float f ; ss1 << sss; ss1 >> f; ss1.clear(); lhs = llround(100*f); } if(isalpha(s[char_place + 1])) { int index = s[char_place + 1] - 'A'; for(size_t k = char_place + 2; k < s.length() ; k++) { rhs_index = 10 * rhs_index + (s[k] - '0'); } if(index >= column_numbeR_max || rhs_index > row_number ) { check[i][j] = -1; output[i][j] = "ERROR"; continue; } else if(rhs_index - 1 == i && index == j) { check[i][j] = -1; output[i][j] = "ERROR"; continue; } else if(check[rhs_index - 1][index] == -1) { check[i][j] = -1; output[i][j] = "ERROR"; continue; } else if (check[rhs_index - 1][index] == 0) { error = error + lhs_index + index + 1; flag_big = 1; continue; } else { rhs = result[rhs_index - 1][index]; } } else { int z = 0; if(s[char_place + 1] == '-') z++; std::string ssss = output[i][j].substr(char_place + 1 + z, s.length() - char_place - 1 - z); float f ; ss1 << ssss; ss1 >> f; ss1.clear(); rhs = llround(100*f); if(z) rhs *= -1; } FixedPoint num1(lhs); FixedPoint num2(rhs); if(c == '+') { long long result_temp = (num1+num2).GetRepr(); if(llabs(result_temp) > 10000000) { check[i][j] = -1; output[i][j] = "ERROR"; continue; } else { output[i][j] = (num1+num2).ToString(); result[i][j] = result_temp; check[i][j] = 1; } } else if(c == '-') { long long result_temp1 = (num1-num2).GetRepr(); if(llabs(result_temp1) > 10000000) { check[i][j] = -1; output[i][j] = "ERROR"; continue; } else { output[i][j] = (num1-num2).ToString(); result[i][j] = result_temp1; check[i][j] = 1; } } else if(c == '*') { long long result_temp2 = (num1*num2).GetRepr(); if(llabs(result_temp2) > 10000000) { check[i][j] = -1; output[i][j] = "ERROR"; continue; } else { output[i][j] = (num1*num2).ToString(); result[i][j] = result_temp2; check[i][j] = 1; } } else if(c == '/') { if(num2.GetRepr() == 0) { check[i][j] = -1; output[i][j] = "ERROR"; continue; } long long result_temp3 = (num1/num2).GetRepr(); if(llabs(result_temp3) > 10000000) { check[i][j] = -1; output[i][j] = "ERROR"; continue; } else { output[i][j] = (num1/num2).ToString(); result[i][j] = result_temp3; check[i][j] = 1; } } if(c == '<') { if(num1 < num2) { check[i][j] = -1; output[i][j] = "true"; continue; } else { check[i][j] = -1; output[i][j] = "false"; continue; } } if(c == '>') { if(num1 > num2) { check[i][j] = -1; output[i][j] = "true"; continue; } else { check[i][j] = -1; output[i][j] = "false"; continue; } } } } } } for(int i = 0 ; i < row_number ; i++) { for(int j = 0 ; j < column_numbeR_max ; j++) { if(output[i][j][0] == '=') { output[i][j] = "ERROR"; } } } for(int i = 0 ; i < row_number ; i++) { for(int j = 0 ; j < column_numbeR_max ; j++) { if(j!= 0) std::cout << ','; std::cout << output[i][j] ; } std::cout << std::endl; } }
Editor is loading...