moved kdeaccessibility kdeaddons kdeadmin kdeartwork kdebindings kdeedu kdegames...
[kdeedu.git] / kbruch / src / task.h
blob3e4c15b618316f63340520a90eb028cfc7c754da
1 /***************************************************************************
2 task.h - class task
3 -------------------
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 /***************************************************************************
10 * *
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. *
15 * *
16 ***************************************************************************/
18 #ifndef TASK_H
19 #define TASK_H
21 #include "ratio.h"
22 #include "primenumber.h"
24 #include <vector>
26 /** important for add_sub and mul_div */
27 #define YES 1
28 #define NO 0
30 /** important for op_vector */
31 #define ADD 0
32 #define SUB 1
33 #define MUL 2
34 #define DIV 3
36 /** to mark a prime factor as used or unused */
37 #define UNUSED 0
38 #define USED 1
40 /** Structure represents a prime factor.
41 * Structure stores a prime factor and its usage status. The factor is marked
42 * as used or unused.
43 **/
44 typedef struct PRIME_FACTOR
46 /** the prime factor */
47 int factor;
49 /** the status of the prime factor (used or unused) */
50 short flag;
52 Tprime_factor;
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
60 * naming:
61 * - a task has at least 2 ratios
62 * - a task has at least 1 operation
64 * \author Sebastian Stein */
65 class task
67 public:
68 /** constructor */
69 task();
71 /** destructor */
72 ~task();
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);
78 /** set ratio n */
79 void set_ratio_n(unsigned short number = 0, int numerator = 0,
80 int denominator = 1);
82 /** set ratio n */
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 */
107 ratio solve();
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;
115 private:
116 /** max. size of main denominator */
117 int max_md;
119 /** how many ratios should the task have */
120 short nr_ratios;
122 /** are add/sub operations allowed */
123 short add_sub;
125 /** are mul/div operations allowed */
126 short mul_div;
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,
145 short pnr_ratios);
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,
158 short pmul_div);
162 /* ------ some prototypes of non class functions ------ */
164 /** it is possible to code: cout << task_object << endl; */
165 QTextStream & operator<<(QTextStream & str, task & ptask);
167 #endif