summaryrefslogtreecommitdiffstats
path: root/kbruch/src/task.h
blob: 3e4c15b618316f63340520a90eb028cfc7c754da (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
/***************************************************************************
                          task.h  -  class task
                             -------------------
    begin                : Tue Nov 27 16:40:42 CET 2001
    copyright            : (C) 2001 by Sebastian Stein
    email                : seb.kde@hpfsc.de
 ***************************************************************************/

/***************************************************************************
 *                                                                         *
 *   This program is free software; you can redistribute it and/or modify  *
 *   it under the terms of the GNU General Public License as published by  *
 *   the Free Software Foundation; either version 2 of the License, or     *
 *   (at your option) any later version.                                   *
 *                                                                         *
 ***************************************************************************/

#ifndef TASK_H
#define TASK_H

#include "ratio.h"
#include "primenumber.h"

#include <vector>

/** important for add_sub and mul_div */
#define YES 1
#define NO  0

/** important for op_vector */
#define ADD 0
#define SUB 1
#define MUL 2
#define DIV 3

/** to mark a prime factor as used or unused */
#define UNUSED 0
#define USED  	1

/** Structure represents a prime factor.
 *  Structure stores a prime factor and its usage status. The factor is marked
 *  as used or unused.
 **/
typedef struct PRIME_FACTOR
{
	/** the prime factor */
	int factor;

	/** the status of the prime factor (used or unused) */
	short flag;
}
Tprime_factor;

/** we use the vector template class to create 3 dynamic types */
typedef QValueVector<ratio> RatioArray;
typedef QValueVector<short> ShortArray;
typedef QValueVector<Tprime_factor> PrimeFactorArray;

/*! class to handle mathematical tasks with ratios
 *  naming:
 *  - a task has at least 2 ratios
 *  - a task has at least 1 operation
 *
 *  \author Sebastian Stein */
class task
{
public:
	/** constructor */
	task();

	/** destructor */
	~task();

	/** automatically generate a new task with the given parameters */
	void create_task(unsigned int pmax_md = 10, short pnr_ratios = 2,
	                 short padd_sub = YES, short pmul_div = NO);

	/** set ratio n */
	void set_ratio_n(unsigned short number = 0, int numerator = 0,
	                 int denominator = 1);

	/** set ratio n */
	void set_ratio_n(unsigned short number = 0, ratio fraction = 0);

	/** returns ration n */
	ratio get_ratio_n(unsigned short number = 0) const;

	/** set operation n */
	void set_op_n(unsigned short number = 0, short operation = ADD);

	/** return operation n */
	short get_op_n(unsigned short number = 0) const;

	/** add a ratio to the end of the task */
	void add_ratio(ratio new_ratio = 0);

	/** add a ratio to the end of the task */
	void add_ratio(int numerator = 0, int denominator = 1);

	/** add an operation at the end of the task */
	void add_operation(short operation = ADD);

	/** display the whole task, mainly for debug */
	QTextStream & display(QTextStream & str);

	/** solves the task and returns the result as ratio */
	ratio solve();

	/** returns the number of ratios in the vector */
	int getNumberOfRatios() const;

	/** returns the number of operations in the vector */
	int getNumberOfOperations() const;

private:
	/** max. size of main denominator */
	int max_md;

	/** how many ratios should the task have */
	short nr_ratios;

	/** are add/sub operations allowed */
	short add_sub;

	/** are mul/div operations allowed */
	short mul_div;

	/** the ratio vector */
	RatioArray ratio_vector;

	/** the operation vector, smaller by one than ratio_vector */
	ShortArray op_vector;

	/** the prime factor vector is used to store all prime factors of the
	 * main denominator */
	PrimeFactorArray prim_fac_vector;

	/** this function is needed by solve() */
	ratio product(RatioArray::iterator & ratio_pointer,
	              ShortArray::iterator & op_pointer);

	/** generate the operations randomly; return how many mul or div
	 * are in one block */
	unsigned short make_operation(short padd_sub, short pmul_div,
	                              short pnr_ratios);

	/** find a denominator for the task */
	int make_main_dn(unsigned int pmax_md, unsigned short max_product_length);

	/** returns the count number's prime factors  */
	unsigned short prim_factor_nr(int number = 1);

	/** set the numerators randomly */
	void make_numerators(int main_denominator, short pnr_ratios);

	/** create the ratios' denominators */
	void make_denominators(int main_denominator, short pmax_md,
	                       short pmul_div);
};


/* ------ some prototypes of non class functions ------ */

/** it is possible to code: cout << task_object << endl; */
QTextStream & operator<<(QTextStream & str, task & ptask);

#endif