14 #include "Calculator.h"
15 #include "InfixToPostfix.h"
16 #include "IException.h"
17 #include "SpecialPixel.h"
31 Calculator::Calculator() {
39 Calculator::~Calculator() {
119 return a > b ? 1.0 : 0.0;
132 return a < b ? 1.0 : 0.0;
145 return a == b ? 1.0 : 0.0;
158 return a >= b ? 1.0 : 0.0;
171 return a <= b ? 1.0 : 0.0;
184 return a != b ? 1.0 : 0.0;
231 return (a > 0) ? (int)(a + 0.5) : (int)(a - 0.5);
283 if (std::isnan(a))
return (a);
284 if (std::isnan(b))
return (b);
285 return (a > b) ? a : b;
297 if (std::isnan(a))
return (a);
298 if (std::isnan(b))
return (b);
299 return (a < b) ? a : b;
307 void Calculator::Negative() {
309 PerformOperation(result, result.begin(), result.end(),
NegateOperator);
317 void Calculator::Multiply() {
322 PerformOperation(result, x.begin(), x.end(), y.begin(), y.end(),
MultiplyOperator);
330 void Calculator::Add() {
334 PerformOperation(result, x.begin(), x.end(), y.begin(), y.end(),
AddOperator);
342 void Calculator::Subtract() {
346 PerformOperation(result, x.begin(), x.end(), y.begin(), y.end(),
SubtractOperator);
354 void Calculator::Divide() {
359 PerformOperation(result, x.begin(), x.end(), y.begin(), y.end(),
DivideOperator);
366 void Calculator::Modulus() {
371 PerformOperation(result, x.begin(), x.end(), y.begin(), y.end(),
ModulusOperator);
382 void Calculator::Exponent() {
387 PerformOperation(result, x.begin(), x.end(), exponent.begin(), exponent.end(), pow);
397 void Calculator::SquareRoot() {
399 PerformOperation(result, result.begin(), result.end(), sqrt);
407 void Calculator::AbsoluteValue() {
409 PerformOperation(result, result.begin(), result.end(), fabs);
419 void Calculator::Log() {
421 PerformOperation(result, result.begin(), result.end(), log);
429 void Calculator::Log10() {
431 PerformOperation(result, result.begin(), result.end(), log10);
441 void Calculator::LeftShift() {
444 IString msg =
"When trying to do a left shift calculation, a non-scalar "
445 "shift value was encountered. Shifting requires scalars.";
446 throw IException(IException::Unknown, msg, _FILEINFO_);
451 if ((
int)y[0] > (
int)x.size()) {
452 IString msg =
"When trying to do a left shift calculation, a shift "
453 "value greater than the data size was encountered. "
454 "Shifting by this value would erase all of the data.";
455 throw IException(IException::Unknown, msg, _FILEINFO_);
459 int shift = (int)y[0];
460 result.resize(x.size());
462 for (
int i = 0; i < result.size(); i++) {
463 if (i + shift < x.size() && i + shift >= 0)
464 result[i] = x[i+shift];
466 result[i] = sqrt(-1.0);
480 void Calculator::RightShift() {
483 IString msg =
"When trying to do a right shift calculation, a non-scalar "
484 "shift value was encountered. Shifting requires scalars.";
485 throw IException(IException::Unknown, msg, _FILEINFO_);
490 if ((
int)y[0] > (
int)x.size()) {
491 IString msg =
"When trying to do a right shift calculation, a shift "
492 "value greater than the data size was encountered. "
493 "Shifting by this value would erase all of the data.";
494 throw IException(IException::Unknown, msg, _FILEINFO_);
498 int shift = (int)y[0];
499 result.resize(x.size());
501 for (
int i = 0; i < (int)result.size(); i++) {
502 if (i - shift < (
int)x.size() && i - shift >= 0) {
503 result[i] = x[i-shift];
506 result[i] = sqrt(-1.0);
518 void Calculator::MinimumLine() {
521 double minVal = result[0];
522 for (
int i = 0; i < result.size(); i++) {
524 minVal = min(minVal, result[i]);
529 result.push_back(minVal);
537 void Calculator::MaximumLine() {
540 double maxVal = result[0];
541 for (
int i = 0; i < result.size(); i++) {
543 maxVal = max(maxVal, result[i]);
548 result.push_back(maxVal);
557 void Calculator::MinimumPixel() {
562 PerformOperation(result, x.begin(), x.end(), y.begin(), y.end(),
572 void Calculator::MaximumPixel() {
577 PerformOperation(result, x.begin(), x.end(), y.begin(), y.end(),
587 void Calculator::GreaterThan() {
592 PerformOperation(result, x.begin(), x.end(), y.begin(), y.end(),
602 void Calculator::LessThan() {
607 PerformOperation(result, x.begin(), x.end(), y.begin(), y.end(),
617 void Calculator::Equal() {
622 PerformOperation(result, x.begin(), x.end(), y.begin(), y.end(),
632 void Calculator::GreaterThanOrEqual() {
637 PerformOperation(result, x.begin(), x.end(), y.begin(), y.end(),
647 void Calculator::LessThanOrEqual() {
652 PerformOperation(result, x.begin(), x.end(), y.begin(), y.end(),
662 void Calculator::NotEqual() {
667 PerformOperation(result, x.begin(), x.end(), y.begin(), y.end(),
679 void Calculator::And() {
684 PerformOperation(result, x.begin(), x.end(), y.begin(), y.end(),
693 void Calculator::Or() {
698 PerformOperation(result, x.begin(), x.end(), y.begin(), y.end(),
707 void Calculator::Sine() {
709 PerformOperation(result, result.begin(), result.end(), sin);
717 void Calculator::Cosine() {
719 PerformOperation(result, result.begin(), result.end(), cos);
727 void Calculator::Tangent() {
729 PerformOperation(result, result.begin(), result.end(), tan);
737 void Calculator::Cosecant() {
747 void Calculator::Secant() {
749 PerformOperation(result, result.begin(), result.end(),
SecantOperator);
757 void Calculator::Cotangent() {
767 void Calculator::Arcsine() {
769 PerformOperation(result, result.begin(), result.end(), asin);
777 void Calculator::Arccosine() {
779 PerformOperation(result, result.begin(), result.end(), acos);
787 void Calculator::Arctangent() {
789 PerformOperation(result, result.begin(), result.end(), atan);
797 void Calculator::ArcsineH() {
799 PerformOperation(result, result.begin(), result.end(), asinh);
807 void Calculator::ArccosineH() {
809 PerformOperation(result, result.begin(), result.end(), acosh);
817 void Calculator::ArctangentH() {
819 PerformOperation(result, result.begin(), result.end(), atanh);
827 void Calculator::Arctangent2() {
832 PerformOperation(result, x.begin(), x.end(), y.begin(), y.end(), atan2);
840 void Calculator::SineH() {
842 PerformOperation(result, result.begin(), result.end(), sinh);
850 void Calculator::CosineH() {
852 PerformOperation(result, result.begin(), result.end(), cosh);
860 void Calculator::TangentH() {
862 PerformOperation(result, result.begin(), result.end(), tanh);
874 int Calculator::StackSize() {
875 return p_valStack->size();
884 p_valStack->push(vect);
893 void Calculator::Push(
double scalar) {
908 for (
int i = 0; i < buff.
size(); i++) {
952 if (p_valStack->empty()) {
953 IString msg =
"Math calculator stack is empty, cannot perform any "
955 throw IException(IException::Unknown, msg, _FILEINFO_);
958 top = p_valStack->top();
961 for (
int i = 0; i < (int)top.size(); i++) {
962 if (std::isnan(top[i])) {
966 else if (top[i] > DBL_MAX) {
970 else if (top[i] < -DBL_MAX) {
989 void Calculator::PrintTop() {
990 if (p_valStack->empty())
return;
995 for (
int i = 0; i < (int)top.size(); i++) {
996 temp += QString::number(top[i]);
1003 temp.replace(QRegExp(
"-nan"),
"nan");
1004 std::cout<<temp<<std::endl;
1013 bool Calculator::Empty() {
1014 return p_valStack->empty();
1021 void Calculator::Clear() {
1022 while (!p_valStack->empty()) {
1040 double operation(
double)) {
1041 results.resize(arg1End - arg1Start);
1043 for (
int pos = 0; pos < results.size(); pos++) {
1044 results[pos] = operation(*arg1Start);
1071 double operation(
double,
double)) {
1072 if (arg1End - arg1Start != 1 && arg2End - arg2Start != 1 &&
1073 arg1End - arg1Start != arg2End - arg2Start) {
1074 IString msg =
"The stack based calculator cannot operate on vectors "
1075 "of differing sizes.";
1076 throw IException(IException::Programmer, msg, _FILEINFO_);
1079 int iSize = max(arg1End - arg1Start, arg2End - arg2Start);
1080 results.resize(iSize);
1082 for (
int pos = 0; pos < results.size(); pos++) {
1083 results[pos] = operation(*arg1Start, *arg2Start);
1085 if (arg1Start + 1 != arg1End) arg1Start++;
1086 if (arg2Start + 1 != arg2End) arg2Start++;