help with classes in c++

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • sajenia
    New Member
    • Oct 2006
    • 8

    help with classes in c++

    i have written a program to generate prime integers between 1-7000.
    here is the code.

    #include <stdio.h>
    main()
    {
    int number,div,if_p rime,primes=0,p ri,es1=0,primes 2=0,primes3=0,p rimes4=0,primes 5=0,primes6=0;
    for(number=2;nu mber<=7000;numb er++)
    {
    for(div=2;div<n umber;div++)
    {
    if(number%div== 0)
    {if_prime=0;
    break;
    }
    if_prime=1;
    }

    if(if_prime)
    {
    if(number>1 && number<=1000)
    primes=primes+1 ;
    else if(number>1001 && number<=2000)
    primes1=primes1 +1;
    else if(number>2001 && number<=3000)
    primes2=primes2 +1;
    else if(number>3001 && number<=4000)
    primes3=primes3 +1;
    else if(number>4001 && number<=5000)
    primes4=primes4 +1;
    else if(number>5001 && number<=6000)
    primes5=primes5 +1;
    else if(number>6001 && number <=7000)
    primes6=primes6 +1;
    }

    cout<<"Range Prime numbers"<<endl;

    cout<<"0001-1000: "primes<<en dl;
    cout<<"1001-2000: "primes1<<e ndl;
    cout<<"2001-3000: "primes2<<e ndl;
    cout<<"3001-4000: "primes3<<e ndl;
    cout<<"4001-5000: "primes4<<e ndl;
    cout<<"5001-6000: "primes5<<e ndl;
    cout<<"6001-7000: "primes6<<e ndl;

    // end of code

    my problem is that i need to display this information in a graphical manner using graphics.h
    how can i do it. maybe u can tell me how to draw a bar chart.

    How can i make this code work with classes?
    pliz help
  • backpacking
    New Member
    • Nov 2006
    • 2

    #2
    I see that you posted a while ago. So you might have already found what you are looking for. I was looking at your code and noticed some areas that I might be able to help. I won't be able to help with the graphics, but will get you to a good point so that the code will work easier with a graph control or whatever you find out there that you like. By the way I am going to use generic programming - meaning that this should be able to be used on any platform that there is a c++ compiler for. Here is what we will do.

    Encapsulate your example into a class
    Optimize the code
    Add a couple of events so that people can do whatever they want to with it.

    put the following code into the file "Primes.h"

    Code:
    #ifndef primes
    #define primes
    #endif
    
    #include "Events.h"
    
    class primeNumbers;
    
    class primeContainer{
        private:
            std::vector<int> cont;
            
            //simple adding method
            void add(int number){
                cont.push_back(number);
                return;
            }
    
        public:
            primeContainer(){};
            ~primeContainer(){};
    
            //simple indexing tool
            int operator[](int index){
                std::vector<int>::iterator iter;
                if(index > cont.end()-cont.begin())
                    iter = cont.end();
                else
                    iter = cont.begin()+index;
    
                return *iter;
            }
            
            int count(){
                return cont.end()-cont.begin();
            }
            
            friend class primeNumbers;
    };
    
    class primeNumbers{
    		public:
    			EventHandler<void (*)(void*,int)> OnFindPrime;
                 
    		primeContainer& find(int end){
    			primeContainer* pc = new primeContainer();//a container to keep all the primes in and pass
    			bool p = true;//marks a number as prime or not
    			int limit;//limits the divisor from going above where it needs to - the approximate square root.
    			int div;//the divisor
    
    			//loops through all the numbers start through end finding the primes
    			for(int current = 2; current <= end; current++){//you would think that you would save skipping even numbers.
    				p = true;
    				limit = current;
    				div = 2;
    
    					//within prime list.  uses the prime list to cut time off of divisions.
    				for(int x = 0;x<pc->count() && div<limit;x++){
    				    div = pc->operator[](x);
    				    limit = current/div;
    				    if(current%div == 0){
    				        p = false;
    				        break;
    					}
    				} if(p==false)continue;//if the number was found to be false then move on
    
    				//after prime list.  if any other divisors need to be calculated
    				for(div;div<limit;div++){//try replacing 'limit' with 'current' to see the time difference
    					limit = current/div;//no sense in going above this point
    					if(current%div == 0){
    						p = false;
    						break;
    					}
    				}
    
    				if(p == true){//if p is still true then it is indeed a prime
    					pc->add(current);
    					//call events
    					for(int x = 0,y = OnFindPrime.size();x<y;x++){
    						OnFindPrime[x].function(OnFindPrime[x].caller,current);
    
    					}
    				}
    			}
    			return *pc;
    		}
    };
    //boy did this code get messy. didn't look like that on my computer


    now put the following code in a file "Events.h"

    Code:
    #ifndef events_h
    #define events_h
    #endif
    
    #include <vector>
    using namespace std;
    
    template<typename functionDef>
    struct Event{
    	functionDef function;//describes a pointer to the described function
    	void* caller;
    };
    
    template<typename functionDef>
    class EventHandler{
    	private:
    		typedef Event<functionDef> event;
    		std::vector<event> container;
    		typedef typename vector<event>::iterator iterator;
    
    	public:
    
    		void AddEvent(functionDef function, void* instance = 0){
    			event E = {function, instance};
    			container.push_back(E);
    		}
    
    		void RemoveEvent(functionDef function, void* instance){
    
    			for(iterator current = container.begin(),end = container.end();current<end;current++){
    				if(current->function == function && current->caller == instance){
    					container.erase(current);
    				}
    			}
    		}
    
    		event& operator[](int index){
    			return container[index];
    		}
    
    		int size(){
    			return container.size();
    		}
    };
    create a static function that looks like the following

    Code:
    void FoundPrime(void* caller, int currentNumber){
         //do whatever you want to in here
    };
    use your main() function to test this out - until you see what I did. Make sure that you put the files in the same file that you main() is located. And then include this at the top

    Code:
    #include "Primes.h"
    Code:
    primeNumbers pn;
    pn.OnFindPrime.AddEvent(FoundPrime, 0);
    primeContainer& pc = pn.find(400000);
    pn.find returns a container to all the primes from 0 to 400000. But you could add code in your event that pulls out the ones you are looking for. This is a better way for a class to view the problem.

    Let me know if this helps. I hope it works on your computer.

    Comment

    • sajenia
      New Member
      • Oct 2006
      • 8

      #3
      Originally posted by backpacking
      I see that you posted a while ago. So you might have already found what you are looking for. I was looking at your code and noticed some areas that I might be able to help. I won't be able to help with the graphics, but will get you to a good point so that the code will work easier with a graph control or whatever you find out there that you like. By the way I am going to use generic programming - meaning that this should be able to be used on any platform that there is a c++ compiler for. Here is what we will do.

      Encapsulate your example into a class
      Optimize the code
      Add a couple of events so that people can do whatever they want to with it.

      put the following code into the file "Primes.h"

      Code:
      #ifndef primes
      #define primes
      #endif
      
      #include "Events.h"
      
      class primeNumbers;
      
      class primeContainer{
          private:
              std::vector<int> cont;
              
              //simple adding method
              void add(int number){
                  cont.push_back(number);
                  return;
              }
      
          public:
              primeContainer(){};
              ~primeContainer(){};
      
              //simple indexing tool
              int operator[](int index){
                  std::vector<int>::iterator iter;
                  if(index > cont.end()-cont.begin())
                      iter = cont.end();
                  else
                      iter = cont.begin()+index;
      
                  return *iter;
              }
              
              int count(){
                  return cont.end()-cont.begin();
              }
              
              friend class primeNumbers;
      };
      
      class primeNumbers{
      		public:
      			EventHandler<void (*)(void*,int)> OnFindPrime;
                   
      		primeContainer& find(int end){
      			primeContainer* pc = new primeContainer();//a container to keep all the primes in and pass
      			bool p = true;//marks a number as prime or not
      			int limit;//limits the divisor from going above where it needs to - the approximate square root.
      			int div;//the divisor
      
      			//loops through all the numbers start through end finding the primes
      			for(int current = 2; current <= end; current++){//you would think that you would save skipping even numbers.
      				p = true;
      				limit = current;
      				div = 2;
      
      					//within prime list.  uses the prime list to cut time off of divisions.
      				for(int x = 0;x<pc->count() && div<limit;x++){
      				    div = pc->operator[](x);
      				    limit = current/div;
      				    if(current%div == 0){
      				        p = false;
      				        break;
      					}
      				} if(p==false)continue;//if the number was found to be false then move on
      
      				//after prime list.  if any other divisors need to be calculated
      				for(div;div<limit;div++){//try replacing 'limit' with 'current' to see the time difference
      					limit = current/div;//no sense in going above this point
      					if(current%div == 0){
      						p = false;
      						break;
      					}
      				}
      
      				if(p == true){//if p is still true then it is indeed a prime
      					pc->add(current);
      					//call events
      					for(int x = 0,y = OnFindPrime.size();x<y;x++){
      						OnFindPrime[x].function(OnFindPrime[x].caller,current);
      
      					}
      				}
      			}
      			return *pc;
      		}
      };
      //boy did this code get messy. didn't look like that on my computer


      now put the following code in a file "Events.h"

      Code:
      #ifndef events_h
      #define events_h
      #endif
      
      #include <vector>
      using namespace std;
      
      template<typename functionDef>
      struct Event{
      	functionDef function;//describes a pointer to the described function
      	void* caller;
      };
      
      template<typename functionDef>
      class EventHandler{
      	private:
      		typedef Event<functionDef> event;
      		std::vector<event> container;
      		typedef typename vector<event>::iterator iterator;
      
      	public:
      
      		void AddEvent(functionDef function, void* instance = 0){
      			event E = {function, instance};
      			container.push_back(E);
      		}
      
      		void RemoveEvent(functionDef function, void* instance){
      
      			for(iterator current = container.begin(),end = container.end();current<end;current++){
      				if(current->function == function && current->caller == instance){
      					container.erase(current);
      				}
      			}
      		}
      
      		event& operator[](int index){
      			return container[index];
      		}
      
      		int size(){
      			return container.size();
      		}
      };
      create a static function that looks like the following

      Code:
      void FoundPrime(void* caller, int currentNumber){
           //do whatever you want to in here
      };
      use your main() function to test this out - until you see what I did. Make sure that you put the files in the same file that you main() is located. And then include this at the top

      Code:
      #include "Primes.h"
      Code:
      primeNumbers pn;
      pn.OnFindPrime.AddEvent(FoundPrime, 0);
      primeContainer& pc = pn.find(400000);
      pn.find returns a container to all the primes from 0 to 400000. But you could add code in your event that pulls out the ones you are looking for. This is a better way for a class to view the problem.

      Let me know if this helps. I hope it works on your computer.

      thanx, will give the code a try n let u know how it goes on my compiler. thank u!!

      Comment

      Working...