40#include <tdemessagebox.h>
41#include <knotifyclient.h>
51#ifdef HAVE_LONG_DOUBLE
52#undef HAVE_LONG_DOUBLE
55#ifndef HAVE_FUNC_ISINF
62int isinf(
double x) {
return !finite(x) && x==x; }
65extern TQPtrList<CALCAMNT> temp_stack;
66last_input_type last_input;
67char display_str[DSP_SIZE+1];
69stack_ptr top_of_stack = NULL;
70stack_ptr top_type_stack[2] = { NULL, NULL };
71int stack_next, stack_last;
72stack_item process_stack[STACK_SIZE];
75int precedence_base = 0;
76num_base current_base = NB_DECIMAL;
81item_contents display_data;
83int display_size = DEC_SIZE;
86int angle_mode = ANG_DEGREE;
94CALCAMNT memory_num = 0.0;
96int precedence[14] = { 0, 1, 2, 3, 4, 4, 5, 5, 6, 6, 6, 7, 7, 6 };
98 int adjust_op[14][3] = {
99 {FUNC_NULL, FUNC_NULL, FUNC_NULL},
100 {FUNC_OR, FUNC_OR, FUNC_XOR },
101 {FUNC_XOR, FUNC_XOR, FUNC_XOR },
102 {FUNC_AND, FUNC_AND, FUNC_AND },
103 {FUNC_LSH, FUNC_LSH, FUNC_RSH },
104 {FUNC_RSH, FUNC_RSH, FUNC_RSH },
105 {FUNC_ADD, FUNC_ADD, FUNC_ADD },
106 {FUNC_SUBTRACT, FUNC_SUBTRACT, FUNC_SUBTRACT},
107 {FUNC_MULTIPLY, FUNC_MULTIPLY, FUNC_MULTIPLY},
108 {FUNC_DIVIDE, FUNC_DIVIDE, FUNC_DIVIDE},
109 {FUNC_MOD, FUNC_MOD, FUNC_INTDIV },
110 {FUNC_POWER, FUNC_POWER, FUNC_PWR_ROOT},
111 {FUNC_PWR_ROOT, FUNC_PWR_ROOT, FUNC_PWR_ROOT},
112 {FUNC_INTDIV, FUNC_INTDIV, FUNC_INTDIV},
133 Arith Arith_ops[14] = { NULL,
139 ExecAdd, ExecSubtract,
142 ExecPower, ExecPwrRoot,
146 Prcnt Prcnt_ops[14] = { NULL,
152 ExecAddSubP, ExecAddSubP,
154 ExecDivideP, ExecDivideP,
155 ExecPowerP, ExecPwrRootP,
160void TQtCalculator::InitializeCalculator(
void) {
171 display_data.s_item_type = ITEM_AMOUNT;
172 display_data.s_item_data.item_amount = 0.0;
173 display_data.s_item_data.item_func_data.item_function = 0;
174 display_data.s_item_data.item_func_data.item_precedence = 0;
176 void fpe_handler(
int fpe_parm);
177 struct sigaction fpe_trap;
179 fpe_trap.sa_handler = &fpe_handler;
181 fpe_trap.sa_flags = SA_RESTART;
184 sigaction(SIGFPE, &fpe_trap, NULL);
190void fpe_handler(
int fpe_parm)
199void TQtCalculator::setData(
const TQRect& _range,
const char *_sheet )
201 sheet_range = _range;
205void TQtCalculator::setValue(
double _value )
208 DISPLAY_AMOUNT = _value;
216void TQtCalculator::setLabel(
const char *_text )
224 calc_display->setText( _text );
227void TQtCalculator::RefreshCalculator(
void)
240void TQtCalculator::EnterDigit(
int data)
247 strcat(display_str,
string.latin1());
248 DISPLAY_AMOUNT = (CALCAMNT) strtod(display_str,0);
255 if (refresh_display) {
262 if (!(input_limit && input_count >= input_limit))
263 if (DISPLAY_AMOUNT < 0)
264 DISPLAY_AMOUNT = decimal_point ?
265 DISPLAY_AMOUNT - ((CALCAMNT)data /
266 POW((
float)current_base, decimal_point++)) :
267 (current_base * DISPLAY_AMOUNT) - data;
269 DISPLAY_AMOUNT = decimal_point ?
270 DISPLAY_AMOUNT + ((CALCAMNT)data /
271 POW((
float)current_base, decimal_point++)) :
272 (current_base * DISPLAY_AMOUNT) + data;
278 printf(
"EnterDigit() inc dec.point:%d\n",input_count);
285void TQtCalculator::button0()
290void TQtCalculator::button1()
295void TQtCalculator::button2()
297 if (current_base == NB_BINARY)
302void TQtCalculator::button3()
304 if (current_base == NB_BINARY)
309void TQtCalculator::button4()
311 if (current_base == NB_BINARY)
316void TQtCalculator::button5()
318 if (current_base == NB_BINARY)
323void TQtCalculator::button6()
325 if (current_base == NB_BINARY)
330void TQtCalculator::button7()
332 if (current_base == NB_BINARY)
337void TQtCalculator::button8()
339 if ((current_base == NB_BINARY) || (current_base == NB_OCTAL))
344void TQtCalculator::button9()
346 if ((current_base == NB_BINARY) || (current_base == NB_OCTAL))
352void TQtCalculator::buttonA()
354 if ((current_base == NB_BINARY) || (current_base == NB_OCTAL)
355 || (current_base == NB_DECIMAL))
361void TQtCalculator::buttonB()
363 if ((current_base == NB_BINARY) || (current_base == NB_OCTAL)
364 || (current_base == NB_DECIMAL))
370void TQtCalculator::buttonC()
372 if ((current_base == NB_BINARY) || (current_base == NB_OCTAL)
373 || (current_base == NB_DECIMAL))
379void TQtCalculator::buttonD()
381 if ((current_base == NB_BINARY) || (current_base == NB_OCTAL)
382 || (current_base == NB_DECIMAL))
388void TQtCalculator::buttonE()
390 if ((current_base == NB_BINARY) || (current_base == NB_OCTAL)
391 || (current_base == NB_DECIMAL))
396void TQtCalculator::buttonF()
398 if ((current_base == NB_BINARY) || (current_base == NB_OCTAL)
399 || (current_base == NB_DECIMAL))
406void TQtCalculator::EnterDecimal()
410 KNotifyClient::beep();
415 if (refresh_display) {
421 if (last_input == DIGIT && !strpbrk( display_str,
".")){
426 calc_display->setText(strcat(display_str,
"."));
437 strcpy(display_str,
"0.");
438 calc_display->setText(display_str);
443void TQtCalculator::Or()
447 EnterStackFunction(2);
451 EnterStackFunction(1);
453 last_input = OPERATION;
456void TQtCalculator::And()
459 last_input = OPERATION;
460 EnterStackFunction(3);
464void TQtCalculator::Shift()
467 last_input = OPERATION;
469 EnterStackFunction(5);
473 EnterStackFunction(4);
478void TQtCalculator::Plus()
481 last_input = OPERATION;
482 EnterStackFunction(6);
485void TQtCalculator::Minus()
488 last_input = OPERATION;
489 EnterStackFunction(7);
493void TQtCalculator::Multiply()
496 last_input = OPERATION;
497 EnterStackFunction(8);
500void TQtCalculator::Divide()
503 last_input = OPERATION;
504 EnterStackFunction(9);
507void TQtCalculator::Mod()
510 last_input = OPERATION;
512 EnterStackFunction(13);
516 EnterStackFunction(10);
520void TQtCalculator::Power()
523 last_input = OPERATION;
525 EnterStackFunction(12);
529 EnterStackFunction(11);
536void TQtCalculator::EnterStackFunction(
int data)
538 item_contents new_item;
555 data = adjust_op[data][dummy];
558 PushStack(&display_data);
560 new_item.s_item_type = ITEM_FUNCTION;
561 new_item.s_item_data.item_func_data.item_function = data;
562 new_item.s_item_data.item_func_data.item_precedence =
563 new_precedence = precedence[data] + precedence_base;
566 if (UpdateStack(new_precedence))
568 PushStack(&new_item);
571void TQtCalculator::EnterNegate()
576 string = display_str;
578 pos =
string.findRev(
'e',-1,
false);
582 if(display_str[pos+1] ==
'+')
583 display_str[pos+1] =
'-';
585 if(display_str[pos+1] ==
'-')
586 display_str[pos+1] =
'+';
588 string.insert(pos +1,
'-');
589 strncpy(display_str,
string.latin1(),DSP_SIZE);
592 DISPLAY_AMOUNT = (CALCAMNT)strtod(display_str,0);
597 if (DISPLAY_AMOUNT != 0) {
598 DISPLAY_AMOUNT *= -1;
605void TQtCalculator::EnterOpenParen()
608 last_input = OPERATION;
609 precedence_base += PRECEDENCE_INCR;
614void TQtCalculator::EnterCloseParen()
617 last_input = OPERATION;
618 PushStack(&display_data);
620 if (UpdateStack(precedence_base))
622 if ((precedence_base -= PRECEDENCE_INCR) < 0)
627void TQtCalculator::EnterRecip()
630 last_input = OPERATION;
631 DISPLAY_AMOUNT = 1 / DISPLAY_AMOUNT;
636void TQtCalculator::EnterInt()
639 CALCAMNT work_amount1 = 0.0, work_amount2 = 0.0;
641 last_input = OPERATION;
643 work_amount2 = MODF(DISPLAY_AMOUNT, &work_amount1);
644 DISPLAY_AMOUNT = work_amount2 ;
647 DISPLAY_AMOUNT = work_amount1;
656void TQtCalculator::EnterFactorial()
659 CALCAMNT work_amount1, work_amount2;
662 MODF(DISPLAY_AMOUNT, &work_amount1);
664 incr = work_amount1 < 0 ? -1 : 1;
665 work_amount2 = work_amount1 - incr;
666 while (work_amount1 != 0 && work_amount2 != 0 && !display_error) {
667 work_amount1 *= work_amount2;
668 work_amount2 -= incr;
669 if(isinf(work_amount1)) {
675 if( work_amount1 == 0.0)
678 DISPLAY_AMOUNT = work_amount1;
680 last_input = OPERATION;
685void TQtCalculator::EnterSquare()
689 DISPLAY_AMOUNT *= DISPLAY_AMOUNT;
691 else if (DISPLAY_AMOUNT < 0)
694 DISPLAY_AMOUNT = SQRT(DISPLAY_AMOUNT);
697 last_input = OPERATION;
702void TQtCalculator::EnterNotCmp()
708 MODF(DISPLAY_AMOUNT, &boh_work_d);
709 if (FABS(boh_work_d) > LONG_MAX)
712 boh_work = (
long int) boh_work_d;
713 DISPLAY_AMOUNT = ~boh_work;
716 last_input = OPERATION;
721void TQtCalculator::EnterHyp()
724 switch(kcalcdefaults.style){
727 if ( !sheet_name.isEmpty() )
732 DISPLAY_AMOUNT = stats.count();
733 last_input = OPERATION;
740 DISPLAY_AMOUNT = stats.sum();
741 last_input = OPERATION;
751 hyp_mode = !hyp_mode;
754 statusHYPLabel->setText(
"HYP");
757 statusHYPLabel->setText(
"");
765void TQtCalculator::ExecSin(){
767 switch(kcalcdefaults.style){
776 if ( !sheet_name.isEmpty() )
784 if ( !sheet_name.isEmpty() )
795void TQtCalculator::ComputeSum()
799 DISPLAY_AMOUNT = stats.sum();
804 last_input = OPERATION;
808void TQtCalculator::ComputeMul()
812 DISPLAY_AMOUNT = stats.mul();
817 last_input = OPERATION;
821void TQtCalculator::ComputeMin()
825 DISPLAY_AMOUNT = stats.min();
830 last_input = OPERATION;
834void TQtCalculator::ComputeMax()
838 DISPLAY_AMOUNT = stats.max();
843 last_input = OPERATION;
847void TQtCalculator::ComputeMean(){
851 DISPLAY_AMOUNT = stats.mean();
856 last_input = OPERATION;
862 DISPLAY_AMOUNT = stats.sum_of_squares();
866 last_input = OPERATION;
873void TQtCalculator::ComputeSin()
875 CALCAMNT work_amount;
877 work_amount = DISPLAY_AMOUNT;
882 DISPLAY_AMOUNT = SINH( work_amount);
885 DISPLAY_AMOUNT = ASINH( work_amount);
886 if (errno == EDOM || errno == ERANGE)
895 switch (angle_mode) {
897 work_amount = DEG2RAD(DISPLAY_AMOUNT);
900 work_amount = GRA2RAD(DISPLAY_AMOUNT);
903 work_amount = DISPLAY_AMOUNT;
906 DISPLAY_AMOUNT = SIN( work_amount);
910 DISPLAY_AMOUNT = ASIN(work_amount);
911 switch (angle_mode) {
913 work_amount = RAD2DEG(DISPLAY_AMOUNT);
916 work_amount = RAD2GRA(DISPLAY_AMOUNT);
919 work_amount = DISPLAY_AMOUNT;
922 DISPLAY_AMOUNT = work_amount;
923 if (errno == EDOM || errno == ERANGE)
931 if (DISPLAY_AMOUNT < POS_ZERO && DISPLAY_AMOUNT > NEG_ZERO)
934 last_input = OPERATION;
939void TQtCalculator::ExecCos(){
941 switch(kcalcdefaults.style){
950 if ( !sheet_name.isEmpty() )
958 if ( !sheet_name.isEmpty() )
969void TQtCalculator::ComputeStd(){
974 DISPLAY_AMOUNT = stats.std();
981 last_input = OPERATION;
988 DISPLAY_AMOUNT = stats.sample_std();
994 last_input = OPERATION;
1003void TQtCalculator::ComputeCos()
1005 CALCAMNT work_amount;
1007 work_amount = DISPLAY_AMOUNT;
1012 DISPLAY_AMOUNT = COSH( work_amount);
1015 DISPLAY_AMOUNT = ACOSH( work_amount);
1016 if (errno == EDOM || errno == ERANGE)
1025 switch (angle_mode) {
1027 work_amount = DEG2RAD(DISPLAY_AMOUNT);
1030 work_amount = GRA2RAD(DISPLAY_AMOUNT);
1033 work_amount = DISPLAY_AMOUNT;
1036 DISPLAY_AMOUNT = COS( work_amount);
1040 DISPLAY_AMOUNT = ACOS(work_amount);
1041 switch (angle_mode) {
1043 work_amount = RAD2DEG(DISPLAY_AMOUNT);
1046 work_amount = RAD2GRA(DISPLAY_AMOUNT);
1049 work_amount = DISPLAY_AMOUNT;
1053 DISPLAY_AMOUNT = work_amount;
1055 if (errno == EDOM || errno == ERANGE)
1064 if (DISPLAY_AMOUNT < POS_ZERO && DISPLAY_AMOUNT > NEG_ZERO)
1066 refresh_display = 1;
1067 last_input = OPERATION;
1072void TQtCalculator::ExecTan(){
1074 switch(kcalcdefaults.style){
1085 if ( !sheet_name.isEmpty() )
1096void TQtCalculator::ComputeMedean(){
1101 DISPLAY_AMOUNT = stats.median();
1107 refresh_display = 1;
1108 last_input = OPERATION;
1115 DISPLAY_AMOUNT = stats.median();
1120 refresh_display = 1;
1121 last_input = OPERATION;
1128void TQtCalculator::ComputeTan()
1130 CALCAMNT work_amount;
1132 work_amount = DISPLAY_AMOUNT;
1137 DISPLAY_AMOUNT = TANH( work_amount);
1140 DISPLAY_AMOUNT = ATANH( work_amount);
1141 if (errno == EDOM || errno == ERANGE)
1150 switch (angle_mode) {
1152 work_amount = DEG2RAD(DISPLAY_AMOUNT);
1155 work_amount = GRA2RAD(DISPLAY_AMOUNT);
1158 work_amount = DISPLAY_AMOUNT;
1161 DISPLAY_AMOUNT = TAN( work_amount);
1162 if (errno == EDOM || errno == ERANGE)
1167 DISPLAY_AMOUNT = ATAN(work_amount);
1168 switch (angle_mode) {
1170 work_amount = RAD2DEG(DISPLAY_AMOUNT);
1173 work_amount = RAD2GRA(DISPLAY_AMOUNT);
1176 work_amount = DISPLAY_AMOUNT;
1180 DISPLAY_AMOUNT = work_amount;
1182 if (errno == EDOM || errno == ERANGE)
1190 if (DISPLAY_AMOUNT < POS_ZERO && DISPLAY_AMOUNT > NEG_ZERO)
1192 refresh_display = 1;
1193 last_input = OPERATION;
1199void TQtCalculator::EnterPercent()
1202 last_input = OPERATION;
1209void TQtCalculator::EnterLogr()
1212 switch(kcalcdefaults.style){
1215 if ( !sheet_name.isEmpty() )
1223 if ( !sheet_name.isEmpty() )
1228 stats.enterData(DISPLAY_AMOUNT);
1229 last_input = OPERATION;
1230 refresh_display = 1;
1231 DISPLAY_AMOUNT = stats.count();
1236 last_input = OPERATION;
1237 refresh_display = 1;
1239 setStatusLabel(
"Last stat item erased");
1240 DISPLAY_AMOUNT = stats.count();
1250 last_input = OPERATION;
1253 if (DISPLAY_AMOUNT <= 0)
1256 DISPLAY_AMOUNT = LOG_TEN(DISPLAY_AMOUNT);
1257 refresh_display = 1;
1259 }
else if (inverse) {
1260 DISPLAY_AMOUNT = POW(10, DISPLAY_AMOUNT);
1261 refresh_display = 1;
1270void TQtCalculator::EnterLogn()
1273 switch(kcalcdefaults.style){
1276 if ( !sheet_name.isEmpty() )
1285 if ( !sheet_name.isEmpty() )
1291 setStatusLabel(i18n(
"Stat mem cleared"));
1303 last_input = OPERATION;
1305 if (DISPLAY_AMOUNT <= 0)
1308 DISPLAY_AMOUNT = LOG(DISPLAY_AMOUNT);
1309 refresh_display = 1;
1311 }
else if (inverse) {
1312 DISPLAY_AMOUNT = EXP(DISPLAY_AMOUNT);
1313 refresh_display = 1;
1324void TQtCalculator::base_selected(
int number){
1345void TQtCalculator::angle_selected(
int number){
1362void TQtCalculator::SetInverse(){
1364 inverse = ! inverse;
1366 statusINVLabel->setText(
"INV");
1369 statusINVLabel->setText(
"NORM");
1374void TQtCalculator::SetDeg() {
1375 angle_mode = ANG_DEGREE;
1378void TQtCalculator::SetGra() {
1379 angle_mode = ANG_GRADIENT;
1382void TQtCalculator::SetRad() {
1383 angle_mode = ANG_RADIAN;
1387void TQtCalculator::SetHex() {
1392 current_base = NB_HEX;
1393 display_size = BOH_SIZE;
1400void TQtCalculator::SetOct() {
1405 current_base = NB_OCTAL;
1406 display_size = BOH_SIZE;
1413void TQtCalculator::SetBin() {
1418 current_base = NB_BINARY;
1419 display_size = BOH_SIZE;
1426void TQtCalculator::SetDec()
1432 current_base = NB_DECIMAL;
1433 display_size = DEC_SIZE;
1441void TQtCalculator::EE()
1444 DISPLAY_AMOUNT = pi;
1453 strcat(display_str,
"e");
1461void TQtCalculator::MR()
1464 last_input = OPERATION;
1465 DISPLAY_AMOUNT = memory_num;
1466 refresh_display = 1;
1471void TQtCalculator::Mplusminus()
1477 memory_num += DISPLAY_AMOUNT;
1479 memory_num -= DISPLAY_AMOUNT;
1484void TQtCalculator::MC()
1488 refresh_display = 1;
1491void TQtCalculator::EnterEqual()
1494 last_input = OPERATION;
1495 PushStack(&display_data);
1496 refresh_display = 1;
1502 precedence_base = 0;
1506 if(temp_stack.count() > TEMP_STACK_SIZE){
1508 number = temp_stack.getFirst();
1509 temp_stack.removeFirst();
1515 number = (CALCAMNT*) malloc(
sizeof(CALCAMNT));
1516 *number = DISPLAY_AMOUNT;
1520 temp_stack.append(number);
1525void TQtCalculator::Clear(){
1532 if(last_input == OPERATION){
1545 refresh_display = 0;
1548 if (!refresh_display) {
1549 DISPLAY_AMOUNT = 0L;
1555void TQtCalculator::ClearAll()
1561 RefreshCalculator();
1562 refresh_display = 1;
1568void TQtCalculator::UpdateDisplay()
1575 CALCAMNT boh_work_d;
1579 if(eestate && (current_base == NB_DECIMAL)){
1581 calc_display->setText(display_str);
1585 if (current_base != NB_DECIMAL) {
1586 MODF(DISPLAY_AMOUNT, &boh_work_d);
1587 if (boh_work_d < LONG_MIN || boh_work_d > ULONG_MAX)
1596 else if (boh_work_d > LONG_MAX) {
1598 LONG_MIN+(boh_work_d-LONG_MAX-1);
1599 boh_work = (long)DISPLAY_AMOUNT;
1602 DISPLAY_AMOUNT = boh_work_d;
1603 boh_work = (long) boh_work_d;
1607 if (!display_error) {
1609 if (current_base == NB_BINARY)
1610 str_size = cvb(display_str,
1613 else if (current_base == NB_OCTAL)
1614 str_size = sprintf(display_str,
1617 else if (current_base == NB_DECIMAL) {
1619 if(!kcalcdefaults.fixed || last_input == DIGIT
1620 || (DISPLAY_AMOUNT > 1.0e+16)){
1626 str_size = sprintf(display_str,
1628#ifdef HAVE_LONG_DOUBLE
1631 kcalcdefaults.precision +1,
1635 kcalcdefaults.precision +1,
1641 str_size = sprintf(display_str,
1643#ifdef HAVE_LONG_DOUBLE
1646 kcalcdefaults.fixedprecision ,
1650 kcalcdefaults.fixedprecision ,
1656 if ( input_count > 0 && !strpbrk(display_str,
"e") &&
1657 last_input == DIGIT ) {
1659#ifdef HAVE_LONG_DOUBLE
1660 str_size = sprintf(display_str,
1662 (kcalcdefaults.precision +1 > input_count)?
1663 input_count : kcalcdefaults.precision ,
1666 str_size = sprintf(display_str,
1668 (kcalcdefaults.precision +1 > input_count)?
1669 input_count : kcalcdefaults.precision ,
1676 if (current_base == NB_HEX)
1677 str_size = sprintf(display_str,
1684 if (display_error || str_size < 0) {
1686 strcpy(display_str,
"Error");
1687 if(kcalcdefaults.beep)
1688 KNotifyClient::beep();
1690 calc_display->setText(display_str);
1694 statusINVLabel->setText(
"INV");
1697 statusINVLabel->setText(
"NORM");
1701 statusHYPLabel->setText(
"HYP");
1704 statusHYPLabel->setText(
"");
1710int cvb(
char *out_str,
long amount,
int max_digits)
1717 char work_str[(
sizeof(amount) * CHAR_BIT) + 1];
1722 work_size =
sizeof(amount) * CHAR_BIT;
1723 unsigned long bit_mask = (1 << ((
sizeof(amount) * CHAR_BIT) - 1));
1727 if (amount & bit_mask) {
1731 work_str[work_char++] =
'1';
1735 work_str[work_char++] =
'0';
1740 work_str[work_char++] =
'0';
1741 work_str[work_char] =
'\0';
1743 if (work_char-lead_one_count < max_digits)
1744 return strlen(strcpy(out_str,
1745 &work_str[lead_one_count ?
1746 work_size - max_digits :
1752int UpdateStack(
int run_precedence)
1754 item_contents new_item, *top_item , *top_function;
1755 CALCAMNT left_op =0.0 , right_op =0.0;
1756 int op_function= 0, return_value = 0;
1758 new_item.s_item_type = ITEM_AMOUNT;
1759 while ((top_function = TopTypeStack(ITEM_FUNCTION)) &&
1760 top_function->s_item_data.item_func_data.item_precedence >=
1765 if ((top_item = PopStack())->s_item_type != ITEM_AMOUNT){
1766 KMessageBox::error( 0,
"Stack processing error - right_op");
1769 right_op = top_item->s_item_data.item_amount;
1771 if (!((top_item = PopStack()) &&
1772 top_item->s_item_type == ITEM_FUNCTION)) {
1773 KMessageBox::error( 0,
"Stack processing error - function");
1777 top_item->s_item_data.item_func_data.item_function;
1779 if (!((top_item = PopStack()) &&
1780 top_item->s_item_type == ITEM_AMOUNT)) {
1781 KMessageBox::error( 0,
"Stack processing error - left_op");
1783 left_op = top_item->s_item_data.item_amount;
1785 new_item.s_item_data.item_amount =
1786 (Arith_ops[op_function])(left_op, right_op);
1787 PushStack(&new_item);
1793 Prcnt_ops[op_function] != NULL){
1794 new_item.s_item_data.item_amount =
1795 (Prcnt_ops[op_function])(left_op,
1797 new_item.s_item_data.item_amount);
1798 PushStack(&new_item);
1801 DISPLAY_AMOUNT = new_item.s_item_data.item_amount;
1803 return return_value;
1806int isoddint(CALCAMNT input)
1812 return (MODF(input, &dummy) == 0.0 &&
1813 MODF(input/2, &dummy) == 0.5);
1816CALCAMNT ExecOr(CALCAMNT left_op, CALCAMNT right_op)
1819 CALCAMNT boh_work_d;
1820 long boh_work_l, boh_work_r;
1822 MODF(left_op, &boh_work_d);
1823 if (FABS(boh_work_d) > LONG_MAX) {
1827 boh_work_l = (
long int)boh_work_d;
1828 MODF(right_op, &boh_work_d);
1829 if (FABS(boh_work_d) > LONG_MAX) {
1833 boh_work_r = (
long int) boh_work_d;
1834 return (boh_work_l | boh_work_r);
1837CALCAMNT ExecXor(CALCAMNT left_op, CALCAMNT right_op)
1840 CALCAMNT boh_work_d;
1841 long boh_work_l, boh_work_r;
1843 MODF(left_op, &boh_work_d);
1844 if (FABS(boh_work_d) > LONG_MAX) {
1848 boh_work_l = (
long int) boh_work_d;
1849 MODF(right_op, &boh_work_d);
1850 if (FABS(boh_work_d) > LONG_MAX) {
1854 boh_work_r = (
long int) boh_work_d;
1855 return (boh_work_l ^ boh_work_r);
1858CALCAMNT ExecAnd(CALCAMNT left_op, CALCAMNT right_op)
1861 CALCAMNT boh_work_d;
1862 long boh_work_l, boh_work_r;
1864 MODF(left_op, &boh_work_d);
1865 if (FABS(boh_work_d) > LONG_MAX) {
1869 boh_work_l = (
long int ) boh_work_d;
1870 MODF(right_op, &boh_work_d);
1871 if (FABS(boh_work_d) > LONG_MAX) {
1875 boh_work_r = (
long int ) boh_work_d;
1876 return (boh_work_l & boh_work_r);
1879CALCAMNT ExecLsh(CALCAMNT left_op, CALCAMNT right_op)
1882 CALCAMNT boh_work_d;
1883 long boh_work_l, boh_work_r;
1885 MODF(left_op, &boh_work_d);
1886 if (FABS(boh_work_d) > LONG_MAX) {
1890 boh_work_l = (
long int) boh_work_d;
1891 MODF(right_op, &boh_work_d);
1892 if (FABS(boh_work_d) > LONG_MAX) {
1896 boh_work_r = (
long int ) boh_work_d;
1897 return (boh_work_l << boh_work_r);
1900CALCAMNT ExecRsh(CALCAMNT left_op, CALCAMNT right_op)
1903 CALCAMNT boh_work_d;
1904 long boh_work_l, boh_work_r;
1906 MODF(left_op, &boh_work_d);
1907 if (FABS(boh_work_d) > LONG_MAX) {
1911 boh_work_l = (
long int ) boh_work_d;
1912 MODF(right_op, &boh_work_d);
1913 if (FABS(boh_work_d) > LONG_MAX) {
1917 boh_work_r = (
long int ) boh_work_d;
1918 return (boh_work_l >> boh_work_r);
1921CALCAMNT ExecAdd(CALCAMNT left_op, CALCAMNT right_op)
1924 return left_op + right_op;
1927CALCAMNT ExecSubtract(CALCAMNT left_op, CALCAMNT right_op)
1930 return left_op - right_op;
1933CALCAMNT ExecMultiply(CALCAMNT left_op, CALCAMNT right_op)
1936 return left_op * right_op;
1939CALCAMNT ExecDivide(CALCAMNT left_op, CALCAMNT right_op)
1942 if (right_op == 0) {
1946 return left_op / right_op;
1949CALCAMNT ExecMod(CALCAMNT left_op, CALCAMNT right_op)
1954 if (right_op == 0) {
1960 right_op = FABS(right_op);
1962 temp = FMOD(left_op, right_op);
1968 if( temp < 0 ) temp = right_op + temp;
1975CALCAMNT ExecIntDiv(CALCAMNT left_op, CALCAMNT right_op)
1978 if (right_op == 0) {
1982 MODF(left_op / right_op, &left_op);
1987CALCAMNT ExecPower(CALCAMNT left_op, CALCAMNT right_op)
1993 if (left_op < 0 && isoddint(1/right_op))
1994 left_op = -1L * POW((-1L * left_op), right_op);
1996 left_op = POW(left_op, right_op);
1997 if (errno == EDOM || errno == ERANGE) {
2004CALCAMNT ExecPwrRoot(CALCAMNT left_op, CALCAMNT right_op)
2008 if (right_op == 0) {
2012 if (left_op < 0 && isoddint(right_op))
2013 left_op = -1L * POW((-1L * left_op), (1L)/right_op);
2015 left_op = POW(left_op, (1L)/right_op);
2016 if (errno == EDOM || errno == ERANGE) {
2025CALCAMNT ExecAddSubP(CALCAMNT left_op, CALCAMNT right_op, CALCAMNT result)
2034 return (result * 100L) / right_op;
2037CALCAMNT ExecMultiplyP(CALCAMNT left_op, CALCAMNT right_op, CALCAMNT result)
2043 return (result / 100L);
2046CALCAMNT ExecDivideP(CALCAMNT left_op, CALCAMNT right_op, CALCAMNT result)
2052 return (result * 100L);
2055CALCAMNT ExecPowerP(CALCAMNT left_op, CALCAMNT right_op, CALCAMNT result)
2059 return ExecPower(left_op, (right_op / 100L));
2062CALCAMNT ExecPwrRootP(CALCAMNT left_op, CALCAMNT right_op, CALCAMNT result)
2067 if (right_op == 0) {
2071 return ExecPower(left_op, (100L / right_op));
2076stack_ptr AllocStackItem (
void) {
2078 if (stack_next <= stack_last) {
2080 process_stack[stack_next].prior_item = NULL;
2081 process_stack[stack_next].prior_type = NULL;
2082 return &process_stack[stack_next++];
2085 KMessageBox::error( 0,
"Stack Error !");
2086 return &process_stack[stack_next];
2089void UnAllocStackItem (stack_ptr return_item) {
2091 if (return_item != &process_stack[--stack_next]) {
2093 KMessageBox::error( 0,
"Stack Error !");
2097void PushStack(item_contents *add_item)
2103 stack_ptr new_item = top_of_stack;
2106 new_item->item_value.s_item_type == add_item->s_item_type)) {
2108 new_item = AllocStackItem();
2114 new_item->prior_item = top_of_stack;
2115 top_of_stack = new_item;
2116 new_item->prior_type = top_type_stack[add_item->s_item_type];
2117 top_type_stack[add_item->s_item_type] = new_item;
2120 new_item->item_value = *add_item;
2124item_contents *PopStack(
void)
2130 static item_contents return_item;
2131 item_contents *return_item_ptr = NULL;
2132 stack_ptr return_stack_ptr;
2134 if ((return_stack_ptr = top_of_stack)) {
2135 return_item = top_of_stack->item_value;
2137 top_type_stack[return_item.s_item_type]
2138 = top_of_stack->prior_type;
2139 top_of_stack = top_of_stack->prior_item;
2141 UnAllocStackItem(return_stack_ptr);
2142 return_item_ptr = &return_item;
2145 return return_item_ptr;
2148item_contents *TopOfStack(
void)
2154 item_contents *return_item_ptr = NULL;
2157 return_item_ptr = &top_of_stack->item_value;
2160 return return_item_ptr;
2163item_contents *TopTypeStack(item_type rqstd_type)
2169 item_contents *return_item_ptr = NULL;
2171 if (top_type_stack[rqstd_type]) {
2172 return_item_ptr = &top_type_stack[rqstd_type]->item_value;
2175 return return_item_ptr;
2185void InitStack (
void) {
2188 stack_last = STACK_SIZE - 1;
2189 top_of_stack = top_type_stack[0] = top_type_stack[1] = NULL;