• Skip to content
  • Skip to link menu
KDE API Reference
  • KDE API Reference
  • kdeedu API Reference
  • KDE Home
  • Contact Us
 

kig

  • sources
  • kde-4.12
  • kdeedu
  • kig
  • misc
equation.cc
Go to the documentation of this file.
1 // Copyright (C) 2005 Pino Toscano <toscano.pino@tiscali.it>
2 
3 // This program is free software; you can redistribute it and/or
4 // modify it under the terms of the GNU General Public License
5 // as published by the Free Software Foundation; either version 2
6 // of the License, or (at your option) any later version.
7 
8 // This program is distributed in the hope that it will be useful,
9 // but WITHOUT ANY WARRANTY; without even the implied warranty of
10 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 // GNU General Public License for more details.
12 
13 // You should have received a copy of the GNU General Public License
14 // along with this program; if not, write to the Free Software
15 // Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
16 // 02110-1301, USA.
17 
18 #include "equation.h"
19 
20 #include <qregexp.h>
21 #include <klocale.h>
22 #include <kglobal.h>
23 #include <cmath>
24 //#include <vector>
25 
26 /*
27  * Definitions for EquationString
28  */
29 
30 EquationString::EquationString( const QString& s )
31  : QString( s )
32 {
33 }
34 
35 double EquationString::trunc( double d )
36 {
37  if ( std::fabs( d ) < 1e-7 ) return 0.0;
38  return d;
39 }
40 
41 void EquationString::addTerm( double coeff, const QString& monomial, bool& needsign )
42 {
43  if ( trunc( coeff ) == 0.0 ) return;
44  if ( needsign )
45  {
46  if ( coeff < 0 )
47  {
48  append( " - " );
49  } else {
50  append( " + " );
51  }
52  } else {
53  needsign = true;
54  if ( coeff < 0 )
55  {
56  append( "- " );
57  }
58  }
59  coeff = std::fabs( coeff );
60  if ( monomial.isEmpty() || std::fabs( coeff - 1.0 ) > 1e-6 )
61  append( KGlobal::locale()->formatNumber( coeff, 3 ) );
62  if ( !monomial.isEmpty() )
63  {
64  append( " " );
65  append( monomial );
66  }
67  return;
68 }
69 
70 const QString EquationString::x3() const
71 {
72  return QString::fromUtf8( "x³" );
73 }
74 
75 const QString EquationString::y3() const
76 {
77  return QString::fromUtf8( "y³" );
78 }
79 
80 const QString EquationString::x2y() const
81 {
82  return QString::fromUtf8( "x²y" );
83 }
84 
85 const QString EquationString::xy2() const
86 {
87  return QString::fromUtf8( "xy²" );
88 }
89 
90 const QString EquationString::x2() const
91 {
92  return QString::fromUtf8( "x²" );
93 }
94 
95 const QString EquationString::y2() const
96 {
97  return QString::fromUtf8( "y²" );
98 }
99 
100 const QString EquationString::xy() const
101 {
102  return "xy";
103 }
104 
105 const QString EquationString::x() const
106 {
107  return "x";
108 }
109 
110 const QString EquationString::y() const
111 {
112  return "y";
113 }
114 
115 /* used in the circle equation */
116 
117 void EquationString::prettify( void )
118 {
119  replace( "( x )", "x" );
120  replace( "( y )", "y" );
121 }
122 
123 const QString EquationString::xnym(int n, int m) const
124 {
125  QString ret="";
126  switch (n)
127  {
128  case 0:
129  break;
130  case 1:
131  ret += 'x';
132  break;
133  case 2:
134  ret += QString::fromUtf8( "x²" );
135  break;
136  case 3:
137  ret += QString::fromUtf8( "x³" );
138  break;
139  case 4:
140  ret += QString::fromUtf8( "x⁴" );
141  break;
142  case 5:
143  ret += QString::fromUtf8( "x⁵" );
144  break;
145  case 6:
146  ret += QString::fromUtf8( "x⁶" );
147  break;
148  default:
149  ret += QString::fromLatin1( "x^" ) + QString::number( n );
150  }
151 
152  switch (m)
153  {
154  case 0:
155  break;
156  case 1:
157  ret += 'y';
158  break;
159  case 2:
160  ret += QString::fromUtf8( "y²" );
161  break;
162  case 3:
163  ret += QString::fromUtf8( "y³" );
164  break;
165  case 4:
166  ret += QString::fromUtf8( "y⁴" );
167  break;
168  case 5:
169  ret += QString::fromUtf8( "y⁵" );
170  break;
171  case 6:
172  ret += QString::fromUtf8( "y⁶" );
173  break;
174  default:
175  ret += QString::fromLatin1( "y^" ) + QString::number( n );
176  }
177 
178  return ret;
179 }
180 
181 //struct EqElem
182 //{
183 // double coeff;
184 // int xpow;
185 // int ypow;
186 //};
187 //
188 //static const QString mon_3 = QString/*::fromUtf8*/( "³" );
189 //static const QString mon_2 = QString/*::fromUtf8*/( "²" );
190 //
191 //static QString varToString( const char* var, int power, bool& outputted )
192 //{
193 // outputted = true;
194 // switch ( power )
195 // {
196 // case 0:
197 // outputted = false;
198 // return "";
199 // break;
200 // case 1:
201 // return QString( var );
202 // break;
203 // case 2:
204 // return QString( var ) + mon_2;
205 // break;
206 // case 3:
207 // return QString( var ) + mon_3;
208 // break;
209 // }
210 // return QString( var ) + "^" + QString::number( power );
211 //}
212 //
213 //static QString sign( double x )
214 //{
215 // if ( x < 0 )
216 // return "-";
217 // else if ( x > 0 )
218 // return "+";
219 // return "";
220 //}
221 //
222 //static QString coeffToString( double coeff, bool with_spaces, bool& iszero )
223 //{
224 // iszero = false;
225 // QString res;
226 // if ( fabs( coeff ) < 1e-8 )
227 // {
228 // iszero = true;
229 // return "";
230 // }
231 // else if ( coeff == 1 )
232 // {
233 // return "+";
234 // }
235 // else
236 // {
237 // return sign( coeff ) + ( with_spaces ? " " : "" ) + QString::number( fabs( coeff ) );
238 // }
239 //}
240 //
241 //static QString EqElemToString( const EqElem& el, bool with_spaces )
242 //{
243 // bool iszero = false;
244 // QString res = coeffToString( el.coeff, with_spaces, iszero )
245 // + ( with_spaces ? " " : "" );
246 // if ( !iszero )
247 // {
248 // bool xout = true;
249 // bool yout = true;
250 // res += varToString( "x", el.xpow, xout )
251 // + varToString( "y", el.ypow, yout );
252 // if ( xout || yout )
253 // res += ( with_spaces ? " " : "" );
254 // }
255 //
256 // return res;
257 //}
258 //
259 //
260 //class Equation::Private
261 //{
262 //public:
263 // Private()
264 // : m_show2ndMember( false ), m_2ndMemberValue( 0.0 )
265 // {
266 // }
267 //
268 // std::vector<EqElem> m_elems;
269 // bool m_show2ndMember;
270 // double m_2ndMemberValue;
271 //};
272 //
273 //static int hash( const EqElem& el )
274 //{
275 // // order by grade, then by x power and then by y power
276 // return ( ( el.xpow + el.ypow ) << 8 ) | ( el.xpow << 4 ) | el.ypow;
277 //}
278 //
279 //Equation::Equation()
280 // : d( new Private )
281 //{
282 //}
283 //
284 //void Equation::addTerm( double coeff, int xpower, int ypower )
285 //{
286 // if ( fabs( coeff ) < 1e-8 )
287 // {
288 // removeTerm( xpower, ypower );
289 // return;
290 // }
291 // EqElem el;
292 // el.coeff = coeff;
293 // el.xpow = xpower;
294 // el.ypow = ypower;
295 // int newhash = hash( el );
296 // bool found = false;
297 // for ( std::vector<EqElem>::iterator it = d->m_elems.begin(); it != d->m_elems.end() && !found; ++it )
298 // {
299 // int currhash = hash( *it );
300 // if ( newhash > currhash )
301 // {
302 // d->m_elems.insert( it, el );
303 // found = true;
304 // }
305 // else if ( newhash == currhash )
306 // {
307 // (*it).coeff = el.coeff;
308 // found = true;
309 // }
310 // }
311 // if ( !found )
312 // {
313 // d->m_elems.push_back( el );
314 // }
315 //}
316 //
317 //void Equation::removeTerm( int xpower, int ypower )
318 //{
319 // bool found = false;
320 // for ( std::vector<EqElem>::iterator it = d->m_elems.begin(); it != d->m_elems.end() && !found; ++it )
321 // {
322 // if ( ( (*it).xpow == xpower ) && ( (*it).ypow == ypower ) )
323 // {
324 // d->m_elems.erase( it );
325 // found = true;
326 // }
327 // }
328 //}
329 //
330 //bool Equation::term( double& value, int xpower, int ypower )
331 //{
332 // bool found = false;
333 // value = 0;
334 // for ( std::vector<EqElem>::iterator it = d->m_elems.begin(); it != d->m_elems.end() && !found; ++it )
335 // {
336 // if ( ( (*it).xpow == xpower ) && ( (*it).ypow == ypower ) )
337 // {
338 // value = (*it).coeff;
339 // found = true;
340 // }
341 // }
342 // return found;
343 //}
344 //
345 //void Equation::clear()
346 //{
347 // d->m_elems.clear();
348 //}
349 //
350 //void Equation::setSecondMemberShown( bool show )
351 //{
352 // d->m_show2ndMember = show;
353 //}
354 //
355 //bool Equation::isSecondMemberShown() const
356 //{
357 // return d->m_show2ndMember;
358 //}
359 //
360 //void Equation::setSecondMemberValue( double value )
361 //{
362 // d->m_2ndMemberValue = value;
363 //}
364 //
365 //double Equation::secondMemberValue() const
366 //{
367 // return d->m_2ndMemberValue;
368 //}
369 //
370 //QString Equation::prettyString( bool with_spaces ) const
371 //{
372 // QString res;
373 // if ( d->m_elems.size() == 0 ) return res;
374 //
375 // for ( std::vector<EqElem>::iterator it = d->m_elems.begin(); it != d->m_elems.end(); ++it )
376 // res += EqElemToString( *it, with_spaces );
377 // if ( d->m_show2ndMember )
378 // res += QString( "=" ) + ( with_spaces ? " " : "" ) + QString::number( d->m_2ndMemberValue );
379 //
380 // res.replace( QRegExp("^\\+\\s?"), "" );
381 // res.replace( QRegExp("\\s?$"), "" );
382 //
383 // return res;
384 //}
385 
EquationString::y
const QString y() const
Definition: equation.cc:110
equation.h
EquationString::xy2
const QString xy2() const
Definition: equation.cc:85
EquationString::x3
const QString x3() const
Definition: equation.cc:70
EquationString::xy
const QString xy() const
Definition: equation.cc:100
EquationString::x
const QString x() const
Definition: equation.cc:105
EquationString::addTerm
void addTerm(double coeff, const QString &unknowns, bool &needsign)
Definition: equation.cc:41
EquationString::y2
const QString y2() const
Definition: equation.cc:95
EquationString::x2
const QString x2() const
Definition: equation.cc:90
EquationString::xnym
const QString xnym(int n, int m) const
Definition: equation.cc:123
EquationString::x2y
const QString x2y() const
Definition: equation.cc:80
EquationString::y3
const QString y3() const
Definition: equation.cc:75
EquationString::EquationString
EquationString(const QString &string)
Definition: equation.cc:30
EquationString::prettify
void prettify(void)
Definition: equation.cc:117
EquationString::trunc
double trunc(double)
Definition: equation.cc:35
This file is part of the KDE documentation.
Documentation copyright © 1996-2014 The KDE developers.
Generated on Tue Oct 14 2014 22:35:39 by doxygen 1.8.7 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.

kig

Skip menu "kig"
  • Main Page
  • Namespace List
  • Namespace Members
  • Alphabetical List
  • Class List
  • Class Hierarchy
  • Class Members
  • File List
  • File Members

kdeedu API Reference

Skip menu "kdeedu API Reference"
  • Analitza
  •     lib
  • kalgebra
  • kalzium
  •   libscience
  • kanagram
  • kig
  •   lib
  • klettres
  • kstars
  • libkdeedu
  •   keduvocdocument
  • marble
  • parley
  • rocs
  •   App
  •   RocsCore
  •   VisualEditor
  •   stepcore

Search



Report problems with this website to our bug tracking system.
Contact the specific authors with questions and comments about the page contents.

KDE® and the K Desktop Environment® logo are registered trademarks of KDE e.V. | Legal