0
我一直在試圖建立Cmplx
類型的新變量(這是我的類名),其數據值是0和arg()
(Arg爲我的類的方法) 的事情是,結果變量的值爲0,0。 有沒有什麼解決辦法,或者我做錯了什麼?傳遞一個方法,構造函數的參數
此外,代碼:
#pragma once
#include <conio.h>
#include <stdio.h>
#include <iostream>
#include <string>
#include <cmath>
using namespace std;
template <typename vartype>
class Cmplx
{
public:
vartype Re, Im;
Cmplx(vartype const Real, vartype const Imag)//constructor
{
Re=Real;
Im=Imag;
}
long double arg()//called method
{
return atan2(Im,Re);
}
long double abs()
{
return sqrt(pow(Re,2)+pow(Im,2));
}
Cmplx<long double> log()//target method
{
Cmplx<long double> IArg(0,arg());
return IArg+log(abs());
}
Cmplx operator +(Cmplx const param)
{
Cmplx Tmp;
Tmp.Im=Im+param.Im;
Tmp.Re=Re+param.Re;
return Tmp;
}
Cmplx operator +(vartype const param)
{
Cmplx Tmp;
Tmp.Re=Re+param;
return Tmp;
}
friend Cmplx operator +(vartype const param, Cmplx const para)
{
Cmplx Tmp;
Tmp.Re=para.Re+param;
return Tmp;
}
friend ostream& operator << (ostream &tmp, Cmplx const ¶m)
{
tmp<<param.Re<<"+"<<param.Im<<"i";
return tmp;
}
friend istream& operator >> (istream &tmp, Cmplx ¶m)
{
tmp>>param.Re;
tmp>>param.Im;
return tmp;
}
};
template <>
class Cmplx<string>
{
public:
Cmplx()
{
cout << "Are you crazy or something?, a complex NUMBER with LETTERS as real part and imaginary part?"
<< "\n" << "Damn you should go to school dude." << endl;
}
};
template <>
class Cmplx<char>
{
public:
Cmplx<string> tmp;
};
template <typename type>
long double abs(Cmplx<type> param)
{
long double tmp;
tmp=sqrt(pow(param.Re,2)+pow(param.Im,2));
return tmp;
}
template <typename type>
long double arg(Cmplx<type> param)
{
return atan2(param.Im,param.Re);
}
template <typename type>
Cmplx<long double> exp(Cmplx <type> param)
{
Cmplx<long double> tmp, exim(cos(param.Im),sin(param.Im));
tmp=exp(param.Re)*exim;
return tmp;
}
template <typename type>
Cmplx <long double> log(Cmplx<type> param)
{
Cmplx<long double> IArg(0,arg(param));
return IArg+log(abs(param));
}
template <typename type, typename paramT>
Cmplx<long double> log_b(Cmplx<type> arg, paramT param)
{
return log(arg)/log(param);
}
那類的實現:
#include"cmplx oper.hpp"
using namespace std;
int main()
{
Cmplx<long double> A(2,3);
cout << log(A);
getch();
return true;
}
結果爲:1.28247+0i
但它應該是1.28247+0.98279i
謝謝,我不很清楚如何使用調試器,但我想調用'ARG()'方法本身,它給了我應該給的東西。 – 2011-05-29 08:12:38
@Juan:我會投入一些時間來學習如何使用調試器來完成一件事情。但是如果'arg()'做了正確的事情,那麼你應該檢查你的+操作符是什麼...... – 2011-05-29 08:13:46
我認爲操作符不是問題,因爲它能夠爲'Re'部分指定一個數字這個數字,實際上是'* this'的絕對值的自然對數,它將被寫爲代碼['log(abs(* this))'] – 2011-05-29 08:17:18