File indexing completed on 2024-04-21 03:41:50

0001 /*
0002     Task.h  -  class Task
0003     SPDX-FileCopyrightText: 2001 Sebastian Stein <seb.kde@hpfsc.de>
0004     SPDX-FileCopyrightText: 2008 Tiago Porangaba <tiago.porangaba@ltia.fc.unesp.br>
0005     SPDX-FileCopyrightText: 2008 Tadeu Araujo <tadeu.araujo@ltia.fc.unesp.br>
0006     SPDX-FileCopyrightText: 2008 Danilo Balzaque <danilo.balzaque@ltia.fc.unesp.br>
0007 
0008     SPDX-License-Identifier: GPL-2.0-or-later
0009 */
0010 
0011 #ifndef TASK_H
0012 #define TASK_H
0013 
0014 #include "Ratio.h"
0015 #include "PrimeNumber.h"
0016 
0017 #include <vector>
0018 #include <QTextStream>
0019 
0020 /** important for add_sub and mul_div */
0021 #define YES 1
0022 #define NO  0
0023 
0024 /** important for op_vector */
0025 #define ADD 0
0026 #define SUB 1
0027 #define MUL 2
0028 #define DIV 3
0029 
0030 /** to mark a prime factor as used or unused */
0031 #define UNUSED 0
0032 #define USED    1
0033 
0034 /** Structure represents a prime factor.
0035  *  Structure stores a prime factor and its usage status. The factor is marked
0036  *  as used or unused.
0037  **/
0038 typedef struct PRIME_FACTOR {
0039     /** the prime factor */
0040     int factor;
0041 
0042     /** the status of the prime factor (used or unused) */
0043     short flag;
0044 }
0045 Tprime_factor;
0046 
0047 /** we use the vector template class to create 3 dynamic types */
0048 typedef QList<Ratio> RatioArray;
0049 typedef QList<short> ShortArray;
0050 typedef QList<Tprime_factor> PrimeFactorArray;
0051 
0052 /*! class to handle mathematical tasks with ratios
0053  *  naming:
0054  *  - a task has at least 2 ratios
0055  *  - a task has at least 1 operation
0056  *
0057  *  \author Sebastian Stein */
0058 class Task
0059 {
0060 public:
0061     /** constructor */
0062     Task();
0063 
0064     /** destructor */
0065     ~Task();
0066 
0067     /** automatically generate a new task with the given parameters */
0068     void create_task(unsigned int pmax_md = 10, short pnr_ratios = 2,
0069                      short padd_add = YES, short padd_div = NO, short padd_mult = NO, short padd_sub = NO);
0070 
0071     /** set ratio n */
0072     void set_ratio_n(unsigned short number = 0, int numerator = 0,
0073                      int denominator = 1);
0074 
0075     /** set ratio n */
0076     void set_ratio_n(unsigned short number, const Ratio &fraction);
0077 
0078     /** returns ration n */
0079     Ratio get_ratio_n(unsigned short number = 0) const;
0080 
0081     /** set operation n */
0082     void set_op_n(unsigned short number = 0, short operation = ADD);
0083 
0084     /** return operation n */
0085     short get_op_n(unsigned short number = 0) const;
0086 
0087     /** add a ratio to the end of the task */
0088     void add_ratio(const Ratio &new_ratio);
0089 
0090     /** add a ratio to the end of the task */
0091     void add_ratio(int numerator = 0, int denominator = 1);
0092 
0093     /** add an operation at the end of the task */
0094     void add_operation(short operation = ADD);
0095 
0096     /** display the whole task, mainly for debug */
0097     QTextStream & display(QTextStream & str);
0098 
0099     /** solves the task and returns the result as ratio */
0100     Ratio solve();
0101 
0102     /** returns the number of ratios in the vector */
0103     int getNumberOfRatios() const;
0104 
0105     /** returns the number of operations in the vector */
0106     int getNumberOfOperations() const;
0107 
0108     /** removes all ratios and operations from the given task */
0109     void clean();
0110 
0111 private:
0112     /** the ratio vector */
0113     RatioArray ratio_vector;
0114 
0115     /** the operation vector, smaller by one than ratio_vector */
0116     ShortArray op_vector;
0117 
0118     /** the prime factor vector is used to store all prime factors of the
0119      * main denominator */
0120     PrimeFactorArray prim_fac_vector;
0121 
0122     /** this function is needed by solve() */
0123     Ratio product(RatioArray::iterator & ratio_pointer,
0124                   ShortArray::iterator & op_pointer);
0125 
0126     /** generate the operations randomly; return how many mul or div
0127      * are in one block */
0128     unsigned short make_operation(short padd_add = YES, short padd_div = NO,
0129                                   short padd_mult = NO, short padd_sub = NO,
0130                                   short pnr_ratios = 0);
0131 
0132     /** find a denominator for the task */
0133     int make_main_dn(unsigned int pmax_md, unsigned short max_product_length);
0134 
0135     /** returns the count number's prime factors  */
0136     unsigned short prim_factor_nr(int number = 1);
0137 
0138     /** set the numerators randomly */
0139     void make_numerators(int main_denominator, short pnr_ratios);
0140 
0141     /** create the ratios' denominators */
0142     void make_denominators(int main_denominator = 0, short pmax_md = 0,
0143                            short padd_div = NO, short padd_mult = NO);
0144 };
0145 
0146 
0147 /* ------ some prototypes of non class functions ------ */
0148 
0149 /** it is possible to code: cout << task_object << endl; */
0150 QTextStream & operator<< (QTextStream & str, Task & ptask);
0151 
0152 #endif