Enviado em 01/09/2021 - 22:02h
o grande problema é fazer as funções soma, subtrair, multiplicar e dividir funcionar. o resultado não está sendo passado para a função de impressão, como resposta está ocorrendo uma falha de segmentação. Gostaria que alguém detectasse a falha do programa.#include <cstdlib>
#include <iostream>
#include "Funcoes22050.h"
using namespace std;
int main (int argc, char *argv[])
{
VetDin v1, v2, v3;
double n;
cout << "Entre com o numero de equações a serem calculadas: ";
cin >> n;
v1 = VetDin(n);
v2 = VetDin(n);
/*VetDin *v1 = new VetDin();
delete v1;
v1 = new VetDin(n); */
v1.le(cout, cin);
v2.le(cout, cin);
v3 = v1 + v2;
v3.exibe(cout);
cout << endl;
return 0;
}
#include "Funcoes22050.h"
#include <cmath>
VetDin::VetDin()
{
n = 0;
a = 0;
b = 0;
}
void VetDin::realoca(int n)
{
if(n != this->n){
T *c = new T[n];
T *d = new T[n];
int menor = n > this->n ? this->n : n;
for(int i = 0; i < menor; i++){
c[i] = a[i];
d[i] = b[i];
}
delete [] a;
delete [] b;
a = c;
b = d;
this->n = n;
}
}
void VetDin::le(ostream &sai, istream &entra)
{
for (int i = 0; i < n; i++){
sai << "Entre com o número real: [" << i << "] = ";
entra >> a[i];
sai << "Entre com o número imaginário: [" << i << "] = ";
entra >> b[i];
}
sai << endl;
}
void VetDin::exibe(ostream &sai)
{
if(n == 0) sai << " " << endl;
else{
for(int i = 0; i < n - 1; i++){
// Sinal da parte real x:
string siRe = a[i] >= 0.0 ? "" : " - ";
// Sinal da parte imaginaria y:
string siIm = b[i] >= 0.0 ? " + " : " - ";
// Impressão
sai << siRe << fabs(a[i]) << siIm << fabs(b[i]) << " i";
sai << endl;
}
}
sai << endl;
}
VetDin::VetDin(int n)
{
this->n = n;
a = new T[n];
b = new T[n];
}
VetDin::~VetDin()
{
delete [] a;
delete [] b;
}
VetDin::VetDin(const VetDin &v)
{
// Verifica uma auto-construção de cópia.
if(this != &v){
n = v.n;
a = new T[n];
b = new T[n];
for(int i = 0; i < n; i++){
a[i] = v.a[i];
b[i] = v.b[i];
}
}
// Chamada explícita ao Construtor Padrão:
else *this = VetDin();
}
VetDin & VetDin::operator = (const VetDin &v)
{
// Verifica uma auto-atribuição.
if (this != &v){
if (a == 0) { // Verifica se não está alocado.
n = v.n;
a = new T[n];
b = new T[n];
}
else if (n != v.n){ // Verifica tamanhos diferentes.
delete [] a;
delete [] b;
n = v.n;
a = new T[n];
b = new T[n];
}
for (int i = 0; i < n; i++){ // Copia os elementos.
a[i] = v.a[i];
b[i] = v.b[i];
}
}
return *this;
}
VetDin::T & VetDin::operator [] (int i)
{
if(0 <= i && i < n) return a[i];
else if(i >= n){
realoca(i + 1);
return a[i];
}
return a[i];
}
/*
double VetDin::quadrado(const VetDin &v)
{
VetDin t;
T *c = new T[n];
for(int i = 0; i < n; i++){
t.c[i] = v.a[i] * v.a[i] + v.b[i] * v.b[i]; // z2²
}
return t;
}
*/
VetDin VetDin::operator +(const VetDin &v)
{
VetDin t;
for(int i = 0; i < n; i++){
t.a[i] = a[i] + v.a[i]; // Parte real.
t.b[i] = b[i] + v.b[i]; // Parte imaginária
}
return t;
}
VetDin VetDin::operator -(const VetDin &v)
{
VetDin t;
for(int i = 0; i < n; i++){
t.a[i] = a[i] - v.a[i]; // Parte real.
t.b[i] = b[i] - v.b[i]; // Parte imaginária
}
return t;
}
VetDin VetDin::operator *(const VetDin &v)
{
VetDin t;
for(int i = 0; i < n; i++){
t.a[i] = a[i]*v.a[i] - b[i]*v.b[i]; // Parte real.
t.b[i] = a[i]*v.b[i] + b[i]*v.a[i]; // Parte imaginária
}
return t;
}
VetDin VetDin::operator /(const VetDin &v)
{
VetDin t;
for(int i = 0; i < n; i++){
t.a[i] = (a[i]*v.a[i] + b[i]*v.b[i])/(v.a[i] * v.a[i] + v.b[i] * v.b[i]); // Parte real.
t.b[i] = ((-a[i])*v.b[i] + b[i]*v.a[i])/(v.a[i] * v.a[i] + v.b[i] * v.b[i]); // Parte imaginária
}
return t;
}
#ifndef VETOR_DINAMICO_COMPLEXO_H
#define VETOR_DINAMICO_COMPLEXO_H
#include <iostream>
using namespace std;
class VetDin
{
private:
typedef double T; // Tipo para os componentes do vetor.
int n; // Tamanho do vetor (quantidade de componentes).
T *a, *b; //Ponteiro para os n componentes do Vetor Dinâmico
void realoca(int n); // Realoca os componentes.
public:
VetDin(); // Construtor Padrão.
VetDin(int n); //Construtor com um parâmetro inteiro.
~VetDin(); // Destruidor.
VetDin(const VetDin &v); // Construtor de Cópia
VetDin & operator = (const VetDin &v); // Atrib.
T & operator [] (int i); // i varia de 0 até n-1.
void le(ostream &sai, istream &entra);
void exibe(ostream &sai);
//double quadrado(const VetDin &v);
VetDin operator +(const VetDin &v); // Soma
VetDin operator -(const VetDin &v); // Subtrai
VetDin operator *(const VetDin &v); // Multiplicação
VetDin operator /(const VetDin &v); // Divide
};
#endif
Pra quem contribui com artigos e dicas (1)
Arch Linux - Guia para Iniciantes (5)
tux-gpt - Assistente de IA para o Terminal
Instalação e configuração do Chrony
Programa IRPF - Guia de Instalação e Resolução de alguns Problemas
O Que Fazer Após Instalar Ubuntu 25.04
O Que Fazer Após Instalar Fedora 42
Debian 12 -- Errata - Correções de segurança
Instalando o Pi-Hole versão v5.18.4 depois do lançamento da versão v6.0
Copiar Layout do Teclado para aplicar em outra Distribuição (2)
Monitor fora de escala ao bootar sistema (10)
Pra quem contribui com artigos e dicas (1)
Alguém poderia me ajudar a escolher peças pra montar um desktop? (19)