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