
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
Cirurgia para acelerar o openSUSE em HD externo via USB
Void Server como Domain Control
Modo Simples de Baixar e Usar o bash-completion
Monitorando o Preço do Bitcoin ou sua Cripto Favorita em Tempo Real com um Widget Flutuante
Atualizar Linux Mint 22.2 para 22.3 beta
Jogar games da Battle.net no Linux com Faugus Launcher
Como fazer a Instalação de aplicativos para acesso remoto ao Linux
Conky, alerta de temperatura alta (8)
Duas Pasta Pessoal Aparecendo no Ubuntu 24.04.3 LTS (42)
Assisti Avatar 3: Fogo e Cinzas (1)









