Extcalc - Scientific Calculator
Version 0.6.5
Inhalt
1. Introduction
2. Source Files
3. Mathematical Classes
3.1 Important Basic Functions
3.2 Class Description
3.3 Usage of the Mathematical Classes
4. Internal Interfaces
4.1 Basic Structures
4.2 Variables
4.3 Internal Interfaces GUI
4.4 Internal Interfaces Script-Interpreter
4.5 Config file and Preferences
5. Mathematical Algorithms
6. Installation Files
1. Introduction
This is the internal documentation of Calc and Extcalc. It is mainly for develops. Users don't need to read that.
It describes the source files and the internal structure of Calc and Extcalc and the Installer.
A description of the mathematical methods and algorithms is also be found here.
2. Source Files
File List:
All the files listed here, belong to the Extcalc program and they were coded by hand.
The files, which were generated by QT, are not mentioned here.
Source Files:
extcalc.cpp
buttons.cpp
calcwidget.cpp
global.cpp
graph.cpp
graphout.cpp
graphpref.cpp
graphsolve.cpp
calctable.cpp
calcinput.cpp
calcpref.cpp
functiontable.cpp
table.cpp
tablepref.cpp
scriptedit.cpp
scriptpref.cpp
scriptio.cpp
Object Header Files:
extcalc.h
buttons.h
calcwidget.h
global.h
graph.h
graphout.h
graphpref.h
graphsolve.h
calctable.h
calcinput.h
calcpref.h
functiontable.h
table.h
tablepref.h
scriptedit.h
scriptpref.h
scriptio.h
Extra Header Files:
list.h
calclocale.h
Source Tree
Here you can see the debpendencies beteween all header files.
calclocale.h list.h
| / | \________________________
________________________________|________________/ | \
/ global.h | calctable.h___________
/ _______________________________|____________________|_____________________________|________________\___________________________________________________________________________________________________________
/ / / \ | \ | \ | \ \ \ \ \ \ \
calcinput.h buttons.h graphout.h graphsolve.h functiontable.h | | | | | | |
\____ _____/ \ \______ ______/ / \ | | | | | | |
\ / \_________________________________ \ /______________________/ \ | | | | | | |
calcwidget.h graph.h table.h scriptedit.h scriptio.h graphpref.h calcpref.h tablepref.h scriptpref.h
\________________________ ___________________/ / / / / / / /
\ /_______________________________________________________________/________________/________________/___________________/_______________/_______________/_____________/
extcalc.h
File Description
extcalc:
Main Object of GUI, includes menus, tab initialisation, loading and saving config files and management of preferences.
calcwidget:
Complete calculator window (calculator tab), which includes and connects all calculator components.
calcinput:
This is the text field of the calculator. It does the calculation itself and controls user input and output.
calcpref:
Preferences dialog for calculator preferences.
graph:
Graphics main window (graphics tab). It includes and connects graph solving window and graph output window and
the function table.
graphout:
This is the GL-component of the calculator. It precalculates the function graphs and shows them in an OpenGL-window.
graphsolve:
This class includes the User Interface of the graph analysis window and the special calculation alogithms for
all the analysis functions. Screenshot saving is also controlled from there .
graphpref:
Preferences dialog for graphics preferences.
table:
Table window (table tab). This is the main window for calculating result tables of functions
tablepref:
Preferences dialog for tables.
scriptedit:
Editor window for creating, editing and managing scripts
scriptio:
Console for the text input and output of calculator scripts
scriptpref:
Preferences dialog for scripts
buttons:
This object includes both button groups of the calculator.
calctable:
Overloaded QT-table which can show text in different colors.
functiontable:
Overloaded calctable class, which manages the functions strings for graphs and tables. It can also manage
the function preferences like color, type, ...
global:
This object includes accerelated calculator class and all needed subroutines.
It also includes the script class for running calculator scripts and functions for processing configfiles and unicode.
In it's header file, all macros and the Preferences struct are defined.
3. Mathematical Functions
From v0.6.5 on, there are only two classes which can be used to process a calculation. The Calculate class and the Script class.
The normal calculator and the script interpreter use the script class. The Calculate class is used for graph drawing, function
tables and function analysis. The console calculator calc only uses the Script class. All mathematical functions and classes
are located in the global.h and global.cpp source files.
3.1 Important Basic Functions
There are several extra functions, that don't belong to any of the calculator classes, which were needed for the parsers.
This chapter describes them.
String Operations: strcut
The string operations were used to simplify string modifications in the parser and preprocessor algorithms.
Prototype:
char*strcut(char*src,int index,int len=1);
Description:
This function cuts out some characters from the given string and returns a pointer to the new string.
Parameters are the source string, the start index and the number of characters to cut out.
The returned char pointer represents a newly allocated string, allocated with new.
String Operations: strins
Prototype:
char*strins(char*dest,const char*src,int index);
Description:
This function inserts the string src into the string dest at position index. It returns a pointer on the result string.
The memory for the result string is allocated with the new operator inside the function.
String Operations: strcopy
Prototype:
int strcopy(char*dest,char*src,int len);
Description:
This function copies a part of the length len from src to dest. The dest string must be allocated before calling this function.
It must be large enough for inserting. If you don't want to copy the string to the start of dest, use for example
dest[start_index]
instead of dest.
Search Functions: bracketFind
The search functions are an important part of the parser algorithms, to find the relevant parts of a string.
Prototype:
int bracketFind(char* string,char* searchString, int start=0);
Description:
This functions searches the string string for the expression searchString. But it searches only outside of brackets.
It counts the following as brackets: ( [ { "
Only if all found open brackets were closed, it continues
searching. It is possible to search for one of the brackets. You can also set the search start index.
Search Functions: bracketFindRev
Prototype:
int bracketFindRev(char* string,char* searchString, int start=-1);
Description:
This function does the same as the function above, but it searches from the end to the start of the string.
The search start index can also be set. -1 means always start at the end.
Preprocessor: checkStringAnsi (Calc: checkString)
The preprocessor functions process a given input string and replace some operators, insert multiply operators where they are needed,
and remove whitespace and other things that are not needed for the parser.
The checkStringAnsi functions is part of Calc and Extcalc, but in Calc it is named checkString. In Extcalc is it called
checkStringAnsi, because it is written in Ansi-C++ without QT commands. Therefore, Extcalc needs an additional preprocessor
function, which is called checkString (in Extcalc). This function replaces the Unicode characters through standard ASCII.
Prototype:
char* checkStringAnsi(char*calcString,Preferences*pref);
Description:
The given parameters are the input string as char pointer and a pointer to the preferences struct.
Be careful with the return value of this function, because it can be NULL and the parser should not be called with NULL.
This function works in five steps with five loops.
The first loop removes whitespace and comments. It returns an error when the string length becomes zero or when it finds the
reserved backslash character.
The second loop replaces some operators (integral, root, ...) through their internal representations. It also replaces
the engeneering signs, for example n through e-9. Finally, it removes unneeded + und - operators. If calculator mode is
scientific, lowercase z and x are replaced through uppercase X and Z.
The third loop inserts all multiply signs that are not written, but for the parser.
The fourth loop replaces numeric constants (pi and the euler constant) through their numeric values.
The fourth loop does the same as the third one. That is needed after inserting the constants.
Preprocessor: checkString (Extcalc only)
Prototype:
char* checkString(QString calcString,Preferences*pref);
Description:
This function replaces unicode characters through ASCII code and runs checkStringAnsi. It returns a pointer to the cleaned string.
The parameters are a pointer to the Preferences struct (only to give it to checkStringAnsi) and the input string as QString.
3.2 Class Description
There are two classes which can be used to process mathematical expressions. The Calculate class and the Script class.
Both classes have the same base class called Math.
These classes build a tree that represents a matematical expression in the memory.
Both classes include a parser method that builds the tree. This parser is normally ran by the constructor of the class.
When the constructer has built an object tree, the expression can be calculated with a second method.
These method can be ran several times with different values of the variables.
Math class
This class contains some main parts, which are needed for both matematic classes.
These are the declaration:
class Math
{
protected: //These are the variables, needed for Calculate and Script
Variable*vars; //The variables array for Calc
Preferences*pref; //The preferences for this calculation
Math*parent; //A pointer to the parent object
//At the root object, this is set to NULL
Math *vertObj,*horzObj; //Pointers to the following objects
double number; //A variable as temporary memory for calculations and to store
//the value of constants
int var; //if the object represents a variable, this contains the index (A is 0)
int operation; //this variable stores the operation code
//the operation codes are defined in global.h
public:
Math(Math*par,Preferences*pr,Variable*va); //constructor
virtual ~Math(); //destructor
int getOperation(); //returns the operation code of the object
virtual double calc() {return 0.0;} //the following virtual methods run the calculator
virtual double calcVertObj() {return 0.0;} //they run recursive
virtual double calcHorzObj() {return 0.0;} //calc is overloaded in Calculate
virtual Number exec() {Number r;r.type=NNONE;return r;} //exec ist used in the Script class
virtual Number execVertObj() {Number r;r.type=NNONE;return r;}
virtual Number execHorzObj() {Number r;r.type=NNONE;return r;}
virtual int split(char*){return 0;} //this method is used to run the interpreter
//normally, this is done by the contructor, so you don't have to use it
virtual char* parse(char*){return 0;} //for the Script parser, this second method is needed
}; //it contains the parser itself, while split decides how to run it
Calculate class
This is a very fast calculator class. I can not process script commands, but it contains all calculator commands,
including integration and differentiation. It uses double variables for calculation, so the precision is limited to 15 decimal digits.
This class is used for drawing graphs and analyse functions.
This is the declaration:
class Calculate :public Math
{
virtual int split(char* line); //this private method contains the script parser
//the constructor runs it automatically
public:
Calculate(Math *par,char* line,Preferences*pr,Variable*va) :Math((Math*)par,pr,va); //constructor
~Calculate(); //destructor
virtual double calc(); //the following methods can be used to run the calculation after building the object tree
virtual double calcVertObj();
virtual double calcHorzObj();
};
Script class
The script class provides lots of matematical operations, complex numbers, string operations, matrix- and vector-processing and
script commands. It uses a Number struct for calculations.
This class is used for the calculator, to run scripts and to draw and analyze complex functions.
This is the declaration:
class Script :public Math
{
Number value; //Variable to store values and temporary memory for calculation
Math*nextObj,*vertObj2,*vertObj3; //additional pointers to build the tree
ThreadSync*eventReciver; //structure for synchronisation with the main application
//and for extra preferences
//it also contains the variables array
private:
inline bool resizeVar(int var,int newlen); //method to manage the variables memory
public:
Script(Script*par,char*line,Preferences*pr,Variable*va,ThreadSync*evrec) :Math((Math*)par,pr,va); //constructor
~Script(); //destructor
char*parse(char*line); //the two methods to build the object tree
virtual int split(char* line);
virtual double calc(); //methods to run calculator
virtual double calcVertObj();
virtual double calcHorzObj();
virtual Number exec();
virtual Number execVertObj();
virtual Number execHorzObj();
};
3.3 Usage of the Matematical Classes
Variables and Preferences
The Script class and the Calculate class need a Preferences structure. This structure contains information
about the calculator mode, how to process angles and if complex numbers are enabled.
The Preferences struct of Extcalc also includes the preferences for the Extcalc user interface.
Here is the Preferences Struct of Calc. It does only contain preferences for the Math classes:
struct Preferences
{
int angle; //angle type DEG,RAD,GRA
int outputType; //how to format output (not important for Math classes)
int outputLength; //number of output digits
int calcType; //calculator mode BASE for logic operations, SCIENTIFIC for normal operation
int base; //base for BASE-mode BIN,DEC,OCT,HEX
int precision; //maximum possible precision on this system
bool complex; //enable or disable complex numbers
};
This structure must be filled, and Script and Calculate will get a pointer to this structure.
The Calculate class uses an array of double variables to store variables.
Here is the definition of the data structure:
typedef double Variable;
Calc and Extcalc need a pointer to an array of Variable-Elements.
The array must have 27 elements for variables from A to Z and the answer memory
No furter initialisation is needed, but you may want to set all elements to 0 or to a stored value.
A short example code:
Variable *vars=(Variable*)malloc(sizeof(Variable)*27);
Extcalc needs an additional struct
Calculate
First, you need the string that contains the matematic operations to process.
char operations[]="3+4*5sinx";
This string must be processed by the preprocessor, which returns another string.
char*cleanString=checkString(operations,&pref); //returns 3+4*5*sinX
The returend string can be used to generate an object tree.
This object tree represents the matematical operations in the memory.
Calculate calc(NULL,cleanString,&pref,vars);
Now, the calc object has been created, and it can be used to run the operations.
double result=calc.calc();
calc() can be used several times with different variable values
for(int c=0; c<100; c++)
{
vars[23]=(double)c; //x is the 23rd element of the vars array
results[c]=calc.calc();
}
This help file is not complete and not up-to-date