Пример #1
0
Matriz *Matriz::potenciaMatriz(int exp)const {
    try {
        if(exp < 0) throw(6);
        Matriz *mat = new Matriz(this->quantidadeDeLinhas,this->quantidadeDeColunas);


        for(int linha=0; linha<quantidadeDeLinhas; linha++)
            for(int coluna=0; coluna<quantidadeDeColunas; coluna++)
            {
                mat->setElemento(linha,coluna,this->getElemento(linha,coluna));   // copia MAT = THIS;
            }


        if(exp == 0) { //expoente igual a zero todos valores sao iguais a 1
            for(int linha=0; linha<quantidadeDeLinhas; linha++)
                for(int coluna=0; coluna<quantidadeDeColunas; coluna++)
                    mat->setElemento(linha,coluna,1);
        }
        else {
            for(int cont=1; cont < exp; cont++) //limite e o expoente
                *(mat) = (*(mat)) * (*(this));

        }

        return mat;

    } catch(int valorErro) {
        throw valorErro;
    }
    catch(std::bad_alloc&) {
        throw(0);
    }
}
Пример #2
0
Matriz* Matriz::operator *(Matriz const * const mat)const{ //CERTO
    if( quantidadeDeColunas  != mat->getQuantidadeDeLinhas() )throw QString("Nao e possivel fazer a multiplicacao das matrizes");
    try{
        Matriz *aux = new Matriz(quantidadeDeLinhas, mat->getQuantidadeDeColunas());
        int a=0,b;
        for(int i=0; i<quantidadeDeLinhas; i++){
            b=0;
            for(int j=0; j<mat->getQuantidadeDeColunas(); j++){
                int valor=0;
                for (int k=0;k<quantidadeDeColunas;k++){
                    valor += this->getElemento(i,k)*mat->getElemento(k,j);
                }
                aux->setElemento(valor,a,b++);
            }
            a++;
        }
        return aux;
    }
    catch(std::bad_alloc&){
        throw QString("Vai comprar memoria");
    }
    catch(QString &erro){
        throw QString("Matriz auxiliar nao Criada nao podemos adicionar as matrizes");
    }
}
Пример #3
0
Matriz *Persistencia::recuperar(std::string const &nomeDoArquivoNoDisco)const{

	std::ifstream arquivoMatriz(nomeDoArquivoNoDisco.c_str());

	try{

		if(!arquivoMatriz.is_open()) throw(4);//arquivo nao pode ser aberto
		int linha, coluna,a;
		arquivoMatriz>>linha;
		arquivoMatriz>>coluna;
		Matriz *matriz = new Matriz(linha,coluna);
		for(int i=0; i<linha; i++)
			for(int j=0; j<coluna; j++){
				arquivoMatriz>>a;
				matriz->setElemento(i,j,a);
			}
		arquivoMatriz.close();
		return matriz;

	}catch(int valorErro){
		if(valorErro != 4)
		arquivoMatriz.close();
		throw valorErro;
	}catch(std::bad_alloc&){
		arquivoMatriz.close();
		throw(0);
	}


}
Пример #4
0
bool Matriz::eMatrizAntiSimetrica()const {
    try {

        Matriz *matTrans = new Matriz(this->quantidadeDeLinhas,this->quantidadeDeColunas);
        Matriz *matOposta = new Matriz(this->quantidadeDeLinhas,this->quantidadeDeColunas);

        matTrans = this->matrizTransposta();

        for(int linha=0; linha<quantidadeDeLinhas; linha++)
            for(int coluna=0; coluna<quantidadeDeColunas; coluna++) {
                int inverso = this->getElemento(linha,coluna)*-1;
                matOposta->setElemento(linha,coluna,inverso);//calcula oposta
            }

        bool ret= *matTrans == *matOposta;//compara transposta com oposta
        delete matTrans;
        delete matOposta;

        return ret;

    } catch(int valorErro) {
        throw valorErro;
    }
    catch(std::bad_alloc&) {
        throw(0);
    }
}
Пример #5
0
Matriz* Matriz::matrizTransp()const{ //CERTO
    Matriz *aux = new Matriz(quantidadeDeColunas,quantidadeDeLinhas);
    for(int i=0;i<getQuantidadeDeLinhas();i++){
            for(int j =0;j<getQuantidadeDeColunas();j++){
                int valor=this->getElemento(i,j);
                aux->setElemento(valor,j,i);

            }
        }
    return aux;
}
Пример #6
0
Matriz* Matriz::potencia(int n)const{ //CERTO
    try{
        Matriz *aux = new Matriz(quantidadeDeLinhas, quantidadeDeColunas);
        if(n==0){
            for(int linha = 0; linha<quantidadeDeLinhas;linha++)
                for(int coluna = 0;coluna<quantidadeDeColunas;coluna++){
                    if(linha == coluna) aux->setElemento(1,linha,coluna);
                    if(linha != coluna) aux->setElemento(0,linha,coluna);
                }
            return aux;
        }
        for(int linha = 0; linha<quantidadeDeLinhas;linha++)
            for(int coluna = 0;coluna<quantidadeDeColunas;coluna++){
                aux->setElemento(this->getElemento(linha,coluna),linha,coluna);
            }
        while(n>1){
            aux = (*this) *(aux);
            n--;
        }
        return aux;
    }catch(std::bad_alloc&){
        throw QString("Vai comprar memoria");
    }
}
Пример #7
0
Matriz *Matriz::matrizTransposta()const {
    try {

        Matriz *mat = new Matriz(this->quantidadeDeLinhas,this->quantidadeDeColunas);

        for(int linha=0; linha<quantidadeDeLinhas; linha++)
            for(int coluna=0; coluna<quantidadeDeColunas; coluna++)
                mat->setElemento(coluna,linha,this->getElemento(linha,coluna));
        // inverte linha,coluna POR coluna,linha;

        return mat;

    } catch(int valorErro) {
        throw valorErro;
    }
    catch(std::bad_alloc&) {
        throw(0);
    }
}
Пример #8
0
Matriz *Matriz::multiplicarPorK(int k)const {
    try {
        Matriz *mat = new Matriz(this->quantidadeDeLinhas,this->quantidadeDeColunas);

        for(int linha=0; linha<quantidadeDeLinhas; linha++)
            for(int coluna=0; coluna<quantidadeDeColunas; coluna++)
                mat->setElemento(linha,coluna,this->getElemento(linha,coluna)*k);


        return mat;

    } catch(int valorErro) {
        throw valorErro;
    }
    catch(std::bad_alloc&) {
        throw(0);
    }


}
Пример #9
0
Matriz *Matriz::operator-(Matriz const &matriz)const {
    try {
        if(		matriz.getQuantidadeLinhas()!= quantidadeDeLinhas ||
                matriz.getQuantidadeColunas() != quantidadeDeColunas) throw (2);

        Matriz *matSub = new Matriz(this->quantidadeDeLinhas,this->quantidadeDeColunas);

        for(int linha=0; linha<quantidadeDeLinhas; linha++)
            for(int coluna=0; coluna<quantidadeDeColunas; coluna++)
                matSub->setElemento(linha,coluna,this->getElemento(linha,coluna) - matriz.getElemento(linha,coluna));


        return matSub;

    } catch(int valorErro) {
        throw valorErro;
    }
    catch(std::bad_alloc&) {
        throw (0);
    }

}