summaryrefslogtreecommitdiff
Unidiff
Diffstat (more/less context) (ignore whitespace changes)
-rw-r--r--noncore/apps/opie-sheet/ChangeLog4
-rw-r--r--noncore/apps/opie-sheet/sheet.cpp10
-rw-r--r--noncore/apps/opie-sheet/sheet.h2
3 files changed, 11 insertions, 5 deletions
diff --git a/noncore/apps/opie-sheet/ChangeLog b/noncore/apps/opie-sheet/ChangeLog
index 56887c5..e8a31ee 100644
--- a/noncore/apps/opie-sheet/ChangeLog
+++ b/noncore/apps/opie-sheet/ChangeLog
@@ -1,64 +1,68 @@
1January 8, 2004
2 - fixed issue : "-if someone enters directly text as parameter to a string function the text renders as uppercase due to the calculation engine that uppercases all the parsing sentence."
3 now the text is handled correctly.
4
1January 7, 2004 5January 7, 2004
2 * Release by hayzel (koppermind@panafonet.gr) 6 * Release by hayzel (koppermind@panafonet.gr)
3 This version has many valuable changes, though It may have some annoying bugs. Please if you are interested in opie-sheet try it hard, so I can fix some of them. Also If you want some other functions that must be here and are missing feel free to ask them. (no financial functions please. :) I really hate them ) 7 This version has many valuable changes, though It may have some annoying bugs. Please if you are interested in opie-sheet try it hard, so I can fix some of them. Also If you want some other functions that must be here and are missing feel free to ask them. (no financial functions please. :) I really hate them )
4 -Fixed a bug with non closed parenthesis editing&recalculation infinite loop. 8 -Fixed a bug with non closed parenthesis editing&recalculation infinite loop.
5 -Added support for functions that can parse parameters not ONLY as numbers but also as strings. 9 -Added support for functions that can parse parameters not ONLY as numbers but also as strings.
6 -Added many functions that cover many computational topics rendering opie-sheet a computational tool-spreadsheet at last. (total 90 functions!) 10 -Added many functions that cover many computational topics rendering opie-sheet a computational tool-spreadsheet at last. (total 90 functions!)
7 -Maintained compatibility with the opie-fileformat. 11 -Maintained compatibility with the opie-fileformat.
8 -New icons. 12 -New icons.
9 -Found that the DataParser was not a real RPN compiler of the expressions. In fact it was returning faulty results in calculations, in both binary or unary operations. A1-A2-A3 was parsed as A1-(A2-A3). -A1 was parsed as A1. 13 -Found that the DataParser was not a real RPN compiler of the expressions. In fact it was returning faulty results in calculations, in both binary or unary operations. A1-A2-A3 was parsed as A1-(A2-A3). -A1 was parsed as A1.
10 -Added new class "Expression" a general Parser for spreadsheet-expression. Imported from an old C# project of mine. 14 -Added new class "Expression" a general Parser for spreadsheet-expression. Imported from an old C# project of mine.
11 -Now can also parse <>=!%&^|"" in expressions. 15 -Now can also parse <>=!%&^|"" in expressions.
12 -Added experimental Excel File format import!. The opie-sheet can import any excel file in BIFF7/BIFF8 format. These formats are used in Excel XP,2000,95. The Excel Importer class is in a good coding level.. BUT it is not complete. Only strings,numbers,formulas are imported. Not formatting rules. Not all the functions are converted in the functions of opie-sheet. Infact FEW functions are converted. 16 -Added experimental Excel File format import!. The opie-sheet can import any excel file in BIFF7/BIFF8 format. These formats are used in Excel XP,2000,95. The Excel Importer class is in a good coding level.. BUT it is not complete. Only strings,numbers,formulas are imported. Not formatting rules. Not all the functions are converted in the functions of opie-sheet. Infact FEW functions are converted.
13 -Fixed a bug with Sheet Recalculation. Added ReCalc() function. Opie-sheet was calculating wrong the values of expression in opening/importing. if a value needed was not loaded yet in the time of calculation. Solved with ReCalc() each time the active sheet is changing. 17 -Fixed a bug with Sheet Recalculation. Added ReCalc() function. Opie-sheet was calculating wrong the values of expression in opening/importing. if a value needed was not loaded yet in the time of calculation. Solved with ReCalc() each time the active sheet is changing.
14 18
15 19
16 *known issues: 20 *known issues:
17 -if someone enters directly text as parameter to a string function the text renders as uppercase due to the calculation engine that uppercases all the parsing sentence. 21 -if someone enters directly text as parameter to a string function the text renders as uppercase due to the calculation engine that uppercases all the parsing sentence.
18 -randbetween return only integer part random... if both limit numbers are integers. 22 -randbetween return only integer part random... if both limit numbers are integers.
19 -skew and kurt function give different results compared to kspread-oofice equivalents. 23 -skew and kurt function give different results compared to kspread-oofice equivalents.
20 -unstable parser Excel Class 24 -unstable parser Excel Class
21 -string vars and string functions are not correctly handled by excel importer. 25 -string vars and string functions are not correctly handled by excel importer.
22 -unicode strings are converted FINE in QString unicode format, but cannot be rendered fine if a suitable unicode font is not setuped as the default string. So the string is junked in the opie-sheet and may crash the parser. 26 -unicode strings are converted FINE in QString unicode format, but cannot be rendered fine if a suitable unicode font is not setuped as the default string. So the string is junked in the opie-sheet and may crash the parser.
23 27
24 *TODOs: 28 *TODOs:
25 -surelly a much full-stable excel importer. 29 -surelly a much full-stable excel importer.
26 -Cell Manipulation of many Data is really slow.... must change the QList data type. To a structure more efficient. 30 -Cell Manipulation of many Data is really slow.... must change the QList data type. To a structure more efficient.
27 -maybe some more functions. 31 -maybe some more functions.
28 -maybe some kind of charts drawing? 32 -maybe some kind of charts drawing?
29 -maybe kspread or ooffice files import/export. 33 -maybe kspread or ooffice files import/export.
30 34
31October 08, 2002 35October 08, 2002
32 36
33 * Release 1.0.2 (by thufir) 37 * Release 1.0.2 (by thufir)
34 - Fixed bug #250 (exhibiting re-calculation error in multi-level calculation). (by thufir) 38 - Fixed bug #250 (exhibiting re-calculation error in multi-level calculation). (by thufir)
35 39
36October 06, 2002 40October 06, 2002
37 41
38 - Fixed data edit box clear problem. (by thufir) 42 - Fixed data edit box clear problem. (by thufir)
39 43
40October 05, 2002 44October 05, 2002
41 45
42 - Fixed bug #252 (starting up in 'mouse button' mode). (by thufir) 46 - Fixed bug #252 (starting up in 'mouse button' mode). (by thufir)
43 47
44August 31, 2002 48August 31, 2002
45 49
46 - Fixed and updated opie-sheet.pro file. (by cniehaus) 50 - Fixed and updated opie-sheet.pro file. (by cniehaus)
47 51
48August 01, 2002 52August 01, 2002
49 53
50 - Fixed gcc3 compile problems. (by harlekin) 54 - Fixed gcc3 compile problems. (by harlekin)
51 55
52July 06, 2002 56July 06, 2002
53 57
54 - Fixed DocLnk management to save documents correctly. (by leseb) 58 - Fixed DocLnk management to save documents correctly. (by leseb)
55 59
56July 04, 2002 60July 04, 2002
57 61
58 * Release 1.0.1 (by thufir) 62 * Release 1.0.1 (by thufir)
59 - Sheet/Qt is now a part of Opie, so its name is changed to Opie Sheet. (by thufir) 63 - Sheet/Qt is now a part of Opie, so its name is changed to Opie Sheet. (by thufir)
60 - Fixed sorting bug. (by thufir) 64 - Fixed sorting bug. (by thufir)
61 65
62April 14, 2002 66April 14, 2002
63 67
64 * Release 1.0.0 (by thufir) 68 * Release 1.0.0 (by thufir)
diff --git a/noncore/apps/opie-sheet/sheet.cpp b/noncore/apps/opie-sheet/sheet.cpp
index 103b83b..e1e4744 100644
--- a/noncore/apps/opie-sheet/sheet.cpp
+++ b/noncore/apps/opie-sheet/sheet.cpp
@@ -1,2565 +1,2567 @@
1/*************************************************************************** 1/***************************************************************************
2 * * 2 * *
3 * This program is free software; you can redistribute it and/or modify * 3 * This program is free software; you can redistribute it and/or modify *
4 * it under the terms of the GNU General Public License as published by * 4 * it under the terms of the GNU General Public License as published by *
5 * the Free Software Foundation; either version 2 of the License, or * 5 * the Free Software Foundation; either version 2 of the License, or *
6 * (at your option) any later version. * 6 * (at your option) any later version. *
7 * * 7 * *
8 ***************************************************************************/ 8 ***************************************************************************/
9 9
10/* 10/*
11 * Opie Sheet (formerly Sheet/Qt) 11 * Opie Sheet (formerly Sheet/Qt)
12 * by Serdar Ozler <sozler@sitebest.com> 12 * by Serdar Ozler <sozler@sitebest.com>
13 */ 13 */
14 14
15#include "sheet.h" 15#include "sheet.h"
16 16
17#include <qmainwindow.h> 17#include <qmainwindow.h>
18#include <qmessagebox.h> 18#include <qmessagebox.h>
19#include <math.h> 19#include <math.h>
20#include <stdlib.h> 20#include <stdlib.h>
21#include <stdio.h> 21#include <stdio.h>
22#include <time.h> 22#include <time.h>
23 23
24#define DEFAULT_COL_WIDTH 50 24#define DEFAULT_COL_WIDTH 50
25 25
26Sheet::Sheet(int numRows, int numCols, QWidget *parent) 26Sheet::Sheet(int numRows, int numCols, QWidget *parent)
27 :QTable(numRows, numCols, parent) 27 :QTable(numRows, numCols, parent)
28{ 28{
29 defaultBorders.right=defaultBorders.bottom=QPen(Qt::gray, 1, Qt::SolidLine); 29 defaultBorders.right=defaultBorders.bottom=QPen(Qt::gray, 1, Qt::SolidLine);
30 defaultCellData.data=""; 30 defaultCellData.data="";
31 defaultCellData.background=QBrush(Qt::white, Qt::SolidPattern); 31 defaultCellData.background=QBrush(Qt::white, Qt::SolidPattern);
32 defaultCellData.alignment=(Qt::AlignmentFlags)(Qt::AlignLeft | Qt::AlignTop); 32 defaultCellData.alignment=(Qt::AlignmentFlags)(Qt::AlignLeft | Qt::AlignTop);
33 defaultCellData.fontColor=Qt::black; 33 defaultCellData.fontColor=Qt::black;
34 defaultCellData.font=font(); 34 defaultCellData.font=font();
35 defaultCellData.borders=defaultBorders; 35 defaultCellData.borders=defaultBorders;
36 36
37 clicksLocked=FALSE; 37 clicksLocked=FALSE;
38 selectionNo=-1; 38 selectionNo=-1;
39 setSelectionMode(QTable::Single); 39 setSelectionMode(QTable::Single);
40 40
41 sheetData.setAutoDelete(TRUE); 41 sheetData.setAutoDelete(TRUE);
42 clipboardData.setAutoDelete(TRUE); 42 clipboardData.setAutoDelete(TRUE);
43 for (int i=0; i<numCols; ++i) 43 for (int i=0; i<numCols; ++i)
44 horizontalHeader()->setLabel(i, getHeaderString(i+1), DEFAULT_COL_WIDTH); 44 horizontalHeader()->setLabel(i, getHeaderString(i+1), DEFAULT_COL_WIDTH);
45 45
46 46
47 connect(this, SIGNAL(currentChanged(int, int)), this, SLOT(slotCellSelected(int, int))); 47 connect(this, SIGNAL(currentChanged(int, int)), this, SLOT(slotCellSelected(int, int)));
48 connect(this, SIGNAL(valueChanged(int, int)), this, SLOT(slotCellChanged(int, int))); 48 connect(this, SIGNAL(valueChanged(int, int)), this, SLOT(slotCellChanged(int, int)));
49} 49}
50 50
51Sheet::~Sheet() 51Sheet::~Sheet()
52{ 52{
53} 53}
54 54
55typeCellData *Sheet::findCellData(int row, int col) 55typeCellData *Sheet::findCellData(int row, int col)
56{ 56{
57 typeCellData *tempCellData; 57 typeCellData *tempCellData;
58 for (tempCellData=sheetData.first(); tempCellData; tempCellData=sheetData.next()) 58 for (tempCellData=sheetData.first(); tempCellData; tempCellData=sheetData.next())
59 { 59 {
60 if (tempCellData->row==row && tempCellData->col==col) return tempCellData; 60 if (tempCellData->row==row && tempCellData->col==col) return tempCellData;
61 } 61 }
62 return NULL; 62 return NULL;
63} 63}
64 64
65void Sheet::slotCellSelected(int row, int col) 65void Sheet::slotCellSelected(int row, int col)
66{ 66{
67 typeCellData *cellData=findCellData(row, col); 67 typeCellData *cellData=findCellData(row, col);
68 if (cellData) 68 if (cellData)
69 { 69 {
70 emit currentDataChanged(cellData->data); 70 emit currentDataChanged(cellData->data);
71 }else 71 }else
72 emit currentDataChanged(""); 72 emit currentDataChanged("");
73} 73}
74 74
75typeCellData *Sheet::createCellData(int row, int col) 75typeCellData *Sheet::createCellData(int row, int col)
76{ 76{
77 if (row<0 || col<0) return NULL; 77 if (row<0 || col<0) return NULL;
78 typeCellData *cellData=new typeCellData; 78 typeCellData *cellData=new typeCellData;
79 cellData->row=row; 79 cellData->row=row;
80 cellData->col=col; 80 cellData->col=col;
81 cellData->data=defaultCellData.data; 81 cellData->data=defaultCellData.data;
82 cellData->borders=defaultCellData.borders; 82 cellData->borders=defaultCellData.borders;
83 cellData->alignment=defaultCellData.alignment; 83 cellData->alignment=defaultCellData.alignment;
84 cellData->font=defaultCellData.font; 84 cellData->font=defaultCellData.font;
85 cellData->fontColor=defaultCellData.fontColor; 85 cellData->fontColor=defaultCellData.fontColor;
86 cellData->background=defaultCellData.background; 86 cellData->background=defaultCellData.background;
87 sheetData.append(cellData); 87 sheetData.append(cellData);
88 return cellData; 88 return cellData;
89} 89}
90 90
91void Sheet::slotCellChanged(int row, int col) 91void Sheet::slotCellChanged(int row, int col)
92{ 92{
93 typeCellData *cellData=findCellData(row, col); 93 typeCellData *cellData=findCellData(row, col);
94 if (!cellData) cellData=createCellData(row, col); 94 if (!cellData) cellData=createCellData(row, col);
95 if (cellData) cellData->data=text(row, col); 95 if (cellData) cellData->data=text(row, col);
96 for (cellData=sheetData.first(); cellData; cellData=sheetData.next()) 96 for (cellData=sheetData.first(); cellData; cellData=sheetData.next())
97 { 97 {
98 // modified by Toussis Manolis koppermind@panafonet.gr 98 // modified by Toussis Manolis koppermind@panafonet.gr
99 // the parser was crashing if there were no closed parenthesis. 99 // the parser was crashing if there were no closed parenthesis.
100 int w1,ii=0; 100 int w1,ii=0;
101 for(w1=0;w1<=(int)text(row, col).length();w1++) 101 for(w1=0;w1<=(int)text(row, col).length();w1++)
102 { 102 {
103 if(text(row,col)[w1]=='(') ii++; 103 if(text(row,col)[w1]=='(') ii++;
104 if(text(row,col)[w1]==')') ii--; 104 if(text(row,col)[w1]==')') ii--;
105 }; 105 };
106 if(ii==0) setText(cellData->row, cellData->col, dataParser(findCellName(cellData->row, cellData->col), cellData->data)); 106 if(ii==0) setText(cellData->row, cellData->col, dataParser(findCellName(cellData->row, cellData->col), cellData->data));
107 //end of modification 107 //end of modification
108 // old was plain: 108 // old was plain:
109 //setText(cellData->row, cellData->col, dataParser(findCellName(cellData->row, cellData->col), cellData->data)); 109 //setText(cellData->row, cellData->col, dataParser(findCellName(cellData->row, cellData->col), cellData->data));
110 }; 110 };
111 emit sheetModified(); 111 emit sheetModified();
112} 112}
113 113
114 114
115void Sheet::ReCalc(void) 115void Sheet::ReCalc(void)
116{ 116{
117 typeCellData* cellData; 117 typeCellData* cellData;
118 for (cellData=sheetData.first(); cellData; cellData=sheetData.next()) 118 for (cellData=sheetData.first(); cellData; cellData=sheetData.next())
119 { 119 {
120 //printf("cellchanged:%d, %d\r\n",cellData->row,cellData->col); 120 //printf("cellchanged:%d, %d\r\n",cellData->row,cellData->col);
121 121
122 slotCellChanged(cellData->row,cellData->col); 122 slotCellChanged(cellData->row,cellData->col);
123 }; 123 };
124}; 124};
125 125
126 126
127 127
128 128
129void Sheet::swapCells(int row1, int col1, int row2, int col2) 129void Sheet::swapCells(int row1, int col1, int row2, int col2)
130{ 130{
131 typeCellData *cellData1=findCellData(row1, col1), *cellData2=findCellData(row2, col2); 131 typeCellData *cellData1=findCellData(row1, col1), *cellData2=findCellData(row2, col2);
132 if (!cellData1) cellData1=createCellData(row1, col1); 132 if (!cellData1) cellData1=createCellData(row1, col1);
133 if (!cellData2) cellData2=createCellData(row2, col2); 133 if (!cellData2) cellData2=createCellData(row2, col2);
134 if (cellData1 && cellData2) 134 if (cellData1 && cellData2)
135 { 135 {
136 QString tempData(cellData1->data); 136 QString tempData(cellData1->data);
137 cellData1->data=cellData2->data; 137 cellData1->data=cellData2->data;
138 cellData2->data=tempData; 138 cellData2->data=tempData;
139 setText(cellData1->row, cellData1->col, dataParser(findCellName(cellData1->row, cellData1->col), cellData1->data)); 139 setText(cellData1->row, cellData1->col, dataParser(findCellName(cellData1->row, cellData1->col), cellData1->data));
140 setText(cellData2->row, cellData2->col, dataParser(findCellName(cellData2->row, cellData2->col), cellData2->data)); 140 setText(cellData2->row, cellData2->col, dataParser(findCellName(cellData2->row, cellData2->col), cellData2->data));
141 emit sheetModified(); 141 emit sheetModified();
142 } 142 }
143} 143}
144 144
145QString Sheet::getParameter(const QString &parameters, int paramNo, bool giveError, const QString funcName) 145QString Sheet::getParameter(const QString &parameters, int paramNo, bool giveError, const QString funcName)
146{ 146{
147 QString params(parameters); 147 QString params(parameters);
148 int position; 148 int position;
149 for (int i=0; i<paramNo; ++i) 149 for (int i=0; i<paramNo; ++i)
150 { 150 {
151 position=params.find(','); 151 position=params.find(',');
152 if (position<0) 152 if (position<0)
153 { 153 {
154 if (giveError) QMessageBox::critical(this, tr("Error"), tr("Too few arguments to function '"+funcName+'\'')); 154 if (giveError) QMessageBox::critical(this, tr("Error"), tr("Too few arguments to function '"+funcName+'\''));
155 //printf("params:%s\r\n",parameters.ascii()); 155 //printf("params:%s\r\n",parameters.ascii());
156 return QString(NULL); 156 return QString(NULL);
157 } 157 }
158 params=params.mid(position+1); 158 params=params.mid(position+1);
159 } 159 }
160 position=params.find(','); 160 position=params.find(',');
161 if (position<0) return params; 161 if (position<0) return params;
162 return params.left(position); 162 return params.left(position);
163} 163}
164 164
165bool Sheet::findRange(const QString &variable1, const QString &variable2, int *row1, int *col1, int *row2, int *col2) 165bool Sheet::findRange(const QString &variable1, const QString &variable2, int *row1, int *col1, int *row2, int *col2)
166{ 166{
167 int row, col; 167 int row, col;
168 if (!findRowColumn(variable1, row1, col1, FALSE) || !findRowColumn(variable2, row2, col2, FALSE)) return FALSE; 168 if (!findRowColumn(variable1, row1, col1, FALSE) || !findRowColumn(variable2, row2, col2, FALSE)) return FALSE;
169 if (*row1>*row2) 169 if (*row1>*row2)
170 { 170 {
171 row=*row1; 171 row=*row1;
172 *row1=*row2; 172 *row1=*row2;
173 *row2=row; 173 *row2=row;
174 } 174 }
175 if (*col1>*col2) 175 if (*col1>*col2)
176 { 176 {
177 col=*col1; 177 col=*col1;
178 *col1=*col2; 178 *col1=*col2;
179 *col2=col; 179 *col2=col;
180 } 180 }
181 return TRUE; 181 return TRUE;
182} 182}
183 183
184bool Sheet::findRowColumn(const QString &variable, int *row, int *col, bool giveError) 184bool Sheet::findRowColumn(const QString &variable, int *row, int *col, bool giveError)
185{ 185{
186 int position=variable.find(QRegExp("\\d")); 186 int position=variable.find(QRegExp("\\d"));
187 if (position<1) 187 if (position<1)
188 { 188 {
189 if (giveError) QMessageBox::critical(this, tr("Error"), tr("Invalid variable: '"+variable+'\'')); 189 if (giveError) QMessageBox::critical(this, tr("Error"), tr("Invalid variable: '"+variable+'\''));
190 return FALSE; 190 return FALSE;
191 } 191 }
192 *row=variable.mid(position).toInt()-1; 192 *row=variable.mid(position).toInt()-1;
193 *col=getHeaderColumn(variable.left(position))-1; 193 *col=getHeaderColumn(variable.left(position))-1;
194 return TRUE; 194 return TRUE;
195} 195}
196 196
197QString Sheet::calculateVariable(const QString &variable) 197QString Sheet::calculateVariable(const QString &variable)
198{ 198{
199 bool ok; 199 bool ok;
200 printf("calculateVariable=%s,len=%d\r\n",variable.ascii(),variable.length()); 200 printf("calculateVariable=%s,len=%d\r\n",variable.ascii(),variable.length());
201 if(variable.left(1)=="\"") return QString(variable.mid(1,variable.length()-2)); 201 if(variable.left(1)=="\"") return QString(variable.mid(1,variable.length()-2));
202 double tempResult=variable.toDouble(&ok); 202 double tempResult=variable.toDouble(&ok);
203 if (ok) 203 if (ok)
204 { 204 {
205 if(tempResult!=0.0) 205 if(tempResult!=0.0)
206 { 206 {
207 return QString::number(tempResult); 207 return QString::number(tempResult);
208 } 208 }
209 else 209 else
210 { 210 {
211 if(variable!="0" || variable!="0.0") return QString(variable); // hereis a string variable 211 if(variable!="0" || variable!="0.0") return QString(variable); // hereis a string variable
212 return QString::number(tempResult); 212 return QString::number(tempResult);
213 }; 213 };
214 }; 214 };
215 215
216 int row, col; 216 int row, col;
217 if(findRowColumn(variable, &row, &col, FALSE)) return dataParser(variable, text(row,col)); 217 if(findRowColumn(variable, &row, &col, FALSE)) return dataParser(variable, text(row,col));
218 //return (findRowColumn(variable, &row, &col, TRUE) ? dataParser(variable, text(row, col)) : 0); 218 //return (findRowColumn(variable, &row, &col, TRUE) ? dataParser(variable, text(row, col)) : 0);
219 return QString(variable); 219 return QString(variable);
220} 220}
221 221
222double Sheet::BesselI0(double x) 222double Sheet::BesselI0(double x)
223{ 223{
224 //Returns the modi ed Bessel function I0(x) for any real x. 224 //Returns the modi ed Bessel function I0(x) for any real x.
225 double ax,ans; 225 double ax,ans;
226 double y; 226 double y;
227 if ((ax=fabs(x)) < 3.75) 227 if ((ax=fabs(x)) < 3.75)
228 { 228 {
229 y=x/3.75; 229 y=x/3.75;
230 y*=y; 230 y*=y;
231 ans=1.0+y*(3.5156229+y*(3.0899424+y*(1.2067492 +y*(0.2659732+y*(0.360768e-1+y*0.45813e-2))))); 231 ans=1.0+y*(3.5156229+y*(3.0899424+y*(1.2067492 +y*(0.2659732+y*(0.360768e-1+y*0.45813e-2)))));
232 }else 232 }else
233 { 233 {
234 y=3.75/ax; 234 y=3.75/ax;
235 ans=(exp(ax)/sqrt(ax))*(0.39894228+y*(0.1328592e-1 +y*(0.225319e-2+y*(-0.157565e-2+y*(0.916281e-2 +y*(-0.2057706e-1+y*(0.2635537e-1+y*(-0.1647633e-1 +y*0.392377e-2)))))))); 235 ans=(exp(ax)/sqrt(ax))*(0.39894228+y*(0.1328592e-1 +y*(0.225319e-2+y*(-0.157565e-2+y*(0.916281e-2 +y*(-0.2057706e-1+y*(0.2635537e-1+y*(-0.1647633e-1 +y*0.392377e-2))))))));
236 } 236 }
237 return ans; 237 return ans;
238}; 238};
239 239
240double Sheet::BesselI1(double x) 240double Sheet::BesselI1(double x)
241{ 241{
242 double ax,ans; 242 double ax,ans;
243 double y; 243 double y;
244 if ((ax=fabs(x)) < 3.75) 244 if ((ax=fabs(x)) < 3.75)
245 { 245 {
246 y=x/3.75; 246 y=x/3.75;
247 y*=y; 247 y*=y;
248 ans=ax*(0.5+y*(0.87890594+y*(0.51498869+y*(0.15084934 +y*(0.2658733e-1+y*(0.301532e-2+y*0.32411e-3)))))); 248 ans=ax*(0.5+y*(0.87890594+y*(0.51498869+y*(0.15084934 +y*(0.2658733e-1+y*(0.301532e-2+y*0.32411e-3))))));
249 } else 249 } else
250 { 250 {
251 y=3.75/ax; 251 y=3.75/ax;
252 ans=0.2282967e-1+y*(-0.2895312e-1+y*(0.1787654e-1 -y*0.420059e-2)); ans=0.39894228+y*(-0.3988024e-1+y*(-0.362018e-2 +y*(0.163801e-2+y*(-0.1031555e-1+y*ans)))); 252 ans=0.2282967e-1+y*(-0.2895312e-1+y*(0.1787654e-1 -y*0.420059e-2)); ans=0.39894228+y*(-0.3988024e-1+y*(-0.362018e-2 +y*(0.163801e-2+y*(-0.1031555e-1+y*ans))));
253 ans *= (exp(ax)/sqrt(ax)); 253 ans *= (exp(ax)/sqrt(ax));
254 } 254 }
255 return x < 0.0 ? -ans : ans; 255 return x < 0.0 ? -ans : ans;
256}; 256};
257 257
258double Sheet::BesselI(int n, double x) 258double Sheet::BesselI(int n, double x)
259{ 259{
260 double ACC=40.0; 260 double ACC=40.0;
261 double BIGNO=1.0e10; 261 double BIGNO=1.0e10;
262 double BIGNI=1.0e-10; 262 double BIGNI=1.0e-10;
263 int j; 263 int j;
264 double bi,bim,bip,tox,ans; 264 double bi,bim,bip,tox,ans;
265 if (n < 2) return 0.0; 265 if (n < 2) return 0.0;
266 if (x == 0.0) return 0.0; else 266 if (x == 0.0) return 0.0; else
267 { 267 {
268 tox=2.0/fabs(x); 268 tox=2.0/fabs(x);
269 bip=ans=0.0; 269 bip=ans=0.0;
270 bi=1.0; 270 bi=1.0;
271 for (j=2*(n+(int) sqrt(ACC*n));j>0;j--) 271 for (j=2*(n+(int) sqrt(ACC*n));j>0;j--)
272 { 272 {
273 bim=bip+j*tox*bi; 273 bim=bip+j*tox*bi;
274 bip=bi; 274 bip=bi;
275 bi=bim; 275 bi=bim;
276 if (fabs(bi) > BIGNO) 276 if (fabs(bi) > BIGNO)
277 { 277 {
278 ans *= BIGNI; 278 ans *= BIGNI;
279 bi *= BIGNI; 279 bi *= BIGNI;
280 bip *= BIGNI; 280 bip *= BIGNI;
281 } 281 }
282 if (j == n) ans=bip; 282 if (j == n) ans=bip;
283 } 283 }
284 ans *= BesselI0(x)/bi; 284 ans *= BesselI0(x)/bi;
285 return x < 0.0 && (n & 1) ? -ans : ans; 285 return x < 0.0 && (n & 1) ? -ans : ans;
286 } 286 }
287}; 287};
288 288
289double Sheet::BesselK0(double x) 289double Sheet::BesselK0(double x)
290{ 290{
291 double y,ans; 291 double y,ans;
292 if (x <= 2.0) 292 if (x <= 2.0)
293 { 293 {
294 y=x*x/4.0; 294 y=x*x/4.0;
295 ans=(-log(x/2.0)*BesselI0(x))+(-0.57721566+y*(0.42278420 +y*(0.23069756+y*(0.3488590e-1+y*(0.262698e-2 +y*(0.10750e-3+y*0.74e-5)))))); 295 ans=(-log(x/2.0)*BesselI0(x))+(-0.57721566+y*(0.42278420 +y*(0.23069756+y*(0.3488590e-1+y*(0.262698e-2 +y*(0.10750e-3+y*0.74e-5))))));
296 } else 296 } else
297 { 297 {
298 y=2.0/x; 298 y=2.0/x;
299 ans=(exp(-x)/sqrt(x))*(1.25331414+y*(-0.7832358e-1 +y*(0.2189568e-1+y*(-0.1062446e-1+y*(0.587872e-2 +y*(-0.251540e-2+y*0.53208e-3)))))); 299 ans=(exp(-x)/sqrt(x))*(1.25331414+y*(-0.7832358e-1 +y*(0.2189568e-1+y*(-0.1062446e-1+y*(0.587872e-2 +y*(-0.251540e-2+y*0.53208e-3))))));
300 } 300 }
301return ans; 301return ans;
302}; 302};
303 303
304double Sheet::BesselK1(double x) 304double Sheet::BesselK1(double x)
305{ 305{
306 double y,ans; 306 double y,ans;
307 if (x <= 2.0) 307 if (x <= 2.0)
308 { 308 {
309 y=x*x/4.0; 309 y=x*x/4.0;
310 ans=(log(x/2.0)*BesselI1(x))+(1.0/x)*(1.0+y*(0.15443144 +y*(-0.67278579+y*(-0.18156897+y*(-0.1919402e-1 +y*(-0.110404e-2+y*(-0.4686e-4))))))); 310 ans=(log(x/2.0)*BesselI1(x))+(1.0/x)*(1.0+y*(0.15443144 +y*(-0.67278579+y*(-0.18156897+y*(-0.1919402e-1 +y*(-0.110404e-2+y*(-0.4686e-4)))))));
311 } else 311 } else
312 { 312 {
313 y=2.0/x; 313 y=2.0/x;
314 ans=(exp(-x)/sqrt(x))*(1.25331414+y*(0.23498619 +y*(-0.3655620e-1+y*(0.1504268e-1+y*(-0.780353e-2 +y*(0.325614e-2+y*(-0.68245e-3))))))); 314 ans=(exp(-x)/sqrt(x))*(1.25331414+y*(0.23498619 +y*(-0.3655620e-1+y*(0.1504268e-1+y*(-0.780353e-2 +y*(0.325614e-2+y*(-0.68245e-3)))))));
315 } 315 }
316 return ans; 316 return ans;
317}; 317};
318 318
319double Sheet::BesselK(int n, double x) 319double Sheet::BesselK(int n, double x)
320{ 320{
321 int j; 321 int j;
322 double bk,bkm,bkp,tox; 322 double bk,bkm,bkp,tox;
323 if (n < 2) return 0.0; 323 if (n < 2) return 0.0;
324 tox=2.0/x; 324 tox=2.0/x;
325 bkm=BesselK0(x); 325 bkm=BesselK0(x);
326 bk=BesselK1(x); 326 bk=BesselK1(x);
327 for (j=1;j<n;j++) 327 for (j=1;j<n;j++)
328 { 328 {
329 bkp=bkm+j*tox*bk; 329 bkp=bkm+j*tox*bk;
330 bkm=bk; 330 bkm=bk;
331 bk=bkp; 331 bk=bkp;
332 } 332 }
333 return bk; 333 return bk;
334}; 334};
335 335
336double Sheet::BesselJ0(double x) 336double Sheet::BesselJ0(double x)
337{ 337{
338 double ax,z; 338 double ax,z;
339 double xx,y,ans,ans1,ans2; 339 double xx,y,ans,ans1,ans2;
340 if ((ax=fabs(x)) < 8.0) 340 if ((ax=fabs(x)) < 8.0)
341 { 341 {
342 y=x*x; 342 y=x*x;
343 ans1=57568490574.0+y*(-13362590354.0+y*(651619640.7 +y*(-11214424.18+y*(77392.33017+y*(-184.9052456))))); 343 ans1=57568490574.0+y*(-13362590354.0+y*(651619640.7 +y*(-11214424.18+y*(77392.33017+y*(-184.9052456)))));
344 ans2=57568490411.0+y*(1029532985.0+y*(9494680.718 +y*(59272.64853+y*(267.8532712+y*1.0)))); 344 ans2=57568490411.0+y*(1029532985.0+y*(9494680.718 +y*(59272.64853+y*(267.8532712+y*1.0))));
345 ans=ans1/ans2; 345 ans=ans1/ans2;
346 } else 346 } else
347 { 347 {
348 z=8.0/ax; 348 z=8.0/ax;
349 y=z*z; 349 y=z*z;
350 xx=ax-0.785398164; 350 xx=ax-0.785398164;
351 ans1=1.0+y*(-0.1098628627e-2+y*(0.2734510407e-4 +y*(-0.2073370639e-5+y*0.2093887211e-6))); 351 ans1=1.0+y*(-0.1098628627e-2+y*(0.2734510407e-4 +y*(-0.2073370639e-5+y*0.2093887211e-6)));
352 ans2 = -0.1562499995e-1+y*(0.1430488765e-3 +y*(-0.6911147651e-5+y*(0.7621095161e-6 -y*0.934935152e-7))); 352 ans2 = -0.1562499995e-1+y*(0.1430488765e-3 +y*(-0.6911147651e-5+y*(0.7621095161e-6 -y*0.934935152e-7)));
353 ans=sqrt(0.636619772/ax)*(cos(xx)*ans1-z*sin(xx)*ans2); 353 ans=sqrt(0.636619772/ax)*(cos(xx)*ans1-z*sin(xx)*ans2);
354 } 354 }
355 return ans; 355 return ans;
356}; 356};
357 357
358double Sheet::BesselY0(double x) 358double Sheet::BesselY0(double x)
359{ 359{
360 double z; 360 double z;
361 double xx,y,ans,ans1,ans2; 361 double xx,y,ans,ans1,ans2;
362 if (x < 8.0) 362 if (x < 8.0)
363 { 363 {
364 y=x*x; 364 y=x*x;
365 ans1 = -2957821389.0+y*(7062834065.0+y*(-512359803.6 +y*(10879881.29+y*(-86327.92757+y*228.4622733)))); 365 ans1 = -2957821389.0+y*(7062834065.0+y*(-512359803.6 +y*(10879881.29+y*(-86327.92757+y*228.4622733))));
366 ans2=40076544269.0+y*(745249964.8+y*(7189466.438 +y*(47447.26470+y*(226.1030244+y*1.0)))); 366 ans2=40076544269.0+y*(745249964.8+y*(7189466.438 +y*(47447.26470+y*(226.1030244+y*1.0))));
367 ans=(ans1/ans2)+0.636619772*BesselJ0(x)*log(x); 367 ans=(ans1/ans2)+0.636619772*BesselJ0(x)*log(x);
368 } else 368 } else
369 { 369 {
370 z=8.0/x; 370 z=8.0/x;
371 y=z*z; 371 y=z*z;
372 xx=x-0.785398164; 372 xx=x-0.785398164;
373 ans1=1.0+y*(-0.1098628627e-2+y*(0.2734510407e-4 +y*(-0.2073370639e-5+y*0.2093887211e-6))); 373 ans1=1.0+y*(-0.1098628627e-2+y*(0.2734510407e-4 +y*(-0.2073370639e-5+y*0.2093887211e-6)));
374 ans2 = -0.1562499995e-1+y*(0.1430488765e-3 +y*(-0.6911147651e-5+y*(0.7621095161e-6 +y*(-0.934945152e-7)))); 374 ans2 = -0.1562499995e-1+y*(0.1430488765e-3 +y*(-0.6911147651e-5+y*(0.7621095161e-6 +y*(-0.934945152e-7))));
375 ans=sqrt(0.636619772/x)*(sin(xx)*ans1+z*cos(xx)*ans2); 375 ans=sqrt(0.636619772/x)*(sin(xx)*ans1+z*cos(xx)*ans2);
376 } 376 }
377 return ans; 377 return ans;
378}; 378};
379 379
380double Sheet::BesselJ1(double x) 380double Sheet::BesselJ1(double x)
381{ 381{
382 double ax,z; 382 double ax,z;
383 double xx,y,ans,ans1,ans2; 383 double xx,y,ans,ans1,ans2;
384 if ((ax=fabs(x)) < 8.0) 384 if ((ax=fabs(x)) < 8.0)
385 { 385 {
386 y=x*x; 386 y=x*x;
387 ans1=x*(72362614232.0+y*(-7895059235.0+y*(242396853.1 +y*(-2972611.439+y*(15704.48260+y*(-30.16036606)))))); 387 ans1=x*(72362614232.0+y*(-7895059235.0+y*(242396853.1 +y*(-2972611.439+y*(15704.48260+y*(-30.16036606))))));
388 ans2=144725228442.0+y*(2300535178.0+y*(18583304.74 +y*(99447.43394+y*(376.9991397+y*1.0)))); 388 ans2=144725228442.0+y*(2300535178.0+y*(18583304.74 +y*(99447.43394+y*(376.9991397+y*1.0))));
389 ans=ans1/ans2; 389 ans=ans1/ans2;
390 } else 390 } else
391 { 391 {
392 z=8.0/ax; y=z*z; xx=ax-2.356194491; 392 z=8.0/ax; y=z*z; xx=ax-2.356194491;
393 ans1=1.0+y*(0.183105e-2+y*(-0.3516396496e-4 +y*(0.2457520174e-5+y*(-0.240337019e-6)))); 393 ans1=1.0+y*(0.183105e-2+y*(-0.3516396496e-4 +y*(0.2457520174e-5+y*(-0.240337019e-6))));
394 ans2=0.04687499995+y*(-0.2002690873e-3 +y*(0.8449199096e-5+y*(-0.88228987e-6 +y*0.105787412e-6))); 394 ans2=0.04687499995+y*(-0.2002690873e-3 +y*(0.8449199096e-5+y*(-0.88228987e-6 +y*0.105787412e-6)));
395 ans=sqrt(0.636619772/ax)*(cos(xx)*ans1-z*sin(xx)*ans2); 395 ans=sqrt(0.636619772/ax)*(cos(xx)*ans1-z*sin(xx)*ans2);
396 if (x < 0.0) ans = -ans; 396 if (x < 0.0) ans = -ans;
397 } 397 }
398 return ans; 398 return ans;
399}; 399};
400 400
401double Sheet::BesselY1(double x) 401double Sheet::BesselY1(double x)
402{ 402{
403 double z; 403 double z;
404 double xx,y,ans,ans1,ans2; 404 double xx,y,ans,ans1,ans2;
405 if (x < 8.0) 405 if (x < 8.0)
406 { 406 {
407 y=x*x; 407 y=x*x;
408 ans1=x*(-0.4900604943e13+y*(0.1275274390e13 +y*(-0.5153438139e11+y*(0.7349264551e9 +y*(-0.4237922726e7+y*0.8511937935e4))))); 408 ans1=x*(-0.4900604943e13+y*(0.1275274390e13 +y*(-0.5153438139e11+y*(0.7349264551e9 +y*(-0.4237922726e7+y*0.8511937935e4)))));
409 ans2=0.2499580570e14+y*(0.4244419664e12 +y*(0.3733650367e10+y*(0.2245904002e8 +y*(0.1020426050e6+y*(0.3549632885e3+y))))); 409 ans2=0.2499580570e14+y*(0.4244419664e12 +y*(0.3733650367e10+y*(0.2245904002e8 +y*(0.1020426050e6+y*(0.3549632885e3+y)))));
410 ans=(ans1/ans2)+0.636619772*(BesselJ1(x)*log(x)-1.0/x); 410 ans=(ans1/ans2)+0.636619772*(BesselJ1(x)*log(x)-1.0/x);
411 } else 411 } else
412 { 412 {
413 z=8.0/x; 413 z=8.0/x;
414 y=z*z; 414 y=z*z;
415 xx=x-2.356194491; 415 xx=x-2.356194491;
416 ans1=1.0+y*(0.183105e-2+y*(-0.3516396496e-4 +y*(0.2457520174e-5+y*(-0.240337019e-6)))); 416 ans1=1.0+y*(0.183105e-2+y*(-0.3516396496e-4 +y*(0.2457520174e-5+y*(-0.240337019e-6))));
417 ans2=0.04687499995+y*(-0.2002690873e-3 +y*(0.8449199096e-5+y*(-0.88228987e-6 +y*0.105787412e-6))); 417 ans2=0.04687499995+y*(-0.2002690873e-3 +y*(0.8449199096e-5+y*(-0.88228987e-6 +y*0.105787412e-6)));
418 ans=sqrt(0.636619772/x)*(sin(xx)*ans1+z*cos(xx)*ans2); 418 ans=sqrt(0.636619772/x)*(sin(xx)*ans1+z*cos(xx)*ans2);
419 } 419 }
420 return ans; 420 return ans;
421}; 421};
422 422
423double Sheet::BesselY(int n, double x) 423double Sheet::BesselY(int n, double x)
424{ 424{
425 int j; 425 int j;
426 double by,bym,byp,tox; 426 double by,bym,byp,tox;
427 if (n < 2) return 0.0; 427 if (n < 2) return 0.0;
428 tox=2.0/x; 428 tox=2.0/x;
429 by=BesselY1(x); 429 by=BesselY1(x);
430 bym=BesselY0(x); 430 bym=BesselY0(x);
431 for (j=1;j<n;j++) 431 for (j=1;j<n;j++)
432 { 432 {
433 byp=j*tox*by-bym; 433 byp=j*tox*by-bym;
434 bym=by; 434 bym=by;
435 by=byp; 435 by=byp;
436 } 436 }
437 return by; 437 return by;
438}; 438};
439 439
440double Sheet::BesselJ(int n, double x) 440double Sheet::BesselJ(int n, double x)
441{ 441{
442 double ACC=40.0; 442 double ACC=40.0;
443 double BIGNO=1.0e10; 443 double BIGNO=1.0e10;
444 double BIGNI=1.0e-10; 444 double BIGNI=1.0e-10;
445 int j,jsum,m; 445 int j,jsum,m;
446 double ax,bj,bjm,bjp,sum,tox,ans; 446 double ax,bj,bjm,bjp,sum,tox,ans;
447 if (n < 2) return 0.0; 447 if (n < 2) return 0.0;
448 ax=fabs(x); 448 ax=fabs(x);
449 if (ax == 0.0) return 0.0; 449 if (ax == 0.0) return 0.0;
450 else if (ax > (double) n) 450 else if (ax > (double) n)
451 { 451 {
452 tox=2.0/ax; 452 tox=2.0/ax;
453 bjm=BesselJ0(ax); 453 bjm=BesselJ0(ax);
454 bj=BesselJ1(ax); 454 bj=BesselJ1(ax);
455 for (j=1;j<n;j++) 455 for (j=1;j<n;j++)
456 { 456 {
457 bjp=j*tox*bj-bjm; 457 bjp=j*tox*bj-bjm;
458 bjm=bj; 458 bjm=bj;
459 bj=bjp; 459 bj=bjp;
460 } 460 }
461 ans=bj; 461 ans=bj;
462 } else 462 } else
463 { 463 {
464 tox=2.0/ax; 464 tox=2.0/ax;
465 m=2*((n+(int) sqrt(ACC*n))/2); 465 m=2*((n+(int) sqrt(ACC*n))/2);
466 jsum=0; 466 jsum=0;
467 bjp=ans=sum=0.0; 467 bjp=ans=sum=0.0;
468 bj=1.0; 468 bj=1.0;
469 for (j=m;j>0;j--) 469 for (j=m;j>0;j--)
470 { 470 {
471 bjm=j*tox*bj-bjp; 471 bjm=j*tox*bj-bjp;
472 bjp=bj; 472 bjp=bj;
473 bj=bjm; 473 bj=bjm;
474 if (fabs(bj) > BIGNO) 474 if (fabs(bj) > BIGNO)
475 { 475 {
476 bj *= BIGNI; 476 bj *= BIGNI;
477 bjp *= BIGNI; 477 bjp *= BIGNI;
478 ans *= BIGNI; 478 ans *= BIGNI;
479 sum *= BIGNI; 479 sum *= BIGNI;
480 } 480 }
481 if (jsum) sum += bj; 481 if (jsum) sum += bj;
482 jsum=!jsum; 482 jsum=!jsum;
483 if (j == n) ans=bjp; 483 if (j == n) ans=bjp;
484 } 484 }
485 sum=2.0*sum-bj; 485 sum=2.0*sum-bj;
486 ans /= sum; 486 ans /= sum;
487 } 487 }
488 return x < 0.0 && (n & 1) ? -ans : ans; 488 return x < 0.0 && (n & 1) ? -ans : ans;
489}; 489};
490 490
491double Sheet::GammaLn(double xx) 491double Sheet::GammaLn(double xx)
492{ 492{
493 double x,y,tmp,ser; 493 double x,y,tmp,ser;
494 static double cof[6]={76.18009172947146,-86.50532032941677, 24.01409824083091,-1.231739572450155, 0.1208650973866179e-2,-0.5395239384953e-5}; 494 static double cof[6]={76.18009172947146,-86.50532032941677, 24.01409824083091,-1.231739572450155, 0.1208650973866179e-2,-0.5395239384953e-5};
495 int j; 495 int j;
496 y=x=xx; 496 y=x=xx;
497 tmp=x+5.5; 497 tmp=x+5.5;
498 tmp -= (x+0.5)*log(tmp); 498 tmp -= (x+0.5)*log(tmp);
499 ser=1.000000000190015; 499 ser=1.000000000190015;
500 for (j=0;j<=5;j++) ser += cof[j]/++y; 500 for (j=0;j<=5;j++) ser += cof[j]/++y;
501 return -tmp+log(2.5066282746310005*ser/x); 501 return -tmp+log(2.5066282746310005*ser/x);
502}; 502};
503 503
504double Sheet::Factorial(double n) 504double Sheet::Factorial(double n)
505{ 505{
506 if (n < 0) return 0.0; 506 if (n < 0) return 0.0;
507 if (n > 100) return 0.0; 507 if (n > 100) return 0.0;
508 return exp(GammaLn(n+1.0)); 508 return exp(GammaLn(n+1.0));
509}; 509};
510 510
511double Sheet::GammaP(double a, double x) 511double Sheet::GammaP(double a, double x)
512{ 512{
513// returns GammaP(a,x) 513// returns GammaP(a,x)
514//void gcf(float *gammcf, float a, float x, float *gln); 514//void gcf(float *gammcf, float a, float x, float *gln);
515//void gser(float *gamser, float a, float x, float *gln); 515//void gser(float *gamser, float a, float x, float *gln);
516 double gamser,gammcf,gln; 516 double gamser,gammcf,gln;
517 if (x < 0.0 || a <= 0.0) return 0.0;//error 517 if (x < 0.0 || a <= 0.0) return 0.0;//error
518 if (x < (a+1.0)) 518 if (x < (a+1.0))
519 { 519 {
520 GammaSeries(&gamser,a,x,&gln); 520 GammaSeries(&gamser,a,x,&gln);
521 return gamser; 521 return gamser;
522 }else 522 }else
523 { 523 {
524 GammaContinuedFraction(&gammcf,a,x,&gln); 524 GammaContinuedFraction(&gammcf,a,x,&gln);
525 return 1.0-gammcf; 525 return 1.0-gammcf;
526 } 526 }
527}; 527};
528 528
529double Sheet::GammaQ(double a,double x) 529double Sheet::GammaQ(double a,double x)
530{ 530{
531 //returns GammaQ(a,x)=1.0 - GammaP(a,x); 531 //returns GammaQ(a,x)=1.0 - GammaP(a,x);
532 return (1.0-GammaP(a,x)); 532 return (1.0-GammaP(a,x));
533}; 533};
534 534
535 535
536void Sheet::GammaSeries(double *gamser, double a, double x, double *gln) 536void Sheet::GammaSeries(double *gamser, double a, double x, double *gln)
537{ 537{
538 double EPS=3.0e-7; 538 double EPS=3.0e-7;
539 int ITMAX=100; 539 int ITMAX=100;
540 int n; 540 int n;
541 double sum,del,ap; 541 double sum,del,ap;
542 *gln=GammaLn(a); 542 *gln=GammaLn(a);
543 if (x <= 0.0) 543 if (x <= 0.0)
544 { 544 {
545 if (x < 0.0) return;//error 545 if (x < 0.0) return;//error
546 *gamser=0.0; 546 *gamser=0.0;
547 return; 547 return;
548 } else 548 } else
549 { 549 {
550 ap=a; 550 ap=a;
551 del=sum=1.0/a; 551 del=sum=1.0/a;
552 for (n=1;n<=ITMAX;n++) 552 for (n=1;n<=ITMAX;n++)
553 { 553 {
554 ++ap; 554 ++ap;
555 del *= x/ap; 555 del *= x/ap;
556 sum += del; 556 sum += del;
557 if (fabs(del) < fabs(sum)*EPS) 557 if (fabs(del) < fabs(sum)*EPS)
558 { 558 {
559 *gamser=sum*exp(-x+a*log(x)-(*gln)); 559 *gamser=sum*exp(-x+a*log(x)-(*gln));
560 return; 560 return;
561 } 561 }
562 } return; 562 } return;
563 return; 563 return;
564 } 564 }
565}; 565};
566 566
567 567
568void Sheet::GammaContinuedFraction(double *gammcf, double a, double x, double *gln) 568void Sheet::GammaContinuedFraction(double *gammcf, double a, double x, double *gln)
569{ 569{
570 double EPS=3.0e-7; 570 double EPS=3.0e-7;
571 double FPMIN=1.0e-30; 571 double FPMIN=1.0e-30;
572 int ITMAX=100; 572 int ITMAX=100;
573 int i; 573 int i;
574 double an,b,c,d,del,h; 574 double an,b,c,d,del,h;
575 *gln=GammaLn(a); 575 *gln=GammaLn(a);
576 b=x+1.0-a; 576 b=x+1.0-a;
577 c=1.0/FPMIN; 577 c=1.0/FPMIN;
578 d=1.0/b; h=d; 578 d=1.0/b; h=d;
579 for (i=1;i<=ITMAX;i++) 579 for (i=1;i<=ITMAX;i++)
580 { 580 {
581 an = -i*(i-a); 581 an = -i*(i-a);
582 b += 2.0; d=an*d+b; 582 b += 2.0; d=an*d+b;
583 if (fabs(d) < FPMIN) d=FPMIN; 583 if (fabs(d) < FPMIN) d=FPMIN;
584 c=b+an/c; 584 c=b+an/c;
585 if (fabs(c) < FPMIN) c=FPMIN; 585 if (fabs(c) < FPMIN) c=FPMIN;
586 d=1.0/d; del=d*c; h *= del; 586 d=1.0/d; del=d*c; h *= del;
587 if (fabs(del-1.0) < EPS) break; 587 if (fabs(del-1.0) < EPS) break;
588 } 588 }
589 if (i > ITMAX) return; 589 if (i > ITMAX) return;
590 *gammcf=exp(-x+a*log(x)-(*gln))*h; 590 *gammcf=exp(-x+a*log(x)-(*gln))*h;
591}; 591};
592 592
593double Sheet::ErrorFunction(double x) 593double Sheet::ErrorFunction(double x)
594{ 594{
595 return x < 0.0 ? -GammaP(0.5,x*x) : GammaP(0.5,x*x); 595 return x < 0.0 ? -GammaP(0.5,x*x) : GammaP(0.5,x*x);
596}; 596};
597 597
598double Sheet::ErrorFunctionComplementary(double x) 598double Sheet::ErrorFunctionComplementary(double x)
599{ 599{
600 return x < 0.0 ? 1.0+GammaP(0.5,x*x) : GammaQ(0.5,x*x); 600 return x < 0.0 ? 1.0+GammaP(0.5,x*x) : GammaQ(0.5,x*x);
601}; 601};
602 602
603double Sheet::Beta(double z, double w) 603double Sheet::Beta(double z, double w)
604{ 604{
605 return exp(GammaLn(z)+GammaLn(w)-GammaLn(z+w)); 605 return exp(GammaLn(z)+GammaLn(w)-GammaLn(z+w));
606}; 606};
607 607
608 608
609double Sheet::BetaContinuedFraction(double a, double b, double x) 609double Sheet::BetaContinuedFraction(double a, double b, double x)
610{ 610{
611 int MAXIT=100; 611 int MAXIT=100;
612 double EPS=3.0e-7; 612 double EPS=3.0e-7;
613 double FPMIN=1.0e-30; 613 double FPMIN=1.0e-30;
614 int m,m2; 614 int m,m2;
615 double aa,c,d,del,h,qab,qam,qap; 615 double aa,c,d,del,h,qab,qam,qap;
616 qab=a+b; 616 qab=a+b;
617 qap=a+1.0; qam=a-1.0; c=1.0; 617 qap=a+1.0; qam=a-1.0; c=1.0;
618 d=1.0-qab*x/qap; 618 d=1.0-qab*x/qap;
619 if (fabs(d) < FPMIN) d=FPMIN; 619 if (fabs(d) < FPMIN) d=FPMIN;
620 d=1.0/d; h=d; 620 d=1.0/d; h=d;
621 for (m=1;m<=MAXIT;m++) 621 for (m=1;m<=MAXIT;m++)
622 { 622 {
623 m2=2*m; aa=m*(b-m)*x/((qam+m2)*(a+m2)); 623 m2=2*m; aa=m*(b-m)*x/((qam+m2)*(a+m2));
624 d=1.0+aa*d; 624 d=1.0+aa*d;
625 if (fabs(d) < FPMIN) d=FPMIN; 625 if (fabs(d) < FPMIN) d=FPMIN;
626 c=1.0+aa/c; 626 c=1.0+aa/c;
627 if (fabs(c) < FPMIN) c=FPMIN; 627 if (fabs(c) < FPMIN) c=FPMIN;
628 d=1.0/d; h *= d*c; 628 d=1.0/d; h *= d*c;
629 aa = -(a+m)*(qab+m)*x/((a+m2)*(qap+m2)); d=1.0+aa*d; 629 aa = -(a+m)*(qab+m)*x/((a+m2)*(qap+m2)); d=1.0+aa*d;
630 if (fabs(d) < FPMIN) d=FPMIN; 630 if (fabs(d) < FPMIN) d=FPMIN;
631 c=1.0+aa/c; 631 c=1.0+aa/c;
632 if (fabs(c) < FPMIN) c=FPMIN; d=1.0/d; 632 if (fabs(c) < FPMIN) c=FPMIN; d=1.0/d;
633 del=d*c; h *= del; 633 del=d*c; h *= del;
634 if (fabs(del-1.0) < EPS) break; 634 if (fabs(del-1.0) < EPS) break;
635 } 635 }
636 if (m > MAXIT) return 0.0; 636 if (m > MAXIT) return 0.0;
637 return h; 637 return h;
638}; 638};
639 639
640double Sheet::BetaIncomplete(double a, double b, double x) 640double Sheet::BetaIncomplete(double a, double b, double x)
641{ 641{
642 double bt; 642 double bt;
643 if (x < 0.0 || x > 1.0) return 0.0; 643 if (x < 0.0 || x > 1.0) return 0.0;
644 if (x == 0.0 || x == 1.0) bt=0.0; else 644 if (x == 0.0 || x == 1.0) bt=0.0; else
645 bt=exp(GammaLn(a+b)-GammaLn(a)-GammaLn(b)+a*log(x)+b*log(1.0-x)); 645 bt=exp(GammaLn(a+b)-GammaLn(a)-GammaLn(b)+a*log(x)+b*log(1.0-x));
646 if (x < (a+1.0)/(a+b+2.0)) return bt*BetaContinuedFraction(a,b,x)/a; else 646 if (x < (a+1.0)/(a+b+2.0)) return bt*BetaContinuedFraction(a,b,x)/a; else
647 return 1.0-bt*BetaContinuedFraction(b,a,1.0-x)/b; 647 return 1.0-bt*BetaContinuedFraction(b,a,1.0-x)/b;
648}; 648};
649 649
650 650
651 651
652double Sheet::functionSum(const QString &param1, const QString &param2) 652double Sheet::functionSum(const QString &param1, const QString &param2)
653{ 653{
654 int row1, col1, row2, col2, row, col; 654 int row1, col1, row2, col2, row, col;
655 double result=0, tempResult; 655 double result=0, tempResult;
656 bool ok; 656 bool ok;
657 if (findRange(param1, param2, &row1, &col1, &row2, &col2)) 657 if (findRange(param1, param2, &row1, &col1, &row2, &col2))
658 { 658 {
659 for (row=row1; row<=row2; ++row) 659 for (row=row1; row<=row2; ++row)
660 for (col=col1; col<=col2; ++col) 660 for (col=col1; col<=col2; ++col)
661 { 661 {
662 tempResult=text(row, col).toDouble(&ok); 662 tempResult=text(row, col).toDouble(&ok);
663 if (ok) result+=tempResult; 663 if (ok) result+=tempResult;
664 } 664 }
665 return result; 665 return result;
666 }else 666 }else
667 { 667 {
668 double d1=0,d2=0; 668 double d1=0,d2=0;
669 d1=calculateVariable(param1).toDouble(&ok); 669 d1=calculateVariable(param1).toDouble(&ok);
670 d2=calculateVariable(param2).toDouble(&ok); 670 d2=calculateVariable(param2).toDouble(&ok);
671 return(d1+d2); 671 return(d1+d2);
672 }; 672 };
673 return 0; 673 return 0;
674} 674}
675 675
676QString Sheet::functionIndex(const QString &param1, const QString &param2, int indx) 676QString Sheet::functionIndex(const QString &param1, const QString &param2, int indx)
677{ 677{
678 int row1, col1, row2, col2, row, col; 678 int row1, col1, row2, col2, row, col;
679 if (!findRange(param1, param2, &row1, &col1, &row2, &col2)) return 0; 679 if (!findRange(param1, param2, &row1, &col1, &row2, &col2)) return 0;
680 int ii=1; 680 int ii=1;
681 for (col=col1; col<=col2; ++col) 681 for (col=col1; col<=col2; ++col)
682 for (row=row1; row<=row2; ++row) 682 for (row=row1; row<=row2; ++row)
683 { 683 {
684 if(ii==indx) return text(row,col); 684 if(ii==indx) return text(row,col);
685 ii++; 685 ii++;
686 } 686 }
687 return QString(""); 687 return QString("");
688} 688}
689 689
690 690
691 691
692double Sheet::functionVariancePopulation(const QString &param1, const QString &param2) 692double Sheet::functionVariancePopulation(const QString &param1, const QString &param2)
693{ 693{
694 int row1, col1, row2, col2, row, col; 694 int row1, col1, row2, col2, row, col;
695 if (!findRange(param1, param2, &row1, &col1, &row2, &col2)) return 0; 695 if (!findRange(param1, param2, &row1, &col1, &row2, &col2)) return 0;
696 double avg1=functionAvg(param1,param2); 696 double avg1=functionAvg(param1,param2);
697 double result=0, tempResult; 697 double result=0, tempResult;
698 int count1=0; 698 int count1=0;
699 bool ok; 699 bool ok;
700 for (row=row1; row<=row2; ++row) 700 for (row=row1; row<=row2; ++row)
701 for (col=col1; col<=col2; ++col) 701 for (col=col1; col<=col2; ++col)
702 { 702 {
703 tempResult=text(row, col).toDouble(&ok); 703 tempResult=text(row, col).toDouble(&ok);
704 if (ok) { result=result + (tempResult - avg1)*(tempResult - avg1); count1++;}; 704 if (ok) { result=result + (tempResult - avg1)*(tempResult - avg1); count1++;};
705 } 705 }
706 if(count1>0) result=result/double(count1); else result=0.0; 706 if(count1>0) result=result/double(count1); else result=0.0;
707 return result; 707 return result;
708}; 708};
709 709
710double Sheet::functionVariance(const QString &param1, const QString &param2) 710double Sheet::functionVariance(const QString &param1, const QString &param2)
711{ 711{
712 int row1, col1, row2, col2, row, col; 712 int row1, col1, row2, col2, row, col;
713 if (!findRange(param1, param2, &row1, &col1, &row2, &col2)) return 0; 713 if (!findRange(param1, param2, &row1, &col1, &row2, &col2)) return 0;
714 double avg1=functionAvg(param1,param2); 714 double avg1=functionAvg(param1,param2);
715 double result=0, tempResult; 715 double result=0, tempResult;
716 int count1=0; 716 int count1=0;
717 bool ok; 717 bool ok;
718 for (row=row1; row<=row2; ++row) 718 for (row=row1; row<=row2; ++row)
719 for (col=col1; col<=col2; ++col) 719 for (col=col1; col<=col2; ++col)
720 { 720 {
721 tempResult=text(row, col).toDouble(&ok); 721 tempResult=text(row, col).toDouble(&ok);
722 if (ok) { result=result + (tempResult - avg1)*(tempResult - avg1); count1++;}; 722 if (ok) { result=result + (tempResult - avg1)*(tempResult - avg1); count1++;};
723 } 723 }
724 if(count1>1) result=result/double(count1-1); else result=0.0; 724 if(count1>1) result=result/double(count1-1); else result=0.0;
725 return result; 725 return result;
726}; 726};
727 727
728double Sheet::functionSkew(const QString &param1, const QString &param2) 728double Sheet::functionSkew(const QString &param1, const QString &param2)
729{ 729{
730 int row1, col1, row2, col2, row, col; 730 int row1, col1, row2, col2, row, col;
731 if (!findRange(param1, param2, &row1, &col1, &row2, &col2)) return 0; 731 if (!findRange(param1, param2, &row1, &col1, &row2, &col2)) return 0;
732 double avg1=functionAvg(param1,param2); 732 double avg1=functionAvg(param1,param2);
733 double var1=sqrt(functionVariancePopulation(param1,param2)); 733 double var1=sqrt(functionVariancePopulation(param1,param2));
734 if(var1==0.0) return 0.0; 734 if(var1==0.0) return 0.0;
735 double result=0, tempResult; 735 double result=0, tempResult;
736 int count1=0; 736 int count1=0;
737 bool ok; 737 bool ok;
738 for (row=row1; row<=row2; ++row) 738 for (row=row1; row<=row2; ++row)
739 for (col=col1; col<=col2; ++col) 739 for (col=col1; col<=col2; ++col)
740 { 740 {
741 tempResult=text(row, col).toDouble(&ok); 741 tempResult=text(row, col).toDouble(&ok);
742 if (ok) 742 if (ok)
743 { 743 {
744 result=result + (tempResult - avg1)*(tempResult - avg1)*(tempResult - avg1)/(var1*var1*var1); 744 result=result + (tempResult - avg1)*(tempResult - avg1)*(tempResult - avg1)/(var1*var1*var1);
745 count1++; 745 count1++;
746 }; 746 };
747 } 747 }
748 if(count1>0) result=result/double(count1); else result=0.0; 748 if(count1>0) result=result/double(count1); else result=0.0;
749 return result; 749 return result;
750}; 750};
751 751
752double Sheet::functionKurt(const QString &param1, const QString &param2) 752double Sheet::functionKurt(const QString &param1, const QString &param2)
753{ 753{
754 int row1, col1, row2, col2, row, col; 754 int row1, col1, row2, col2, row, col;
755 if (!findRange(param1, param2, &row1, &col1, &row2, &col2)) return 0; 755 if (!findRange(param1, param2, &row1, &col1, &row2, &col2)) return 0;
756 double avg1=functionAvg(param1,param2); 756 double avg1=functionAvg(param1,param2);
757 double var1=sqrt(functionVariancePopulation(param1,param2)); 757 double var1=sqrt(functionVariancePopulation(param1,param2));
758 if(var1==0.0) return 0.0; 758 if(var1==0.0) return 0.0;
759 double result=0, tempResult; 759 double result=0, tempResult;
760 int count1=0; 760 int count1=0;
761 bool ok; 761 bool ok;
762 for (row=row1; row<=row2; ++row) 762 for (row=row1; row<=row2; ++row)
763 for (col=col1; col<=col2; ++col) 763 for (col=col1; col<=col2; ++col)
764 { 764 {
765 tempResult=text(row, col).toDouble(&ok); 765 tempResult=text(row, col).toDouble(&ok);
766 if (ok) 766 if (ok)
767 { 767 {
768 result=result + (tempResult - avg1)*(tempResult - avg1)* 768 result=result + (tempResult - avg1)*(tempResult - avg1)*
769 (tempResult - avg1)*(tempResult - avg1)/(var1*var1*var1*var1); 769 (tempResult - avg1)*(tempResult - avg1)/(var1*var1*var1*var1);
770 count1++; 770 count1++;
771 }; 771 };
772 } 772 }
773 if(count1>0) result=result/double(count1)-3.0; else result=0.0; 773 if(count1>0) result=result/double(count1)-3.0; else result=0.0;
774 return result; 774 return result;
775}; 775};
776 776
777 777
778 778
779double Sheet::functionSumSQ(const QString &param1, const QString &param2) 779double Sheet::functionSumSQ(const QString &param1, const QString &param2)
780{ 780{
781 int row1, col1, row2, col2, row, col; 781 int row1, col1, row2, col2, row, col;
782 double result=0, tempResult; 782 double result=0, tempResult;
783 bool ok; 783 bool ok;
784 if (findRange(param1, param2, &row1, &col1, &row2, &col2)) 784 if (findRange(param1, param2, &row1, &col1, &row2, &col2))
785 { 785 {
786 for (row=row1; row<=row2; ++row) 786 for (row=row1; row<=row2; ++row)
787 for (col=col1; col<=col2; ++col) 787 for (col=col1; col<=col2; ++col)
788 { 788 {
789 tempResult=text(row, col).toDouble(&ok); 789 tempResult=text(row, col).toDouble(&ok);
790 if (ok) result+=tempResult*tempResult; 790 if (ok) result+=tempResult*tempResult;
791 } 791 }
792 return result; 792 return result;
793 }else 793 }else
794 { 794 {
795 double d1=0,d2=0; 795 double d1=0,d2=0;
796 d1=calculateVariable(param1).toDouble(&ok); 796 d1=calculateVariable(param1).toDouble(&ok);
797 d2=calculateVariable(param2).toDouble(&ok); 797 d2=calculateVariable(param2).toDouble(&ok);
798 return(d1*d1+d2*d2); 798 return(d1*d1+d2*d2);
799 }; 799 };
800 return 0; 800 return 0;
801} 801}
802 802
803 803
804 804
805double Sheet::functionMin(const QString &param1, const QString &param2) 805double Sheet::functionMin(const QString &param1, const QString &param2)
806{ 806{
807 int row1, col1, row2, col2, row, col; 807 int row1, col1, row2, col2, row, col;
808 double min=0, tempMin; 808 double min=0, tempMin;
809 bool ok, init=FALSE; 809 bool ok, init=FALSE;
810 if (findRange(param1, param2, &row1, &col1, &row2, &col2)) 810 if (findRange(param1, param2, &row1, &col1, &row2, &col2))
811 { 811 {
812 for (row=row1; row<=row2; ++row) 812 for (row=row1; row<=row2; ++row)
813 for (col=col1; col<=col2; ++col) 813 for (col=col1; col<=col2; ++col)
814 { 814 {
815 tempMin=text(row, col).toDouble(&ok); 815 tempMin=text(row, col).toDouble(&ok);
816 if (ok && (!init || tempMin<min)) 816 if (ok && (!init || tempMin<min))
817 { 817 {
818 min=tempMin; 818 min=tempMin;
819 init=TRUE; 819 init=TRUE;
820 } 820 }
821 } 821 }
822 return min; 822 return min;
823 }else 823 }else
824 { 824 {
825 double d1=0,d2=0; 825 double d1=0,d2=0;
826 d1=calculateVariable(param1).toDouble(&ok); 826 d1=calculateVariable(param1).toDouble(&ok);
827 d2=calculateVariable(param2).toDouble(&ok); 827 d2=calculateVariable(param2).toDouble(&ok);
828 if(d1<d2) return(d1); else return(d2); 828 if(d1<d2) return(d1); else return(d2);
829 }; 829 };
830 return 0; 830 return 0;
831} 831}
832 832
833double Sheet::functionMax(const QString &param1, const QString &param2) 833double Sheet::functionMax(const QString &param1, const QString &param2)
834{ 834{
835 int row1, col1, row2, col2, row, col; 835 int row1, col1, row2, col2, row, col;
836 double max=0, tempMax; 836 double max=0, tempMax;
837 bool ok, init=FALSE; 837 bool ok, init=FALSE;
838 if (findRange(param1, param2, &row1, &col1, &row2, &col2)) 838 if (findRange(param1, param2, &row1, &col1, &row2, &col2))
839 { 839 {
840 for (row=row1; row<=row2; ++row) 840 for (row=row1; row<=row2; ++row)
841 for (col=col1; col<=col2; ++col) 841 for (col=col1; col<=col2; ++col)
842 { 842 {
843 tempMax=text(row, col).toDouble(&ok); 843 tempMax=text(row, col).toDouble(&ok);
844 if (ok && (!init || tempMax>max)) 844 if (ok && (!init || tempMax>max))
845 { 845 {
846 max=tempMax; 846 max=tempMax;
847 init=TRUE; 847 init=TRUE;
848 } 848 }
849 }; 849 };
850 return max; 850 return max;
851 }else 851 }else
852 { 852 {
853 double d1=0,d2=0; 853 double d1=0,d2=0;
854 d1=calculateVariable(param1).toDouble(&ok); 854 d1=calculateVariable(param1).toDouble(&ok);
855 d2=calculateVariable(param2).toDouble(&ok); 855 d2=calculateVariable(param2).toDouble(&ok);
856 if(d1>d2) return(d1); else return(d2); 856 if(d1>d2) return(d1); else return(d2);
857 }; 857 };
858 return 0; 858 return 0;
859} 859}
860 860
861double Sheet::functionAvg(const QString &param1, const QString &param2) 861double Sheet::functionAvg(const QString &param1, const QString &param2)
862{ 862{
863 double resultSum=functionSum(param1, param2), resultCount=functionCount(param1, param2); 863 double resultSum=functionSum(param1, param2), resultCount=functionCount(param1, param2);
864 return (resultCount>0 ? resultSum/resultCount : 0); 864 return (resultCount>0 ? resultSum/resultCount : 0);
865} 865}
866 866
867double Sheet::functionCount(const QString &param1, const QString &param2) 867double Sheet::functionCount(const QString &param1, const QString &param2)
868{ 868{
869 int row1, col1, row2, col2, row, col; 869 int row1, col1, row2, col2, row, col;
870 int divider=0; 870 int divider=0;
871 bool ok; 871 bool ok;
872 if (findRange(param1, param2, &row1, &col1, &row2, &col2)) 872 if (findRange(param1, param2, &row1, &col1, &row2, &col2))
873 { 873 {
874 for (row=row1; row<=row2; ++row) 874 for (row=row1; row<=row2; ++row)
875 for (col=col1; col<=col2; ++col) 875 for (col=col1; col<=col2; ++col)
876 { 876 {
877 text(row, col).toDouble(&ok); 877 text(row, col).toDouble(&ok);
878 if (ok) ++divider; 878 if (ok) ++divider;
879 }; 879 };
880 return divider; 880 return divider;
881 }else 881 }else
882 { 882 {
883 double d1=0,d2=0;int ii=0; 883 double d1=0,d2=0;int ii=0;
884 d1=calculateVariable(param1).toDouble(&ok); 884 d1=calculateVariable(param1).toDouble(&ok);
885 if (ok) ii++; 885 if (ok) ii++;
886 d2=calculateVariable(param2).toDouble(&ok); 886 d2=calculateVariable(param2).toDouble(&ok);
887 if (ok) ii++; 887 if (ok) ii++;
888 return(ii); 888 return(ii);
889 }; 889 };
890 return 0; 890 return 0;
891} 891}
892 892
893double Sheet::functionCountIf(const QString &param1, const QString &param2, const QString &param3) 893double Sheet::functionCountIf(const QString &param1, const QString &param2, const QString &param3)
894{ 894{
895 int row1, col1, row2, col2, row, col; 895 int row1, col1, row2, col2, row, col;
896 if (!findRange(param1, param2, &row1, &col1, &row2, &col2)) return 0; 896 if (!findRange(param1, param2, &row1, &col1, &row2, &col2)) return 0;
897 //same as count except check if each field is equal to param3 897 //same as count except check if each field is equal to param3
898 int divider=0; 898 int divider=0;
899 QString s2; 899 QString s2;
900 bool ok; 900 bool ok;
901 s2=calculateVariable(param3); 901 s2=calculateVariable(param3);
902 for (row=row1; row<=row2; ++row) 902 for (row=row1; row<=row2; ++row)
903 for (col=col1; col<=col2; ++col) 903 for (col=col1; col<=col2; ++col)
904 { 904 {
905 text(row, col).toDouble(&ok); 905 text(row, col).toDouble(&ok);
906 if (ok && (s2==text(row,col)) ) ++divider; 906 if (ok && (s2==text(row,col)) ) ++divider;
907 } 907 }
908 return divider; 908 return divider;
909} 909}
910 910
911 911
912QString Sheet::calculateFunction(const QString &function, const QString &parameters, int NumOfParams) 912QString Sheet::calculateFunction(const QString &func, const QString &parameters, int NumOfParams)
913{ 913{
914 bool ok; 914 bool ok;
915 double val1=0.0,val2=0.0,val3=0.0; 915 double val1=0.0,val2=0.0,val3=0.0;
916 long int vali=0; 916 long int vali=0;
917 int w1,w2; 917 int w1,w2;
918 int row,col; 918 int row,col;
919 QString s1,s2; 919 QString s1,s2;
920//basic functions 920//basic functions
921 QString function;
922 function=func.upper();
921 if (function=="+") 923 if (function=="+")
922 { 924 {
923 s1=calculateVariable(getParameter(parameters, 0)); 925 s1=calculateVariable(getParameter(parameters, 0));
924 s2=calculateVariable(getParameter(parameters, 1)); 926 s2=calculateVariable(getParameter(parameters, 1));
925 val1=s1.toDouble(&ok)+s2.toDouble(&ok); 927 val1=s1.toDouble(&ok)+s2.toDouble(&ok);
926 return QString::number(val1); 928 return QString::number(val1);
927 929
928 }; 930 };
929 if (function=="-") 931 if (function=="-")
930 { 932 {
931 s1=calculateVariable(getParameter(parameters, 0)); 933 s1=calculateVariable(getParameter(parameters, 0));
932 s2=calculateVariable(getParameter(parameters, 1)); 934 s2=calculateVariable(getParameter(parameters, 1));
933 val1=s1.toDouble(&ok)-s2.toDouble(&ok); 935 val1=s1.toDouble(&ok)-s2.toDouble(&ok);
934 return QString::number(val1); 936 return QString::number(val1);
935 }; 937 };
936 if (function=="*") 938 if (function=="*")
937 { 939 {
938 val1=calculateVariable( 940 val1=calculateVariable(
939 getParameter(parameters, 0)).toDouble(&ok) 941 getParameter(parameters, 0)).toDouble(&ok)
940 *calculateVariable(getParameter(parameters, 1)).toDouble(&ok); 942 *calculateVariable(getParameter(parameters, 1)).toDouble(&ok);
941 return QString::number(val1); 943 return QString::number(val1);
942 }; 944 };
943 if (function=="/") 945 if (function=="/")
944 { 946 {
945 val1=calculateVariable(getParameter(parameters, 0)).toDouble(&ok); 947 val1=calculateVariable(getParameter(parameters, 0)).toDouble(&ok);
946 val2=calculateVariable(getParameter(parameters, 1)).toDouble(&ok); 948 val2=calculateVariable(getParameter(parameters, 1)).toDouble(&ok);
947 if(val2==0.0) return QString("Err101"); 949 if(val2==0.0) return QString("Err101");
948 val1=val1/val2; 950 val1=val1/val2;
949 return QString::number(val1); 951 return QString::number(val1);
950 }; 952 };
951 if (function==">") 953 if (function==">")
952 { 954 {
953 val1=calculateVariable(getParameter(parameters, 0)).toDouble(&ok); 955 val1=calculateVariable(getParameter(parameters, 0)).toDouble(&ok);
954 val2=calculateVariable(getParameter(parameters, 1)).toDouble(&ok); 956 val2=calculateVariable(getParameter(parameters, 1)).toDouble(&ok);
955 if(val1>val2) return QString::number(1); else return QString::number(0); 957 if(val1>val2) return QString::number(1); else return QString::number(0);
956 }; 958 };
957 if (function=="<") 959 if (function=="<")
958 { 960 {
959 val1=calculateVariable(getParameter(parameters, 0)).toDouble(&ok); 961 val1=calculateVariable(getParameter(parameters, 0)).toDouble(&ok);
960 val2=calculateVariable(getParameter(parameters, 1)).toDouble(&ok); 962 val2=calculateVariable(getParameter(parameters, 1)).toDouble(&ok);
961 if(val1<val2) return QString::number(1); else return QString::number(0); 963 if(val1<val2) return QString::number(1); else return QString::number(0);
962 }; 964 };
963 if (function==">=") 965 if (function==">=")
964 { 966 {
965 val1=calculateVariable(getParameter(parameters, 0)).toDouble(&ok); 967 val1=calculateVariable(getParameter(parameters, 0)).toDouble(&ok);
966 val2=calculateVariable(getParameter(parameters, 1)).toDouble(&ok); 968 val2=calculateVariable(getParameter(parameters, 1)).toDouble(&ok);
967 if(val1>=val2) return QString::number(1); else return QString::number(0); 969 if(val1>=val2) return QString::number(1); else return QString::number(0);
968 }; 970 };
969 if (function=="<=") 971 if (function=="<=")
970 { 972 {
971 val1=calculateVariable(getParameter(parameters, 0)).toDouble(&ok); 973 val1=calculateVariable(getParameter(parameters, 0)).toDouble(&ok);
972 val2=calculateVariable(getParameter(parameters, 1)).toDouble(&ok); 974 val2=calculateVariable(getParameter(parameters, 1)).toDouble(&ok);
973 if(val1<=val2) return QString::number(1); else return QString::number(0); 975 if(val1<=val2) return QString::number(1); else return QString::number(0);
974 }; 976 };
975 if (function=="!=") 977 if (function=="!=")
976 { 978 {
977 val1=calculateVariable(getParameter(parameters, 0)).toDouble(&ok); 979 val1=calculateVariable(getParameter(parameters, 0)).toDouble(&ok);
978 val2=calculateVariable(getParameter(parameters, 1)).toDouble(&ok); 980 val2=calculateVariable(getParameter(parameters, 1)).toDouble(&ok);
979 if(val1!=val2) return QString::number(1); else return QString::number(0); 981 if(val1!=val2) return QString::number(1); else return QString::number(0);
980 }; 982 };
981 if (function=="=="||function=="=") 983 if (function=="=="||function=="=")
982 { 984 {
983 val1=calculateVariable(getParameter(parameters, 0)).toDouble(&ok); 985 val1=calculateVariable(getParameter(parameters, 0)).toDouble(&ok);
984 val2=calculateVariable(getParameter(parameters, 1)).toDouble(&ok); 986 val2=calculateVariable(getParameter(parameters, 1)).toDouble(&ok);
985 if(val1==val2) return QString::number(1); else return QString::number(0); 987 if(val1==val2) return QString::number(1); else return QString::number(0);
986 }; 988 };
987 989
988 //LOGICAL / INFO 990 //LOGICAL / INFO
989 if (function=="ISBLANK") 991 if (function=="ISBLANK")
990 { 992 {
991 if(findRowColumn(getParameter(parameters, 0), &row, &col, FALSE)) 993 if(findRowColumn(getParameter(parameters, 0), &row, &col, FALSE))
992 { 994 {
993 if(text(row,col).length()==0) val1=1; else val1=0; 995 if(text(row,col).length()==0) val1=1; else val1=0;
994 }else 996 }else
995 { 997 {
996 if(findRowColumn(calculateVariable(getParameter(parameters, 0)), &row,&col, FALSE)) 998 if(findRowColumn(calculateVariable(getParameter(parameters, 0)), &row,&col, FALSE))
997 { 999 {
998 if(text(row,col).length()==0) val1=1; else val1=0; 1000 if(text(row,col).length()==0) val1=1; else val1=0;
999 }else 1001 }else
1000 { 1002 {
1001 val1=0; 1003 val1=0;
1002 }; 1004 };
1003 }; 1005 };
1004 return QString::number(val1); 1006 return QString::number(val1);
1005 }; 1007 };
1006 1008
1007 1009
1008 if (function=="ISNUMBER") 1010 if (function=="ISNUMBER")
1009 { 1011 {
1010 if(findRowColumn(getParameter(parameters, 0, TRUE, function), &row, &col, FALSE)) 1012 if(findRowColumn(getParameter(parameters, 0, TRUE, function), &row, &col, FALSE))
1011 { 1013 {
1012 val1=text(row,col).toDouble(&ok); 1014 val1=text(row,col).toDouble(&ok);
1013 if(ok) val1=1; else val1=0; 1015 if(ok) val1=1; else val1=0;
1014 }else 1016 }else
1015 { 1017 {
1016 if(findRowColumn(calculateVariable(getParameter(parameters, 0, TRUE, function)), &row,&col, FALSE)) 1018 if(findRowColumn(calculateVariable(getParameter(parameters, 0, TRUE, function)), &row,&col, FALSE))
1017 { 1019 {
1018 val1=text(row,col).toDouble(&ok); 1020 val1=text(row,col).toDouble(&ok);
1019 if(ok) val1=1; else val1=0; 1021 if(ok) val1=1; else val1=0;
1020 }else 1022 }else
1021 { 1023 {
1022 val1=0; 1024 val1=0;
1023 }; 1025 };
1024 }; 1026 };
1025 return QString::number(val1); 1027 return QString::number(val1);
1026 }; 1028 };
1027 if (function=="AND") 1029 if (function=="AND")
1028 { 1030 {
1029 vali=calculateVariable(getParameter(parameters, 0, TRUE, function)).toInt(&ok) 1031 vali=calculateVariable(getParameter(parameters, 0, TRUE, function)).toInt(&ok)
1030 & calculateVariable(getParameter(parameters, 1, TRUE, function)).toInt(&ok); 1032 & calculateVariable(getParameter(parameters, 1, TRUE, function)).toInt(&ok);
1031 return QString::number(vali); 1033 return QString::number(vali);
1032 }; 1034 };
1033 if (function=="OR") 1035 if (function=="OR")
1034 { 1036 {
1035 vali=calculateVariable(getParameter(parameters, 0, TRUE, function)).toInt(&ok) 1037 vali=calculateVariable(getParameter(parameters, 0, TRUE, function)).toInt(&ok)
1036 | calculateVariable(getParameter(parameters, 1, TRUE, function)).toInt(&ok); 1038 | calculateVariable(getParameter(parameters, 1, TRUE, function)).toInt(&ok);
1037 return QString::number(vali); 1039 return QString::number(vali);
1038 }; 1040 };
1039 if (function=="NOT") 1041 if (function=="NOT")
1040 { 1042 {
1041 vali=!calculateVariable(getParameter(parameters, 0, TRUE, function)).toInt(&ok); 1043 vali=!calculateVariable(getParameter(parameters, 0, TRUE, function)).toInt(&ok);
1042 return QString::number(vali); 1044 return QString::number(vali);
1043 }; 1045 };
1044 1046
1045 // MATHEMATICAL FUNCTIONS 1047 // MATHEMATICAL FUNCTIONS
1046 if (function=="ABS") 1048 if (function=="ABS")
1047 { 1049 {
1048 val1=fabs(calculateVariable(getParameter(parameters, 0, TRUE, function)).toDouble(&ok)); 1050 val1=fabs(calculateVariable(getParameter(parameters, 0, TRUE, function)).toDouble(&ok));
1049 return QString::number(val1); 1051 return QString::number(val1);
1050 }; 1052 };
1051 if (function=="ACOS") 1053 if (function=="ACOS")
1052 { 1054 {
1053 val1=acos(calculateVariable(getParameter(parameters, 0, TRUE, function)).toDouble(&ok)); 1055 val1=acos(calculateVariable(getParameter(parameters, 0, TRUE, function)).toDouble(&ok));
1054 return QString::number(val1); 1056 return QString::number(val1);
1055 }; 1057 };
1056 if (function=="ACOSH") 1058 if (function=="ACOSH")
1057 { 1059 {
1058 val1=calculateVariable(getParameter(parameters, 0, TRUE, function)).toDouble(&ok); 1060 val1=calculateVariable(getParameter(parameters, 0, TRUE, function)).toDouble(&ok);
1059 if(val1<1.0) return QString::number(0); 1061 if(val1<1.0) return QString::number(0);
1060 val1=acosh(val1); 1062 val1=acosh(val1);
1061 return QString::number(val1); 1063 return QString::number(val1);
1062 }; 1064 };
1063 if (function=="ASIN") 1065 if (function=="ASIN")
1064 { 1066 {
1065 val1=asin(calculateVariable(getParameter(parameters, 0, TRUE, function)).toDouble(&ok)); 1067 val1=asin(calculateVariable(getParameter(parameters, 0, TRUE, function)).toDouble(&ok));
1066 return QString::number(val1); 1068 return QString::number(val1);
1067 }; 1069 };
1068 if (function=="ASINH") 1070 if (function=="ASINH")
1069 { 1071 {
1070 val1=asinh(calculateVariable(getParameter(parameters, 0, TRUE, function)).toDouble(&ok)); 1072 val1=asinh(calculateVariable(getParameter(parameters, 0, TRUE, function)).toDouble(&ok));
1071 return QString::number(val1); 1073 return QString::number(val1);
1072 }; 1074 };
1073 if (function=="ATAN") 1075 if (function=="ATAN")
1074 { 1076 {
1075 val1=atan(calculateVariable(getParameter(parameters, 0, TRUE, function)).toDouble(&ok)); 1077 val1=atan(calculateVariable(getParameter(parameters, 0, TRUE, function)).toDouble(&ok));
1076 return QString::number(val1); 1078 return QString::number(val1);
1077 }; 1079 };
1078 if (function=="ATAN2") 1080 if (function=="ATAN2")
1079 { 1081 {
1080 val1=atan2(calculateVariable(getParameter(parameters, 1, TRUE, function)).toDouble(&ok), 1082 val1=atan2(calculateVariable(getParameter(parameters, 1, TRUE, function)).toDouble(&ok),
1081 calculateVariable(getParameter(parameters, 0, TRUE, function)).toDouble(&ok)); 1083 calculateVariable(getParameter(parameters, 0, TRUE, function)).toDouble(&ok));
1082 return QString::number(val1); 1084 return QString::number(val1);
1083 }; 1085 };
1084 if (function=="ATANH") 1086 if (function=="ATANH")
1085 { 1087 {
1086 val1=calculateVariable(getParameter(parameters, 0, TRUE, function)).toDouble(&ok); 1088 val1=calculateVariable(getParameter(parameters, 0, TRUE, function)).toDouble(&ok);
1087 if(val1<=-1.0 || val1>=1.0) return QString("Err101"); 1089 if(val1<=-1.0 || val1>=1.0) return QString("Err101");
1088 val1=atanh(val1); 1090 val1=atanh(val1);
1089 return QString::number(val1); 1091 return QString::number(val1);
1090 }; 1092 };
1091 if (function=="CEILING") 1093 if (function=="CEILING")
1092 { 1094 {
1093 // rounds up param1 to specified accuracy param2 1095 // rounds up param1 to specified accuracy param2
1094 val1=calculateVariable(getParameter(parameters, 0, TRUE, function)).toDouble(&ok); 1096 val1=calculateVariable(getParameter(parameters, 0, TRUE, function)).toDouble(&ok);
1095 val2=calculateVariable(getParameter(parameters, 1, TRUE, function)).toDouble(&ok); 1097 val2=calculateVariable(getParameter(parameters, 1, TRUE, function)).toDouble(&ok);
1096 if(val2==0.0) return QString::number(val1); 1098 if(val2==0.0) return QString::number(val1);
1097 val1=ceil(val1/val2)*val2; 1099 val1=ceil(val1/val2)*val2;
1098 return QString::number(val1); 1100 return QString::number(val1);
1099 }; 1101 };
1100 if (function=="COS") 1102 if (function=="COS")
1101 { 1103 {
1102 val1=cos(calculateVariable(getParameter(parameters, 0, TRUE, function)).toDouble(&ok)); 1104 val1=cos(calculateVariable(getParameter(parameters, 0, TRUE, function)).toDouble(&ok));
1103 return QString::number(val1); 1105 return QString::number(val1);
1104 }; 1106 };
1105 if (function=="COSH") 1107 if (function=="COSH")
1106 { 1108 {
1107 val1=cosh(calculateVariable(getParameter(parameters, 0, TRUE, function)).toDouble(&ok)); 1109 val1=cosh(calculateVariable(getParameter(parameters, 0, TRUE, function)).toDouble(&ok));
1108 return QString::number(val1); 1110 return QString::number(val1);
1109 }; 1111 };
1110 if (function=="DEGREES") 1112 if (function=="DEGREES")
1111 { 1113 {
1112 val1=calculateVariable(getParameter(parameters, 0, TRUE, function)).toDouble(&ok)*180.0/M_PI; 1114 val1=calculateVariable(getParameter(parameters, 0, TRUE, function)).toDouble(&ok)*180.0/M_PI;
1113 return QString::number(val1); 1115 return QString::number(val1);
1114 }; 1116 };
1115 if (function=="EXP") 1117 if (function=="EXP")
1116 { 1118 {
1117 val1=exp(calculateVariable(getParameter(parameters, 0, TRUE, function)).toDouble(&ok)); 1119 val1=exp(calculateVariable(getParameter(parameters, 0, TRUE, function)).toDouble(&ok));
1118 return QString::number(val1); 1120 return QString::number(val1);
1119 }; 1121 };
1120 if (function=="FACT") 1122 if (function=="FACT")
1121 { 1123 {
1122 val1=calculateVariable(getParameter(parameters, 0, TRUE, function)).toDouble(&ok); 1124 val1=calculateVariable(getParameter(parameters, 0, TRUE, function)).toDouble(&ok);
1123 val2=Factorial(val1); 1125 val2=Factorial(val1);
1124 return QString::number(val2); 1126 return QString::number(val2);
1125 }; 1127 };
1126 if (function=="FLOOR") 1128 if (function=="FLOOR")
1127 { 1129 {
1128 // rounds down param1 to specified accuracy param2 1130 // rounds down param1 to specified accuracy param2
1129 val1=calculateVariable(getParameter(parameters, 0, TRUE, function)).toDouble(&ok); 1131 val1=calculateVariable(getParameter(parameters, 0, TRUE, function)).toDouble(&ok);
1130 val2=calculateVariable(getParameter(parameters, 1, TRUE, function)).toDouble(&ok); 1132 val2=calculateVariable(getParameter(parameters, 1, TRUE, function)).toDouble(&ok);
1131 if(val2==0.0) return QString::number(val1); 1133 if(val2==0.0) return QString::number(val1);
1132 val1=floor(val1/val2)*val2; 1134 val1=floor(val1/val2)*val2;
1133 return QString::number(val1); 1135 return QString::number(val1);
1134 }; 1136 };
1135 if (function=="INT") 1137 if (function=="INT")
1136 { 1138 {
1137 // rounds down param1 1139 // rounds down param1
1138 val1=int(calculateVariable(getParameter(parameters, 0, TRUE, function)).toDouble(&ok)); 1140 val1=int(calculateVariable(getParameter(parameters, 0, TRUE, function)).toDouble(&ok));
1139 return QString::number(val1); 1141 return QString::number(val1);
1140 }; 1142 };
1141 if (function=="EVEN") 1143 if (function=="EVEN")
1142 { 1144 {
1143 //converts param1 to even 1145 //converts param1 to even
1144 vali=int(calculateVariable(getParameter(parameters, 0, TRUE, function)).toDouble(&ok)); 1146 vali=int(calculateVariable(getParameter(parameters, 0, TRUE, function)).toDouble(&ok));
1145 if(vali % 2 !=0) val1=vali+1; else val1=vali; 1147 if(vali % 2 !=0) val1=vali+1; else val1=vali;
1146 return QString::number(val1); 1148 return QString::number(val1);
1147 }; 1149 };
1148 if (function=="ODD") 1150 if (function=="ODD")
1149 { 1151 {
1150 //converts param1 to odd 1152 //converts param1 to odd
1151 vali=int(calculateVariable(getParameter(parameters, 0, TRUE, function)).toDouble(&ok)); 1153 vali=int(calculateVariable(getParameter(parameters, 0, TRUE, function)).toDouble(&ok));
1152 if(vali % 2 !=0) val1=vali; else val1=vali+1; 1154 if(vali % 2 !=0) val1=vali; else val1=vali+1;
1153 return QString::number(val1); 1155 return QString::number(val1);
1154 }; 1156 };
1155 if (function=="ISEVEN") 1157 if (function=="ISEVEN")
1156 { 1158 {
1157 //Is Even param1? 1159 //Is Even param1?
1158 vali=int(calculateVariable(getParameter(parameters, 0, TRUE, function)).toDouble(&ok)); 1160 vali=int(calculateVariable(getParameter(parameters, 0, TRUE, function)).toDouble(&ok));
1159 if(vali % 2 == 0) val1=1; else val1=0; 1161 if(vali % 2 == 0) val1=1; else val1=0;
1160 return QString::number(val1); 1162 return QString::number(val1);
1161 }; 1163 };
1162 if (function=="ISODD") 1164 if (function=="ISODD")
1163 { 1165 {
1164 //Is odd param1? 1166 //Is odd param1?
1165 vali=int(calculateVariable(getParameter(parameters, 0, TRUE, function)).toDouble(&ok)); 1167 vali=int(calculateVariable(getParameter(parameters, 0, TRUE, function)).toDouble(&ok));
1166 if(vali % 2 == 0) val1=0; else val1=1; 1168 if(vali % 2 == 0) val1=0; else val1=1;
1167 return QString::number(val1); 1169 return QString::number(val1);
1168 }; 1170 };
1169 if (function=="LN") 1171 if (function=="LN")
1170 { 1172 {
1171 // returns the natural logarithm of param1 1173 // returns the natural logarithm of param1
1172 val1=calculateVariable(getParameter(parameters, 0, TRUE, function)).toDouble(&ok); 1174 val1=calculateVariable(getParameter(parameters, 0, TRUE, function)).toDouble(&ok);
1173 if(val1<=0.0) return QString("Err101"); 1175 if(val1<=0.0) return QString("Err101");
1174 val1=log(val1); 1176 val1=log(val1);
1175 return QString::number(val1); 1177 return QString::number(val1);
1176 }; 1178 };
1177 if (function=="LOG10") 1179 if (function=="LOG10")
1178 { 1180 {
1179 // returns the base-10 logarithm of param1 1181 // returns the base-10 logarithm of param1
1180 val1=calculateVariable(getParameter(parameters, 0, TRUE, function)).toDouble(&ok); 1182 val1=calculateVariable(getParameter(parameters, 0, TRUE, function)).toDouble(&ok);
1181 if(val1<=0.0) return QString("Err101"); 1183 if(val1<=0.0) return QString("Err101");
1182 val1=log10(val1); 1184 val1=log10(val1);
1183 return QString::number(val1); 1185 return QString::number(val1);
1184 }; 1186 };
1185 if (function=="LOG") 1187 if (function=="LOG")
1186 { 1188 {
1187 // return the base-param2 logarithm of param1 1189 // return the base-param2 logarithm of param1
1188 val1=calculateVariable(getParameter(parameters, 0, TRUE, function)).toDouble(&ok); 1190 val1=calculateVariable(getParameter(parameters, 0, TRUE, function)).toDouble(&ok);
1189 val2=calculateVariable(getParameter(parameters, 1, TRUE, function)).toDouble(&ok); 1191 val2=calculateVariable(getParameter(parameters, 1, TRUE, function)).toDouble(&ok);
1190 if(val1<=0.0 || val2<=0.0 ) return QString("Err101"); 1192 if(val1<=0.0 || val2<=0.0 ) return QString("Err101");
1191 val1=log(val1)/log(val2); 1193 val1=log(val1)/log(val2);
1192 return QString::number(val1); 1194 return QString::number(val1);
1193 }; 1195 };
1194 if (function=="MOD") 1196 if (function=="MOD")
1195 { 1197 {
1196 // return the modulus of param1/param2 1198 // return the modulus of param1/param2
1197 val1=calculateVariable(getParameter(parameters, 0, TRUE, function)).toDouble(&ok); 1199 val1=calculateVariable(getParameter(parameters, 0, TRUE, function)).toDouble(&ok);
1198 val2=calculateVariable(getParameter(parameters, 1, TRUE, function)).toDouble(&ok); 1200 val2=calculateVariable(getParameter(parameters, 1, TRUE, function)).toDouble(&ok);
1199 if(val2==0.0) return QString("Err101"); 1201 if(val2==0.0) return QString("Err101");
1200 val1=(int(val1) % int(val2)); 1202 val1=(int(val1) % int(val2));
1201 return QString::number(val1); 1203 return QString::number(val1);
1202 }; 1204 };
1203 if (function=="POWER") 1205 if (function=="POWER")
1204 { 1206 {
1205 // return the param1^param2 1207 // return the param1^param2
1206 val1=calculateVariable(getParameter(parameters, 0, TRUE, function)).toDouble(&ok); 1208 val1=calculateVariable(getParameter(parameters, 0, TRUE, function)).toDouble(&ok);
1207 val2=calculateVariable(getParameter(parameters, 1, TRUE, function)).toDouble(&ok); 1209 val2=calculateVariable(getParameter(parameters, 1, TRUE, function)).toDouble(&ok);
1208 if(val1<0.0 && (floor(val2)!=val2)) return QString("Err101"); 1210 if(val1<0.0 && (floor(val2)!=val2)) return QString("Err101");
1209 val1=pow(val1,val2); 1211 val1=pow(val1,val2);
1210 return QString::number(val1); 1212 return QString::number(val1);
1211 }; 1213 };
1212 if (function=="PI") 1214 if (function=="PI")
1213 { 1215 {
1214 return QString::number(M_PI); 1216 return QString::number(M_PI);
1215 }; 1217 };
1216 if (function=="RADIANS") 1218 if (function=="RADIANS")
1217 { 1219 {
1218 // param1 deg->rad 1220 // param1 deg->rad
1219 val1=calculateVariable(getParameter(parameters, 0, TRUE, function)).toDouble(&ok)*M_PI/180.0; 1221 val1=calculateVariable(getParameter(parameters, 0, TRUE, function)).toDouble(&ok)*M_PI/180.0;
1220 return QString::number(val1); 1222 return QString::number(val1);
1221 }; 1223 };
1222 if (function=="RAND") 1224 if (function=="RAND")
1223 { 1225 {
1224 // retuns random number 0>x>1 1226 // retuns random number 0>x>1
1225 srand((unsigned int)time((time_t *)NULL)); 1227 srand((unsigned int)time((time_t *)NULL));
1226 val1=double(rand())/double(RAND_MAX); 1228 val1=double(rand())/double(RAND_MAX);
1227 return QString::number(val1); 1229 return QString::number(val1);
1228 }; 1230 };
1229 if (function=="RANDBETWEEN") 1231 if (function=="RANDBETWEEN")
1230 { 1232 {
1231 // returns random number between param1>x>param2 1233 // returns random number between param1>x>param2
1232 //TOFIX: this is not ok because I think results is always int related. 1234 //TOFIX: this is not ok because I think results is always int related.
1233 srand((unsigned int)time((time_t *)NULL)); 1235 srand((unsigned int)time((time_t *)NULL));
1234 val1=calculateVariable(getParameter(parameters, 0, TRUE, function)).toDouble(&ok); 1236 val1=calculateVariable(getParameter(parameters, 0, TRUE, function)).toDouble(&ok);
1235 val2=calculateVariable(getParameter(parameters, 1, TRUE, function)).toDouble(&ok); 1237 val2=calculateVariable(getParameter(parameters, 1, TRUE, function)).toDouble(&ok);
1236 val1=fmod(double(rand()),(val2-val1))+val1; 1238 val1=fmod(double(rand()),(val2-val1))+val1;
1237 return QString::number(val1); 1239 return QString::number(val1);
1238 }; 1240 };
1239 if (function=="ROUND") 1241 if (function=="ROUND")
1240 { 1242 {
1241 // rounds down param1 to specified digits param2 (positive decimal digits) 1243 // rounds down param1 to specified digits param2 (positive decimal digits)
1242 val1=calculateVariable(getParameter(parameters, 0, TRUE, function)).toDouble(&ok); 1244 val1=calculateVariable(getParameter(parameters, 0, TRUE, function)).toDouble(&ok);
1243 val2=calculateVariable(getParameter(parameters, 1, TRUE, function)).toDouble(&ok); 1245 val2=calculateVariable(getParameter(parameters, 1, TRUE, function)).toDouble(&ok);
1244 val2=pow(10.0,-val2); 1246 val2=pow(10.0,-val2);
1245 val1=floor(val1/val2)*val2; 1247 val1=floor(val1/val2)*val2;
1246 return QString::number(val1); 1248 return QString::number(val1);
1247 }; 1249 };
1248 if (function=="SIGN") 1250 if (function=="SIGN")
1249 { 1251 {
1250 val1=calculateVariable(getParameter(parameters, 0, TRUE, function)).toDouble(&ok); 1252 val1=calculateVariable(getParameter(parameters, 0, TRUE, function)).toDouble(&ok);
1251 if(val1>=0.0) return QString::number(1.0); else return QString::number(-1.0); 1253 if(val1>=0.0) return QString::number(1.0); else return QString::number(-1.0);
1252 }; 1254 };
1253 if (function=="CHGSGN")//changes sign (for unary operator) 1255 if (function=="CHGSGN")//changes sign (for unary operator)
1254 { 1256 {
1255 val1=calculateVariable(getParameter(parameters, 0, TRUE, function)).toDouble(&ok); 1257 val1=calculateVariable(getParameter(parameters, 0, TRUE, function)).toDouble(&ok);
1256 return QString::number((-1.0)*val1); 1258 return QString::number((-1.0)*val1);
1257 }; 1259 };
1258 if (function=="SIN") 1260 if (function=="SIN")
1259 { 1261 {
1260 val1=sin(calculateVariable(getParameter(parameters, 0, TRUE, function)).toDouble(&ok)); 1262 val1=sin(calculateVariable(getParameter(parameters, 0, TRUE, function)).toDouble(&ok));
1261 return QString::number(val1); 1263 return QString::number(val1);
1262 }; 1264 };
1263 if (function=="SINH") 1265 if (function=="SINH")
1264 { 1266 {
1265 val1=sinh(calculateVariable(getParameter(parameters, 0, TRUE, function)).toDouble(&ok)); 1267 val1=sinh(calculateVariable(getParameter(parameters, 0, TRUE, function)).toDouble(&ok));
1266 return QString::number(val1); 1268 return QString::number(val1);
1267 }; 1269 };
1268 if (function=="TAN") 1270 if (function=="TAN")
1269 { 1271 {
1270 val1=tan(calculateVariable(getParameter(parameters, 0, TRUE, function)).toDouble(&ok)); 1272 val1=tan(calculateVariable(getParameter(parameters, 0, TRUE, function)).toDouble(&ok));
1271 return QString::number(val1); 1273 return QString::number(val1);
1272 }; 1274 };
1273 if (function=="TANH") 1275 if (function=="TANH")
1274 { 1276 {
1275 val1=tanh(calculateVariable(getParameter(parameters, 0, TRUE, function)).toDouble(&ok)); 1277 val1=tanh(calculateVariable(getParameter(parameters, 0, TRUE, function)).toDouble(&ok));
1276 return QString::number(val1); 1278 return QString::number(val1);
1277 }; 1279 };
1278 if (function=="SQRT") 1280 if (function=="SQRT")
1279 { 1281 {
1280 val1=sqrt(calculateVariable(getParameter(parameters, 0, TRUE, function)).toDouble(&ok)); 1282 val1=sqrt(calculateVariable(getParameter(parameters, 0, TRUE, function)).toDouble(&ok));
1281 return QString::number(val1); 1283 return QString::number(val1);
1282 }; 1284 };
1283 1285
1284 1286
1285// STRING FUNCTIONS 1287// STRING FUNCTIONS
1286 if (function=="CONCATENATE") 1288 if (function=="CONCATENATE")
1287 { 1289 {
1288 // concatenates strings together 1290 // concatenates strings together
1289 w1=0; 1291 w1=0;
1290 s1=""; 1292 s1="";
1291 while(getParameter(parameters, w1, FALSE, function)!="") //parse all params; 1293 while(getParameter(parameters, w1, FALSE, function)!="") //parse all params;
1292 { 1294 {
1293 s1=s1+calculateVariable(getParameter(parameters, w1)); 1295 s1=s1+calculateVariable(getParameter(parameters, w1));
1294 w1++; 1296 w1++;
1295 }; 1297 };
1296 return QString(s1); 1298 return QString(s1);
1297 }; 1299 };
1298 if (function=="EXACT") 1300 if (function=="EXACT")
1299 { 1301 {
1300 // compare two string if they are exactly the same 1302 // compare two string if they are exactly the same
1301 s1=calculateVariable(getParameter(parameters, 0, TRUE, function)); 1303 s1=calculateVariable(getParameter(parameters, 0, TRUE, function));
1302 s2=calculateVariable(getParameter(parameters, 1, TRUE, function)); 1304 s2=calculateVariable(getParameter(parameters, 1, TRUE, function));
1303 if(s1==s2) return QString::number(1); else return QString::number(0); 1305 if(s1==s2) return QString::number(1); else return QString::number(0);
1304 }; 1306 };
1305 if (function=="FIND") 1307 if (function=="FIND")
1306 { 1308 {
1307 // finds param1 in param2 from pos param3 and after 1309 // finds param1 in param2 from pos param3 and after
1308 // returns -1 if not found 1310 // returns -1 if not found
1309 s1=calculateVariable(getParameter(parameters, 0, TRUE, function)); 1311 s1=calculateVariable(getParameter(parameters, 0, TRUE, function));
1310 s2=calculateVariable(getParameter(parameters, 1, TRUE, function)); 1312 s2=calculateVariable(getParameter(parameters, 1, TRUE, function));
1311 vali=calculateVariable(getParameter(parameters, 2, TRUE, function)).toInt(&ok); 1313 vali=calculateVariable(getParameter(parameters, 2, TRUE, function)).toInt(&ok);
1312 val1=s2.find(s1,vali); 1314 val1=s2.find(s1,vali);
1313 return QString::number(val1); 1315 return QString::number(val1);
1314 }; 1316 };
1315 if (function=="LEFT") 1317 if (function=="LEFT")
1316 { 1318 {
1317 // returns the param2 left chars from param1 string 1319 // returns the param2 left chars from param1 string
1318 s1=calculateVariable(getParameter(parameters, 0, TRUE, function)); 1320 s1=calculateVariable(getParameter(parameters, 0, TRUE, function));
1319 vali=calculateVariable(getParameter(parameters, 1, TRUE, function)).toInt(&ok); 1321 vali=calculateVariable(getParameter(parameters, 1, TRUE, function)).toInt(&ok);
1320 s2=s1.left(vali); 1322 s2=s1.left(vali);
1321 return QString(s2); 1323 return QString(s2);
1322 }; 1324 };
1323 if (function=="LEN") 1325 if (function=="LEN")
1324 { 1326 {
1325 // return the length of a string(param1) 1327 // return the length of a string(param1)
1326 s1=calculateVariable(getParameter(parameters, 0, TRUE, function)); 1328 s1=calculateVariable(getParameter(parameters, 0, TRUE, function));
1327 return QString::number(s1.length()); 1329 return QString::number(s1.length());
1328 }; 1330 };
1329 if (function=="MID") 1331 if (function=="MID")
1330 { 1332 {
1331 // returns the mid word of string param1 with start param2 and len param3 1333 // returns the mid word of string param1 with start param2 and len param3
1332 s1=calculateVariable(getParameter(parameters, 0, TRUE, function)); 1334 s1=calculateVariable(getParameter(parameters, 0, TRUE, function));
1333 w1=calculateVariable(getParameter(parameters, 1, TRUE, function)).toInt(&ok); 1335 w1=calculateVariable(getParameter(parameters, 1, TRUE, function)).toInt(&ok);
1334 w2=calculateVariable(getParameter(parameters, 2, TRUE, function)).toInt(&ok); 1336 w2=calculateVariable(getParameter(parameters, 2, TRUE, function)).toInt(&ok);
1335 s2=s1.mid(w1,w2); 1337 s2=s1.mid(w1,w2);
1336 return QString(s2); 1338 return QString(s2);
1337 }; 1339 };
1338 if (function=="REPLACE") 1340 if (function=="REPLACE")
1339 { 1341 {
1340 //replace in param1 text in pos param2 and length param3 to newtext param4 1342 //replace in param1 text in pos param2 and length param3 to newtext param4
1341 s1=calculateVariable(getParameter(parameters, 0, TRUE, function)); 1343 s1=calculateVariable(getParameter(parameters, 0, TRUE, function));
1342 w1=calculateVariable(getParameter(parameters, 1, TRUE, function)).toInt(&ok); 1344 w1=calculateVariable(getParameter(parameters, 1, TRUE, function)).toInt(&ok);
1343 w2=calculateVariable(getParameter(parameters, 2, TRUE, function)).toInt(&ok); 1345 w2=calculateVariable(getParameter(parameters, 2, TRUE, function)).toInt(&ok);
1344 s2=calculateVariable(getParameter(parameters, 3, TRUE, function)); 1346 s2=calculateVariable(getParameter(parameters, 3, TRUE, function));
1345 if(w1<0 || w2<0) return QString(s1); 1347 if(w1<0 || w2<0) return QString(s1);
1346 s1=s1.left(w2-1)+s2+s1.right(s1.length()-w1-w2); 1348 s1=s1.left(w2-1)+s2+s1.right(s1.length()-w1-w2);
1347 return QString(s1); 1349 return QString(s1);
1348 }; 1350 };
1349 if (function=="REPT") 1351 if (function=="REPT")
1350 { 1352 {
1351 //repeats param1 string param2 times 1353 //repeats param1 string param2 times
1352 s1=calculateVariable(getParameter(parameters, 0, TRUE, function)); 1354 s1=calculateVariable(getParameter(parameters, 0, TRUE, function));
1353 w1=calculateVariable(getParameter(parameters, 1, TRUE, function)).toInt(&ok); 1355 w1=calculateVariable(getParameter(parameters, 1, TRUE, function)).toInt(&ok);
1354 for(w2=1;w2<=w1;w2++) 1356 for(w2=1;w2<=w1;w2++)
1355 { 1357 {
1356 s2=s2.append(s1); 1358 s2=s2.append(s1);
1357 }; 1359 };
1358 return QString(s2); 1360 return QString(s2);
1359 }; 1361 };
1360 if (function=="RIGHT") 1362 if (function=="RIGHT")
1361 { 1363 {
1362 // returns the param2 right chars from param1 string 1364 // returns the param2 right chars from param1 string
1363 s1=calculateVariable(getParameter(parameters, 0, TRUE, function)); 1365 s1=calculateVariable(getParameter(parameters, 0, TRUE, function));
1364 vali=calculateVariable(getParameter(parameters, 1, TRUE, function)).toInt(&ok); 1366 vali=calculateVariable(getParameter(parameters, 1, TRUE, function)).toInt(&ok);
1365 s2=s1.right(vali); 1367 s2=s1.right(vali);
1366 return QString(s2); 1368 return QString(s2);
1367 }; 1369 };
1368 if (function=="UPPER") 1370 if (function=="UPPER")
1369 { 1371 {
1370 // returns the upper param1 string 1372 // returns the upper param1 string
1371 s1=calculateVariable(getParameter(parameters, 0, TRUE, function)); 1373 s1=calculateVariable(getParameter(parameters, 0, TRUE, function));
1372 s1=s1.upper(); 1374 s1=s1.upper();
1373 return QString(s1); 1375 return QString(s1);
1374 }; 1376 };
1375 if (function=="LOWER") 1377 if (function=="LOWER")
1376 { 1378 {
1377 // returns the lower param1 string 1379 // returns the lower param1 string
1378 s1=calculateVariable(getParameter(parameters, 0, TRUE, function)); 1380 s1=calculateVariable(getParameter(parameters, 0, TRUE, function));
1379 s1=s1.lower(); 1381 s1=s1.lower();
1380 return QString(s1); 1382 return QString(s1);
1381 }; 1383 };
1382 if (function=="IF") 1384 if (function=="IF")
1383 { 1385 {
1384 //usage: IF(param1,param2,param3) 1386 //usage: IF(param1,param2,param3)
1385 //returns param4 if true(param1)/ param5 if false(param1) 1387 //returns param4 if true(param1)/ param5 if false(param1)
1386 val1=getParameter(parameters, 0, TRUE, function).toDouble(&ok); 1388 val1=getParameter(parameters, 0, TRUE, function).toDouble(&ok);
1387 if(val1==1.0) 1389 if(val1==1.0)
1388 { 1390 {
1389 s1=calculateVariable(getParameter(parameters, 1, TRUE, function)); 1391 s1=calculateVariable(getParameter(parameters, 1, TRUE, function));
1390 return QString(s1); 1392 return QString(s1);
1391 }else 1393 }else
1392 { 1394 {
1393 s1=calculateVariable(getParameter(parameters, 2, TRUE, function)); 1395 s1=calculateVariable(getParameter(parameters, 2, TRUE, function));
1394 return QString(s1); 1396 return QString(s1);
1395 }; 1397 };
1396 }; 1398 };
1397 if (function=="SUM") 1399 if (function=="SUM")
1398 { 1400 {
1399 //NumOfParams 1401 //NumOfParams
1400 val2=0.0; 1402 val2=0.0;
1401 for(w1=1;w1<=(NumOfParams/2);w1++) 1403 for(w1=1;w1<=(NumOfParams/2);w1++)
1402 { 1404 {
1403 val1=functionSum(getParameter(parameters, (w1-1)*2, FALSE, function), getParameter(parameters, (w1-1)*2+1, TRUE, function)); 1405 val1=functionSum(getParameter(parameters, (w1-1)*2, FALSE, function), getParameter(parameters, (w1-1)*2+1, TRUE, function));
1404 val2=val2+val1; 1406 val2=val2+val1;
1405 }; 1407 };
1406 if(NumOfParams%2==1) 1408 if(NumOfParams%2==1)
1407 { 1409 {
1408 val2=val2+calculateVariable(getParameter(parameters,NumOfParams-1,FALSE, function)).toDouble(&ok); 1410 val2=val2+calculateVariable(getParameter(parameters,NumOfParams-1,FALSE, function)).toDouble(&ok);
1409 }; 1411 };
1410 return QString::number(val2); 1412 return QString::number(val2);
1411 }; 1413 };
1412 if (function=="INDEX") 1414 if (function=="INDEX")
1413 { 1415 {
1414 s1=functionIndex(getParameter(parameters,0,TRUE,function), getParameter(parameters, 1, TRUE, function), getParameter(parameters,2,TRUE,function).toInt(&ok)); 1416 s1=functionIndex(getParameter(parameters,0,TRUE,function), getParameter(parameters, 1, TRUE, function), getParameter(parameters,2,TRUE,function).toInt(&ok));
1415 return QString(s1); 1417 return QString(s1);
1416 }; 1418 };
1417 if (function=="SUMSQ") 1419 if (function=="SUMSQ")
1418 { 1420 {
1419 //NumOfParams 1421 //NumOfParams
1420 val2=0.0; 1422 val2=0.0;
1421 for(w1=1;w1<=(NumOfParams/2);w1++) 1423 for(w1=1;w1<=(NumOfParams/2);w1++)
1422 { 1424 {
1423 val1=functionSumSQ(getParameter(parameters, (w1-1)*2, FALSE, function), getParameter(parameters, (w1-1)*2+1, TRUE, function)); 1425 val1=functionSumSQ(getParameter(parameters, (w1-1)*2, FALSE, function), getParameter(parameters, (w1-1)*2+1, TRUE, function));
1424 val2=val2+val1; 1426 val2=val2+val1;
1425 }; 1427 };
1426 if(NumOfParams%2==1) 1428 if(NumOfParams%2==1)
1427 { 1429 {
1428 val1=calculateVariable(getParameter(parameters,NumOfParams-1,FALSE, function)).toDouble(&ok); 1430 val1=calculateVariable(getParameter(parameters,NumOfParams-1,FALSE, function)).toDouble(&ok);
1429 val2=val2+val1*val1; 1431 val2=val2+val1*val1;
1430 }; 1432 };
1431 return QString::number(val2); 1433 return QString::number(val2);
1432 }; 1434 };
1433 if (function=="COUNT") 1435 if (function=="COUNT")
1434 { 1436 {
1435 //NumOfParams 1437 //NumOfParams
1436 val2=0.0; 1438 val2=0.0;
1437 for(w1=1;w1<=(NumOfParams/2);w1++) 1439 for(w1=1;w1<=(NumOfParams/2);w1++)
1438 { 1440 {
1439 val1=functionCount(getParameter(parameters, (w1-1)*2, FALSE, function), getParameter(parameters, (w1-1)*2+1, TRUE, function)); 1441 val1=functionCount(getParameter(parameters, (w1-1)*2, FALSE, function), getParameter(parameters, (w1-1)*2+1, TRUE, function));
1440 val2=val2+val1; 1442 val2=val2+val1;
1441 }; 1443 };
1442 if(NumOfParams%2==1) 1444 if(NumOfParams%2==1)
1443 { 1445 {
1444 val1=calculateVariable(getParameter(parameters,NumOfParams-1,FALSE, function)).toDouble(&ok); 1446 val1=calculateVariable(getParameter(parameters,NumOfParams-1,FALSE, function)).toDouble(&ok);
1445 if(ok) val2=val2+1; 1447 if(ok) val2=val2+1;
1446 }; 1448 };
1447 return QString::number(val2); 1449 return QString::number(val2);
1448 }; 1450 };
1449 if (function=="COUNTIF") 1451 if (function=="COUNTIF")
1450 { 1452 {
1451 //NumOfParams 1453 //NumOfParams
1452 val1=functionCountIf(getParameter(parameters, 0, TRUE, function), getParameter(parameters, 1, TRUE, function), getParameter(parameters, 2, TRUE, function)); 1454 val1=functionCountIf(getParameter(parameters, 0, TRUE, function), getParameter(parameters, 1, TRUE, function), getParameter(parameters, 2, TRUE, function));
1453 return QString::number(val1); 1455 return QString::number(val1);
1454 }; 1456 };
1455 if (function=="MIN") 1457 if (function=="MIN")
1456 { 1458 {
1457 //NumOfParams 1459 //NumOfParams
1458 val2=0.0; 1460 val2=0.0;
1459 for(w1=1;w1<=(NumOfParams/2);w1++) 1461 for(w1=1;w1<=(NumOfParams/2);w1++)
1460 { 1462 {
1461 val1=functionMin(getParameter(parameters, (w1-1)*2, FALSE, function), getParameter(parameters, (w1-1)*2+1, TRUE, function)); 1463 val1=functionMin(getParameter(parameters, (w1-1)*2, FALSE, function), getParameter(parameters, (w1-1)*2+1, TRUE, function));
1462 val2=val1; 1464 val2=val1;
1463 }; 1465 };
1464 if(NumOfParams%2==1) 1466 if(NumOfParams%2==1)
1465 { 1467 {
1466 val1=calculateVariable(getParameter(parameters,NumOfParams-1,FALSE, function)).toDouble(&ok); 1468 val1=calculateVariable(getParameter(parameters,NumOfParams-1,FALSE, function)).toDouble(&ok);
1467 if(val1<val2) val2=val1; 1469 if(val1<val2) val2=val1;
1468 }; 1470 };
1469 return QString::number(val2); 1471 return QString::number(val2);
1470 }; 1472 };
1471 if (function=="MAX") 1473 if (function=="MAX")
1472 { 1474 {
1473 //NumOfParams 1475 //NumOfParams
1474 val2=0.0; 1476 val2=0.0;
1475 for(w1=1;w1<=(NumOfParams/2);w1++) 1477 for(w1=1;w1<=(NumOfParams/2);w1++)
1476 { 1478 {
1477 val1=functionMax(getParameter(parameters, (w1-1)*2, FALSE, function), getParameter(parameters, (w1-1)*2+1, TRUE, function)); 1479 val1=functionMax(getParameter(parameters, (w1-1)*2, FALSE, function), getParameter(parameters, (w1-1)*2+1, TRUE, function));
1478 val2=val1; 1480 val2=val1;
1479 }; 1481 };
1480 if(NumOfParams%2==1) 1482 if(NumOfParams%2==1)
1481 { 1483 {
1482 val1=calculateVariable(getParameter(parameters,NumOfParams-1,FALSE, function)).toDouble(&ok); 1484 val1=calculateVariable(getParameter(parameters,NumOfParams-1,FALSE, function)).toDouble(&ok);
1483 if(val1>val2) val2=val1; 1485 if(val1>val2) val2=val1;
1484 }; 1486 };
1485 return QString::number(val2); 1487 return QString::number(val2);
1486 }; 1488 };
1487 if (function=="AVERAGE") 1489 if (function=="AVERAGE")
1488 { 1490 {
1489 val1=functionAvg(getParameter(parameters, 0, TRUE, function), getParameter(parameters, 1, TRUE, function)); 1491 val1=functionAvg(getParameter(parameters, 0, TRUE, function), getParameter(parameters, 1, TRUE, function));
1490 return QString::number(val1); 1492 return QString::number(val1);
1491 }; 1493 };
1492 1494
1493 if(function=="BESSELI") 1495 if(function=="BESSELI")
1494 { 1496 {
1495 // BesselI (x,n) 1497 // BesselI (x,n)
1496 val1=calculateVariable(getParameter(parameters, 0, TRUE, function)).toDouble(&ok); 1498 val1=calculateVariable(getParameter(parameters, 0, TRUE, function)).toDouble(&ok);
1497 vali=calculateVariable(getParameter(parameters, 1, TRUE, function)).toInt(&ok); 1499 vali=calculateVariable(getParameter(parameters, 1, TRUE, function)).toInt(&ok);
1498 val2=BesselI(vali,val1); 1500 val2=BesselI(vali,val1);
1499 return QString::number(val2); 1501 return QString::number(val2);
1500 }; 1502 };
1501 if(function=="BESSELJ") 1503 if(function=="BESSELJ")
1502 { 1504 {
1503 // BesselJ (x,n) 1505 // BesselJ (x,n)
1504 val1=calculateVariable(getParameter(parameters, 0, TRUE, function)).toDouble(&ok); 1506 val1=calculateVariable(getParameter(parameters, 0, TRUE, function)).toDouble(&ok);
1505 vali=calculateVariable(getParameter(parameters, 1, TRUE, function)).toInt(&ok); 1507 vali=calculateVariable(getParameter(parameters, 1, TRUE, function)).toInt(&ok);
1506 val2=BesselJ(vali,val1); 1508 val2=BesselJ(vali,val1);
1507 return QString::number(val2); 1509 return QString::number(val2);
1508 }; 1510 };
1509 if(function=="BESSELK") 1511 if(function=="BESSELK")
1510 { 1512 {
1511 // BesselK (x,n) 1513 // BesselK (x,n)
1512 val1=calculateVariable(getParameter(parameters, 0, TRUE, function)).toDouble(&ok); 1514 val1=calculateVariable(getParameter(parameters, 0, TRUE, function)).toDouble(&ok);
1513 vali=calculateVariable(getParameter(parameters, 1, TRUE, function)).toInt(&ok); 1515 vali=calculateVariable(getParameter(parameters, 1, TRUE, function)).toInt(&ok);
1514 val2=BesselK(vali,val1); 1516 val2=BesselK(vali,val1);
1515 return QString::number(val2); 1517 return QString::number(val2);
1516 }; 1518 };
1517 if(function=="BESSELY") 1519 if(function=="BESSELY")
1518 { 1520 {
1519 // BesselY (x,n) 1521 // BesselY (x,n)
1520 val1=calculateVariable(getParameter(parameters, 0, TRUE, function)).toDouble(&ok); 1522 val1=calculateVariable(getParameter(parameters, 0, TRUE, function)).toDouble(&ok);
1521 vali=calculateVariable(getParameter(parameters, 1, TRUE, function)).toInt(&ok); 1523 vali=calculateVariable(getParameter(parameters, 1, TRUE, function)).toInt(&ok);
1522 val2=BesselY(vali,val1); 1524 val2=BesselY(vali,val1);
1523 return QString::number(val2); 1525 return QString::number(val2);
1524 }; 1526 };
1525 if(function=="GAMMALN") 1527 if(function=="GAMMALN")
1526 { 1528 {
1527 val1=calculateVariable(getParameter(parameters, 0, TRUE, function)).toDouble(&ok); 1529 val1=calculateVariable(getParameter(parameters, 0, TRUE, function)).toDouble(&ok);
1528 val2=GammaLn(val1); 1530 val2=GammaLn(val1);
1529 return QString::number(val2); 1531 return QString::number(val2);
1530 }; 1532 };
1531 if(function=="ERF") 1533 if(function=="ERF")
1532 { 1534 {
1533 // ERF (a,b) 1535 // ERF (a,b)
1534 val1=calculateVariable(getParameter(parameters, 0, TRUE, function)).toDouble(&ok); 1536 val1=calculateVariable(getParameter(parameters, 0, TRUE, function)).toDouble(&ok);
1535 val2=calculateVariable(getParameter(parameters, 1, TRUE, function)).toDouble(&ok); 1537 val2=calculateVariable(getParameter(parameters, 1, TRUE, function)).toDouble(&ok);
1536 return QString::number(ErrorFunction(val2)-ErrorFunction(val1)); 1538 return QString::number(ErrorFunction(val2)-ErrorFunction(val1));
1537 }; 1539 };
1538 if(function=="ERFC") 1540 if(function=="ERFC")
1539 { 1541 {
1540 // ERFC (a,b) 1542 // ERFC (a,b)
1541 val1=calculateVariable(getParameter(parameters, 0, TRUE, function)).toDouble(&ok); 1543 val1=calculateVariable(getParameter(parameters, 0, TRUE, function)).toDouble(&ok);
1542 val2=calculateVariable(getParameter(parameters, 1, TRUE, function)).toDouble(&ok); 1544 val2=calculateVariable(getParameter(parameters, 1, TRUE, function)).toDouble(&ok);
1543 return QString::number(ErrorFunctionComplementary(val2)-ErrorFunctionComplementary(val1)); 1545 return QString::number(ErrorFunctionComplementary(val2)-ErrorFunctionComplementary(val1));
1544 }; 1546 };
1545 if(function=="POISSON") 1547 if(function=="POISSON")
1546 { 1548 {
1547 // POISSON DISTR(x,n,distr/desnt) 1549 // POISSON DISTR(x,n,distr/desnt)
1548 val1=calculateVariable(getParameter(parameters, 0, TRUE, function)).toDouble(&ok); 1550 val1=calculateVariable(getParameter(parameters, 0, TRUE, function)).toDouble(&ok);
1549 val2=calculateVariable(getParameter(parameters, 1, TRUE, function)).toDouble(&ok); 1551 val2=calculateVariable(getParameter(parameters, 1, TRUE, function)).toDouble(&ok);
1550 vali=calculateVariable(getParameter(parameters, 2, TRUE, function)).toInt(&ok); 1552 vali=calculateVariable(getParameter(parameters, 2, TRUE, function)).toInt(&ok);
1551 if(vali==1) 1553 if(vali==1)
1552 { 1554 {
1553 return QString::number(GammaQ(floor(val1)+1, val2)); 1555 return QString::number(GammaQ(floor(val1)+1, val2));
1554 }else 1556 }else
1555 { 1557 {
1556 return QString::number(exp(-val2)*pow(val2,val1)/exp(GammaLn(val1+1.0))); 1558 return QString::number(exp(-val2)*pow(val2,val1)/exp(GammaLn(val1+1.0)));
1557 }; 1559 };
1558 }; 1560 };
1559 if(function=="CHIDIST") 1561 if(function=="CHIDIST")
1560 { 1562 {
1561 // POISSON CHIDIST(x,n,distr/density) 1563 // POISSON CHIDIST(x,n,distr/density)
1562 val1=calculateVariable(getParameter(parameters, 0, TRUE, function)).toDouble(&ok); 1564 val1=calculateVariable(getParameter(parameters, 0, TRUE, function)).toDouble(&ok);
1563 val2=calculateVariable(getParameter(parameters, 1, TRUE, function)).toDouble(&ok); 1565 val2=calculateVariable(getParameter(parameters, 1, TRUE, function)).toDouble(&ok);
1564 vali=calculateVariable(getParameter(parameters, 2, TRUE, function)).toInt(&ok); 1566 vali=calculateVariable(getParameter(parameters, 2, TRUE, function)).toInt(&ok);
1565 if(vali==1) 1567 if(vali==1)
1566 { 1568 {
1567 return QString::number(GammaP(val2/2.0,val1*val1/2.0)); 1569 return QString::number(GammaP(val2/2.0,val1*val1/2.0));
1568 } else 1570 } else
1569 { 1571 {
1570 return QString::number( 1572 return QString::number(
1571 pow(val1,val2-1.0)*exp(-val1*val1/2)/ ( pow(2,val2/2.0-1.0)*exp(GammaLn(val2/2.0))) 1573 pow(val1,val2-1.0)*exp(-val1*val1/2)/ ( pow(2,val2/2.0-1.0)*exp(GammaLn(val2/2.0)))
1572 ); 1574 );
1573 }; 1575 };
1574 }; 1576 };
1575 if(function=="CHI2DIST") 1577 if(function=="CHI2DIST")
1576 { 1578 {
1577 // POISSON CHISQUAREDIST(x,n,distr/density) 1579 // POISSON CHISQUAREDIST(x,n,distr/density)
1578 val1=calculateVariable(getParameter(parameters, 0, TRUE, function)).toDouble(&ok); 1580 val1=calculateVariable(getParameter(parameters, 0, TRUE, function)).toDouble(&ok);
1579 val2=calculateVariable(getParameter(parameters, 1, TRUE, function)).toDouble(&ok); 1581 val2=calculateVariable(getParameter(parameters, 1, TRUE, function)).toDouble(&ok);
1580 vali=calculateVariable(getParameter(parameters, 2, TRUE, function)).toInt(&ok); 1582 vali=calculateVariable(getParameter(parameters, 2, TRUE, function)).toInt(&ok);
1581 if(vali==1) 1583 if(vali==1)
1582 { 1584 {
1583 return QString::number(GammaP(val2/2.0,val1/2.0)); 1585 return QString::number(GammaP(val2/2.0,val1/2.0));
1584 } else 1586 } else
1585 { 1587 {
1586 return QString::number( 1588 return QString::number(
1587 pow(val1,val2/2.0-1.0)/(exp(val1/2.0)*pow(sqrt(2.0),val2)*exp(GammaLn(val2/2.0))) 1589 pow(val1,val2/2.0-1.0)/(exp(val1/2.0)*pow(sqrt(2.0),val2)*exp(GammaLn(val2/2.0)))
1588 ); 1590 );
1589 }; 1591 };
1590 }; 1592 };
1591 if(function=="BETAI") 1593 if(function=="BETAI")
1592 { 1594 {
1593 // BETA INCOMPLETE BETA(x,a,b) 1595 // BETA INCOMPLETE BETA(x,a,b)
1594 val1=calculateVariable(getParameter(parameters, 0, TRUE, function)).toDouble(&ok); 1596 val1=calculateVariable(getParameter(parameters, 0, TRUE, function)).toDouble(&ok);
1595 val2=calculateVariable(getParameter(parameters, 1, TRUE, function)).toDouble(&ok); 1597 val2=calculateVariable(getParameter(parameters, 1, TRUE, function)).toDouble(&ok);
1596 val3=calculateVariable(getParameter(parameters, 2, TRUE, function)).toDouble(&ok); 1598 val3=calculateVariable(getParameter(parameters, 2, TRUE, function)).toDouble(&ok);
1597 return QString::number(BetaIncomplete(val2,val3,val1)); 1599 return QString::number(BetaIncomplete(val2,val3,val1));
1598 }; 1600 };
1599 if(function=="GAMMAP") 1601 if(function=="GAMMAP")
1600 { 1602 {
1601 // GammaP (x,a) 1603 // GammaP (x,a)
1602 val1=calculateVariable(getParameter(parameters, 0, TRUE, function)).toDouble(&ok); 1604 val1=calculateVariable(getParameter(parameters, 0, TRUE, function)).toDouble(&ok);
1603 val2=calculateVariable(getParameter(parameters, 1, TRUE, function)).toDouble(&ok); 1605 val2=calculateVariable(getParameter(parameters, 1, TRUE, function)).toDouble(&ok);
1604 return QString::number(GammaP(val2,val1)); 1606 return QString::number(GammaP(val2,val1));
1605 }; 1607 };
1606 if(function=="GAMMAQ") 1608 if(function=="GAMMAQ")
1607 { 1609 {
1608 // GammaQ (x,a) 1610 // GammaQ (x,a)
1609 val1=calculateVariable(getParameter(parameters, 0, TRUE, function)).toDouble(&ok); 1611 val1=calculateVariable(getParameter(parameters, 0, TRUE, function)).toDouble(&ok);
1610 val2=calculateVariable(getParameter(parameters, 1, TRUE, function)).toDouble(&ok); 1612 val2=calculateVariable(getParameter(parameters, 1, TRUE, function)).toDouble(&ok);
1611 return QString::number(GammaQ(val2,val1)); 1613 return QString::number(GammaQ(val2,val1));
1612 }; 1614 };
1613 if (function=="VAR") 1615 if (function=="VAR")
1614 { 1616 {
1615 val1=functionVariance(getParameter(parameters, 0, TRUE, function), getParameter(parameters, 1, TRUE, function)); 1617 val1=functionVariance(getParameter(parameters, 0, TRUE, function), getParameter(parameters, 1, TRUE, function));
1616 return QString::number(val1); 1618 return QString::number(val1);
1617 }; 1619 };
1618 if (function=="VARP") 1620 if (function=="VARP")
1619 { 1621 {
1620 val1=functionVariancePopulation(getParameter(parameters, 0, TRUE, function), getParameter(parameters, 1, TRUE, function)); 1622 val1=functionVariancePopulation(getParameter(parameters, 0, TRUE, function), getParameter(parameters, 1, TRUE, function));
1621 return QString::number(val1); 1623 return QString::number(val1);
1622 }; 1624 };
1623 if (function=="STDEV") 1625 if (function=="STDEV")
1624 { 1626 {
1625 val1=functionVariance(getParameter(parameters, 0, TRUE, function), getParameter(parameters, 1, TRUE, function)); 1627 val1=functionVariance(getParameter(parameters, 0, TRUE, function), getParameter(parameters, 1, TRUE, function));
1626 if(val1<=0.0) return QString::number(0.0); 1628 if(val1<=0.0) return QString::number(0.0);
1627 return QString::number(sqrt(val1)); 1629 return QString::number(sqrt(val1));
1628 }; 1630 };
1629 if (function=="STDEVP") 1631 if (function=="STDEVP")
1630 { 1632 {
1631 val1=functionVariancePopulation(getParameter(parameters, 0, TRUE, function), getParameter(parameters, 1, TRUE, function)); 1633 val1=functionVariancePopulation(getParameter(parameters, 0, TRUE, function), getParameter(parameters, 1, TRUE, function));
1632 if(val1<=0.0) return QString::number(0.0); 1634 if(val1<=0.0) return QString::number(0.0);
1633 return QString::number(sqrt(val1)); 1635 return QString::number(sqrt(val1));
1634 }; 1636 };
1635 if (function=="SKEW") 1637 if (function=="SKEW")
1636 { 1638 {
1637 val1=functionSkew(getParameter(parameters, 0, TRUE, function), getParameter(parameters, 1, TRUE, function)); 1639 val1=functionSkew(getParameter(parameters, 0, TRUE, function), getParameter(parameters, 1, TRUE, function));
1638 return QString::number(val1); 1640 return QString::number(val1);
1639 }; 1641 };
1640 if (function=="KURT") 1642 if (function=="KURT")
1641 { 1643 {
1642 val1=functionKurt(getParameter(parameters, 0, TRUE, function), getParameter(parameters, 1, TRUE, function)); 1644 val1=functionKurt(getParameter(parameters, 0, TRUE, function), getParameter(parameters, 1, TRUE, function));
1643 return QString::number(val1); 1645 return QString::number(val1);
1644 }; 1646 };
1645 if(function=="GAMMADIST") 1647 if(function=="GAMMADIST")
1646 { 1648 {
1647 // GAMMADIST (x,alpha,beta,distribution?density1:0) 1649 // GAMMADIST (x,alpha,beta,distribution?density1:0)
1648 val1=calculateVariable(getParameter(parameters, 0, TRUE, function)).toDouble(&ok); 1650 val1=calculateVariable(getParameter(parameters, 0, TRUE, function)).toDouble(&ok);
1649 val2=calculateVariable(getParameter(parameters, 1, TRUE, function)).toDouble(&ok); 1651 val2=calculateVariable(getParameter(parameters, 1, TRUE, function)).toDouble(&ok);
1650 val3=calculateVariable(getParameter(parameters, 2, TRUE, function)).toDouble(&ok); 1652 val3=calculateVariable(getParameter(parameters, 2, TRUE, function)).toDouble(&ok);
1651 vali=calculateVariable(getParameter(parameters, 3, TRUE, function)).toInt(&ok); 1653 vali=calculateVariable(getParameter(parameters, 3, TRUE, function)).toInt(&ok);
1652 if(vali==1)//distribution 1654 if(vali==1)//distribution
1653 { 1655 {
1654 if(val3==0.0) return QString::number(0.0); 1656 if(val3==0.0) return QString::number(0.0);
1655 else 1657 else
1656 return QString::number(GammaP(val2,val1/val3)); 1658 return QString::number(GammaP(val2,val1/val3));
1657 }else //density 1659 }else //density
1658 { 1660 {
1659 return QString::number( 1661 return QString::number(
1660 pow(val1,val2-1.0)*exp(-val1/val3) / (pow(val3,val2)*exp(GammaLn(val2))) 1662 pow(val1,val2-1.0)*exp(-val1/val3) / (pow(val3,val2)*exp(GammaLn(val2)))
1661 ); 1663 );
1662 }; 1664 };
1663 }; 1665 };
1664 if(function=="BETADIST") 1666 if(function=="BETADIST")
1665 { 1667 {
1666 // BETADIST (z,alpha,beta,distribution?density1:0) 1668 // BETADIST (z,alpha,beta,distribution?density1:0)
1667 val1=calculateVariable(getParameter(parameters, 0, TRUE, function)).toDouble(&ok); 1669 val1=calculateVariable(getParameter(parameters, 0, TRUE, function)).toDouble(&ok);
1668 val2=calculateVariable(getParameter(parameters, 1, TRUE, function)).toDouble(&ok); 1670 val2=calculateVariable(getParameter(parameters, 1, TRUE, function)).toDouble(&ok);
1669 val3=calculateVariable(getParameter(parameters, 2, TRUE, function)).toDouble(&ok); 1671 val3=calculateVariable(getParameter(parameters, 2, TRUE, function)).toDouble(&ok);
1670 vali=calculateVariable(getParameter(parameters, 3, TRUE, function)).toInt(&ok); 1672 vali=calculateVariable(getParameter(parameters, 3, TRUE, function)).toInt(&ok);
1671 if(vali==1)//distribution 1673 if(vali==1)//distribution
1672 { 1674 {
1673 return QString::number(BetaIncomplete(val2,val3,val1)); 1675 return QString::number(BetaIncomplete(val2,val3,val1));
1674 }else //density 1676 }else //density
1675 { 1677 {
1676 return QString::number( 1678 return QString::number(
1677 pow(val1,val2-1.0)*pow(1.0-val1,val3-1.0) / Beta(val2,val3) 1679 pow(val1,val2-1.0)*pow(1.0-val1,val3-1.0) / Beta(val2,val3)
1678 ); 1680 );
1679 }; 1681 };
1680 }; 1682 };
1681 if(function=="FDIST") 1683 if(function=="FDIST")
1682 { 1684 {
1683 // FDIST (z,d1,d2,distribution?density1:0) 1685 // FDIST (z,d1,d2,distribution?density1:0)
1684 val1=calculateVariable(getParameter(parameters, 0, TRUE, function)).toDouble(&ok); 1686 val1=calculateVariable(getParameter(parameters, 0, TRUE, function)).toDouble(&ok);
1685 val2=calculateVariable(getParameter(parameters, 1, TRUE, function)).toDouble(&ok); 1687 val2=calculateVariable(getParameter(parameters, 1, TRUE, function)).toDouble(&ok);
1686 val3=calculateVariable(getParameter(parameters, 2, TRUE, function)).toDouble(&ok); 1688 val3=calculateVariable(getParameter(parameters, 2, TRUE, function)).toDouble(&ok);
1687 vali=calculateVariable(getParameter(parameters, 3, TRUE, function)).toInt(&ok); 1689 vali=calculateVariable(getParameter(parameters, 3, TRUE, function)).toInt(&ok);
1688 if(vali==1)//distribution 1690 if(vali==1)//distribution
1689 { 1691 {
1690 return QString::number( 1692 return QString::number(
1691 -BetaIncomplete(val3/2,val2/2,val3/(val3+val2*val1)) 1693 -BetaIncomplete(val3/2,val2/2,val3/(val3+val2*val1))
1692 +BetaIncomplete(val3/2,val2/2,1) 1694 +BetaIncomplete(val3/2,val2/2,1)
1693 ); 1695 );
1694 }else //density 1696 }else //density
1695 { 1697 {
1696 return QString::number( 1698 return QString::number(
1697 pow(val2,val2/2)*pow(val3,val3/2)*pow(val1,val2/2-1)/ 1699 pow(val2,val2/2)*pow(val3,val3/2)*pow(val1,val2/2-1)/
1698 (pow(val3+val2*val1,(val2+val3)/2)*Beta(val2/2,val3/2)) 1700 (pow(val3+val2*val1,(val2+val3)/2)*Beta(val2/2,val3/2))
1699 ); 1701 );
1700 }; 1702 };
1701 }; 1703 };
1702 if(function=="NORMALDIST") 1704 if(function=="NORMALDIST")
1703 { 1705 {
1704 // NORMALDIST (x,m,s,distribution?density1:0) 1706 // NORMALDIST (x,m,s,distribution?density1:0)
1705 val1=calculateVariable(getParameter(parameters, 0, TRUE, function)).toDouble(&ok); 1707 val1=calculateVariable(getParameter(parameters, 0, TRUE, function)).toDouble(&ok);
1706 val2=calculateVariable(getParameter(parameters, 1, TRUE, function)).toDouble(&ok); 1708 val2=calculateVariable(getParameter(parameters, 1, TRUE, function)).toDouble(&ok);
1707 val3=calculateVariable(getParameter(parameters, 2, TRUE, function)).toDouble(&ok); 1709 val3=calculateVariable(getParameter(parameters, 2, TRUE, function)).toDouble(&ok);
1708 vali=calculateVariable(getParameter(parameters, 3, TRUE, function)).toInt(&ok); 1710 vali=calculateVariable(getParameter(parameters, 3, TRUE, function)).toInt(&ok);
1709 if(vali==1)//distribution 1711 if(vali==1)//distribution
1710 { 1712 {
1711 return QString::number( 1713 return QString::number(
1712 (ErrorFunction((val1-val2)/(sqrt(2)*val3))+1)/2.0 1714 (ErrorFunction((val1-val2)/(sqrt(2)*val3))+1)/2.0
1713 ); 1715 );
1714 }else //density 1716 }else //density
1715 { 1717 {
1716 return QString::number( 1718 return QString::number(
1717 exp(-pow(((val1-val2)/val3),2)/2)/(val3*sqrt(2*M_PI)) 1719 exp(-pow(((val1-val2)/val3),2)/2)/(val3*sqrt(2*M_PI))
1718 ); 1720 );
1719 }; 1721 };
1720 }; 1722 };
1721 if(function=="PHI") 1723 if(function=="PHI")
1722 { 1724 {
1723 // NORMALDIST (x,distribution?density1:0) with mean=0 s=1.0 1725 // NORMALDIST (x,distribution?density1:0) with mean=0 s=1.0
1724 val1=calculateVariable(getParameter(parameters, 0, TRUE, function)).toDouble(&ok); 1726 val1=calculateVariable(getParameter(parameters, 0, TRUE, function)).toDouble(&ok);
1725 vali=calculateVariable(getParameter(parameters, 1, TRUE, function)).toInt(&ok); 1727 vali=calculateVariable(getParameter(parameters, 1, TRUE, function)).toInt(&ok);
1726 if(vali==1)//distribution 1728 if(vali==1)//distribution
1727 { 1729 {
1728 return QString::number( 1730 return QString::number(
1729 (ErrorFunction(val1/(sqrt(2)))+1)/2.0 1731 (ErrorFunction(val1/(sqrt(2)))+1)/2.0
1730 ); 1732 );
1731 }else //density 1733 }else //density
1732 { 1734 {
1733 return QString::number( 1735 return QString::number(
1734 exp(-pow(val1,2)/2)/(sqrt(2*M_PI)) 1736 exp(-pow(val1,2)/2)/(sqrt(2*M_PI))
1735 ); 1737 );
1736 }; 1738 };
1737 }; 1739 };
1738 /* 1740 /*
1739 StudentTDistribution/: PDF[StudentTDistribution[n_], x_] := 1741 StudentTDistribution/: PDF[StudentTDistribution[n_], x_] :=
1740 1/(Sqrt[n] Beta[n/2, 1/2]) Sqrt[n/(n+x^2)]^(n+1) /; 1742 1/(Sqrt[n] Beta[n/2, 1/2]) Sqrt[n/(n+x^2)]^(n+1) /;
1741 ParameterQ[StudentTDistribution[n]] 1743 ParameterQ[StudentTDistribution[n]]
1742 1744
1743 StudentTDistribution/: CDF[StudentTDistribution[n_], x_] := 1745 StudentTDistribution/: CDF[StudentTDistribution[n_], x_] :=
1744 (1 + Sign[x] BetaRegularized[n/(n+x^2), 1, n/2, 1/2])/2 /; 1746 (1 + Sign[x] BetaRegularized[n/(n+x^2), 1, n/2, 1/2])/2 /;
1745 ParameterQ[StudentTDistribution[n]] 1747 ParameterQ[StudentTDistribution[n]]
1746 */ 1748 */
1747 1749
1748 1750
1749 1751
1750 return 0; 1752 return 0;
1751}; 1753};
1752 1754
1753 1755
1754 1756
1755 1757
1756QString Sheet::dataParserHelper(const QString &data) 1758QString Sheet::dataParserHelper(const QString &data)
1757{ 1759{
1758 if(data.left(1)=="""" && data.right(1)=="""") return QString(data); 1760 if(data.left(1)=="""" && data.right(1)=="""") return QString(data);
1759 Expression exp1(data); 1761 Expression exp1(data);
1760 exp1.Parse(); 1762 exp1.Parse();
1761 QStack<QString> stack1; 1763 QStack<QString> stack1;
1762 stack1.setAutoDelete(TRUE); 1764 stack1.setAutoDelete(TRUE);
1763 int i=0; 1765 int i=0;
1764 QString* s1; 1766 QString* s1;
1765 QString* s2=NULL; 1767 QString* s2=NULL;
1766 int* i1; 1768 int* i1;
1767 int args,tokentype; 1769 int args,tokentype;
1768 QString tempval; 1770 QString tempval;
1769 s1=exp1.CompiledBody.first();i1=exp1.CompiledBodyType.first(); 1771 s1=exp1.CompiledBody.first();i1=exp1.CompiledBodyType.first();
1770 while(i<=(int)exp1.CompiledBody.count()-1) 1772 while(i<=(int)exp1.CompiledBody.count()-1)
1771 { 1773 {
1772 args= ((*i1) & 0xFF00)>>8; tokentype=(*i1) & 0x00FF; 1774 args= ((*i1) & 0xFF00)>>8; tokentype=(*i1) & 0x00FF;
1773 if(tokentype==NUMBER_TOKEN) 1775 if(tokentype==NUMBER_TOKEN)
1774 { 1776 {
1775 stack1.push(new QString(*s1)); 1777 stack1.push(new QString(*s1));
1776 //printf("Parse:Number=%s\r\n",s1->latin1()); 1778 //printf("Parse:Number=%s\r\n",s1->latin1());
1777 } 1779 }
1778 else if(tokentype==VARIABLE_TOKEN) 1780 else if(tokentype==VARIABLE_TOKEN)
1779 { 1781 {
1780 stack1.push(new QString(*s1)); 1782 stack1.push(new QString(QString(*s1).upper()));
1781 //printf("Parse:Var=%s\r\n",s1->latin1()); 1783 //printf("Parse:Var=%s\r\n",s1->latin1());
1782 //here to put implementation of other types of variables except cell. 1784 //here to put implementation of other types of variables except cell.
1783 //for example names 1785 //for example names
1784 } 1786 }
1785 else if(tokentype==STRING_TOKEN) 1787 else if(tokentype==STRING_TOKEN)
1786 { 1788 {
1787 stack1.push(new QString(*s1)); 1789 stack1.push(new QString(*s1));
1788 //printf("Parse:String=%s\r\n",s1->ascii()); 1790 //printf("Parse:String=%s\r\n",s1->ascii());
1789 } 1791 }
1790 else if(tokentype==FUNCTION_TOKEN) 1792 else if(tokentype==FUNCTION_TOKEN)
1791 { 1793 {
1792 QString params=""; 1794 QString params="";
1793 for(int w1=1;w1<=args;w1++) 1795 for(int w1=1;w1<=args;w1++)
1794 { 1796 {
1795 if((int)stack1.count()!=0) s2=stack1.pop(); 1797 if((int)stack1.count()!=0) s2=stack1.pop();
1796 params=*s2+params;//args in reverse order 1798 params=*s2+params;//args in reverse order
1797 params=","+params; 1799 params=","+params;
1798 }; 1800 };
1799 params=params.mid(1); 1801 params=params.mid(1);
1800 if(params==NULL) params="0"; 1802 if(params==NULL) params="0";
1801 //printf("Parse:Func=%s, params=%s, stackcount=%d,args=%d\r\n" 1803 //printf("Parse:Func=%s, params=%s, stackcount=%d,args=%d\r\n"
1802 //,s1->latin1(),params.latin1(),stack1.count(),args); 1804 //,s1->latin1(),params.latin1(),stack1.count(),args);
1803 tempval=calculateFunction(*s1,params,args); 1805 tempval=calculateFunction(*s1,params,args);
1804 tempval=tempval.upper(); 1806 tempval=tempval;
1805 stack1.push(new QString(tempval)); 1807 stack1.push(new QString(tempval));
1806 }; 1808 };
1807 1809
1808 //loops to next token 1810 //loops to next token
1809 if(exp1.CompiledBody.next()!=NULL) s1=exp1.CompiledBody.current(); else break; 1811 if(exp1.CompiledBody.next()!=NULL) s1=exp1.CompiledBody.current(); else break;
1810 if(exp1.CompiledBodyType.next()!=NULL) i1=exp1.CompiledBodyType.current(); else break; 1812 if(exp1.CompiledBodyType.next()!=NULL) i1=exp1.CompiledBodyType.current(); else break;
1811 i++; 1813 i++;
1812 }; 1814 };
1813 if((int)stack1.count()!=0)s2=stack1.pop(); else s2=new QString("!ERROR"); 1815 if((int)stack1.count()!=0)s2=stack1.pop(); else s2=new QString("!ERROR");
1814 tempval=*s2; 1816 tempval=*s2;
1815 return(tempval); 1817 return(tempval);
1816}; 1818};
1817 1819
1818 1820
1819 1821
1820QString Sheet::dataParser(const QString &cell, const QString &data) 1822QString Sheet::dataParser(const QString &cell, const QString &data)
1821{ 1823{
1822 QString strippedData(data); 1824 QString strippedData(data);
1823 strippedData.replace(QRegExp("\\s"), ""); 1825 strippedData.replace(QRegExp("\\s"), "");
1824 if (strippedData.isEmpty() || strippedData[0]!='=') return data; 1826 if (strippedData.isEmpty() || strippedData[0]!='=') return data;
1825 if (listDataParser.find(cell)!=listDataParser.end()) return "0"; 1827 if (listDataParser.find(cell)!=listDataParser.end()) return "0";
1826 listDataParser.append(cell); 1828 listDataParser.append(cell);
1827 // printf("DATAPARSER: data=%s, cell=%s\r\n",data.ascii(),cell.ascii()); 1829 // printf("DATAPARSER: data=%s, cell=%s\r\n",data.ascii(),cell.ascii());
1828 strippedData=dataParserHelper(strippedData.remove(0, 1).upper().replace(QRegExp(":"), ",")); 1830 strippedData=dataParserHelper(strippedData.remove(0, 1).replace(QRegExp(":"), ","));
1829 1831
1830 int i=0; 1832 int i=0;
1831 QString tempParameter(getParameter(strippedData, i)), result=""; 1833 QString tempParameter(getParameter(strippedData, i)), result="";
1832 do 1834 do
1833 { 1835 {
1834 result+=","+calculateVariable(tempParameter); 1836 result+=","+calculateVariable(tempParameter);
1835 tempParameter=getParameter(strippedData, ++i); 1837 tempParameter=getParameter(strippedData, ++i);
1836 } 1838 }
1837 while (!tempParameter.isNull()); 1839 while (!tempParameter.isNull());
1838 listDataParser.remove(cell); 1840 listDataParser.remove(cell);
1839 return result.mid(1); 1841 return result.mid(1);
1840} 1842}
1841 1843
1842 1844
1843void Sheet::setData(const QString &data) 1845void Sheet::setData(const QString &data)
1844{ 1846{
1845 setText(currentRow(), currentColumn(), data); 1847 setText(currentRow(), currentColumn(), data);
1846 slotCellChanged(currentRow(), currentColumn()); 1848 slotCellChanged(currentRow(), currentColumn());
1847 activateNextCell(); 1849 activateNextCell();
1848} 1850}
1849 1851
1850QString Sheet::getData() 1852QString Sheet::getData()
1851{ 1853{
1852 typeCellData *cellData=findCellData(currentRow(), currentColumn()); 1854 typeCellData *cellData=findCellData(currentRow(), currentColumn());
1853 if (cellData) 1855 if (cellData)
1854 return cellData->data; 1856 return cellData->data;
1855 return ""; 1857 return "";
1856} 1858}
1857 1859
1858void Sheet::lockClicks(bool lock) 1860void Sheet::lockClicks(bool lock)
1859{ 1861{
1860 clicksLocked=lock; 1862 clicksLocked=lock;
1861} 1863}
1862 1864
1863void Sheet::paintCell(QPainter *p, int row, int col, const QRect & cr, bool selected) 1865void Sheet::paintCell(QPainter *p, int row, int col, const QRect & cr, bool selected)
1864{ 1866{
1865 if (selected && row==currentRow() && col==currentColumn()) selected=FALSE; 1867 if (selected && row==currentRow() && col==currentColumn()) selected=FALSE;
1866 1868
1867 int sheetDataCurrent=sheetData.at(); 1869 int sheetDataCurrent=sheetData.at();
1868 typeCellData *cellData=findCellData(row, col); 1870 typeCellData *cellData=findCellData(row, col);
1869 if (sheetDataCurrent>=0) sheetData.at(sheetDataCurrent); 1871 if (sheetDataCurrent>=0) sheetData.at(sheetDataCurrent);
1870 if (!cellData) cellData=&defaultCellData; 1872 if (!cellData) cellData=&defaultCellData;
1871 if (selected) 1873 if (selected)
1872 p->fillRect(0, 0, cr.width(), cr.height(), colorGroup().highlight()); 1874 p->fillRect(0, 0, cr.width(), cr.height(), colorGroup().highlight());
1873 else 1875 else
1874 { 1876 {
1875 p->fillRect(0, 0, cr.width(), cr.height(), colorGroup().base()); 1877 p->fillRect(0, 0, cr.width(), cr.height(), colorGroup().base());
1876 p->fillRect(0, 0, cr.width(), cr.height(), cellData->background); 1878 p->fillRect(0, 0, cr.width(), cr.height(), cellData->background);
1877 } 1879 }
1878 1880
1879 QTableItem *cellItem=item(row, col); 1881 QTableItem *cellItem=item(row, col);
1880 if (cellItem) 1882 if (cellItem)
1881 { 1883 {
1882 p->setPen(selected ? colorGroup().highlightedText() : cellData->fontColor); 1884 p->setPen(selected ? colorGroup().highlightedText() : cellData->fontColor);
1883 p->setFont(cellData->font); 1885 p->setFont(cellData->font);
1884 QString str=cellItem->text(); 1886 QString str=cellItem->text();
1885 p->drawText(2, 2, cr.width()-4, cr.height()-4, cellData->alignment, cellItem->text()); 1887 p->drawText(2, 2, cr.width()-4, cr.height()-4, cellData->alignment, cellItem->text());
1886 } 1888 }
1887 1889
1888 int rx=cr.width()-1, ry=cr.height()-1; 1890 int rx=cr.width()-1, ry=cr.height()-1;
1889 QPen pen(p->pen()); 1891 QPen pen(p->pen());
1890 p->setPen(cellData->borders.right); 1892 p->setPen(cellData->borders.right);
1891 p->drawLine(rx, 0, rx, ry); 1893 p->drawLine(rx, 0, rx, ry);
1892 p->setPen(cellData->borders.bottom); 1894 p->setPen(cellData->borders.bottom);
1893 p->drawLine(0, ry, rx, ry); 1895 p->drawLine(0, ry, rx, ry);
1894 p->setPen(pen); 1896 p->setPen(pen);
1895} 1897}
1896 1898
1897void Sheet::viewportMousePressEvent(QMouseEvent *e) 1899void Sheet::viewportMousePressEvent(QMouseEvent *e)
1898{ 1900{
1899 QMouseEvent ce(e->type(), viewportToContents(e->pos()), e->globalPos(), e->button(), e->state()); 1901 QMouseEvent ce(e->type(), viewportToContents(e->pos()), e->globalPos(), e->button(), e->state());
1900 if (clicksLocked) 1902 if (clicksLocked)
1901 { 1903 {
1902 if (selectionNo<0) 1904 if (selectionNo<0)
1903 { 1905 {
1904 clearSelection(); 1906 clearSelection();
1905 QTableSelection newSelection; 1907 QTableSelection newSelection;
1906 newSelection.init(rowAt(ce.pos().y()), columnAt(ce.pos().x())); 1908 newSelection.init(rowAt(ce.pos().y()), columnAt(ce.pos().x()));
1907 newSelection.expandTo(newSelection.anchorRow(), newSelection.anchorCol()); 1909 newSelection.expandTo(newSelection.anchorRow(), newSelection.anchorCol());
1908 selectionNo=addSelection(newSelection); 1910 selectionNo=addSelection(newSelection);
1909 } 1911 }
1910 } 1912 }
1911 else 1913 else
1912 QTable::contentsMousePressEvent(&ce); 1914 QTable::contentsMousePressEvent(&ce);
1913} 1915}
1914 1916
1915void Sheet::viewportMouseMoveEvent(QMouseEvent *e) 1917void Sheet::viewportMouseMoveEvent(QMouseEvent *e)
1916{ 1918{
1917 QMouseEvent ce(e->type(), viewportToContents(e->pos()), e->globalPos(), e->button(), e->state()); 1919 QMouseEvent ce(e->type(), viewportToContents(e->pos()), e->globalPos(), e->button(), e->state());
1918 if (clicksLocked) 1920 if (clicksLocked)
1919 { 1921 {
1920 if (selectionNo>=0) 1922 if (selectionNo>=0)
1921 { 1923 {
1922 QTableSelection oldSelection(selection(selectionNo)); 1924 QTableSelection oldSelection(selection(selectionNo));
1923 oldSelection.expandTo(rowAt(ce.pos().y()), columnAt(ce.pos().x())); 1925 oldSelection.expandTo(rowAt(ce.pos().y()), columnAt(ce.pos().x()));
1924 if (!(oldSelection==selection(selectionNo))) 1926 if (!(oldSelection==selection(selectionNo)))
1925 { 1927 {
1926 removeSelection(selectionNo); 1928 removeSelection(selectionNo);
1927 selectionNo=addSelection(oldSelection); 1929 selectionNo=addSelection(oldSelection);
1928 } 1930 }
1929 } 1931 }
1930 } 1932 }
1931 else 1933 else
1932 QTable::contentsMouseMoveEvent(&ce); 1934 QTable::contentsMouseMoveEvent(&ce);
1933} 1935}
1934 1936
1935void Sheet::viewportMouseReleaseEvent(QMouseEvent *e) 1937void Sheet::viewportMouseReleaseEvent(QMouseEvent *e)
1936{ 1938{
1937 QMouseEvent ce(e->type(), viewportToContents(e->pos()), e->globalPos(), e->button(), e->state()); 1939 QMouseEvent ce(e->type(), viewportToContents(e->pos()), e->globalPos(), e->button(), e->state());
1938 if (clicksLocked && selectionNo>=0) 1940 if (clicksLocked && selectionNo>=0)
1939 { 1941 {
1940 QTableSelection oldSelection(selection(selectionNo)); 1942 QTableSelection oldSelection(selection(selectionNo));
1941 oldSelection.expandTo(rowAt(ce.pos().y()), columnAt(ce.pos().x())); 1943 oldSelection.expandTo(rowAt(ce.pos().y()), columnAt(ce.pos().x()));
1942 removeSelection(selectionNo); 1944 removeSelection(selectionNo);
1943 selectionNo=-1; 1945 selectionNo=-1;
1944 if (oldSelection.topRow()==oldSelection.bottomRow() && oldSelection.leftCol()==oldSelection.rightCol()) 1946 if (oldSelection.topRow()==oldSelection.bottomRow() && oldSelection.leftCol()==oldSelection.rightCol())
1945 emit cellClicked(findCellName(oldSelection.topRow(), oldSelection.leftCol())); 1947 emit cellClicked(findCellName(oldSelection.topRow(), oldSelection.leftCol()));
1946 else 1948 else
1947 emit cellClicked(findCellName(oldSelection.topRow(), oldSelection.leftCol())+','+findCellName(oldSelection.bottomRow(), oldSelection.rightCol())); 1949 emit cellClicked(findCellName(oldSelection.topRow(), oldSelection.leftCol())+','+findCellName(oldSelection.bottomRow(), oldSelection.rightCol()));
1948 } 1950 }
1949 else 1951 else
1950 QTable::contentsMouseReleaseEvent(&ce); 1952 QTable::contentsMouseReleaseEvent(&ce);
1951} 1953}
1952 1954
1953QString Sheet::findCellName(int row, int col) 1955QString Sheet::findCellName(int row, int col)
1954{ 1956{
1955 return (getHeaderString(col+1)+QString::number(row+1)); 1957 return (getHeaderString(col+1)+QString::number(row+1));
1956} 1958}
1957 1959
1958void Sheet::copySheetData(QList<typeCellData> *destSheetData) 1960void Sheet::copySheetData(QList<typeCellData> *destSheetData)
1959{ 1961{
1960 typeCellData *tempCellData, *newCellData; 1962 typeCellData *tempCellData, *newCellData;
1961 destSheetData->clear(); 1963 destSheetData->clear();
1962 1964
1963 for (tempCellData=sheetData.first(); tempCellData; tempCellData=sheetData.next()) 1965 for (tempCellData=sheetData.first(); tempCellData; tempCellData=sheetData.next())
1964 { 1966 {
1965 newCellData=new typeCellData; 1967 newCellData=new typeCellData;
1966 *newCellData=*tempCellData; 1968 *newCellData=*tempCellData;
1967 destSheetData->append(newCellData); 1969 destSheetData->append(newCellData);
1968 } 1970 }
1969} 1971}
1970 1972
1971void Sheet::setSheetData(QList<typeCellData> *srcSheetData) 1973void Sheet::setSheetData(QList<typeCellData> *srcSheetData)
1972{ 1974{
1973 typeCellData *tempCellData, *newCellData; 1975 typeCellData *tempCellData, *newCellData;
1974 1976
1975 for (tempCellData=sheetData.first(); tempCellData; tempCellData=sheetData.next()) 1977 for (tempCellData=sheetData.first(); tempCellData; tempCellData=sheetData.next())
1976 { 1978 {
1977 clearCell(tempCellData->row, tempCellData->col); 1979 clearCell(tempCellData->row, tempCellData->col);
1978 updateCell(tempCellData->row, tempCellData->col); 1980 updateCell(tempCellData->row, tempCellData->col);
1979 } 1981 }
1980 sheetData.clear(); 1982 sheetData.clear();
1981 1983
1982 for (tempCellData=srcSheetData->first(); tempCellData; tempCellData=srcSheetData->next()) 1984 for (tempCellData=srcSheetData->first(); tempCellData; tempCellData=srcSheetData->next())
1983 { 1985 {
1984 newCellData=new typeCellData; 1986 newCellData=new typeCellData;
1985 *newCellData=*tempCellData; 1987 *newCellData=*tempCellData;
1986 sheetData.append(newCellData); 1988 sheetData.append(newCellData);
1987 setText(newCellData->row, newCellData->col, dataParser(findCellName(newCellData->row, newCellData->col), newCellData->data)); 1989 setText(newCellData->row, newCellData->col, dataParser(findCellName(newCellData->row, newCellData->col), newCellData->data));
1988 } 1990 }
1989 emit sheetModified(); 1991 emit sheetModified();
1990} 1992}
1991 1993
1992void Sheet::setName(const QString &name) 1994void Sheet::setName(const QString &name)
1993{ 1995{
1994 sheetName=name; 1996 sheetName=name;
1995 emit sheetModified(); 1997 emit sheetModified();
1996} 1998}
1997 1999
1998QString Sheet::getName() 2000QString Sheet::getName()
1999{ 2001{
2000 return sheetName; 2002 return sheetName;
2001} 2003}
2002 2004
2003void Sheet::setBrush(int row, int col, const QBrush &brush) 2005void Sheet::setBrush(int row, int col, const QBrush &brush)
2004{ 2006{
2005 typeCellData *cellData=findCellData(row, col); 2007 typeCellData *cellData=findCellData(row, col);
2006 if (!cellData) cellData=createCellData(row, col); 2008 if (!cellData) cellData=createCellData(row, col);
2007 if (cellData) 2009 if (cellData)
2008 { 2010 {
2009 cellData->background=brush; 2011 cellData->background=brush;
2010 emit sheetModified(); 2012 emit sheetModified();
2011 } 2013 }
2012} 2014}
2013 2015
2014QBrush Sheet::getBrush(int row, int col) 2016QBrush Sheet::getBrush(int row, int col)
2015{ 2017{
2016 typeCellData *cellData=findCellData(row, col); 2018 typeCellData *cellData=findCellData(row, col);
2017 if (!cellData) cellData=&defaultCellData; 2019 if (!cellData) cellData=&defaultCellData;
2018 return cellData->background; 2020 return cellData->background;
2019} 2021}
2020 2022
2021void Sheet::setTextAlign(int row, int col, Qt::AlignmentFlags flags) 2023void Sheet::setTextAlign(int row, int col, Qt::AlignmentFlags flags)
2022{ 2024{
2023 typeCellData *cellData=findCellData(row, col); 2025 typeCellData *cellData=findCellData(row, col);
2024 if (!cellData) cellData=createCellData(row, col); 2026 if (!cellData) cellData=createCellData(row, col);
2025 if (cellData) 2027 if (cellData)
2026 { 2028 {
2027 cellData->alignment=flags; 2029 cellData->alignment=flags;
2028 emit sheetModified(); 2030 emit sheetModified();
2029 } 2031 }
2030} 2032}
2031 2033
2032Qt::AlignmentFlags Sheet::getAlignment(int row, int col) 2034Qt::AlignmentFlags Sheet::getAlignment(int row, int col)
2033{ 2035{
2034 typeCellData *cellData=findCellData(row, col); 2036 typeCellData *cellData=findCellData(row, col);
2035 if (!cellData) cellData=&defaultCellData; 2037 if (!cellData) cellData=&defaultCellData;
2036 return cellData->alignment; 2038 return cellData->alignment;
2037} 2039}
2038 2040
2039void Sheet::setTextFont(int row, int col, const QFont &font, const QColor &color) 2041void Sheet::setTextFont(int row, int col, const QFont &font, const QColor &color)
2040{ 2042{
2041 typeCellData *cellData=findCellData(row, col); 2043 typeCellData *cellData=findCellData(row, col);
2042 if (!cellData) cellData=createCellData(row, col); 2044 if (!cellData) cellData=createCellData(row, col);
2043 if (cellData) 2045 if (cellData)
2044 { 2046 {
2045 cellData->font=font; 2047 cellData->font=font;
2046 cellData->fontColor=color; 2048 cellData->fontColor=color;
2047 emit sheetModified(); 2049 emit sheetModified();
2048 } 2050 }
2049} 2051}
2050 2052
2051QFont Sheet::getFont(int row, int col) 2053QFont Sheet::getFont(int row, int col)
2052{ 2054{
2053 typeCellData *cellData=findCellData(row, col); 2055 typeCellData *cellData=findCellData(row, col);
2054 if (!cellData) cellData=&defaultCellData; 2056 if (!cellData) cellData=&defaultCellData;
2055 return cellData->font; 2057 return cellData->font;
2056} 2058}
2057 2059
2058QColor Sheet::getFontColor(int row, int col) 2060QColor Sheet::getFontColor(int row, int col)
2059{ 2061{
2060 typeCellData *cellData=findCellData(row, col); 2062 typeCellData *cellData=findCellData(row, col);
2061 if (!cellData) cellData=&defaultCellData; 2063 if (!cellData) cellData=&defaultCellData;
2062 return cellData->fontColor; 2064 return cellData->fontColor;
2063} 2065}
2064 2066
2065void Sheet::setPen(int row, int col, int vertical, const QPen &pen) 2067void Sheet::setPen(int row, int col, int vertical, const QPen &pen)
2066{ 2068{
2067 typeCellData *cellData=findCellData(row, col); 2069 typeCellData *cellData=findCellData(row, col);
2068 if (!cellData) cellData=createCellData(row, col); 2070 if (!cellData) cellData=createCellData(row, col);
2069 if (cellData) 2071 if (cellData)
2070 { 2072 {
2071 if (vertical) 2073 if (vertical)
2072 cellData->borders.right=pen; 2074 cellData->borders.right=pen;
2073 else 2075 else
2074 cellData->borders.bottom=pen; 2076 cellData->borders.bottom=pen;
2075 emit sheetModified(); 2077 emit sheetModified();
2076 } 2078 }
2077} 2079}
2078 2080
2079QPen Sheet::getPen(int row, int col, int vertical) 2081QPen Sheet::getPen(int row, int col, int vertical)
2080{ 2082{
2081 typeCellData *cellData=findCellData(row, col); 2083 typeCellData *cellData=findCellData(row, col);
2082 if (!cellData) cellData=&defaultCellData; 2084 if (!cellData) cellData=&defaultCellData;
2083 return (vertical ? cellData->borders.right : cellData->borders.bottom); 2085 return (vertical ? cellData->borders.right : cellData->borders.bottom);
2084} 2086}
2085 2087
2086void Sheet::getSelection(int *row1, int *col1, int *row2, int *col2) 2088void Sheet::getSelection(int *row1, int *col1, int *row2, int *col2)
2087{ 2089{
2088 int selectionNo=currentSelection(); 2090 int selectionNo=currentSelection();
2089 if (selectionNo>=0) 2091 if (selectionNo>=0)
2090 { 2092 {
2091 QTableSelection selection(selection(selectionNo)); 2093 QTableSelection selection(selection(selectionNo));
2092 *row1=selection.topRow(); 2094 *row1=selection.topRow();
2093 *row2=selection.bottomRow(); 2095 *row2=selection.bottomRow();
2094 *col1=selection.leftCol(); 2096 *col1=selection.leftCol();
2095 *col2=selection.rightCol(); 2097 *col2=selection.rightCol();
2096 } 2098 }
2097 else 2099 else
2098 { 2100 {
2099 *row1=*row2=currentRow(); 2101 *row1=*row2=currentRow();
2100 *col1=*col2=currentColumn(); 2102 *col1=*col2=currentColumn();
2101 } 2103 }
2102} 2104}
2103 2105
2104void Sheet::editClear() 2106void Sheet::editClear()
2105{ 2107{
2106 int row1, row2, col1, col2; 2108 int row1, row2, col1, col2;
2107 getSelection(&row1, &col1, &row2, &col2); 2109 getSelection(&row1, &col1, &row2, &col2);
2108 2110
2109 int row, col; 2111 int row, col;
2110 for (row=row1; row<=row2; ++row) 2112 for (row=row1; row<=row2; ++row)
2111 for (col=col1; col<=col2; ++col) 2113 for (col=col1; col<=col2; ++col)
2112 { 2114 {
2113 setText(row, col, ""); 2115 setText(row, col, "");
2114 slotCellChanged(row, col); 2116 slotCellChanged(row, col);
2115 } 2117 }
2116} 2118}
2117 2119
2118void Sheet::editCopy() 2120void Sheet::editCopy()
2119{ 2121{
2120 clipboardData.clear(); 2122 clipboardData.clear();
2121 2123
2122 int row1, row2, col1, col2; 2124 int row1, row2, col1, col2;
2123 getSelection(&row1, &col1, &row2, &col2); 2125 getSelection(&row1, &col1, &row2, &col2);
2124 2126
2125 typeCellData *cellData, *newCellData; 2127 typeCellData *cellData, *newCellData;
2126 int row, col; 2128 int row, col;
2127 for (row=row1; row<=row2; ++row) 2129 for (row=row1; row<=row2; ++row)
2128 for (col=col1; col<=col2; ++col) 2130 for (col=col1; col<=col2; ++col)
2129 { 2131 {
2130 cellData=findCellData(row, col); 2132 cellData=findCellData(row, col);
2131 if (cellData) 2133 if (cellData)
2132 { 2134 {
2133 newCellData=new typeCellData; 2135 newCellData=new typeCellData;
2134 *newCellData=*cellData; 2136 *newCellData=*cellData;
2135 newCellData->row-=row1; 2137 newCellData->row-=row1;
2136 newCellData->col-=col1; 2138 newCellData->col-=col1;
2137 clipboardData.append(newCellData); 2139 clipboardData.append(newCellData);
2138 } 2140 }
2139 } 2141 }
2140} 2142}
2141 2143
2142void Sheet::editCut() 2144void Sheet::editCut()
2143{ 2145{
2144 editCopy(); 2146 editCopy();
2145 editClear(); 2147 editClear();
2146} 2148}
2147 2149
2148void Sheet::editPaste(bool onlyContents) 2150void Sheet::editPaste(bool onlyContents)
2149{ 2151{
2150 int row1=currentRow(), col1=currentColumn(); 2152 int row1=currentRow(), col1=currentColumn();
2151 typeCellData *cellData, *tempCellData; 2153 typeCellData *cellData, *tempCellData;
2152 2154
2153 for (tempCellData=clipboardData.first(); tempCellData; tempCellData=clipboardData.next()) 2155 for (tempCellData=clipboardData.first(); tempCellData; tempCellData=clipboardData.next())
2154 { 2156 {
2155 cellData=findCellData(tempCellData->row+row1, tempCellData->col+col1); 2157 cellData=findCellData(tempCellData->row+row1, tempCellData->col+col1);
2156 if (!cellData) cellData=createCellData(tempCellData->row+row1, tempCellData->col+col1); 2158 if (!cellData) cellData=createCellData(tempCellData->row+row1, tempCellData->col+col1);
2157 if (cellData) 2159 if (cellData)
2158 { 2160 {
2159 if (onlyContents) 2161 if (onlyContents)
2160 cellData->data=tempCellData->data; 2162 cellData->data=tempCellData->data;
2161 else 2163 else
2162 { 2164 {
2163 *cellData=*tempCellData; 2165 *cellData=*tempCellData;
2164 cellData->row+=row1; 2166 cellData->row+=row1;
2165 cellData->col+=col1; 2167 cellData->col+=col1;
2166 } 2168 }
2167 setText(cellData->row, cellData->col, dataParser(findCellName(cellData->row, cellData->col), cellData->data)); 2169 setText(cellData->row, cellData->col, dataParser(findCellName(cellData->row, cellData->col), cellData->data));
2168 emit sheetModified(); 2170 emit sheetModified();
2169 } 2171 }
2170 } 2172 }
2171} 2173}
2172 2174
2173void Sheet::insertRows(int no, bool allColumns) 2175void Sheet::insertRows(int no, bool allColumns)
2174{ 2176{
2175 setNumRows(numRows()+no); 2177 setNumRows(numRows()+no);
2176 2178
2177 typeCellData *tempCellData; 2179 typeCellData *tempCellData;
2178 int row=currentRow(), col=currentColumn(); 2180 int row=currentRow(), col=currentColumn();
2179 2181
2180 for (tempCellData=sheetData.first(); tempCellData; tempCellData=sheetData.next()) 2182 for (tempCellData=sheetData.first(); tempCellData; tempCellData=sheetData.next())
2181 if (tempCellData->row>=row && (allColumns || tempCellData->col==col)) 2183 if (tempCellData->row>=row && (allColumns || tempCellData->col==col))
2182 { 2184 {
2183 clearCell(tempCellData->row, tempCellData->col); 2185 clearCell(tempCellData->row, tempCellData->col);
2184 tempCellData->row+=no; 2186 tempCellData->row+=no;
2185 } 2187 }
2186 for (tempCellData=sheetData.first(); tempCellData; tempCellData=sheetData.next()) 2188 for (tempCellData=sheetData.first(); tempCellData; tempCellData=sheetData.next())
2187 if (tempCellData->row>=row && (allColumns || tempCellData->col==col)) 2189 if (tempCellData->row>=row && (allColumns || tempCellData->col==col))
2188 { 2190 {
2189 updateCell(tempCellData->row-no, tempCellData->col); 2191 updateCell(tempCellData->row-no, tempCellData->col);
2190 setText(tempCellData->row, tempCellData->col, dataParser(findCellName(tempCellData->row, tempCellData->col), tempCellData->data)); 2192 setText(tempCellData->row, tempCellData->col, dataParser(findCellName(tempCellData->row, tempCellData->col), tempCellData->data));
2191 } 2193 }
2192 emit sheetModified(); 2194 emit sheetModified();
2193} 2195}
2194 2196
2195void Sheet::insertColumns(int no, bool allRows) 2197void Sheet::insertColumns(int no, bool allRows)
2196{ 2198{
2197 int noCols=numCols(); 2199 int noCols=numCols();
2198 int newCols=noCols+no; 2200 int newCols=noCols+no;
2199 setNumCols(newCols); 2201 setNumCols(newCols);
2200 for (int i=noCols; i<newCols; ++i) 2202 for (int i=noCols; i<newCols; ++i)
2201 horizontalHeader()->setLabel(i, getHeaderString(i+1), DEFAULT_COL_WIDTH); 2203 horizontalHeader()->setLabel(i, getHeaderString(i+1), DEFAULT_COL_WIDTH);
2202 2204
2203 typeCellData *tempCellData; 2205 typeCellData *tempCellData;
2204 int col=currentColumn(), row=currentRow(); 2206 int col=currentColumn(), row=currentRow();
2205 2207
2206 for (tempCellData=sheetData.first(); tempCellData; tempCellData=sheetData.next()) 2208 for (tempCellData=sheetData.first(); tempCellData; tempCellData=sheetData.next())
2207 if (tempCellData->col>=col && (allRows || tempCellData->row==row)) 2209 if (tempCellData->col>=col && (allRows || tempCellData->row==row))
2208 { 2210 {
2209 clearCell(tempCellData->row, tempCellData->col); 2211 clearCell(tempCellData->row, tempCellData->col);
2210 tempCellData->col+=no; 2212 tempCellData->col+=no;
2211 } 2213 }
2212 for (tempCellData=sheetData.first(); tempCellData; tempCellData=sheetData.next()) 2214 for (tempCellData=sheetData.first(); tempCellData; tempCellData=sheetData.next())
2213 if (tempCellData->col>=col && (allRows || tempCellData->row==row)) 2215 if (tempCellData->col>=col && (allRows || tempCellData->row==row))
2214 { 2216 {
2215 updateCell(tempCellData->row, tempCellData->col-no); 2217 updateCell(tempCellData->row, tempCellData->col-no);
2216 setText(tempCellData->row, tempCellData->col, dataParser(findCellName(tempCellData->row, tempCellData->col), tempCellData->data)); 2218 setText(tempCellData->row, tempCellData->col, dataParser(findCellName(tempCellData->row, tempCellData->col), tempCellData->data));
2217 } 2219 }
2218 emit sheetModified(); 2220 emit sheetModified();
2219} 2221}
2220 2222
2221void Sheet::dataFindReplace(const QString &findStr, const QString &replaceStr, bool matchCase, bool allCells, bool entireCell, bool replace, bool replaceAll) 2223void Sheet::dataFindReplace(const QString &findStr, const QString &replaceStr, bool matchCase, bool allCells, bool entireCell, bool replace, bool replaceAll)
2222{ 2224{
2223 typeCellData *tempCellData; 2225 typeCellData *tempCellData;
2224 int row1, col1, row2, col2; 2226 int row1, col1, row2, col2;
2225 getSelection(&row1, &col1, &row2, &col2); 2227 getSelection(&row1, &col1, &row2, &col2);
2226 bool found=FALSE; 2228 bool found=FALSE;
2227 2229
2228 for (tempCellData=sheetData.first(); tempCellData; tempCellData=sheetData.next()) 2230 for (tempCellData=sheetData.first(); tempCellData; tempCellData=sheetData.next())
2229 if (allCells || (tempCellData->row>=row1 && tempCellData->row<=row2 && tempCellData->col>=col1 && tempCellData->col<=col2)) 2231 if (allCells || (tempCellData->row>=row1 && tempCellData->row<=row2 && tempCellData->col>=col1 && tempCellData->col<=col2))
2230 { 2232 {
2231 QTableItem *cellItem=item(tempCellData->row, tempCellData->col); 2233 QTableItem *cellItem=item(tempCellData->row, tempCellData->col);
2232 if (cellItem && (entireCell ? (matchCase ? cellItem->text()==findStr : cellItem->text().upper()==findStr.upper()) : cellItem->text().find(findStr, 0, matchCase)>=0)) 2234 if (cellItem && (entireCell ? (matchCase ? cellItem->text()==findStr : cellItem->text().upper()==findStr.upper()) : cellItem->text().find(findStr, 0, matchCase)>=0))
2233 { 2235 {
2234 if (!found) 2236 if (!found)
2235 { 2237 {
2236 found=TRUE; 2238 found=TRUE;
2237 clearSelection(); 2239 clearSelection();
2238 } 2240 }
2239 setCurrentCell(tempCellData->row, tempCellData->col); 2241 setCurrentCell(tempCellData->row, tempCellData->col);
2240 if (replace) 2242 if (replace)
2241 { 2243 {
2242 tempCellData->data=cellItem->text().replace(QRegExp(findStr, matchCase), replaceStr); 2244 tempCellData->data=cellItem->text().replace(QRegExp(findStr, matchCase), replaceStr);
2243 setText(tempCellData->row, tempCellData->col, dataParser(findCellName(tempCellData->row, tempCellData->col), tempCellData->data)); 2245 setText(tempCellData->row, tempCellData->col, dataParser(findCellName(tempCellData->row, tempCellData->col), tempCellData->data));
2244 } 2246 }
2245 if (!replace || !replaceAll) break; 2247 if (!replace || !replaceAll) break;
2246 } 2248 }
2247 } 2249 }
2248 2250
2249 if (found) 2251 if (found)
2250 { 2252 {
2251 if (replace) 2253 if (replace)
2252 slotCellChanged(currentRow(), currentColumn()); 2254 slotCellChanged(currentRow(), currentColumn());
2253 } 2255 }
2254 else 2256 else
2255 QMessageBox::warning(this, tr("Error"), tr("Search key not found!")); 2257 QMessageBox::warning(this, tr("Error"), tr("Search key not found!"));
2256} 2258}
2257 2259
2258// 2260//
2259// Static functions 2261// Static functions
2260// 2262//
2261 2263
2262QString Sheet::getHeaderString(int section) 2264QString Sheet::getHeaderString(int section)
2263{ 2265{
2264 if (section<1) return ""; 2266 if (section<1) return "";
2265 return getHeaderString((section-1)/26)+QChar('A'+(section-1)%26); 2267 return getHeaderString((section-1)/26)+QChar('A'+(section-1)%26);
2266} 2268}
2267 2269
2268int Sheet::getHeaderColumn(const QString &section) 2270int Sheet::getHeaderColumn(const QString &section)
2269{ 2271{
2270 if (section.isEmpty()) return 0; 2272 if (section.isEmpty()) return 0;
2271 return (section[section.length()-1]-'A'+1)+getHeaderColumn(section.left(section.length()-1))*26; 2273 return (section[section.length()-1]-'A'+1)+getHeaderColumn(section.left(section.length()-1))*26;
2272} 2274}
2273 2275
2274 2276
2275//Expression Parser Class Definition 2277//Expression Parser Class Definition
2276 2278
2277 2279
2278QChar Expression::chunk0(void) 2280QChar Expression::chunk0(void)
2279{ 2281{
2280 if(chunk.length()>0) return(chunk[0]); else return('\0'); 2282 if(chunk.length()>0) return(chunk[0]); else return('\0');
2281}; 2283};
2282 2284
2283Expression::Expression(QString expr1)// constructor 2285Expression::Expression(QString expr1)// constructor
2284{ 2286{
2285 Body=expr1; 2287 Body=expr1;
2286 SYMBOL="+-*/%^=()<>&|!,"; 2288 SYMBOL="+-*/%^=()<>&|!,";
2287 MATHSYMBOL="+-*/%^=<>&|!,"; 2289 MATHSYMBOL="+-*/%^=<>&|!,";
2288 // lnlim=1.0e-36; // Smallest number allowed 2290 // lnlim=1.0e-36; // Smallest number allowed
2289 // loglim=1.0e-10 ; // Smallest number allowed in call to log10() * 2291 // loglim=1.0e-10 ; // Smallest number allowed in call to log10() *
2290 ErrorFound=TRUE; 2292 ErrorFound=TRUE;
2291 n=0;chunk="";SymbGroup=NONE_TOKEN;InExpr=Body; 2293 n=0;chunk="";SymbGroup=NONE_TOKEN;InExpr=Body;
2292 ArgsOfFunc=0; 2294 ArgsOfFunc=0;
2293 CompiledBody.setAutoDelete(TRUE); 2295 CompiledBody.setAutoDelete(TRUE);
2294 CompiledBodyType.setAutoDelete(TRUE); 2296 CompiledBodyType.setAutoDelete(TRUE);
2295 //CompiledBody=QStringList(0); 2297 //CompiledBody=QStringList(0);
2296}; 2298};
2297 2299
2298bool Expression::isSymbol(QChar ch) 2300bool Expression::isSymbol(QChar ch)
2299{ 2301{
2300 int j = 0; 2302 int j = 0;
2301 while (j<=((int)SYMBOL.length()-1) && ch!=SYMBOL[j]) j++; 2303 while (j<=((int)SYMBOL.length()-1) && ch!=SYMBOL[j]) j++;
2302 if(j<((int)SYMBOL.length())) return true; else return false; 2304 if(j<((int)SYMBOL.length())) return true; else return false;
2303}; 2305};
2304 2306
2305bool Expression::isMathSymbol(QChar ch) 2307bool Expression::isMathSymbol(QChar ch)
2306{ 2308{
2307 int j = 0; 2309 int j = 0;
2308 while (j<=((int)MATHSYMBOL.length()-1) && ch!=MATHSYMBOL[j]) j++; 2310 while (j<=((int)MATHSYMBOL.length()-1) && ch!=MATHSYMBOL[j]) j++;
2309 if(j<((int)MATHSYMBOL.length())) return true; else return false; 2311 if(j<((int)MATHSYMBOL.length())) return true; else return false;
2310}; 2312};
2311 2313
2312void Expression::GetNext() 2314void Expression::GetNext()
2313{ 2315{
2314 chunk=""; 2316 chunk="";
2315 if(n>=(int)InExpr.length()) return; 2317 if(n>=(int)InExpr.length()) return;
2316 while (InExpr[n]==' ') n++; 2318 while (InExpr[n]==' ') n++;
2317 if(InExpr[n]=='\"') 2319 if(InExpr[n]=='\"')
2318 { 2320 {
2319 while ( (n<(int)InExpr.length()) && (InExpr[n+1]!='\"') ) 2321 while ( (n<(int)InExpr.length()) && (InExpr[n+1]!='\"') )
2320 { 2322 {
2321 printf("chunk=%s\r\n",chunk.latin1()); 2323 printf("chunk=%s\r\n",chunk.latin1());
2322 chunk+=InExpr[n]; 2324 chunk+=InExpr[n];
2323 n++; 2325 n++;
2324 }; 2326 };
2325 chunk+=InExpr[n]; 2327 chunk+=InExpr[n];
2326 printf("2\r\n"); 2328 printf("2\r\n");
2327 SymbGroup=STRING_TOKEN; 2329 SymbGroup=STRING_TOKEN;
2328 } 2330 }
2329 else if (isSymbol(InExpr[n])) 2331 else if (isSymbol(InExpr[n]))
2330 { 2332 {
2331 SymbGroup=SYMBOL_TOKEN; 2333 SymbGroup=SYMBOL_TOKEN;
2332 chunk+=InExpr[n]; 2334 chunk+=InExpr[n];
2333 n++; 2335 n++;
2334 if( (n<(int)InExpr.length()) && 2336 if( (n<(int)InExpr.length()) &&
2335 isMathSymbol(InExpr[n-1]) && 2337 isMathSymbol(InExpr[n-1]) &&
2336 isMathSymbol(InExpr[n]) ) 2338 isMathSymbol(InExpr[n]) )
2337 { 2339 {
2338 SymbGroup=SYMBOL_TOKEN; 2340 SymbGroup=SYMBOL_TOKEN;
2339 chunk+=InExpr[n]; 2341 chunk+=InExpr[n];
2340 n++; 2342 n++;
2341 }; 2343 };
2342 } 2344 }
2343 else if ((InExpr[n].isLetter())||(InExpr[n]=='#')) 2345 else if ((InExpr[n].isLetter())||(InExpr[n]=='#'))
2344 { 2346 {
2345 while ( (n<(int)InExpr.length()) && !isSymbol(InExpr[n]) ) 2347 while ( (n<(int)InExpr.length()) && !isSymbol(InExpr[n]) )
2346 { 2348 {
2347 if (!(InExpr[n]==' ')) chunk+=InExpr[n]; 2349 if (!(InExpr[n]==' ')) chunk+=InExpr[n];
2348 n++; 2350 n++;
2349 }; 2351 };
2350 if (InExpr[n]=='(') SymbGroup=FUNCTION_TOKEN; // function TOKEN 2352 if (InExpr[n]=='(') SymbGroup=FUNCTION_TOKEN; // function TOKEN
2351 else SymbGroup=VARIABLE_TOKEN; 2353 else SymbGroup=VARIABLE_TOKEN;
2352 } 2354 }
2353 else if((n<(int)InExpr.length()) && 2355 else if((n<(int)InExpr.length()) &&
2354 ((InExpr[n].isDigit()) || (InExpr[n]=='.'))) 2356 ((InExpr[n].isDigit()) || (InExpr[n]=='.')))
2355 { 2357 {
2356 while( n<(int)InExpr.length() ) 2358 while( n<(int)InExpr.length() )
2357 { 2359 {
2358 if((InExpr[n].isDigit()) || InExpr[n]=='.') 2360 if((InExpr[n].isDigit()) || InExpr[n]=='.')
2359 { 2361 {
2360 chunk+=InExpr[n]; 2362 chunk+=InExpr[n];
2361 SymbGroup=NUMBER_TOKEN; 2363 SymbGroup=NUMBER_TOKEN;
2362 n++; 2364 n++;
2363 } 2365 }
2364 else if(InExpr[n]=='e') 2366 else if(InExpr[n]=='e')
2365 { 2367 {
2366 if((n+1)<(int)InExpr.length()) 2368 if((n+1)<(int)InExpr.length())
2367 { 2369 {
2368 if(InExpr[n+1]=='-' || InExpr[n+1]=='+' || InExpr[n+1].isDigit()) 2370 if(InExpr[n+1]=='-' || InExpr[n+1]=='+' || InExpr[n+1].isDigit())
2369 { 2371 {
2370 chunk+=InExpr[n]; 2372 chunk+=InExpr[n];
2371 chunk+=InExpr[n+1]; 2373 chunk+=InExpr[n+1];
2372 SymbGroup=NUMBER_TOKEN; 2374 SymbGroup=NUMBER_TOKEN;
2373 n+=2; 2375 n+=2;
2374 } 2376 }
2375 } 2377 }
2376 else 2378 else
2377 { 2379 {
2378 break; 2380 break;
2379 } 2381 }
2380 } 2382 }
2381 else 2383 else
2382 { 2384 {
2383 break; 2385 break;
2384 } 2386 }
2385 }//while 2387 }//while
2386 }//else if 2388 }//else if
2387};//end function 2389};//end function
2388 2390
2389 2391
2390void Expression::First() 2392void Expression::First()
2391{ 2393{
2392 GetNext(); 2394 GetNext();
2393 if (!(chunk=="") && !ErrorFound) Third(); 2395 if (!(chunk=="") && !ErrorFound) Third();
2394 else ErrorFound = true; 2396 else ErrorFound = true;
2395}; 2397};
2396 2398
2397void Expression::Third() 2399void Expression::Third()
2398{ 2400{
2399 QChar sign, secS='\0'; 2401 QChar sign, secS='\0';
2400 Fourth(); 2402 Fourth();
2401 sign = chunk0(); 2403 sign = chunk0();
2402 if((int)chunk.length()>1) secS=chunk[1]; 2404 if((int)chunk.length()>1) secS=chunk[1];
2403 while( sign == '+' || sign == '-'|| 2405 while( sign == '+' || sign == '-'||
2404 sign == '<' || sign == '>'|| sign == '%'|| 2406 sign == '<' || sign == '>'|| sign == '%'||
2405 sign == '&' || sign == '|' || sign == '!' || sign == '=' 2407 sign == '&' || sign == '|' || sign == '!' || sign == '='
2406 ) 2408 )
2407 { 2409 {
2408 GetNext(); 2410 GetNext();
2409 Fourth(); 2411 Fourth();
2410 QString name; 2412 QString name;
2411 if( sign == '+' ) name= "+" ; 2413 if( sign == '+' ) name= "+" ;
2412 else if(sign=='-') name= "-" ; 2414 else if(sign=='-') name= "-" ;
2413 else if(sign=='>' && secS=='\0') name= ">" ; 2415 else if(sign=='>' && secS=='\0') name= ">" ;
2414 else if(sign=='<' && secS=='\0') name= "<" ; 2416 else if(sign=='<' && secS=='\0') name= "<" ;
2415 else if(sign=='=' && secS=='=') name= "==" ; 2417 else if(sign=='=' && secS=='=') name= "==" ;
2416 else if(sign=='!' && secS=='=') name= "!=" ; 2418 else if(sign=='!' && secS=='=') name= "!=" ;
2417 else if(sign=='>' && secS=='=') name= ">=" ; 2419 else if(sign=='>' && secS=='=') name= ">=" ;
2418 else if(sign=='<' && secS=='=') name= "<=" ; 2420 else if(sign=='<' && secS=='=') name= "<=" ;
2419 else if(sign=='&' && secS=='&') name= "AND" ; 2421 else if(sign=='&' && secS=='&') name= "AND" ;
2420 else if(sign=='|' && secS=='|') name= "OR" ; 2422 else if(sign=='|' && secS=='|') name= "OR" ;
2421 else if(sign=='%') name= "MOD" ; 2423 else if(sign=='%') name= "MOD" ;
2422 CompiledBody.append(new QString(name)); // not sure if pushed in the back. 2424 CompiledBody.append(new QString(name)); // not sure if pushed in the back.
2423 CompiledBodyType.append(new int(FUNCTION_TOKEN | 2<<8)); //2 argument functions 2425 CompiledBodyType.append(new int(FUNCTION_TOKEN | 2<<8)); //2 argument functions
2424 sign = chunk0(); 2426 sign = chunk0();
2425 } 2427 }
2426}; 2428};
2427 2429
2428void Expression::Fourth() 2430void Expression::Fourth()
2429{ 2431{
2430 QChar sign; 2432 QChar sign;
2431 Fifth(); 2433 Fifth();
2432 sign = chunk0(); 2434 sign = chunk0();
2433 while( sign == '*' || sign == '/' ) 2435 while( sign == '*' || sign == '/' )
2434 { 2436 {
2435 GetNext(); 2437 GetNext();
2436 Fifth(); 2438 Fifth();
2437 QString name; 2439 QString name;
2438 if( sign == '*' ) name= "*" ; 2440 if( sign == '*' ) name= "*" ;
2439 else name= "/" ; 2441 else name= "/" ;
2440 CompiledBody.append(new QString(name)); 2442 CompiledBody.append(new QString(name));
2441 CompiledBodyType.append(new int(FUNCTION_TOKEN | 2<<8)); //2 arguments functions 2443 CompiledBodyType.append(new int(FUNCTION_TOKEN | 2<<8)); //2 arguments functions
2442 sign = chunk0(); 2444 sign = chunk0();
2443 } 2445 }
2444}; 2446};
2445 2447
2446void Expression::Fifth() 2448void Expression::Fifth()
2447{ 2449{
2448 Sixth(); 2450 Sixth();
2449 //if(chunk.Length==0) return; 2451 //if(chunk.Length==0) return;
2450 if( chunk0() == '^' ) 2452 if( chunk0() == '^' )
2451 { 2453 {
2452 GetNext(); 2454 GetNext();
2453 Fifth(); 2455 Fifth();
2454 CompiledBody.append(new QString("POWER")); 2456 CompiledBody.append(new QString("POWER"));
2455 CompiledBodyType.append(new int(FUNCTION_TOKEN | 2<<8)); // 2 argument functions 2457 CompiledBodyType.append(new int(FUNCTION_TOKEN | 2<<8)); // 2 argument functions
2456 } 2458 }
2457}; 2459};
2458 2460
2459void Expression::Sixth() 2461void Expression::Sixth()
2460{ 2462{
2461 char sign; 2463 char sign;
2462 sign = ' '; 2464 sign = ' ';
2463 if(SymbGroup== SYMBOL_TOKEN && 2465 if(SymbGroup== SYMBOL_TOKEN &&
2464 chunk0() == '+' || chunk0() == '-' | chunk0() == '!') 2466 chunk0() == '+' || chunk0() == '-' | chunk0() == '!')
2465 { 2467 {
2466 sign = chunk0(); 2468 sign = chunk0();
2467 GetNext(); 2469 GetNext();
2468 } 2470 }
2469 Seventh(); 2471 Seventh();
2470 if( sign == '-' ) 2472 if( sign == '-' )
2471 { 2473 {
2472 CompiledBody.append(new QString("CHGSGN")); // unary minus 2474 CompiledBody.append(new QString("CHGSGN")); // unary minus
2473 CompiledBodyType.append(new int(FUNCTION_TOKEN | 1<<8)); //1 argument 2475 CompiledBodyType.append(new int(FUNCTION_TOKEN | 1<<8)); //1 argument
2474 } 2476 }
2475 if( sign == '!' ) 2477 if( sign == '!' )
2476 { 2478 {
2477 CompiledBody.append(new QString("NOT")); // unary minus 2479 CompiledBody.append(new QString("NOT")); // unary minus
2478 CompiledBodyType.append(new int(FUNCTION_TOKEN | 1<<8)); //1 argument 2480 CompiledBodyType.append(new int(FUNCTION_TOKEN | 1<<8)); //1 argument
2479 } 2481 }
2480}; 2482};
2481 2483
2482 2484
2483void Expression::Seventh() 2485void Expression::Seventh()
2484{ 2486{
2485 if( chunk0() == '(' && SymbGroup==SYMBOL_TOKEN) 2487 if( chunk0() == '(' && SymbGroup==SYMBOL_TOKEN)
2486 { 2488 {
2487 GetNext(); 2489 GetNext();
2488 Third(); //parse the insides until we get a ')' 2490 Third(); //parse the insides until we get a ')'
2489 if (chunk0() != ')') ErrorFound = true; 2491 if (chunk0() != ')') ErrorFound = true;
2490 GetNext(); 2492 GetNext();
2491 } 2493 }
2492 else Eighth(); 2494 else Eighth();
2493}; 2495};
2494 2496
2495void Expression::Eighth() 2497void Expression::Eighth()
2496{ 2498{
2497 if ( SymbGroup== NUMBER_TOKEN ) 2499 if ( SymbGroup== NUMBER_TOKEN )
2498 { 2500 {
2499 CompiledBody.append(new QString(chunk)); 2501 CompiledBody.append(new QString(chunk));
2500 CompiledBodyType.append(new int(NUMBER_TOKEN)); 2502 CompiledBodyType.append(new int(NUMBER_TOKEN));
2501 GetNext(); 2503 GetNext();
2502 } 2504 }
2503 else if ( SymbGroup== VARIABLE_TOKEN ) 2505 else if ( SymbGroup== VARIABLE_TOKEN )
2504 { 2506 {
2505 CompiledBody.append(new QString(chunk)); 2507 CompiledBody.append(new QString(chunk));
2506 CompiledBodyType.append(new int(VARIABLE_TOKEN)); 2508 CompiledBodyType.append(new int(VARIABLE_TOKEN));
2507 GetNext(); 2509 GetNext();
2508 } 2510 }
2509 else if (SymbGroup== STRING_TOKEN ) 2511 else if (SymbGroup== STRING_TOKEN )
2510 { 2512 {
2511 CompiledBody.append(new QString(chunk+QString("\""))); 2513 CompiledBody.append(new QString(chunk+QString("\"")));
2512 CompiledBodyType.append(new int(STRING_TOKEN)); 2514 CompiledBodyType.append(new int(STRING_TOKEN));
2513 GetNext(); 2515 GetNext();
2514 } 2516 }
2515 else Ninth(); 2517 else Ninth();
2516}; 2518};
2517 2519
2518void Expression::Ninth() 2520void Expression::Ninth()
2519{ 2521{
2520 if ( SymbGroup== FUNCTION_TOKEN ) 2522 if ( SymbGroup== FUNCTION_TOKEN )
2521 { 2523 {
2522 QString TempFunk = chunk ; 2524 QString TempFunk = chunk ;
2523 GetNext(); 2525 GetNext();
2524 if(chunk0() == '(' ) 2526 if(chunk0() == '(' )
2525 { 2527 {
2526 FuncDepth++; 2528 FuncDepth++;
2527 ArgsOfFunc.resize(FuncDepth+1); 2529 ArgsOfFunc.resize(FuncDepth+1);
2528 ArgsOfFunc[FuncDepth]=1; 2530 ArgsOfFunc[FuncDepth]=1;
2529 //ArgsOfFunc=1; 2531 //ArgsOfFunc=1;
2530 GetNext(); 2532 GetNext();
2531 Third(); 2533 Third();
2532 while(chunk0()==',') //function arguments separator 2534 while(chunk0()==',') //function arguments separator
2533 { 2535 {
2534 //ArgsOfFunc++; 2536 //ArgsOfFunc++;
2535 ArgsOfFunc[FuncDepth]++; 2537 ArgsOfFunc[FuncDepth]++;
2536 GetNext(); 2538 GetNext();
2537 Third(); 2539 Third();
2538 }; 2540 };
2539 if (chunk0() != ')') ErrorFound = true; 2541 if (chunk0() != ')') ErrorFound = true;
2540 2542
2541 CompiledBody.append(new QString(TempFunk)); 2543 CompiledBody.append(new QString(TempFunk));
2542 if (TempFunk=="PI") ArgsOfFunc[FuncDepth]=0; 2544 if (TempFunk=="PI") ArgsOfFunc[FuncDepth]=0;
2543 // couldn't find a better way to parse function PI() with 0 args. :) 2545 // couldn't find a better way to parse function PI() with 0 args. :)
2544 CompiledBodyType.append(new int(FUNCTION_TOKEN | (ArgsOfFunc[FuncDepth]<<8) )); 2546 CompiledBodyType.append(new int(FUNCTION_TOKEN | (ArgsOfFunc[FuncDepth]<<8) ));
2545 //the mask &FF00 gives the arguments of the functions passed. 2547 //the mask &FF00 gives the arguments of the functions passed.
2546 FuncDepth--; 2548 FuncDepth--;
2547 ArgsOfFunc.resize(FuncDepth+1); 2549 ArgsOfFunc.resize(FuncDepth+1);
2548 GetNext(); 2550 GetNext();
2549 } 2551 }
2550 } 2552 }
2551 else 2553 else
2552 ErrorFound = true; 2554 ErrorFound = true;
2553}; 2555};
2554 2556
2555bool Expression::Parse() 2557bool Expression::Parse()
2556{ 2558{
2557 CompiledBody.clear(); 2559 CompiledBody.clear();
2558 ErrorFound = false; 2560 ErrorFound = false;
2559 n = 0;ArgsOfFunc.resize(0);FuncDepth=0; 2561 n = 0;ArgsOfFunc.resize(0);FuncDepth=0;
2560 InExpr=Body; 2562 InExpr=Body;
2561 First(); 2563 First();
2562 return(!ErrorFound); 2564 return(!ErrorFound);
2563}; 2565};
2564 2566
2565 2567
diff --git a/noncore/apps/opie-sheet/sheet.h b/noncore/apps/opie-sheet/sheet.h
index 41f1b86..f705cd0 100644
--- a/noncore/apps/opie-sheet/sheet.h
+++ b/noncore/apps/opie-sheet/sheet.h
@@ -1,211 +1,211 @@
1/*************************************************************************** 1/***************************************************************************
2 * * 2 * *
3 * This program is free software; you can redistribute it and/or modify * 3 * This program is free software; you can redistribute it and/or modify *
4 * it under the terms of the GNU General Public License as published by * 4 * it under the terms of the GNU General Public License as published by *
5 * the Free Software Foundation; either version 2 of the License, or * 5 * the Free Software Foundation; either version 2 of the License, or *
6 * (at your option) any later version. * 6 * (at your option) any later version. *
7 * * 7 * *
8 ***************************************************************************/ 8 ***************************************************************************/
9 9
10/* 10/*
11 * Opie Sheet (formerly Sheet/Qt) 11 * Opie Sheet (formerly Sheet/Qt)
12 * by Serdar Ozler <sozler@sitebest.com> 12 * by Serdar Ozler <sozler@sitebest.com>
13 */ 13 */
14 14
15#ifndef SHEET_H 15#ifndef SHEET_H
16#define SHEET_H 16#define SHEET_H
17 17
18#include <qtable.h> 18#include <qtable.h>
19#include <qstack.h> 19#include <qstack.h>
20 20
21typedef struct typeCellBorders 21typedef struct typeCellBorders
22{ 22{
23 QPen right, bottom; 23 QPen right, bottom;
24}; 24};
25 25
26typedef struct typeCellData 26typedef struct typeCellData
27{ 27{
28 int col, row; 28 int col, row;
29 typeCellBorders borders; 29 typeCellBorders borders;
30 QBrush background; 30 QBrush background;
31 Qt::AlignmentFlags alignment; 31 Qt::AlignmentFlags alignment;
32 QColor fontColor; 32 QColor fontColor;
33 QFont font; 33 QFont font;
34 QString data; 34 QString data;
35}; 35};
36 36
37 37
38#define NONE_TOKEN 0 38#define NONE_TOKEN 0
39#define NUMBER_TOKEN 1 39#define NUMBER_TOKEN 1
40#define VARIABLE_TOKEN 2 40#define VARIABLE_TOKEN 2
41#define FUNCTION_TOKEN 3 41#define FUNCTION_TOKEN 3
42#define SYMBOL_TOKEN 4 42#define SYMBOL_TOKEN 4
43#define STRING_TOKEN 5 43#define STRING_TOKEN 5
44 44
45class Expression 45class Expression
46{ 46{
47public: 47public:
48 QString Body; 48 QString Body;
49 QList<QString> CompiledBody; 49 QList<QString> CompiledBody;
50 QList<int> CompiledBodyType; 50 QList<int> CompiledBodyType;
51 QString SYMBOL; 51 QString SYMBOL;
52 QString MATHSYMBOL; 52 QString MATHSYMBOL;
53 QArray<int> ArgsOfFunc; 53 QArray<int> ArgsOfFunc;
54 int FuncDepth; 54 int FuncDepth;
55 bool ErrorFound; 55 bool ErrorFound;
56 int n; // holds the current parser position 56 int n; // holds the current parser position
57 QString chunk; // the piece in the parser we are on 57 QString chunk; // the piece in the parser we are on
58 int SymbGroup; // the current type 58 int SymbGroup; // the current type
59 59
60 QString InExpr; 60 QString InExpr;
61 61
62 QChar chunk0(void); // retunrs the first char of expression; 62 QChar chunk0(void); // retunrs the first char of expression;
63 Expression(QString expr1);// constructor 63 Expression(QString expr1);// constructor
64 64
65 bool isSymbol(QChar ch); 65 bool isSymbol(QChar ch);
66 bool isMathSymbol(QChar ch); 66 bool isMathSymbol(QChar ch);
67 void GetNext(); 67 void GetNext();
68 void First(); 68 void First();
69 void Third(); 69 void Third();
70 void Fourth(); 70 void Fourth();
71 void Fifth(); 71 void Fifth();
72 void Sixth(); 72 void Sixth();
73 void Seventh(); 73 void Seventh();
74 void Eighth(); 74 void Eighth();
75 void Ninth(); 75 void Ninth();
76 76
77 bool Expression::Parse(); //parses the expression in RPN format; 77 bool Expression::Parse(); //parses the expression in RPN format;
78 78
79}; 79};
80 80
81 81
82 82
83class Sheet: public QTable 83class Sheet: public QTable
84{ 84{
85 Q_OBJECT 85 Q_OBJECT
86 86
87 // Variables 87 // Variables
88 bool clicksLocked; 88 bool clicksLocked;
89 int selectionNo; 89 int selectionNo;
90 typeCellBorders defaultBorders; 90 typeCellBorders defaultBorders;
91 typeCellData defaultCellData; 91 typeCellData defaultCellData;
92 92
93 // QT objects 93 // QT objects
94 QList<typeCellData> sheetData, clipboardData; 94 QList<typeCellData> sheetData, clipboardData;
95 QString pressedCell, releasedCell, sheetName; 95 QString pressedCell, releasedCell, sheetName;
96 QStringList listDataParser; 96 QStringList listDataParser;
97 97
98 // Private functions 98 // Private functions
99 bool findRowColumn(const QString &variable, int *row, int *col, bool giveError=FALSE); 99 bool findRowColumn(const QString &variable, int *row, int *col, bool giveError=FALSE);
100 QString findCellName(int row, int col); 100 QString findCellName(int row, int col);
101 bool findRange(const QString &variable1, const QString &variable2, int *row1, int *col1, int *row2, int *col2); 101 bool findRange(const QString &variable1, const QString &variable2, int *row1, int *col1, int *row2, int *col2);
102 QString calculateVariable(const QString &variable); 102 QString calculateVariable(const QString &variable);
103 QString calculateFunction(const QString &function, const QString &parameters, int NumOfParams); 103 QString calculateFunction(const QString &func, const QString &parameters, int NumOfParams);
104 QString getParameter(const QString &parameters, int paramNo, bool giveError=FALSE, const QString funcName=""); 104 QString getParameter(const QString &parameters, int paramNo, bool giveError=FALSE, const QString funcName="");
105 QString dataParser(const QString &cell, const QString &data); 105 QString dataParser(const QString &cell, const QString &data);
106 QString dataParserHelper(const QString &data); 106 QString dataParserHelper(const QString &data);
107 typeCellData *createCellData(int row, int col); 107 typeCellData *createCellData(int row, int col);
108 typeCellData *findCellData(int row, int col); 108 typeCellData *findCellData(int row, int col);
109 109
110 110
111//LOGICAL / INFO 111//LOGICAL / INFO
112 double functionCountIf(const QString &param1, const QString &param2, const QString &param3); 112 double functionCountIf(const QString &param1, const QString &param2, const QString &param3);
113 double functionSumSQ(const QString &param1, const QString &param2); //sum of squares 113 double functionSumSQ(const QString &param1, const QString &param2); //sum of squares
114 QString functionIndex(const QString &param1, const QString &param2, int indx); 114 QString functionIndex(const QString &param1, const QString &param2, int indx);
115//math functions computations 115//math functions computations
116 double BesselI0(double x); 116 double BesselI0(double x);
117 double BesselI(int n, double x); 117 double BesselI(int n, double x);
118 double BesselK0(double x); 118 double BesselK0(double x);
119 double BesselI1(double x); 119 double BesselI1(double x);
120 double BesselK1(double x); 120 double BesselK1(double x);
121 double BesselK(int n, double x); 121 double BesselK(int n, double x);
122 double BesselJ0(double x); 122 double BesselJ0(double x);
123 double BesselY0(double x); 123 double BesselY0(double x);
124 double BesselJ1(double x); 124 double BesselJ1(double x);
125 double BesselY1(double x); 125 double BesselY1(double x);
126 double BesselY(int n, double x); 126 double BesselY(int n, double x);
127 double BesselJ(int n, double x); 127 double BesselJ(int n, double x);
128 double GammaLn(double xx); 128 double GammaLn(double xx);
129 double Factorial(double n); 129 double Factorial(double n);
130 double GammaP(double a, double x); 130 double GammaP(double a, double x);
131 double GammaQ(double a,double x); 131 double GammaQ(double a,double x);
132 void GammaSeries(double *gamser, double a, double x, double *gln); 132 void GammaSeries(double *gamser, double a, double x, double *gln);
133 void GammaContinuedFraction(double *gammcf, double a, double x, double *gln); 133 void GammaContinuedFraction(double *gammcf, double a, double x, double *gln);
134 double ErrorFunction(double x); 134 double ErrorFunction(double x);
135 double ErrorFunctionComplementary(double x); 135 double ErrorFunctionComplementary(double x);
136 double Beta(double z, double w); 136 double Beta(double z, double w);
137 double BetaContinuedFraction(double a, double b, double x); 137 double BetaContinuedFraction(double a, double b, double x);
138 double BetaIncomplete(double a, double b, double x); 138 double BetaIncomplete(double a, double b, double x);
139 double functionVariance(const QString &param1, const QString &param2); 139 double functionVariance(const QString &param1, const QString &param2);
140 double functionVariancePopulation(const QString &param1, const QString &param2); 140 double functionVariancePopulation(const QString &param1, const QString &param2);
141 double functionSkew(const QString &param1, const QString &param2); 141 double functionSkew(const QString &param1, const QString &param2);
142 double functionKurt(const QString &param1, const QString &param2); 142 double functionKurt(const QString &param1, const QString &param2);
143 143
144 // Sheet/Qt parser functions 144 // Sheet/Qt parser functions
145 double functionSum(const QString &param1, const QString &param2); 145 double functionSum(const QString &param1, const QString &param2);
146 double functionAvg(const QString &param1, const QString &param2); 146 double functionAvg(const QString &param1, const QString &param2);
147 double functionMax(const QString &param1, const QString &param2); 147 double functionMax(const QString &param1, const QString &param2);
148 double functionMin(const QString &param1, const QString &param2); 148 double functionMin(const QString &param1, const QString &param2);
149 double functionCount(const QString &param1, const QString &param2); 149 double functionCount(const QString &param1, const QString &param2);
150 150
151 // Reimplemented QTable functions 151 // Reimplemented QTable functions
152 void paintCell(QPainter *p, int row, int col, const QRect & cr, bool selected); 152 void paintCell(QPainter *p, int row, int col, const QRect & cr, bool selected);
153 void viewportMousePressEvent(QMouseEvent *e); 153 void viewportMousePressEvent(QMouseEvent *e);
154 void viewportMouseMoveEvent(QMouseEvent *e); 154 void viewportMouseMoveEvent(QMouseEvent *e);
155 void viewportMouseReleaseEvent(QMouseEvent *e); 155 void viewportMouseReleaseEvent(QMouseEvent *e);
156 156
157 public slots: 157 public slots:
158 void slotCellSelected(int row, int col); 158 void slotCellSelected(int row, int col);
159 void slotCellChanged(int row, int col); 159 void slotCellChanged(int row, int col);
160 160
161 public: 161 public:
162 Sheet(int numRows, int numCols, QWidget *parent); 162 Sheet(int numRows, int numCols, QWidget *parent);
163 ~Sheet(); 163 ~Sheet();
164 void ReCalc(void); 164 void ReCalc(void);
165 void setData(const QString &data); 165 void setData(const QString &data);
166 QString getData(); 166 QString getData();
167 167
168 void setName(const QString &data); 168 void setName(const QString &data);
169 QString getName(); 169 QString getName();
170 170
171 void setPen(int row, int col, int vertical, const QPen &pen); 171 void setPen(int row, int col, int vertical, const QPen &pen);
172 QPen getPen(int row, int col, int vertical); 172 QPen getPen(int row, int col, int vertical);
173 173
174 void setBrush(int row, int col, const QBrush &brush); 174 void setBrush(int row, int col, const QBrush &brush);
175 QBrush getBrush(int row, int col); 175 QBrush getBrush(int row, int col);
176 176
177 void setTextAlign(int row, int col, Qt::AlignmentFlags flags); 177 void setTextAlign(int row, int col, Qt::AlignmentFlags flags);
178 Qt::AlignmentFlags getAlignment(int row, int col); 178 Qt::AlignmentFlags getAlignment(int row, int col);
179 179
180 void setTextFont(int row, int col, const QFont &font, const QColor &color); 180 void setTextFont(int row, int col, const QFont &font, const QColor &color);
181 QFont getFont(int row, int col); 181 QFont getFont(int row, int col);
182 QColor getFontColor(int row, int col); 182 QColor getFontColor(int row, int col);
183 183
184 void lockClicks(bool lock=TRUE); 184 void lockClicks(bool lock=TRUE);
185 void copySheetData(QList<typeCellData> *destSheetData); 185 void copySheetData(QList<typeCellData> *destSheetData);
186 void setSheetData(QList<typeCellData> *srcSheetData); 186 void setSheetData(QList<typeCellData> *srcSheetData);
187 void getSelection(int *row1, int *col1, int *row2, int *col2); 187 void getSelection(int *row1, int *col1, int *row2, int *col2);
188 188
189 void insertRows(int no=1, bool allColumns=TRUE); 189 void insertRows(int no=1, bool allColumns=TRUE);
190 void insertColumns(int no=1, bool allRows=TRUE); 190 void insertColumns(int no=1, bool allRows=TRUE);
191 191
192 void dataFindReplace(const QString &find, const QString &replace, bool matchCase=TRUE, bool allCells=TRUE, bool entireCell=FALSE, bool replace=FALSE, bool replaceAll=FALSE); 192 void dataFindReplace(const QString &find, const QString &replace, bool matchCase=TRUE, bool allCells=TRUE, bool entireCell=FALSE, bool replace=FALSE, bool replaceAll=FALSE);
193 193
194 // Static functions 194 // Static functions
195 static int getHeaderColumn(const QString &section); 195 static int getHeaderColumn(const QString &section);
196 static QString getHeaderString(int section); 196 static QString getHeaderString(int section);
197 197
198 public slots: 198 public slots:
199 void editCut(); 199 void editCut();
200 void editCopy(); 200 void editCopy();
201 void editPaste(bool onlyContents=FALSE); 201 void editPaste(bool onlyContents=FALSE);
202 void editClear(); 202 void editClear();
203 void swapCells(int row1, int col1, int row2, int col2); 203 void swapCells(int row1, int col1, int row2, int col2);
204 204
205 signals: 205 signals:
206 void currentDataChanged(const QString &data); 206 void currentDataChanged(const QString &data);
207 void cellClicked(const QString &cell); 207 void cellClicked(const QString &cell);
208 void sheetModified(); 208 void sheetModified();
209}; 209};
210 210
211#endif 211#endif