1 /***************************************************************************
4 begin : Tue Nov 27 16:40:42 CET 2001
5 copyright : (C) 2001 by Sebastian Stein
6 email : seb.kde@hpfsc.de
7 ***************************************************************************/
9 /***************************************************************************
11 * This program is free software; you can redistribute it and/or modify *
12 * it under the terms of the GNU General Public License as published by *
13 * the Free Software Foundation; either version 2 of the License, or *
14 * (at your option) any later version. *
16 ***************************************************************************/
22 #include "primenumber.h"
26 /** important for add_sub and mul_div */
30 /** important for op_vector */
36 /** to mark a prime factor as used or unused */
40 /** Structure represents a prime factor.
41 * Structure stores a prime factor and its usage status. The factor is marked
44 typedef struct PRIME_FACTOR
46 /** the prime factor */
49 /** the status of the prime factor (used or unused) */
54 /** we use the vector template class to create 3 dynamic types */
55 typedef QValueVector
<ratio
> RatioArray
;
56 typedef QValueVector
<short> ShortArray
;
57 typedef QValueVector
<Tprime_factor
> PrimeFactorArray
;
59 /*! class to handle mathematical tasks with ratios
61 * - a task has at least 2 ratios
62 * - a task has at least 1 operation
64 * \author Sebastian Stein */
74 /** automatically generate a new task with the given parameters */
75 void create_task(unsigned int pmax_md
= 10, short pnr_ratios
= 2,
76 short padd_sub
= YES
, short pmul_div
= NO
);
79 void set_ratio_n(unsigned short number
= 0, int numerator
= 0,
83 void set_ratio_n(unsigned short number
= 0, ratio fraction
= 0);
85 /** returns ration n */
86 ratio
get_ratio_n(unsigned short number
= 0) const;
88 /** set operation n */
89 void set_op_n(unsigned short number
= 0, short operation
= ADD
);
91 /** return operation n */
92 short get_op_n(unsigned short number
= 0) const;
94 /** add a ratio to the end of the task */
95 void add_ratio(ratio new_ratio
= 0);
97 /** add a ratio to the end of the task */
98 void add_ratio(int numerator
= 0, int denominator
= 1);
100 /** add an operation at the end of the task */
101 void add_operation(short operation
= ADD
);
103 /** display the whole task, mainly for debug */
104 QTextStream
& display(QTextStream
& str
);
106 /** solves the task and returns the result as ratio */
109 /** returns the number of ratios in the vector */
110 int getNumberOfRatios() const;
112 /** returns the number of operations in the vector */
113 int getNumberOfOperations() const;
116 /** max. size of main denominator */
119 /** how many ratios should the task have */
122 /** are add/sub operations allowed */
125 /** are mul/div operations allowed */
128 /** the ratio vector */
129 RatioArray ratio_vector
;
131 /** the operation vector, smaller by one than ratio_vector */
132 ShortArray op_vector
;
134 /** the prime factor vector is used to store all prime factors of the
135 * main denominator */
136 PrimeFactorArray prim_fac_vector
;
138 /** this function is needed by solve() */
139 ratio
product(RatioArray::iterator
& ratio_pointer
,
140 ShortArray::iterator
& op_pointer
);
142 /** generate the operations randomly; return how many mul or div
143 * are in one block */
144 unsigned short make_operation(short padd_sub
, short pmul_div
,
147 /** find a denominator for the task */
148 int make_main_dn(unsigned int pmax_md
, unsigned short max_product_length
);
150 /** returns the count number's prime factors */
151 unsigned short prim_factor_nr(int number
= 1);
153 /** set the numerators randomly */
154 void make_numerators(int main_denominator
, short pnr_ratios
);
156 /** create the ratios' denominators */
157 void make_denominators(int main_denominator
, short pmax_md
,
162 /* ------ some prototypes of non class functions ------ */
164 /** it is possible to code: cout << task_object << endl; */
165 QTextStream
& operator<<(QTextStream
& str
, task
& ptask
);