⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 gwcalc.cpp

📁 嵌入式linux下的一个高级计算器程序的源码
💻 CPP
📖 第 1 页 / 共 3 页
字号:
/*    gwCalc.cpp  by Dr. Gerhard Wanderer   (gerhard.wanderer@i-dail.de)    $Id: gwCalc.cpp,v 1.2 2002/12/17 15:31:55 gerhard Exp gerhard $    Pocket Calculator for the Zaurus and for Linux with QT-Library    =====================================================================*/#define VERSION "gwCalc V1.0"#include <ctype.h>#include <malloc.h>#include <math.h>#include <stdio.h>#include <stdlib.h>#include <string.h>#include <unistd.h>#ifdef ZAURUS#  include <qpe/qpeapplication.h>#else#  include <sys/wait.h>#  include <qapplication.h>#endif#include <qwidget.h>#include <qpushbutton.h>#include <qfont.h>#include <qlcdnumber.h>#include <qmultilineedit.h>#include <qpixmap.h>#include <qlabel.h>#include <qframe.h>#include <qclipboard.h>#include <qcombobox.h>#include "gwCalc.h"#include "pict/ln.xpm"#include "pict/e-exp.xpm"#include "pict/y-exp.xpm" #include "pict/log10.xpm" #include "pict/10x.xpm" #include "pict/bsqrt.xpm"#include "pict/log2.xpm"#include "pict/2-exp.xpm"#include "pict/x-quad.xpm" #include "pict/div.xpm" #include "pict/abs.xpm"#include "pict/sqrt.xpm" #include "pict/cot.xpm" #include "pict/sec.xpm" #include "pict/csc.xpm" #include "pict/atan2.xpm" #include "pict/sign.xpm" #include "pict/log.xpm" #include "pict/inv.xpm"#include "pict/hyp.xpm"#include "pict/leer.xpm"#include "pict/sin.xpm"#include "pict/cos.xpm"#include "pict/tan.xpm"#include "pict/switch.xpm"#include "pict/pi.xpm"#include "pict/up.xpm"#include "pict/down.xpm"QClipboard      *clipboard;//// -------------------------------------------------------------------// own definition of MultilinekEdit so that the backspace function can// be connected to a button gwCalcEdit::gwCalcEdit(QWidget *parent) : QMultiLineEdit(parent)  { }voidgwCalcEdit::gwCalcEditDel() {  backspace();}// -------------------------------------------------------------------//  The Main Widget: Signal Functions for all Buttons of the main//  Keypad//  most of them simply insert a character into the MultilineEdit//  Field, which is used to type in the formula////  -------------- main keypad ---------------------------------------voidgwCalcMain::InsertButtonB01() {  inpfeld->insert("1");  }voidgwCalcMain::InsertButtonB02() {  inpfeld->insert("2");  }voidgwCalcMain::InsertButtonB03() {  inpfeld->insert("3");  }voidgwCalcMain::InsertButtonB04() {  inpfeld->insert("4");  }voidgwCalcMain::InsertButtonB05() {  inpfeld->insert("5"); }voidgwCalcMain::InsertButtonB06() {  inpfeld->insert("6");  }voidgwCalcMain::InsertButtonB07() {  inpfeld->insert("7");  }voidgwCalcMain::InsertButtonB08() {  inpfeld->insert("8");  }voidgwCalcMain::InsertButtonB09() {  inpfeld->insert("9");  }voidgwCalcMain::InsertButtonB10() {  inpfeld->insert("0"); }voidgwCalcMain::InsertButtonB11() {  inpfeld->insert(",");  }voidgwCalcMain::InsertButtonB12() {  inpfeld->insert("e");  }voidgwCalcMain::InsertButtonB13()  // Clear {  inpfeld->clear();   InsertButtonB24();  b13->setPalette(*cclr ); }voidgwCalcMain::InsertButtonB14() {  inpfeld->insert("ANS "); }voidgwCalcMain::InsertButtonB15() {  inpfeld->insert("Pi ");  }voidgwCalcMain::InsertButtonB16() {  inpfeld->insert("("); }voidgwCalcMain::InsertButtonB17() {  inpfeld->insert(")");  }voidgwCalcMain::InsertButtonB18() {  inpfeld->insert("/");  }voidgwCalcMain::InsertButtonB19() {  inpfeld->insert("*"); }voidgwCalcMain::InsertButtonB20() {  inpfeld->insert("-");  }voidgwCalcMain::InsertButtonB21(){  inpfeld->insert("+");  }// ---------- command keypad ---------------------------------------voidgwCalcMain::InsertButtonB23()  // Backspace one character{  inpfeld->gwCalcEditDel();}voidgwCalcMain::InsertButtonB24()  // EXE: evaluate the formula {  char         result[40], exp[10], ausg[40], *text, *tmp, *thist, *p1;  QString      qs;  double       res, man, expo;  int          iexpo, i, k, j;  res = 0;  CalculatorError = ERR_NOERROR;  // get the formula  qs = inpfeld->text();  if(qs) {    tmp = (char*)qs.latin1();     if (tmp && strlen(tmp)) {      text = (char*)malloc(strlen(tmp)+1);      strcpy(text, tmp);      // remember the formula, because "Calculator" destroys the string      thist = (char*) malloc(strlen(text)+1);      strcpy(thist, text);      // evaluate the formula      res = Calculator(text, &CalculatorFkt[0]);      if ((!CalculatorError) && ((CalculatorCmdHistoryAnz == 0) || 	  (strcmp(thist,                   CalculatorCmdHistory[CalculatorCmdHistoryAnz-1])))) {	// if there is no mistake in the formula and the formula is not        // already the last element of the history list, put the formual 	// to the history list	if (CalculatorCmdHistoryAnz == 100) {	  // the history list is full, remove the oldes entry	  for (i = 0; i < 99; i++) 	    CalculatorCmdHistory[i] = CalculatorCmdHistory[i+1];	  CalculatorCmdHistoryAnz--;	}	CalculatorCmdHistory[CalculatorCmdHistoryAnz] = thist;	CalculatorCmdCurhist = CalculatorCmdHistoryAnz;	CalculatorCmdHistoryAnz++;	free(text);      }      else {	free(text);	free(thist);      }    }  }  if (CalculatorError) {    // got an error, display Errormessage    lcd->setText(calculator_errtxt[CalculatorError-1]);    return;  }  // don't display garbage values  if (fabs(res) > 1.e-301)     expo  = log10(fabs(res));  else    expo  = 0;  if (CalculatorFmt == 1) {    // -------------  Engineering Formt ----------------------------------    // "normalize" the exponent for display in engineering format    if (expo < -2) expo -= 3;    iexpo = (int)(expo/3)*3;     // check for floating point errors, which were not signaled by    // the variable CalculatorError    if ((iexpo > 1000) || (iexpo < -1000)){      lcd->setText("Floating Point Error");      return;    }#ifdef ZAURUS    // crashes on Linus Desktop, don't know why    sprintf (result, "%18.16g", res);    clipboard->setText(result);#endif    // calculate the mantissa for the choosen exponent    man   = res / pow(10, (double)iexpo);     sprintf (result, "%14.12f", man);        k = -100;    j = 0;    // insert blankes after group of 3 digits to make display    // more readable    for (i = 0; (unsigned int)i < strlen(result); i++){      ausg[j++] = result[i];      k++;      if (result[i] == '.') k = 0;      if (k == 3){	ausg[j++] = ' ';	k = 0;      }    }    ausg[j++] = 0;        // remove '0' at end of string    for (i = strlen(ausg)-1; i > 0; i--) {      if ((ausg[i] == '0') || (ausg[i] == ' '))	ausg[i] = 0;      else	break;    }    if (iexpo == 0)      exp[0] = 0;    else      sprintf (exp,    "%+04d", iexpo);  }  if (CalculatorFmt == 2) {    // -------------  Standard Formt ----------------------------------    sprintf (result, "%18.16g", res);    p1 = strchr(result, 'e');    if (p1) {      for (k = 1; k < strlen(p1); k++) {	exp[k-1] = p1[k];	exp[k] = 0;      }      p1[0] = 0;    }    strcpy(ausg, result);  }  // display the result  lcd->setText(ausg);  lcdexp->setText(exp);  b24->setPalette(*cexe );  // clear the formula input  inpfeld->clear();  CalculatorCmdCurhist = CalculatorCmdHistoryAnz;  if (CalculatorAutoAns) {    inpfeld->insert("ANS ");     if (CalculatorAutoHighlight)      inpfeld->setSelection (0, 0, 0, 4 );  }}voidgwCalcMain::InputReturn(){  // Enter pressed in the input field starts the evaluation  // of the formula. The 'Return' character is removed from  // the formula  inpfeld->gwCalcEditDel();  InsertButtonB24();}// -------------------  functions keypad --------------------// //  This keys represent the build in functions. All keys have//  represent different functions depending on the status of//  the flag ifkt. ifkt represents the function layer, which//  is displayed on the buttons.voidgwCalcMain::InsertButtonF01(){  switch (ifkt) {  case 1:    inpfeld->insert("LN(");    break;  case 2:     if (iinv) {      iinv = 0;      ql01->setText("");    }    else {      iinv = 1;      ql01->setText("INV");    }    break;  case 3:    inpfeld->insert("ABS(");    break;  }}voidgwCalcMain::InsertButtonF02()

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -