void parseStr(const char* input_str, char** ret, int count, int idx) { int i = 0; while (isspace(input_str[i])) ++i; int spaces = getSpaces(input_str, count); ret[idx] = (char*)malloc(count - spaces); // *** error (need + 1) char* begin = ret[idx]; while (!isspace(input_str[i]) && !ispunct(input_str[i])) { // first line if (isCap(input_str[i]) && !isNum(input_str[i])) *ret[idx] = (char)((input_str[i]) - 'A' + 'a'); else *ret[idx] = input_str[i]; ret[idx]++; ++i; } ++i; if (!ispunct(input_str[i])) { int prev = 1; for (; i < count; ++i) { // maybe use isspace || isalpha if (prev && !isspace(input_str[i])) { // make sure not more than one consecutive space if (!isCap(input_str[i]) && !isNum(input_str[i])) *ret[idx] = (char)((input_str[i]) - 'a' + 'A'); else *ret[idx] = input_str[i]; ret[idx]++; prev = 0; if (isNum(input_str[i])) prev = 1; } else if (isCap(input_str[i]) && !isspace(input_str[i])) { *ret[idx] = (char)((input_str[i]) - 'A' + 'a'); ret[idx]++; } else if (!isCap(input_str[i]) && !isspace(input_str[i])) { *ret[idx] = input_str[i]; ret[idx]++; } else if (isspace(input_str[i])) { prev = 1; } } } *ret[idx] = '\0'; // end string ret[idx] = begin; }
bool Calculator::getValue() { while (!valStack.empty()) valStack.pop(); string::iterator it = formulaSuffixExpress.begin(); while (it != formulaSuffixExpress.end()) { if (isCap(*it)) { valStack.push(values[*it]); } else { bool ans1, ans2; switch (*it) { case '!': ans1 = valStack.top(); valStack.pop(); ans1 = !ans1; valStack.push(ans1); break; case '&': ans1 = valStack.top(); valStack.pop(); ans2 = valStack.top(); valStack.pop(); ans1 = ans1 & ans2; valStack.push(ans1); break; case '|': ans1 = valStack.top(); valStack.pop(); ans2 = valStack.top(); valStack.pop(); ans1 = ans1 | ans2; valStack.push(ans1); break; case '>': ans1 = valStack.top(); valStack.pop(); ans2 = valStack.top(); valStack.pop(); ans1 = ans1 | !ans2; valStack.push(ans1); break; default: break; } } it++; } return valStack.top(); }
void Calculator::count() { charSet.clear(); string::iterator it = formula.begin(); while (it != formula.end()) { if (isCap(*it)) charSet.insert(*it); it++; } propositionNumber = charSet.size(); }
void Calculator::getFormulaSuffixExpress() { string::iterator it = formula.begin(); while (!operators.empty()) operators.pop(); while(it != formula.end()) { if (isCap(*it)) { formulaSuffixExpress += *it; } else { if (!operators.empty() && *it != '(') { while (!operators.empty() && operatorPri[operators.top()] >= operatorPri[*it]) { formulaSuffixExpress += operators.top(); operators.pop(); } if (*it == ')') { while (!operators.empty()) { if (operators.top() != '(' && operators.top() != ')') formulaSuffixExpress += operators.top(); operators.pop(); } } else { operators.push(*it); } } else { operators.push(*it); } } it++; } while (!operators.empty()) { char ch = operators.top(); if (ch != '(' && ch != ')') formulaSuffixExpress += ch; operators.pop(); } }