Welcome, Guest: Register On Nairaland / LOGIN! / Trending / Recent / NewStats: 3,208,835 members, 8,003,969 topics. Date: Saturday, 16 November 2024 at 03:14 AM |
Nairaland Forum / Science/Technology / Programming / An Open Source Mathematics C++ Class That Solves Mathematical Problems On Set (1350 Views)
[source Code] A Program That Solves A Transportation Problem - By Frank UMEADI / [C++ Open Source] Relax! ( File Management Tool) / Creating An Open-source Java Web Service (2) (3) (4)
An Open Source Mathematics C++ Class That Solves Mathematical Problems On Set by 2mNaira: 1:20pm On Nov 23, 2014 |
I recently stumbled into the thread, https://www.nairaland.com/1962286/c-programmer-house and made an effort to assist the poster in writing the class, but I some how have the feeling that I might need a class like that in the nearest future. The unfortunate thing is that I am an extremely busy person and cannot, at this time, afford the time to write a comprehensive MATHEMATICS SET class. So I thought to involve others and and just lead the way. What I wish to write on this thread is a Mathematics Set class that can solve any conceivable mathematics set problem, including drawing of venn diagrams. This means that you ask the class to solve practically any set problem. The class is expected to be cross-platform, meaning it would work on practically any operating symtem including windows, linux, mackintosh, android, blackberry, iOs, etc. This is important because the class will be expected to carry out some graphical drawings. Those who develop school management apps may find the class useful. So, if you are such a person I wil advise you to contribute to the writing of this class. Althought the class will be developed in C/C++, people who can write code to interface it will all other relevant programming language ( once the first version of the class ie completed), will be needed. Along side, it will be neccessary to write an exception class for the thread too. The class will adequately inform users of the kind of misuse that occur and possibly offer, a correct use suggestion. Please, start bringing in your suggestions, and contributions, but kindly wait for me to create the first next neccessary four posts. |
Re: An Open Source Mathematics C++ Class That Solves Mathematical Problems On Set by 2mNaira: 1:21pm On Nov 23, 2014 |
//class definition of a mathematics set classes developed on nairaland //This class defintion may be used or modified freely in as much as the above comment line is included //This following is meant to handle different operation sytem. //At the meoment it is aplace holder. Suggestions on how best to //write is is welcome #define WINDOWS // this line of code will eventually go for cross-platform capability #ifdef WINDOWS #define DEVICE HDC #endif #ifdef LINUX #endif #ifdef UNIX #endif //A generic set class for holding a set neede for asking set questions enum SetType { T_UNSPECIFIED,T_INTEGER, T_DOUBLE, T_CHAR,T_STRING}; template <typename T> class MathSet { T *m_MSet;//Holds set of Elements int m_iElementCount;//Holds the number of elements in set SetType m_eSetType;//Holds information about the type of sets static char chSetName;//Holds the character part of a set's name static char iSetName;//Holds the integer part of a set's name char szSetName[3];//Holds a set's name e.g A1, A2, A3 etc public: MathSet(); MathSet(SetType eSetType); MathSet( T *MSet, int iElementCount); MathSet(const MathSet &ExistingObject); MathSet operator = (const MathSet &Obj); bool operator == (const MathSet &Obj); bool operator != (const MathSet &Obj); bool operator > (const MathSet &Obj); bool operator < (const MathSet &Obj); bool IsEmptySet(); bool IsSuperSet(const MathSet &SecondSet); bool IsSubSet(const MathSet &SecondSet); bool IsProperSubSet(const MathSet &SecondSet); MathSet Intersection(const MathSet &SecondSet); MathSet Union(const MathSet &SecondSet); void Add(T NewELement); int Remove(T Element); void Remove(int Position, int NElements); void RemoveAll(); int Remove(vector<MathSet> vSet); //Additional memebers int Cardinality(); // returns the number of elements in the set }; //this is a generic array class used for holding a set of element template <typename T> class SArray { T * m_Array;// Array of elements int m_iCount;//Count of element in array public: SArray(); SArray( const SArray &Obj); SArray operator = (const SArray &Obj); T ElemetAt(int iIndex);//return element at index iIndex T operator [] (int iIndex);//return element at index iIndex bool operator == (const SArray &Obj); bool operator != (const SArray &Obj); bool operator >(const SArray &Obj); bool operator <(const SArray &Obj); void Add( T Element);//Add new elemnt int Remove(T Element);//Remove specified element int Remove (int iPostion, int iCount = 0);//Remove the first iCount elements //starting from position, iPosition void RemoveAll();//Remove All elements int GetSize();//Get number of lements in array int Count(T Element);//Count the number of times element Element occur in the array }; enum Token {TT_UNION,TT_INTERSECTION,TT_COMPLEMENT,TT_SETNAME,TT_CARDINALITY,TT_FIND,TT_EVALUATE,TT_GIVEN,TT_EXPAND,TT_SIMPLIFY}; //Interprtation of token: //U = UNION //^ = INTERSECTION //! = COMPLEMENT //SETNAME is strictly of the form "letter-numeral", e.g. A1,A2,A3,A4, etc //CARDINALITY of a set is the number of elements in the set and must be represented by letter n. //nA1 = number of elements in A1, nA2 = no of elements in A2 etc //Keyword FIND, is used to instruct the class to find a set solution e.g. FIND A1 U A2 //Keyword EVALUATE is used to instruct the class to find a value e.g EVALUATE n(AuB^c) //Keyword GIVEN is use to specify a comma seperated list of Cardinalitie //e.g. GIVEN: n(A1^B1) = 12, nA1! = 4; EVALUATE nB1 //Keyword EXPAND, is used to instruct the class to expand an expression. e.g. //EXPAND A!^B^C! //Keyword SIMPLIFY is used to instruct the class to simplify an expression. e.g. //(A U B) ^ (C U D)! //so the parser will pass for the characters:U ^ n ! ( ) , ; : //It will also parse for the keywords: FIND,EVALUATE,GIVEN,EXPAND,SIMLIFY //Note that keyword and characters are not case sensitive. //For instance, FIND and find are the same, n and N are thesame //This class is used for parsing a question for tokens class Parser { SArray<Token> m_TokenList; char *pBuffer; public: Parser(const char *pQuestion);//is created with the set question SArray<Token> GetTokenList() {return m_TokenList;}//return a token list private: Token NextToken();//get next token bool Scan();//Scan for a token in question }; enum SolutionType { SOL_SET, SOL_INTEGER, SOL_STRING}; template <typename T> class SetProblem { MathSet<T> m_UniversalSet;//holds the universal set SArray<MathSet<T>> m_Array;//holds the sets needed for the question string m_Question;//holds the question SArray<Token> m_TokenList;// holds token list int m_iSolution;// holds integer solution MathSet<T> m_Solution;// Holds set solution string m_stSolution//Holds string solution public: SetProblem(const <MathSet<T> &UniversalSet);//this object must be created with the universal set void Ask( char * pQuestion);// A mathematical set question posed to the class to solve void GetSolution(SolutionType &eType);//There three possible solutions, integer,set or string. //The return value should be cast to the //type of solution specified by eTypes void AddSet(vector<Mathset> VSet);//add a set needed for question void ChangeUniversalSet(const <MathSet<T> &UniversalSet); int Remove(MathSet<T> MSet);//remove a set int Remove(int iPosition, int Count = 0);//Remove the first Count det starting from iPosition void RemoveAll(); void RemoveUniversalSet();//Remove a universal set int Count(); private: void Solve(SolutionType &eType);//There three possible solutions, integer,set or string //The return value should be cast to the //type of solution specfied by eTypes }; enum SolutionType { SOL_SET, SOL_INTEGER}; template <typename T> class SetProblem { MathSet<T> m_UniversalSet;//holds the universal set SArray<MathSet<T>> m_Array;//holds the sets needed for the question char m_Question[5000];//holds the question SArray<Token> m_TokenList;// holds token list int m_iSolution;// holds integer solution MathSet<T> m_Solution;// Holds set solution public: SetProblem(const <MathSet<T> &UniversalSet);//this object must be created with the universal set void Ask( char * pQuestion);// A mathematical set question posed to the class to solve void GetSolution(SolutionType &eType);//There two possible solutions, integer or set. //The return value should be cast to the //type of solution spefified by eTypes void AddSet(vector<Mathset> VSet);//add a set needed for question void ChangeUniversalSet(const <MathSet<T> &UniversalSet); int Remove(MathSet<T> MSet);//remove a set int Remove(int iPosition, int Count = 0);//Remove the first Count det starting from iPosition void RemoveAll(); void RemoveUniversalSet();//Remove a universal set int Count(); void DrawVennDiagram();//Draws venn diagram to console void DrawVennDiagram( DEVICE pDevice, int iXPoint,int YPoint));// Draw venn diagram to window private: void Solve(SolutionType &eType);//There two possible solutions, integer or set. //The return value should be cast to the //type of solution spefified by eTypes }; class SException : public exception { int m_iErrorCode; string m_Description; string m_Suggestion; public: SException(int iErrorCode); SException(const SException &obj); SException operator = (const SException &obj); }; //This is the first version of the class definitions, please kindly bring in your improvement or additional feature or additional functions suggestion and your own version of the implementation of the functions of the class. Please feel free to use and modify or improve the class implementation that I used in a previous thread |
Re: An Open Source Mathematics C++ Class That Solves Mathematical Problems On Set by 2mNaira: 1:21pm On Nov 23, 2014 |
.. |
Re: An Open Source Mathematics C++ Class That Solves Mathematical Problems On Set by 2mNaira: 1:22pm On Nov 23, 2014 |
... |
Re: An Open Source Mathematics C++ Class That Solves Mathematical Problems On Set by 2mNaira: 1:23pm On Nov 23, 2014 |
..... |
Re: An Open Source Mathematics C++ Class That Solves Mathematical Problems On Set by asalimpo(m): 2:41pm On Nov 23, 2014 |
Wen u said class ,i thought "classroom". I think, program/app would b less confusg. To solve any set problem, how wud d queries b entered? In strict unambiguous format e.g A U B = ? Or as a wrd problem. Wrd problem parsing wud require advanced ai level complexity. |
Re: An Open Source Mathematics C++ Class That Solves Mathematical Problems On Set by 2mNaira: 2:52pm On Nov 23, 2014 |
asalimpo: Perhaps you could, help with that. The member function. Question takes a string that will be passed. This means someone may need to come up with a grammer / parser for parsing the the question. Thanks for your comment on the title, I will correct it now. |
Re: An Open Source Mathematics C++ Class That Solves Mathematical Problems On Set by 2mNaira: 3:04pm On Nov 23, 2014 |
asalimpo: Word problems can be fed in, but it must be done according to a specific language grammer determined by the programmer. It will be made as close as possible to spoken language but not to the extent that it makes it too difficult to write a parser for it. I can develop the grammer and write the parser but I can do everything. If nobody comes up with any grammer/parser, I post a sample one here as giude for people to use. |
Re: An Open Source Mathematics C++ Class That Solves Mathematical Problems On Set by 2mNaira: 3:35pm On Nov 23, 2014 |
If you are not a programmer but you are well versed in maths , specifically set theory, please will need your suggestion of concept/ features/problem you would like the class to solve. |
Re: An Open Source Mathematics C++ Class That Solves Mathematical Problems On Set by 2mNaira: 3:36pm On Nov 23, 2014 |
Ok. I am throwing this open. Which would you prefer, for the class to be implemented using template or strictly overloaded functions. Please let me know what you think and why. Its all towards a better class implementation. If there is a superior argument against template, then I will re-write the class definition. If there isn't template will be retained. |
Re: An Open Source Mathematics C++ Class That Solves Mathematical Problems On Set by asalimpo(m): 9:52am On Nov 24, 2014 |
Even the simplest grammer would require lots of code to parse correctly. I suggest you make it all symbolic ,like mathematics is, and place d burden on d user to discipline himself to use it rather than the other way round. The program would also run faster without d parsing overhead. Just suggesting. Put the code on a public repo for ppl to partcpate in. My strenght is java though. I maynt b of much coding help. |
Re: An Open Source Mathematics C++ Class That Solves Mathematical Problems On Set by prodam(m): 11:22pm On Nov 24, 2014 |
aint a c++ person..but am interested in this project.. Nothing much to put in yet. Abt the parser thing, I think a kinda load-balancing. .asalimpo has said somthing related tho. balancing things is gonna be great too...there might just be a format and av a some filter method to re format /trim/sanitise(probably ignore white spaces, some extra xters n all dat...) We could validate this at the point of data entry using a simple regex based on that format...exception class would act/respond accordingly giving the user the cause of the error by showing what part of the 'question' is unsupported or being rejected by the program? Am I talking any sense here..I need to even know.. |
Re: An Open Source Mathematics C++ Class That Solves Mathematical Problems On Set by WhiZTiM(m): 2:33pm On Nov 25, 2014 |
Hmmn.... I just took a brief look at your code using a Code formatter; That's a good one... A few gotchas though: 1. In the era of C++11/14, you should minimize the use of raw pointers, rather use, Unique pointers 2. As a rule of thumb, your exception classes should always have a grandfather, std::exception... 3. Drawing of Venn diagram is an entirely separate concern, the best you want to do is to provide Adapter classes 4. Good use of templates, the STL provides the fundamental set operations we need such as: -- -- std::set_difference -- -- std::set_intersection -- -- std::set_union +++ This effectively reduces our worries: I must commend you, neat code. I love it. Your coding style is consistent. And your implementation is brilliant sir. I will try and write some code and post 2 Likes |
Re: An Open Source Mathematics C++ Class That Solves Mathematical Problems On Set by WhiZTiM(m): 11:39pm On Nov 25, 2014 |
I just took some time to code a little implementation ... I am using a standard container to make the Mathematical set ...I implemented a compile-time switch depending on the size of the data WHAT TO KNOW: 1. The MathSet class is a Generic And User Friendly Frontend Code that uses an underlying container to store and manipulate it's data. 2. That container is not accessed directly, rather, it's accessed through an Adaptor class... 3. it is privately inherited at compile time to maintain some design invariants 4. To use another kind of 'special' container, all you need to do is to create an Adaptor class for that container and implement all member functions of MathSet<> ChecK: http://pastebin.com/mx1dEJY9 or https://gist.github.com/WhiZTiM/518cd9ace9c5b8a18ba3#file-mathset_draft-cpp |
Re: An Open Source Mathematics C++ Class That Solves Mathematical Problems On Set by 2mNaira: 4:50pm On Nov 30, 2014 |
I apologise for my late response.I have been very busy. |
Re: An Open Source Mathematics C++ Class That Solves Mathematical Problems On Set by 2mNaira: 4:54pm On Nov 30, 2014 |
asalimpo: I agree with u.But I will soon write a parser throw it open for criticism. If majority condemn it.It will be dropped. |
Re: An Open Source Mathematics C++ Class That Solves Mathematical Problems On Set by 2mNaira: 4:56pm On Nov 30, 2014 |
prodam: Of course u are talkg sense, I will soon write and post an update. |
Re: An Open Source Mathematics C++ Class That Solves Mathematical Problems On Set by 2mNaira: 8:25pm On Nov 30, 2014 |
WhiZTiM: I have seen your class. It is a most excellent and efficient class. How ever I have some questions and concerns. |
Re: An Open Source Mathematics C++ Class That Solves Mathematical Problems On Set by WhiZTiM(m): 8:40pm On Nov 30, 2014 |
mnairaland:...please ask... |
Re: An Open Source Mathematics C++ Class That Solves Mathematical Problems On Set by 2mNaira: 10:22pm On Nov 30, 2014 |
WhiZTiM: Don.t you think using STL will make the class bulky. I won't be surprised if someone decides to use the class to implement set theory in a calculator. Will size of the class not be too big for for such implementation? As to my questions, I will sign up on one or both of the sights you used paste the code and add my questions as comment to the code section concerned. That aside, I think your Mathset class should define an assignment operator, equality operator and non-equality operator, should a need arise for the class to be used in larger expressions. Your class should include a function Cardinality (or ElementsNo) that returns the number of elements in the set. Please, see the modified exception class and above and below and bear the associated comments in mind in your implementation. |
Re: An Open Source Mathematics C++ Class That Solves Mathematical Problems On Set by 2mNaira: 10:58pm On Nov 30, 2014 |
Modified exception class class SException : public exception { int m_ErrorCode; char *m_pDescription; public: SException(int iErrorCode); SException(const SException &obj); SException operator = (const SException &obj); }; |
Re: An Open Source Mathematics C++ Class That Solves Mathematical Problems On Set by 2mNaira: 10:43am On Dec 01, 2014 |
//This is the most recent exception class enum SErrorCode {EC_EMPTYSET,EC_INVAID_SET,EC_EMPTY_QUESTION,EC_INVALID_QUESTION};//Of course this is just a few of the error codes, you //are expected to add yours as you write your implementation. //All implementation should throw exceptions for error use and abnormal use and transfer that function to the runtime. //All implementation are expected to contruct an ecxeption object using the error code. class SException : public exception { int m_iErrorCode; string m_Description; string m_Suggestion; public: SException(int iErrorCode); SException(const SException &obj); SException operator = (const SException &obj); }; SException::SException(int iErrorCode) { m_iErrorCode = iErrorCode; switch(iErrorCode) { case EC_EMPTYSET: m_Description = "Attempted to use empty set"; m_Suggestion = "Provide a non-empty set"; break; case EC_INVAID_SET: m_Description = "Attempted to use an invalid set"; m_Suggestion = "Provide a valid set"; case EC_EMPTY_QUESTION: m_Description = "Attempted to use an empty question"; m_Suggestion = "Provide a non-empty question"; case EC_INVALID_QUESTION: m_Description = "Attempted to use empty question"; m_Suggestion = "Provide a non-empty question"; default: m_Description = "Unknown error"; m_Suggestion = "No suggestion"; break; } } Read friendly version is located at https://gist.github.com/mnairaland/30ca049feb115c89ca05 |
Re: An Open Source Mathematics C++ Class That Solves Mathematical Problems On Set by 2mNaira: 10:46am On Dec 01, 2014 |
//This is the most recent parser class enum Token {TT_UNION,TT_INTERSECTION,TT_COMPLEMENT,TT_SETNAME,TT_CARDINALITY,TT_FIND,TT_EVALUATE,TT_GIVEN,TT_EXPAND,TT_SIMPLIFY}; //Interprtation of token: //U = UNION //^ = INTERSECTION //! = COMPLEMENT //SETNAME is strictly of the form "letter-numeral", e.g. A1,A2,A3,A4, etc //CARDINALITY of a set is the number of elements in the set and must be represented by letter n. //nA1 = number of elements in A1, nA2 = no of elements in A2 etc //Keyword FIND, is used to instruct the class to find a set solution e.g. FIND A1 U A2 //Keyword EVALUATE is used to instruct the class to find a value e.g EVALUATE n(AuB^c) //Keyword GIVEN is use to specify a comma seperated list of Cardinalitie //e.g. GIVEN: n(A1^B1) = 12, nA1! = 4; EVALUATE nB1 //Keyword EXPAND, is used to instruct the class to expand an expression. e.g. //EXPAND A!^B^C! //Keyword SIMPLIFY is used to instruct the class to simplify an expression. e.g. //(A U B) ^ (C U D)! //so the parser will pass for the characters:U ^ n ! ( ) , ; : //It will also parse for the keywords: FIND,EVALUATE,GIVEN,EXPAND,SIMLIFY //Note that keyword and characters are not case sensitive. //For instance, FIND and find are the same, n and N are thesame //This class is used for parsing a question for tokens class Parser { SArray<Token> m_TokenList; char *pBuffer; public: Parser(const char *pQuestion);//is created with the set question SArray<Token> GetTokenList() {return m_TokenList;}//return a token list private: Token NextToken();//get next token bool Scan();//Scan for a token in question }; enum SolutionType { SOL_SET, SOL_INTEGER, SOL_STRING}; template <typename T> class SetProblem { MathSet<T> m_UniversalSet;//holds the universal set SArray<MathSet<T>> m_Array;//holds the sets needed for the question string m_Question;//holds the question SArray<Token> m_TokenList;// holds token list int m_iSolution;// holds integer solution MathSet<T> m_Solution;// Holds set solution string m_stSolution//Holds string solution public: SetProblem(const <MathSet<T> &UniversalSet);//this object must be created with the universal set void Ask( char * pQuestion);// A mathematical set question posed to the class to solve void GetSolution(SolutionType &eType);//There three possible solutions, integer,set or string. //The return value should be cast to the //type of solution specified by eTypes void AddSet(vector<Mathset> VSet);//add a set needed for question void ChangeUniversalSet(const <MathSet<T> &UniversalSet); int Remove(MathSet<T> MSet);//remove a set int Remove(int iPosition, int Count = 0);//Remove the first Count det starting from iPosition void RemoveAll(); void RemoveUniversalSet();//Remove a universal set int Count(); private: void Solve(SolutionType &eType);//There three possible solutions, integer,set or string //The return value should be cast to the //type of solution specfied by eTypes }; |
Re: An Open Source Mathematics C++ Class That Solves Mathematical Problems On Set by 2mNaira: 11:09am On Dec 01, 2014 |
On a second thought I decided to move the whole thing to gist hub because the site is code friendly. @ WhiZTiM, I have copied your code so that others can see the most recent stage of the class all in one place The latest classes can be found at: https://gist.github.com/mnairaland/30ca049feb115c89ca05 |
Re: An Open Source Mathematics C++ Class That Solves Mathematical Problems On Set by 2mNaira: 11:11am On Dec 01, 2014 |
I f you have any code kindly post it as a comment at or new gist at gisthub and notify us here. |
Re: An Open Source Mathematics C++ Class That Solves Mathematical Problems On Set by asalimpo(m): 1:20pm On Dec 01, 2014 |
unfortunately, i am a java guy, For now. (the most convenient general purpose language in my opinion). What compiler dyu use for c++? I've been put off by unwieldy tools. |
Re: An Open Source Mathematics C++ Class That Solves Mathematical Problems On Set by 2mNaira: 1:33pm On Dec 01, 2014 |
asalimpo: I use visual studio. Java guys are needed. You could help write interface to java. I know it is possible. |
Re: An Open Source Mathematics C++ Class That Solves Mathematical Problems On Set by talk2chichi: 4:22pm On Dec 03, 2014 |
asalimpo: Please can you help me answer some Java Questions? Thanks |
Re: An Open Source Mathematics C++ Class That Solves Mathematical Problems On Set by asalimpo(m): 10:35pm On Dec 03, 2014 |
wat kinda of question? Hope it aint solicitation to do your assignmnt for u? Shoot |
(1) (Reply)
Need Visual Studio / Mysql: I Find This Silly...pls Help Me Out / Biolinks - Instagram Bio Links & Url Shortener (script)
(Go Up)
Sections: politics (1) business autos (1) jobs (1) career education (1) romance computers phones travel sports fashion health religion celebs tv-movies music-radio literature webmasters programming techmarket Links: (1) (2) (3) (4) (5) (6) (7) (8) (9) (10) Nairaland - Copyright © 2005 - 2024 Oluwaseun Osewa. All rights reserved. See How To Advertise. 76 |