[ create a new paste ] login | about

Link: http://codepad.org/jOl6nNy1    [ raw code | output | fork | 1 comment ]

C++, pasted on Mar 20:
#include <iostream>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <math.h>

using namespace std ;

class MojException : public exception {
  private:
    string s;
  public:
    MojException(string ss) : s(ss) {};
    virtual ~MojException() throw() {};
    virtual const char* what() const throw() { return s.c_str(); };
};

bool is_nonneg_number(char* input) throw (MojException){
	
	if(input[0]==45){
		throw MojException(" is lower than zero"); 
		return false;
	}
	int i;	
	int dlugosc=(unsigned)strlen(input);
	
	for(i=0; i<dlugosc; i++){
		if(!(48 <= input[i] && input[i] <=57)){
			throw MojException(" is not positive or not total or not integer ");
			return false;
		}
	
	}
	return true;
}

int str_to_int(char* input) throw (MojException){
	int number=0, i, j = 0;
	for (i = strlen(input) - 1; i >= 0;i--, j++){
		number+= (input[i] -48) * pow(10,j);
		if(number<0)
			throw MojException(" is too big");
	}

  return number;
}




class RozkladLiczby{
	
	private: int Array[], Array2[];
	
	public: 
		RozkladLiczby(int n){
			int i;
			for(i=0; i<=n+2; i++){
				Array[i]=0;
			}
			Array[1] = 1; 
			for (i = 2; i < n; i+=2){     
				Array[i] = 2;								 
			}
			for (int i = 3; i < n; i+=2){					
				if ( Array[i] == 0 ){			
					Array[i]=i;
					for(int j = i+i; j < n; j+=i){  	
						if(Array[j]==0)
							Array[j] = i;
					}
				}
			}

		//for(i=0; i<n; i++){	cout<<" n = "<<n<<", i = "<<i<<", Array[i] = "<<Array[i]<<endl;}
			
		
		
		}
		
		int najmniejszy_dzielnik(int m){
			cout<<"Array["<<m<<"] = "<<Array[m]<<endl;
			return Array[m];
		}
		
		int* czynniki_pierwsze(int m){
			int temp, size, i, helper;
			for(temp=m, i=1; 1 < temp; i++){
				helper=najmniejszy_dzielnik(temp);
				cout<<"test1, array[5] = "<<Array[5]<<endl;
				
				Array2[i]=helper;
				cout<<"test2, array[5] = "<<Array[5]<<endl;
				temp/=helper;
				cout<<"test3, array[5] = "<<Array[5]<<endl;
				
			}
			size=i;
			
			for(i=1; i<size; i++){	cout<<" m = "<<m<<", size = "<<size<<", i = "<<i<<", Array2[i] = "<<Array2[i]<<endl;}
			Array2[0]=size;
			return Array2;
			
		}
		
		void wypisz(int tab[]){
		int potega=1;

		for (unsigned int i=1; i < (unsigned)tab[0]; i++ ){
			if(tab[i]==tab[i+1]){
				potega++;
				continue;
			}
			else{
				if(tab[i]>2)
					cout<<"*";
				cout<<tab[i];
				if(potega>1){
					cout<<"^"<<potega;
					potega=1;
				}
			}
		}
		cout<<endl;
	}

	~RozkladLiczby(){ delete Array; delete Array2;}
	
};


int main(int argc, char* argv[]){
	
	int i, liczba, biggest=0;
	for(i=1; i<(unsigned)argc; i++){
		try{
			if(is_nonneg_number(argv[i])){ //throwing exceptions
			liczba=str_to_int(argv[i]);
			}
		}
		catch(MojException& w){
			cout << argv[i] <<  w.what() << endl;
			continue;
		}
		cout<<"liczba = "<<liczba<<endl;
		if(biggest<liczba)
			biggest=liczba;
	}
	cout << "biggest = " << biggest<<endl;

	RozkladLiczby *s = new RozkladLiczby(biggest+1);
	
	for(i=1; i<(unsigned)argc; i++){
	s->wypisz(s->czynniki_pierwsze(str_to_int(argv[i])));
	}
	return 0;	
}


Output:
1
2
3
In function 'int str_to_int(char*)':
Line 40: error: call of overloaded 'pow(int, int&)' is ambiguous
compilation terminated due to -Wfatal-errors.


Create a new paste based on this one


Comments:
posted by wojteo171 on Mar 20
main goal is to create prime factors.
eg: 8736 = 2^5 * 3 * * 13
eg of error: 360 = 2^3 * 3^3
reply